a lot oh changes to the test framework and new test files t_builtin-fmt_test.go and t_plugin_test.go added
This commit is contained in:
parent
ef1baa11a8
commit
ba9b9cb28f
@ -63,5 +63,5 @@ func TestFuncBase(t *testing.T) {
|
||||
t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// parserTestSpec(t, section, inputs, 2)
|
||||
parserTest(t, section, inputs)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
49
t_builtin-fmt_test.go
Normal file
49
t_builtin-fmt_test.go
Normal file
@ -0,0 +1,49 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// t_builtin-fmt.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
// "errors"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFuncFmt(t *testing.T) {
|
||||
section := "Builtin-Fmt"
|
||||
|
||||
inputs := []inputType{
|
||||
/* 1 */ {`builtin "fmt"; print("ciao")`, int64(4), nil},
|
||||
/* 2 */ {`builtin "fmt"; println(" ciao")`, int64(6), nil},
|
||||
}
|
||||
|
||||
//t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// parserTestSpec(t, section, inputs, 19)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
|
||||
func TestFmt(t *testing.T) {
|
||||
section := "Builtin-Fmt"
|
||||
|
||||
text := "ciao mondo"
|
||||
inputs := []inputType{
|
||||
/* 1 */ {fmt.Sprintf(`println("%s")`, text), int64(11), nil},
|
||||
}
|
||||
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
var b bytes.Buffer
|
||||
ctx := NewSimpleStore()
|
||||
currentStdout := SetCtrl(ctx, ControlStdout, &b)
|
||||
|
||||
runCtxTestSuite(t, ctx, section, inputs)
|
||||
|
||||
SetCtrl(ctx, ControlStdout, currentStdout)
|
||||
if b.String() != text + "\n" {
|
||||
t.Errorf("println(): Got: %q, Want: %q", b.String(), text+"\n")
|
||||
}
|
||||
}
|
@ -20,5 +20,5 @@ func TestFuncImport(t *testing.T) {
|
||||
t.Setenv("EXPR_PATH", "test-resources")
|
||||
|
||||
// parserTestSpec(t, section, inputs, 69)
|
||||
parserTest(t, section, inputs)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
@ -25,5 +25,5 @@ func TestFuncMathArith(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// parserTestSpec(t, section, inputs, 69)
|
||||
parserTest(t, section, inputs)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
@ -19,11 +19,12 @@ func TestFuncOs(t *testing.T) {
|
||||
/* 5 */ {`builtin "os.file"; handle=fileAppend("/tmp/dummy"); fileWriteText(handle, "bye-bye"); fileClose(handle)`, true, nil},
|
||||
/* 6 */ {`builtin "os.file"; handle=fileOpen("/tmp/dummy"); word=fileReadText(handle, "-"); fileClose(handle);word`, "bye", nil},
|
||||
/* 7 */ {`builtin "os.file"; word=fileReadText(nil, "-")`, nil, errors.New(`fileReadText(): invalid file handle`)},
|
||||
/* 7 */ {`builtin "os.file"; fileWriteText(nil, "bye")`, nil, errors.New(`fileWriteText(): invalid file handle`)},
|
||||
/* 8 */ {`builtin "os.file"; fileWriteText(nil, "bye")`, nil, errors.New(`fileWriteText(): invalid file handle`)},
|
||||
/* 9 */ {`builtin "os.file"; handle=fileOpen()`, nil, errors.New(`fileOpen(): too few params -- expected 1, got 0`)},
|
||||
}
|
||||
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// parserTestSpec(t, section, inputs, 69)
|
||||
parserTest(t, section, inputs)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
@ -65,5 +65,5 @@ func TestFuncString(t *testing.T) {
|
||||
//t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// parserTestSpec(t, section, inputs, 19)
|
||||
parserTest(t, section, inputs)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
127
t_common_test.go
Normal file
127
t_common_test.go
Normal file
@ -0,0 +1,127 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// t_common_test.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type inputType struct {
|
||||
source string
|
||||
wantResult any
|
||||
wantErr error
|
||||
}
|
||||
|
||||
func runCtxTestSuiteSpec(t *testing.T, ctx ExprContext, section string, inputs []inputType, spec ...int) {
|
||||
succeeded := 0
|
||||
failed := 0
|
||||
for _, count := range spec {
|
||||
good := doTest(t, ctx, section, &inputs[count-1], count)
|
||||
|
||||
if good {
|
||||
succeeded++
|
||||
} else {
|
||||
failed++
|
||||
}
|
||||
}
|
||||
t.Logf("%s -- test count: %d, succeeded: %d, failed: %d", section, len(spec), succeeded, failed)
|
||||
}
|
||||
|
||||
func runTestSuiteSpec(t *testing.T, section string, inputs []inputType, spec ...int) {
|
||||
runCtxTestSuiteSpec(t, nil, section, inputs, spec...)
|
||||
/*
|
||||
succeeded := 0
|
||||
failed := 0
|
||||
for _, count := range spec {
|
||||
good := doTest(t, nil, section, &inputs[count-1], count)
|
||||
|
||||
if good {
|
||||
succeeded++
|
||||
} else {
|
||||
failed++
|
||||
}
|
||||
}
|
||||
t.Logf("%s -- test count: %d, succeeded: %d, failed: %d", section, len(spec), succeeded, failed)
|
||||
*/
|
||||
}
|
||||
|
||||
func runCtxTestSuite(t *testing.T, ctx ExprContext, section string, inputs []inputType) {
|
||||
|
||||
succeeded := 0
|
||||
failed := 0
|
||||
|
||||
for i, input := range inputs {
|
||||
good := doTest(t, ctx, section, &input, i+1)
|
||||
if good {
|
||||
succeeded++
|
||||
} else {
|
||||
failed++
|
||||
}
|
||||
}
|
||||
t.Logf("%s -- test count: %d, succeeded: %d, failed: %d", section, len(inputs), succeeded, failed)
|
||||
}
|
||||
func runTestSuite(t *testing.T, section string, inputs []inputType) {
|
||||
runCtxTestSuite(t, nil, section, inputs)
|
||||
/*
|
||||
succeeded := 0
|
||||
failed := 0
|
||||
|
||||
for i, input := range inputs {
|
||||
good := doTest(t, nil, section, &input, i+1)
|
||||
if good {
|
||||
succeeded++
|
||||
} else {
|
||||
failed++
|
||||
}
|
||||
}
|
||||
t.Logf("%s -- test count: %d, succeeded: %d, failed: %d", section, len(inputs), succeeded, failed)
|
||||
*/
|
||||
}
|
||||
|
||||
func doTest(t *testing.T, ctx ExprContext, section string, input *inputType, count int) (good bool) {
|
||||
var expr Expr
|
||||
var gotResult any
|
||||
var gotErr error
|
||||
|
||||
parser := NewParser()
|
||||
if ctx == nil {
|
||||
ctx = NewSimpleStore()
|
||||
}
|
||||
|
||||
logTest(t, count, section, input.source, input.wantResult, input.wantErr)
|
||||
|
||||
r := strings.NewReader(input.source)
|
||||
scanner := NewScanner(r, DefaultTranslations())
|
||||
|
||||
good = true
|
||||
if expr, gotErr = parser.Parse(scanner); gotErr == nil {
|
||||
gotResult, gotErr = expr.Eval(ctx)
|
||||
}
|
||||
|
||||
eq := reflect.DeepEqual(gotResult, input.wantResult)
|
||||
|
||||
if !eq /*gotResult != input.wantResult*/ {
|
||||
t.Errorf("%d: %q -> result = %v [%s], want = %v [%s]", count, input.source, gotResult, TypeName(gotResult), input.wantResult, TypeName(input.wantResult))
|
||||
good = false
|
||||
}
|
||||
|
||||
if gotErr != input.wantErr {
|
||||
if input.wantErr == nil || gotErr == nil || (gotErr.Error() != input.wantErr.Error()) {
|
||||
t.Errorf("%d: %q -> got-err = <%v>, expected-err = <%v>", count, input.source, gotErr, input.wantErr)
|
||||
good = false
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func logTest(t *testing.T, n int, section, source string, wantResult any, wantErr error) {
|
||||
if wantErr == nil {
|
||||
t.Logf("[+]%s nr %3d -- %q --> %v", section, n, source, wantResult)
|
||||
} else {
|
||||
t.Logf("[-]%s nr %3d -- %q --> %v", section, n, source, wantErr)
|
||||
}
|
||||
}
|
@ -33,5 +33,5 @@ func TestExpr(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// parserTestSpec(t, section, inputs, 3)
|
||||
parserTest(t, section, inputs)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ func TestFractionsParser(t *testing.T) {
|
||||
/* 20 */ {`fract("1a")`, (*FractionType)(nil), errors.New(`strconv.ParseInt: parsing "1a": invalid syntax`)},
|
||||
/* 21 */ {`fract(1,0)`, nil, errors.New(`fract(): division by zero`)},
|
||||
}
|
||||
parserTest(t, section, inputs)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestFractionToStringSimple(t *testing.T) {
|
||||
|
@ -29,13 +29,14 @@ func TestFuncs(t *testing.T) {
|
||||
/* 15 */ {`f=func(x,n=2){x+n}; f(3)`, int64(5), nil},
|
||||
/* 16 */ {`f=func(x,n=2,y){x+n}`, nil, errors.New(`[1:16] can't mix default and non-default parameters`)},
|
||||
/* 17 */ {`f=func(x,n){1}; f(3,4,)`, nil, errors.New(`[1:24] expected "function-param-value", got ")"`)},
|
||||
/* 18 */ {`factory=func(base){func(){@base=base+1}}; inc10=factory(10); inc5=factory(5); inc10(); inc5(); inc10()`, int64(12), nil},
|
||||
// /* 18 */ {`f=func(a){a*2}`, nil, errors.New(`[1:24] expected "function-param-value", got ")"`)},
|
||||
}
|
||||
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// parserTestSpec(t, section, inputs, 17)
|
||||
parserTest(t, section, inputs)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func dummy(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
|
@ -23,5 +23,5 @@ func TestCollections(t *testing.T) {
|
||||
t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// parserTestSpec(t, section, inputs, 5)
|
||||
parserTest(t, section, inputs)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
@ -22,5 +22,5 @@ func TestIteratorParser(t *testing.T) {
|
||||
// inputs1 := []inputType{
|
||||
// /* 1 */ {`0?{}`, nil, nil},
|
||||
// }
|
||||
parserTest(t, "Iterator", inputs)
|
||||
runTestSuite(t, "Iterator", inputs)
|
||||
}
|
||||
|
@ -58,5 +58,5 @@ func TestListParser(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// parserTestSpec(t, section, inputs, 17)
|
||||
parserTest(t, section, inputs)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
@ -6,17 +6,9 @@ package expr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type inputType struct {
|
||||
source string
|
||||
wantResult any
|
||||
wantErr error
|
||||
}
|
||||
|
||||
func TestGeneralParser(t *testing.T) {
|
||||
section := "Parser"
|
||||
|
||||
@ -148,78 +140,5 @@ func TestGeneralParser(t *testing.T) {
|
||||
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
// parserTestSpec(t, section, inputs, 102)
|
||||
parserTest(t, section, inputs)
|
||||
}
|
||||
|
||||
func parserTestSpec(t *testing.T, section string, inputs []inputType, spec ...int) {
|
||||
succeeded := 0
|
||||
failed := 0
|
||||
for _, count := range spec {
|
||||
good := doTest(t, section, &inputs[count-1], count)
|
||||
|
||||
if good {
|
||||
succeeded++
|
||||
} else {
|
||||
failed++
|
||||
}
|
||||
}
|
||||
t.Logf("%s -- test count: %d, succeeded: %d, failed: %d", section, len(spec), succeeded, failed)
|
||||
}
|
||||
|
||||
func parserTest(t *testing.T, section string, inputs []inputType) {
|
||||
|
||||
succeeded := 0
|
||||
failed := 0
|
||||
|
||||
for i, input := range inputs {
|
||||
good := doTest(t, section, &input, i+1)
|
||||
if good {
|
||||
succeeded++
|
||||
} else {
|
||||
failed++
|
||||
}
|
||||
}
|
||||
t.Logf("%s -- test count: %d, succeeded: %d, failed: %d", section, len(inputs), succeeded, failed)
|
||||
}
|
||||
|
||||
func doTest(t *testing.T, section string, input *inputType, count int) (good bool) {
|
||||
var expr Expr
|
||||
var gotResult any
|
||||
var gotErr error
|
||||
|
||||
ctx := NewSimpleStore()
|
||||
parser := NewParser()
|
||||
|
||||
logTest(t, count, section, input.source, input.wantResult, input.wantErr)
|
||||
|
||||
r := strings.NewReader(input.source)
|
||||
scanner := NewScanner(r, DefaultTranslations())
|
||||
|
||||
good = true
|
||||
if expr, gotErr = parser.Parse(scanner); gotErr == nil {
|
||||
gotResult, gotErr = expr.Eval(ctx)
|
||||
}
|
||||
|
||||
eq := reflect.DeepEqual(gotResult, input.wantResult)
|
||||
|
||||
if !eq /*gotResult != input.wantResult*/ {
|
||||
t.Errorf("%d: %q -> result = %v [%s], want = %v [%s]", count, input.source, gotResult, TypeName(gotResult), input.wantResult, TypeName(input.wantResult))
|
||||
good = false
|
||||
}
|
||||
|
||||
if gotErr != input.wantErr {
|
||||
if input.wantErr == nil || gotErr == nil || (gotErr.Error() != input.wantErr.Error()) {
|
||||
t.Errorf("%d: %q -> got-err = <%v>, expected-err = <%v>", count, input.source, gotErr, input.wantErr)
|
||||
good = false
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func logTest(t *testing.T, n int, section, source string, wantResult any, wantErr error) {
|
||||
if wantErr == nil {
|
||||
t.Logf("[+]%s nr %3d -- %q --> %v", section, n, source, wantResult)
|
||||
} else {
|
||||
t.Logf("[-]%s nr %3d -- %q --> %v", section, n, source, wantErr)
|
||||
}
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
31
t_plugin_test.go
Normal file
31
t_plugin_test.go
Normal file
@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// t_plugin_test.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func _TestImportPlugin(t *testing.T) {
|
||||
if err := importPlugin([]string{"test-resources"}, "json"); err != nil {
|
||||
t.Errorf("importPlugin() failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginExists(t *testing.T) {
|
||||
name := "json"
|
||||
exists := pluginExists(name)
|
||||
t.Logf("pluginExists(%v): %v", name, exists)
|
||||
|
||||
}
|
||||
|
||||
func TestMakePluginName(t *testing.T) {
|
||||
name := "json"
|
||||
want := "expr-" + name + "-plugin.so"
|
||||
|
||||
if got := makePluginName(name); got != want {
|
||||
t.Errorf("makePluginName(%q) failed: Got: %q, Want: %q", name, got, want)
|
||||
}
|
||||
}
|
@ -48,5 +48,5 @@ func TestRelational(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// parserTestSpec(t, section, inputs, 31)
|
||||
parserTest(t, section, inputs)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
@ -17,5 +17,5 @@ func TestStringsParser(t *testing.T) {
|
||||
/* 5 */ {`"abc"[1]`, `b`, nil},
|
||||
/* 6 */ {`#"abc"`, int64(3), nil},
|
||||
}
|
||||
parserTest(t, "String", inputs)
|
||||
runTestSuite(t, "String", inputs)
|
||||
}
|
||||
|
@ -17,5 +17,5 @@ func TestSomething(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// parserTestSpec(t, section, inputs, 1)
|
||||
parserTest(t, section, inputs)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ package expr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -154,3 +155,12 @@ func TestAnyInteger(t *testing.T) {
|
||||
}
|
||||
t.Logf("%s -- test count: %d, succeeded: %d, failed: %d", section, len(inputs), succeeded, failed)
|
||||
}
|
||||
|
||||
func TestCopyMap(t *testing.T) {
|
||||
source := map[string]int{"one": 1, "two": 2, "three": 3}
|
||||
dest := make(map[string]int)
|
||||
result := CopyMap(dest, source)
|
||||
if !reflect.DeepEqual(result, source) {
|
||||
t.Errorf("utils.CopyMap() failed")
|
||||
}
|
||||
}
|
||||
|
BIN
test-resources/expr-json-plugin.so
Normal file
BIN
test-resources/expr-json-plugin.so
Normal file
Binary file not shown.
BIN
test-resources/expr-json-plugin.so.debug
Normal file
BIN
test-resources/expr-json-plugin.so.debug
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user