New test on the tilde operator. Now it is based on simple-var-store
This commit is contained in:
parent
37f0de5902
commit
d20027296c
@ -11,58 +11,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type testContext struct {
|
func addFunc(ctx exprContext, name string, args []any) (result any, err error) {
|
||||||
store map[string]any
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTestContext() *testContext {
|
|
||||||
return &testContext{
|
|
||||||
store: make(map[string]any),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ctx *testContext) GetValue(varName string) (v any, exists bool) {
|
|
||||||
v, exists = ctx.store[varName]
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ctx *testContext) SetValue(varName string, value any) {
|
|
||||||
ctx.store[varName] = value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ctx *testContext) GetFuncInfo(name string) (f exprFunc) {
|
|
||||||
if name == "ADD" {
|
|
||||||
f = &testAddFunc{}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ctx *testContext) Call(name string, args []any) (result any, err error) {
|
|
||||||
if name == "ADD" {
|
|
||||||
funcObj := &testAddFunc{}
|
|
||||||
result, err = funcObj.Invoke(ctx, args)
|
|
||||||
} else {
|
|
||||||
err = fmt.Errorf("unknown function %q", name)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type testAddFunc struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *testAddFunc) Name() string {
|
|
||||||
return "ADD"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *testAddFunc) MinArgs() int {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *testAddFunc) MaxArgs() int {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *testAddFunc) Invoke(ctx exprContext, args []any) (result any, err error) {
|
|
||||||
var sumAsFloat = false
|
var sumAsFloat = false
|
||||||
var floatSum float64 = 0.0
|
var floatSum float64 = 0.0
|
||||||
var intSum int64 = 0
|
var intSum int64 = 0
|
||||||
@ -101,9 +50,15 @@ func TestParser(t *testing.T) {
|
|||||||
wantErr error
|
wantErr error
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := newTestContext()
|
// ctx := newTestContext()
|
||||||
|
ctx := NewSimpleFuncStore()
|
||||||
ctx.SetValue("var1", int64(123))
|
ctx.SetValue("var1", int64(123))
|
||||||
ctx.SetValue("var2", "abc")
|
ctx.SetValue("var2", "abc")
|
||||||
|
ctx.addFunc("add", addFunc)
|
||||||
|
|
||||||
|
// inputs1 := []inputType{
|
||||||
|
// {`add(1,2,3)`, int64(6), nil},
|
||||||
|
// }
|
||||||
|
|
||||||
inputs := []inputType{
|
inputs := []inputType{
|
||||||
/* 1 */ {`1+/*5*/2`, int64(3), nil},
|
/* 1 */ {`1+/*5*/2`, int64(3), nil},
|
||||||
@ -139,7 +94,7 @@ func TestParser(t *testing.T) {
|
|||||||
/* 31 */ {"(1+1)*5", int64(10), nil},
|
/* 31 */ {"(1+1)*5", int64(10), nil},
|
||||||
/* 32 */ {"200 / (1+1) - 1", int64(99), nil},
|
/* 32 */ {"200 / (1+1) - 1", int64(99), nil},
|
||||||
/* 33 */ {`add(1,2,3)`, int64(6), nil},
|
/* 33 */ {`add(1,2,3)`, int64(6), nil},
|
||||||
/* 34 */ {`mul(1,2,3)`, nil, errors.New(`unknown function "MUL"`)},
|
/* 34 */ {`mul(1,2,3)`, nil, errors.New(`unknown function "mul"`)},
|
||||||
/* 35 */ {`add(1+4,3+2,5*(3-2))`, int64(15), nil},
|
/* 35 */ {`add(1+4,3+2,5*(3-2))`, int64(15), nil},
|
||||||
/* 36 */ {`add(add(1+4),3+2,5*(3-2))`, int64(15), nil},
|
/* 36 */ {`add(add(1+4),3+2,5*(3-2))`, int64(15), nil},
|
||||||
/* 37 */ {`add(add(1+4),/*3+2,*/5*(3-2))`, int64(10), nil},
|
/* 37 */ {`add(add(1+4),/*3+2,*/5*(3-2))`, int64(10), nil},
|
||||||
@ -191,8 +146,18 @@ func TestParser(t *testing.T) {
|
|||||||
/* 83 */ {`5 % (-2)`, int64(1), nil},
|
/* 83 */ {`5 % (-2)`, int64(1), nil},
|
||||||
/* 84 */ {`-5 % 2`, int64(-1), nil},
|
/* 84 */ {`-5 % 2`, int64(-1), nil},
|
||||||
/* 85 */ {`5 % 2.0`, nil, errors.New(`left operand '5' [int64] is not compatible with right operand '2' [float64] with respect to operator "%"`)},
|
/* 85 */ {`5 % 2.0`, nil, errors.New(`left operand '5' [int64] is not compatible with right operand '2' [float64] with respect to operator "%"`)},
|
||||||
|
/* 86 */ {`"a" < "b" AND NOT (2 < 1)`, true, nil},
|
||||||
|
/* 87 */ {`"a" < "b" AND NOT (2 == 1)`, true, nil},
|
||||||
|
/* 88 */ {`"a" < "b" AND ~ 2 == 1`, true, nil},
|
||||||
|
/* 89 */ {`~ 2 > 1`, false, nil},
|
||||||
|
/* 90 */ {`~ true && true`, false, nil},
|
||||||
|
/* 91 */ {`~ false || true`, true, nil},
|
||||||
|
/* 92 */ {`~ (false || true)`, false, nil},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
succeeded := 0
|
||||||
|
failed := 0
|
||||||
|
|
||||||
parser := NewParser(ctx)
|
parser := NewParser(ctx)
|
||||||
for i, input := range inputs {
|
for i, input := range inputs {
|
||||||
var expr *ast
|
var expr *ast
|
||||||
@ -208,18 +173,29 @@ func TestParser(t *testing.T) {
|
|||||||
r := strings.NewReader(input.source)
|
r := strings.NewReader(input.source)
|
||||||
scanner := NewScanner(r, DefaultTranslations())
|
scanner := NewScanner(r, DefaultTranslations())
|
||||||
|
|
||||||
|
good := true
|
||||||
if expr, gotErr = parser.parse(scanner); gotErr == nil {
|
if expr, gotErr = parser.parse(scanner); gotErr == nil {
|
||||||
gotResult, gotErr = expr.eval(ctx)
|
gotResult, gotErr = expr.eval(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
if gotResult != input.wantResult {
|
if gotResult != input.wantResult {
|
||||||
t.Errorf("%d: %q -> result = %v [%T], want %v [%T]", i+1, input.source, gotResult, gotResult, input.wantResult, input.wantResult)
|
t.Errorf("%d: %q -> result = %v [%T], want %v [%T]", i+1, input.source, gotResult, gotResult, input.wantResult, input.wantResult)
|
||||||
|
good = false
|
||||||
}
|
}
|
||||||
|
|
||||||
if gotErr != input.wantErr {
|
if gotErr != input.wantErr {
|
||||||
if input.wantErr == nil || gotErr == nil || (gotErr.Error() != input.wantErr.Error()) {
|
if input.wantErr == nil || gotErr == nil || (gotErr.Error() != input.wantErr.Error()) {
|
||||||
t.Errorf("%d: %q -> err = <%v>, want <%v>", i+1, input.source, gotErr, input.wantErr)
|
t.Errorf("%d: %q -> err = <%v>, want <%v>", i+1, input.source, gotErr, input.wantErr)
|
||||||
|
good = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if good {
|
||||||
|
succeeded++
|
||||||
|
} else {
|
||||||
|
failed++
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
t.Log(fmt.Sprintf("test count: %d, succeeded count: %d, failed count: %d", len(inputs), succeeded, failed))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user