the common test framework now supports error, string and nil as value of the wantErr field

This commit is contained in:
2024-07-06 16:43:13 +02:00
parent 9fc20077a1
commit 896844e772
12 changed files with 74 additions and 83 deletions
+16 -16
View File
@@ -5,6 +5,7 @@
package expr
import (
"errors"
"reflect"
"strings"
"testing"
@@ -13,7 +14,7 @@ import (
type inputType struct {
source string
wantResult any
wantErr error
wantErr any
}
func runCtxTestSuiteSpec(t *testing.T, ctx ExprContext, section string, inputs []inputType, spec ...int) {
@@ -52,20 +53,17 @@ func runCtxTestSuite(t *testing.T, ctx ExprContext, section string, inputs []inp
}
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++
func getWantedError(input *inputType) error {
var wantErr error
var ok bool
if wantErr, ok = input.wantErr.(error); !ok {
if msg, ok := input.wantErr.(string); ok {
wantErr = errors.New(msg)
}
}
t.Logf("%s -- test count: %d, succeeded: %d, failed: %d", section, len(inputs), succeeded, failed)
*/
return wantErr
}
func doTest(t *testing.T, ctx ExprContext, section string, input *inputType, count int) (good bool) {
@@ -73,12 +71,14 @@ func doTest(t *testing.T, ctx ExprContext, section string, input *inputType, cou
var gotResult any
var gotErr error
wantErr := getWantedError(input)
parser := NewParser()
if ctx == nil {
ctx = NewSimpleStore()
}
logTest(t, count, section, input.source, input.wantResult, input.wantErr)
logTest(t, count, section, input.source, input.wantResult, wantErr)
r := strings.NewReader(input.source)
scanner := NewScanner(r, DefaultTranslations())
@@ -95,9 +95,9 @@ func doTest(t *testing.T, ctx ExprContext, section string, input *inputType, cou
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)
if gotErr != wantErr {
if wantErr == nil || gotErr == nil || (gotErr.Error() != wantErr.Error()) {
t.Errorf("%d: %q -> got-err = <%v>, expected-err = <%v>", count, input.source, gotErr, wantErr)
good = false
}
}