completed list-type to array-type conversion and fixed common test module
This commit is contained in:
+9
-3
@@ -61,8 +61,8 @@ func isRationalFunc(ctx kern.ExprContext, name string, args map[string]any) (res
|
||||
return
|
||||
}
|
||||
|
||||
func isListFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
|
||||
result = array.IsList(args[kern.ParamValue])
|
||||
func isArrayFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
|
||||
result = array.IsArray(args[kern.ParamValue])
|
||||
return
|
||||
}
|
||||
|
||||
@@ -71,6 +71,11 @@ func isDictionaryFunc(ctx kern.ExprContext, name string, args map[string]any) (r
|
||||
return
|
||||
}
|
||||
|
||||
func isListFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
|
||||
result = list.IsLinkedList(args[kern.ParamValue])
|
||||
return
|
||||
}
|
||||
|
||||
func boolFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
|
||||
switch v := args[kern.ParamValue].(type) {
|
||||
case int64:
|
||||
@@ -307,8 +312,9 @@ func ImportBuiltinsFuncs(ctx kern.ExprContext) {
|
||||
ctx.RegisterFunc("isString", kern.NewGolangFunctor(isStringFunc), kern.TypeBoolean, anyParams)
|
||||
ctx.RegisterFunc("isFract", kern.NewGolangFunctor(isFractionFunc), kern.TypeBoolean, anyParams)
|
||||
ctx.RegisterFunc("isRational", kern.NewGolangFunctor(isRationalFunc), kern.TypeBoolean, anyParams)
|
||||
ctx.RegisterFunc("isList", kern.NewGolangFunctor(isListFunc), kern.TypeBoolean, anyParams)
|
||||
ctx.RegisterFunc("isArray", kern.NewGolangFunctor(isArrayFunc), kern.TypeBoolean, anyParams)
|
||||
ctx.RegisterFunc("isDict", kern.NewGolangFunctor(isDictionaryFunc), kern.TypeBoolean, anyParams)
|
||||
ctx.RegisterFunc("isList", kern.NewGolangFunctor(isListFunc), kern.TypeBoolean, anyParams)
|
||||
|
||||
ctx.RegisterFunc("bool", kern.NewGolangFunctor(boolFunc), kern.TypeBoolean, anyParams)
|
||||
ctx.RegisterFunc("int", kern.NewGolangFunctor(intFunc), kern.TypeInt, anyParams)
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
func checkNumberParamExpected(funcName string, paramValue any, paramPos, level, subPos int) (err error) {
|
||||
if !(types.IsNumber(paramValue) || fract.IsFraction(paramValue)) /*|| isList(paramValue)*/ {
|
||||
if !(types.IsNumber(paramValue) || fract.IsFraction(paramValue)) /*|| isArray(paramValue)*/ {
|
||||
err = fmt.Errorf("%s(): param nr %d (%d in %d) has wrong type %T, number expected",
|
||||
funcName, paramPos+1, subPos+1, level, paramValue)
|
||||
}
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ const (
|
||||
)
|
||||
|
||||
func checkStringParamExpected(funcName string, paramValue any, paramPos int) (err error) {
|
||||
if !(str.IsString(paramValue) /*|| isList(paramValue)*/) {
|
||||
if !(str.IsString(paramValue) /*|| isArray(paramValue)*/) {
|
||||
err = fmt.Errorf("%s(): param nr %d has wrong type %s, string expected", funcName, paramPos+1, kern.TypeName(paramValue))
|
||||
}
|
||||
return
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
package kern
|
||||
|
||||
type Cloner interface {
|
||||
Clone() any
|
||||
Clone() Cloner
|
||||
}
|
||||
|
||||
func IsCloner(v any) (ok bool) {
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ func evalIn(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if array.IsList(rightValue) {
|
||||
if array.IsArray(rightValue) {
|
||||
a, _ := rightValue.(*array.ArrayType)
|
||||
v = a.IndexDeepSameCmp(leftValue) >= 0
|
||||
} else if dict.IsDict(rightValue) {
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ func evalInclude(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
}
|
||||
|
||||
count := 0
|
||||
if array.IsList(childValue) {
|
||||
if array.IsArray(childValue) {
|
||||
a, _ := childValue.(*array.ArrayType)
|
||||
for i, filePathSpec := range *a {
|
||||
if filePath, ok := filePathSpec.(string); ok {
|
||||
|
||||
+15
-15
@@ -124,23 +124,23 @@ func evalAppend(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// func evalAssignAppend(ctx ExprContext, self *term) (v any, err error) {
|
||||
// var leftValue, rightValue any
|
||||
// // func evalAssignAppend(ctx ExprContext, self *term) (v any, err error) {
|
||||
// // var leftValue, rightValue any
|
||||
|
||||
// if leftValue, rightValue, err = self.evalInfix(ctx); err != nil {
|
||||
// return
|
||||
// }
|
||||
// // if leftValue, rightValue, err = self.evalInfix(ctx); err != nil {
|
||||
// // return
|
||||
// // }
|
||||
|
||||
// if IsList(leftValue) {
|
||||
// list, _ := leftValue.(*ListType)
|
||||
// newList := append(*list, rightValue)
|
||||
// v = &newList
|
||||
// if
|
||||
// } else {
|
||||
// err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// // if IsArray(leftValue) {
|
||||
// // list, _ := leftValue.(*ListType)
|
||||
// // newList := append(*list, rightValue)
|
||||
// // v = &newList
|
||||
// // if
|
||||
// // } else {
|
||||
// // err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
// // }
|
||||
// // return
|
||||
// // }
|
||||
|
||||
// init
|
||||
func init() {
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ func evalLength(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if array.IsList(childValue) {
|
||||
if array.IsArray(childValue) {
|
||||
ls, _ := childValue.(*array.ArrayType)
|
||||
v = int64(len(*ls))
|
||||
} else if str.IsString(childValue) {
|
||||
|
||||
+3
-3
@@ -46,7 +46,7 @@ func equals(a, b any, deepCmp kern.DeepFuncTemplate) (eq bool, err error) {
|
||||
eq = cmp == 0
|
||||
}
|
||||
}
|
||||
} else if deepCmp != nil && array.IsList(a) && array.IsList(b) {
|
||||
} else if deepCmp != nil && array.IsArray(a) && array.IsArray(b) {
|
||||
eq, err = deepCmp(a, b)
|
||||
} else {
|
||||
eq = reflect.DeepEqual(a, b)
|
||||
@@ -119,7 +119,7 @@ func lessThan(self *scan.Term, a, b any) (isLess bool, err error) {
|
||||
rs, _ := b.(string)
|
||||
isLess = ls < rs
|
||||
// Inclusion test
|
||||
} else if array.IsList(a) && array.IsList(b) {
|
||||
} else if array.IsArray(a) && array.IsArray(b) {
|
||||
aList, _ := a.(*array.ArrayType)
|
||||
bList, _ := b.(*array.ArrayType)
|
||||
isLess = len(*aList) < len(*bList) && bList.Contains(aList)
|
||||
@@ -154,7 +154,7 @@ func newLessEqualTerm(tk *scan.Token) (inst *scan.Term) {
|
||||
func lessThanOrEqual(self *scan.Term, a, b any) (isLessEq bool, err error) {
|
||||
if isLessEq, err = lessThan(self, a, b); err == nil {
|
||||
if !isLessEq {
|
||||
if array.IsList(a) && array.IsList(b) {
|
||||
if array.IsArray(a) && array.IsArray(b) {
|
||||
isLessEq, err = array.SameContent(a, b)
|
||||
} else {
|
||||
isLessEq, err = equals(a, b, nil)
|
||||
|
||||
+4
-3
@@ -41,7 +41,7 @@ func sumValues(plusTerm *scan.Term, leftValue, rightValue any) (v any, err error
|
||||
rightInt, _ := rightValue.(int64)
|
||||
v = leftInt + rightInt
|
||||
}
|
||||
} else if array.IsList(leftValue) && array.IsList(rightValue) {
|
||||
} else if array.IsArray(leftValue) && array.IsArray(rightValue) {
|
||||
var leftList, rightList *array.ArrayType
|
||||
leftList, _ = leftValue.(*array.ArrayType)
|
||||
rightList, _ = rightValue.(*array.ArrayType)
|
||||
@@ -59,7 +59,8 @@ func sumValues(plusTerm *scan.Term, leftValue, rightValue any) (v any, err error
|
||||
} else if dict.IsDict(leftValue) && dict.IsDict(rightValue) {
|
||||
leftDict, _ := leftValue.(*dict.DictType)
|
||||
rightDict, _ := rightValue.(*dict.DictType)
|
||||
c := leftDict.Clone()
|
||||
clone := leftDict.Clone()
|
||||
c := clone.(*dict.DictType)
|
||||
c.Merge(rightDict)
|
||||
v = c
|
||||
} else if fract.IsFraction(leftValue) && fract.IsFraction(rightValue) {
|
||||
@@ -103,7 +104,7 @@ func diffValues(minusTerm *scan.Term, leftValue, rightValue any) (v any, err err
|
||||
rightInt, _ := rightValue.(int64)
|
||||
v = leftInt - rightInt
|
||||
}
|
||||
} else if array.IsList(leftValue) && array.IsList(rightValue) {
|
||||
} else if array.IsArray(leftValue) && array.IsArray(rightValue) {
|
||||
leftList, _ := leftValue.(*array.ArrayType)
|
||||
rightList, _ := rightValue.(*array.ArrayType)
|
||||
diffList := make(array.ArrayType, 0, len(*leftList)-len(*rightList))
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ func evalUnset(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
}
|
||||
|
||||
count := 0
|
||||
if array.IsList(childValue) {
|
||||
if array.IsArray(childValue) {
|
||||
list, _ := childValue.(*array.ArrayType)
|
||||
for _, item := range *list {
|
||||
if deleted, err = deleteContextItem(ctx, opTerm, item); err != nil {
|
||||
|
||||
+6
-5
@@ -522,11 +522,12 @@ func (scanner *Scanner) parseNumber(firstCh byte) (tk *Token) {
|
||||
} else {
|
||||
value, err = strconv.ParseInt(txt, numBase, 64)
|
||||
}
|
||||
if err == nil {
|
||||
tk = scanner.MakeValueToken(sym, txt, value)
|
||||
} else {
|
||||
tk = scanner.makeErrorToken(err)
|
||||
}
|
||||
tk = scanner.MakeValueToken(sym, txt, value)
|
||||
// if err == nil {
|
||||
// tk = scanner.MakeValueToken(sym, txt, value)
|
||||
// } else {
|
||||
// tk = scanner.makeErrorToken(err)
|
||||
// }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ func TestBool(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 13)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestBoolNoShortcut(t *testing.T) {
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestFuncBase(t *testing.T) {
|
||||
/* 14 */ {`isFloat(3.1)`, true, nil},
|
||||
/* 15 */ {`isString("3.1")`, true, nil},
|
||||
/* 16 */ {`isString("3" + 1)`, true, nil},
|
||||
/* 17 */ {`isList(["3", 1])`, true, nil},
|
||||
/* 17 */ {`isArray(["3", 1])`, true, nil},
|
||||
/* 18 */ {`isDict({"a":"3", "b":1})`, true, nil},
|
||||
/* 19 */ {`isFract(1:3)`, true, nil},
|
||||
/* 20 */ {`isFract(3:1)`, false, nil},
|
||||
@@ -71,7 +71,7 @@ func TestFuncBase(t *testing.T) {
|
||||
t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 49)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestFuncBaseString(t *testing.T) {
|
||||
@@ -89,7 +89,7 @@ func TestFuncBaseString(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 49)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestFuncBaseFraction(t *testing.T) {
|
||||
@@ -110,8 +110,8 @@ func TestFuncBaseFraction(t *testing.T) {
|
||||
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 5)
|
||||
runTestSuite(t, section, inputs)
|
||||
// RunTestSuiteSpec(t, section, inputs, 10)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestFuncBaseOthers(t *testing.T) {
|
||||
@@ -124,6 +124,6 @@ func TestFuncBaseOthers(t *testing.T) {
|
||||
// /* 4 */ {`seq(1,2,4)`, kern.NewLinkedListA(int64(1), int64(2), int64(3)), nil},
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 4)
|
||||
runTestSuite(t, section, inputs)
|
||||
// RunTestSuiteSpec(t, section, inputs, 3)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestFuncFmt(t *testing.T) {
|
||||
//t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 1)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestFmt(t *testing.T) {
|
||||
|
||||
@@ -20,5 +20,5 @@ func TestFuncImport(t *testing.T) {
|
||||
t.Setenv("EXPR_PATH", "test-resources")
|
||||
|
||||
//runTestSuiteSpec(t, section, inputs, 1)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
@@ -27,5 +27,5 @@ func TestFuncRun(t *testing.T) {
|
||||
//t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 3)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
@@ -29,5 +29,5 @@ func TestFuncMathArith(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
//runTestSuiteSpec(t, section, inputs, 10)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ func TestFuncOs(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 24)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestFuncOsByteIter(t *testing.T) {
|
||||
@@ -52,5 +52,5 @@ func TestFuncOsByteIter(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 24)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ func TestFuncString(t *testing.T) {
|
||||
isFloat(any) -> boolean,
|
||||
isFract(any) -> boolean,
|
||||
isInt(any) -> boolean,
|
||||
isList(any) -> boolean,
|
||||
isArray(any) -> boolean,
|
||||
isNil(any) -> boolean,
|
||||
isString(any) -> boolean,
|
||||
joinStr(separator, item="" ...) -> string,
|
||||
@@ -68,5 +68,5 @@ func TestFuncString(t *testing.T) {
|
||||
//t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 19)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
+35
-6
@@ -6,6 +6,8 @@ package expr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -19,9 +21,27 @@ type inputType struct {
|
||||
wantErr any
|
||||
}
|
||||
|
||||
func getSourceName(startFrame int) (sourcename string) {
|
||||
for i := startFrame; i < 10; i++ {
|
||||
_, sourcepath, _, ok := runtime.Caller(i)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
sourcename = path.Base(sourcepath)
|
||||
if sourcename != "t_common_test.go" {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func RunCtxTestSuiteSpec(t *testing.T, ctx kern.ExprContext, section string, inputs []inputType, spec ...int) {
|
||||
succeeded := 0
|
||||
failed := 0
|
||||
|
||||
sourcename := getSourceName(2)
|
||||
t.Logf("Test suite %s from file %s", section, sourcename)
|
||||
|
||||
for _, count := range spec {
|
||||
index := count - 1
|
||||
if index >= 0 && index < len(inputs) {
|
||||
@@ -49,6 +69,10 @@ func RunCtxTestSuite(t *testing.T, ctx kern.ExprContext, section string, inputs
|
||||
succeeded := 0
|
||||
failed := 0
|
||||
|
||||
sourcename := getSourceName(2)
|
||||
|
||||
t.Logf("Test suite %s from file %s", section, sourcename)
|
||||
|
||||
for i, input := range inputs {
|
||||
// fmt.Printf("%3d: %s\n", i+1, input.source)
|
||||
|
||||
@@ -61,7 +85,7 @@ func RunCtxTestSuite(t *testing.T, ctx kern.ExprContext, section string, inputs
|
||||
}
|
||||
t.Logf("%s -- test count: %d, succeeded: %d, failed: %d", section, len(inputs), succeeded, failed)
|
||||
}
|
||||
func runTestSuite(t *testing.T, section string, inputs []inputType) {
|
||||
func RunTestSuite(t *testing.T, section string, inputs []inputType) {
|
||||
RunCtxTestSuite(t, nil, section, inputs)
|
||||
}
|
||||
|
||||
@@ -98,16 +122,21 @@ func doTest(t *testing.T, ctx kern.ExprContext, section string, input *inputType
|
||||
gotResult, gotErr = ast.Eval(ctx)
|
||||
}
|
||||
|
||||
if gotErr == nil && wantErr != nil {
|
||||
eq := kern.Equal(gotResult, input.wantResult)
|
||||
if gotErr == nil {
|
||||
if wantErr == nil {
|
||||
eq := kern.Equal(gotResult, input.wantResult)
|
||||
|
||||
if !eq /*gotResult != input.wantResult*/ {
|
||||
t.Errorf(">>>%s/%d: `%s` -> result = %v [%s], want = %v [%s]", section, count, input.source, gotResult, kern.TypeName(gotResult), input.wantResult, kern.TypeName(input.wantResult))
|
||||
if !eq /*gotResult != input.wantResult*/ {
|
||||
t.Errorf(">>>%s/%d: `%s` -> result = %v [%s], want = %v [%s]", section, count, input.source, gotResult, kern.TypeName(gotResult), input.wantResult, kern.TypeName(input.wantResult))
|
||||
good = false
|
||||
}
|
||||
} else {
|
||||
t.Errorf(">>>%s/%d: `%s` -> got-err = <nil>, expected-err = %v [%s]", section, count, input.source, input.wantResult, kern.TypeName(input.wantResult))
|
||||
good = false
|
||||
}
|
||||
}
|
||||
|
||||
if gotErr != wantErr {
|
||||
if good && gotErr != wantErr {
|
||||
if wantErr == nil || gotErr == nil || (gotErr.Error() != wantErr.Error()) {
|
||||
t.Errorf(">>>%s/%d: %s -> got-err = <%v>, expected-err = <%v>", section, count, input.source, gotErr, wantErr)
|
||||
good = false
|
||||
|
||||
+1
-1
@@ -88,6 +88,6 @@ func TestList(t *testing.T) {
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 3)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
|
||||
}
|
||||
|
||||
+15
-2
@@ -49,8 +49,21 @@ func TestDictParser(t *testing.T) {
|
||||
/* 24 */ {`f=func(n){"x"+n}; d={f(5) but "z":10}`, dict.NewDict(map[any]any{"z": int64(10)}), nil},
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 23, 24)
|
||||
runTestSuite(t, section, inputs)
|
||||
// RunTestSuiteSpec(t, section, inputs, 1)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestOperations(t *testing.T) {
|
||||
section := "Dict-Operations"
|
||||
inputs := []inputType{
|
||||
/* 1 */ {`{"a":1} + {"b":2}`, dict.NewDict(map[any]any{"a": int64(1), "b": int64(2)}), nil},
|
||||
/* 2 */ {`D={"a":1}; C={"b":2}; X=D+C; C."b" = 3; X`, dict.NewDict(map[any]any{"a": int64(1), "b": int64(2)}), nil},
|
||||
/* 3 */ {`D={"a":1}; C={"b":2}; X=D+C; D."a" = 3; X`, dict.NewDict(map[any]any{"a": int64(1), "b": int64(2)}), nil},
|
||||
/* 4 */ {`L=[8,9]; D={"a":L}; L[0]=11; D."a"[0]`, int64(11), nil},
|
||||
/* 5 */ {`L=[8,9]; D:={"a":L}; L[0]=11; D."a"[0]`, int64(8), nil},
|
||||
}
|
||||
// runTestSuiteSpec(t, section, inputs, 1)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestAccessSubFields(t *testing.T) {
|
||||
|
||||
+1
-1
@@ -50,5 +50,5 @@ func TestExpr(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 21)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ func TestFractionsParser(t *testing.T) {
|
||||
/* 19 */ {`1:2 == 0.5`, true, nil},
|
||||
}
|
||||
// runTestSuiteSpec(t, section, inputs, 26)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestFractionToStringSimple(t *testing.T) {
|
||||
|
||||
+2
-2
@@ -46,7 +46,7 @@ func TestFuncs(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
//runTestSuiteSpec(t, section, inputs, 19)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestFuncs2(t *testing.T) {
|
||||
@@ -57,7 +57,7 @@ func TestFuncs2(t *testing.T) {
|
||||
/* 3 */ {`builtin "iterator"; times=func(a,b){a*b}; run($(["1", "2", "3"]), times)`, nil, `operator(): missing params -- a, b`},
|
||||
}
|
||||
// runTestSuiteSpec(t, section, inputs, 4)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func dummy(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
|
||||
|
||||
+1
-1
@@ -26,5 +26,5 @@ func TestCollections(t *testing.T) {
|
||||
t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 5)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ func TestIterIterator(t *testing.T) {
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 4)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
// func TestNewIterIterator(t *testing.T) {
|
||||
|
||||
+5
-5
@@ -45,7 +45,7 @@ func TestIteratorParser(t *testing.T) {
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 25)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestCallOpIntIter(t *testing.T) {
|
||||
@@ -132,7 +132,7 @@ func TestFilterIterator(t *testing.T) {
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 2)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestDigestIterator(t *testing.T) {
|
||||
@@ -144,7 +144,7 @@ func TestDigestIterator(t *testing.T) {
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 2)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestCatIterator(t *testing.T) {
|
||||
@@ -159,7 +159,7 @@ func TestCatIterator(t *testing.T) {
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 6)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestMapIterator(t *testing.T) {
|
||||
@@ -173,5 +173,5 @@ func TestMapIterator(t *testing.T) {
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 2)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
+2
-2
@@ -64,7 +64,7 @@ func TestListParser(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 44)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestLinkedListParser(t *testing.T) {
|
||||
@@ -81,5 +81,5 @@ func TestLinkedListParser(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 4)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
+5
-5
@@ -48,7 +48,7 @@ func TestOperator(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 22)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestOperatorInsert(t *testing.T) {
|
||||
@@ -77,7 +77,7 @@ func TestOperatorInsert(t *testing.T) {
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 9)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestOperatorDeepAssign(t *testing.T) {
|
||||
@@ -90,7 +90,7 @@ func TestOperatorDeepAssign(t *testing.T) {
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 4)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestOperatorDigest(t *testing.T) {
|
||||
@@ -100,7 +100,7 @@ func TestOperatorDigest(t *testing.T) {
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 29)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestOperatorGroupBy(t *testing.T) {
|
||||
@@ -131,5 +131,5 @@ func TestOperatorGroupBy(t *testing.T) {
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 3)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
+2
-2
@@ -146,7 +146,7 @@ func TestGeneralParser(t *testing.T) {
|
||||
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
// runTestSuiteSpec(t, section, inputs, 114)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
func TestSpecialParser(t *testing.T) {
|
||||
@@ -158,5 +158,5 @@ func TestSpecialParser(t *testing.T) {
|
||||
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
// runTestSuiteSpec(t, section, inputs, 114)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// t_template_test.go
|
||||
// t_relational_test.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
@@ -40,13 +40,17 @@ func TestRelational(t *testing.T) {
|
||||
/* 27 */ {`[1,2,3] > [9]`, false, nil},
|
||||
/* 28 */ {`[1,2,3] >= [6]`, false, nil},
|
||||
/* 29 */ {`[1,2,3] >= [2,6]`, false, nil},
|
||||
/* 30 */ {`[1,[2,3],4] > [[3,2]]`, true, nil},
|
||||
/* 31 */ {`[1,[2,[3,4]],5] > [[[4,3],2]]`, true, nil},
|
||||
/* 32 */ {`[[4,3],2] IN [1,[2,[3,4]],5]`, true, nil},
|
||||
/* 30 */ {`[1,[2,3],4] > [[3,2]]`, false, nil},
|
||||
/* 31 */ {`[1,[2,3],4] > [[2,3]]`, true, nil},
|
||||
/* 32 */ {`[1,[2,[3,4]],5] > [[[4,3],2]]`, false, nil},
|
||||
/* 33 */ {`[1,[2,[3,4]],5] > [[2,[3,4]]]`, true, nil},
|
||||
/* 34 */ {`[[4,3],2] IN [1,[2,[3,4]],5]`, false, nil},
|
||||
/* 35 */ {`[2,[3,4]] IN [1,[2,[3,4]],5]`, true, nil},
|
||||
}
|
||||
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// parserTestSpec(t, section, inputs, 31)
|
||||
runTestSuite(t, section, inputs)
|
||||
// RunTestSuiteSpec(t, section, inputs, 35)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
+1
-1
@@ -26,5 +26,5 @@ func TestStringsParser(t *testing.T) {
|
||||
}
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 8)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
+1
-1
@@ -17,5 +17,5 @@ func TestSomething(t *testing.T) {
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 1)
|
||||
runTestSuite(t, section, inputs)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
@@ -1,226 +0,0 @@
|
||||
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// list-type.go
|
||||
package array
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
)
|
||||
|
||||
type ArrayType []any
|
||||
|
||||
func IsList(v any) (ok bool) {
|
||||
_, ok = v.(*ArrayType)
|
||||
return ok
|
||||
}
|
||||
|
||||
func NewArrayA(listAny ...any) (list *ArrayType) {
|
||||
if listAny == nil {
|
||||
listAny = []any{}
|
||||
}
|
||||
return NewArray(listAny)
|
||||
}
|
||||
|
||||
func NewArray(arrayAny []any) (aRef *ArrayType) {
|
||||
if arrayAny != nil {
|
||||
a := make(ArrayType, len(arrayAny))
|
||||
copy(a, arrayAny)
|
||||
aRef = &a
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func MakeArray(length, capacity int) (list *ArrayType) {
|
||||
if capacity < length {
|
||||
capacity = length
|
||||
}
|
||||
ls := make(ArrayType, length, capacity)
|
||||
list = &ls
|
||||
return
|
||||
}
|
||||
|
||||
func ArrayFromStrings(stringSlice []string) (aRef *ArrayType) {
|
||||
aRef = MakeArray(len(stringSlice), 0)
|
||||
for i, s := range stringSlice {
|
||||
(*aRef)[i] = s
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (aRef *ArrayType) ToString(opt kern.FmtOpt) (s string) {
|
||||
indent := kern.GetFormatIndent(opt)
|
||||
flags := kern.GetFormatFlags(opt)
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteByte('[')
|
||||
if len(*aRef) > 0 {
|
||||
innerOpt := kern.MakeFormatOptions(flags, indent+1)
|
||||
nest := strings.Repeat(" ", indent+1)
|
||||
|
||||
if flags&kern.MultiLine != 0 {
|
||||
sb.WriteByte('\n')
|
||||
sb.WriteString(nest)
|
||||
}
|
||||
for i, item := range []any(*aRef) {
|
||||
if i > 0 {
|
||||
if flags&kern.MultiLine != 0 {
|
||||
sb.WriteString(",\n")
|
||||
sb.WriteString(nest)
|
||||
} else {
|
||||
sb.WriteString(", ")
|
||||
}
|
||||
}
|
||||
kern.Format(&sb, item, innerOpt)
|
||||
}
|
||||
if flags&kern.MultiLine != 0 {
|
||||
sb.WriteByte('\n')
|
||||
sb.WriteString(strings.Repeat(" ", indent))
|
||||
}
|
||||
}
|
||||
sb.WriteByte(']')
|
||||
s = sb.String()
|
||||
if flags&kern.Truncate != 0 && len(s) > kern.TruncateSize {
|
||||
s = kern.TruncateString(s)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (aRef *ArrayType) String() string {
|
||||
return aRef.ToString(0)
|
||||
}
|
||||
|
||||
func (aRef *ArrayType) TypeName() string {
|
||||
return kern.TypeArray
|
||||
}
|
||||
|
||||
func (aRef *ArrayType) Contains(t *ArrayType) (answer bool) {
|
||||
if len(*aRef) >= len(*t) {
|
||||
answer = true
|
||||
for _, item := range *t {
|
||||
if answer = aRef.IndexDeepSameCmp(item) >= 0; !answer {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (aRef *ArrayType) EqualTo(other kern.Equaler) (equal bool) {
|
||||
if otherList, ok := other.(*ArrayType); ok {
|
||||
equal = aRef.Equals(*otherList)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (aRef *ArrayType) Equals(ls2 ArrayType) (answer bool) {
|
||||
if ls2 != nil && len(*aRef) == len(ls2) {
|
||||
answer = true
|
||||
for index, i1 := range *aRef {
|
||||
// if !reflect.DeepEqual(i1, ls2[index]) {
|
||||
// answer = false
|
||||
// break
|
||||
// }
|
||||
if !kern.Equal(i1, ls2[index]) {
|
||||
answer = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (aRef *ArrayType) Clone() (ls2 *ArrayType) {
|
||||
ls := make(ArrayType, len(*aRef))
|
||||
for i, item := range *aRef {
|
||||
ls[i] = kern.Clone(item)
|
||||
}
|
||||
ls2 = &ls
|
||||
return
|
||||
}
|
||||
|
||||
func (aRef *ArrayType) IndexDeepSameCmp(target any) (index int) {
|
||||
index = -1
|
||||
for i, item := range *aRef {
|
||||
if kern.Equal(item, target) {
|
||||
index = i
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// func (ls *ListType) IndexDeepSameCmp(target any) (index int) {
|
||||
// var eq bool
|
||||
// var err error
|
||||
// index = -1
|
||||
// for i, item := range *ls {
|
||||
// if eq, err = deepSame(item, target, SameContent); err != nil {
|
||||
// break
|
||||
// } else if eq {
|
||||
// index = i
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
func SameContent(a, b any) (same bool, err error) {
|
||||
aRef, aValid := a.(*ArrayType)
|
||||
bRef, bValid := b.(*ArrayType)
|
||||
if !aValid || !bValid {
|
||||
err = fmt.Errorf("invalid type for comparison")
|
||||
return
|
||||
}
|
||||
if len(*aRef) == len(*bRef) {
|
||||
same = true
|
||||
for _, item := range *aRef {
|
||||
if pos := bRef.IndexDeepSameCmp(item); pos < 0 {
|
||||
same = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// func deepSame(a, b any, deepCmp kern.DeepFuncTemplate) (eq bool, err error) {
|
||||
// if IsNumOrFract(a) && IsNumOrFract(b) {
|
||||
// if IsNumber(a) && IsNumber(b) {
|
||||
// if IsInteger(a) && IsInteger(b) {
|
||||
// li, _ := a.(int64)
|
||||
// ri, _ := b.(int64)
|
||||
// eq = li == ri
|
||||
// } else {
|
||||
// eq = NumAsFloat(a) == NumAsFloat(b)
|
||||
// }
|
||||
// } else {
|
||||
// var cmp int
|
||||
// if cmp, err = fract.CmpAnyFract(a, b); err == nil {
|
||||
// eq = cmp == 0
|
||||
// }
|
||||
// }
|
||||
// } else if deepCmp != nil && IsList(a) && IsList(b) {
|
||||
// eq, err = deepCmp(a, b)
|
||||
// } else {
|
||||
// eq = reflect.DeepEqual(a, b)
|
||||
// }
|
||||
|
||||
// return
|
||||
// }
|
||||
|
||||
func (aRef *ArrayType) SetItem(index int64, value any) (err error) {
|
||||
if index >= 0 && index < int64(len(*aRef)) {
|
||||
(*aRef)[index] = value
|
||||
} else {
|
||||
err = fmt.Errorf("index %d out of bounds (0, %d)", index, len(*aRef)-1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (aRef *ArrayType) AppendItem(value any) {
|
||||
*aRef = append(*aRef, value)
|
||||
}
|
||||
+1
-1
@@ -23,7 +23,7 @@ func Equal(value1, value2 any) (equal bool) {
|
||||
equal = false
|
||||
} else if boolean.IsBool(value1) && boolean.IsBool(value2) {
|
||||
equal = value1.(bool) == value2.(bool)
|
||||
} else if array.IsList(value1) && array.IsList(value2) {
|
||||
} else if array.IsArray(value1) && array.IsArray(value2) {
|
||||
ls1 := value1.(*array.ArrayType)
|
||||
ls2 := value2.(*array.ArrayType)
|
||||
equal = ls1.Equals(*ls2)
|
||||
|
||||
+10
-3
@@ -160,12 +160,12 @@ func (dict *DictType) GetItem(key any) (value any, exists bool) {
|
||||
return
|
||||
}
|
||||
|
||||
func (dict *DictType) Clone() (c *DictType) {
|
||||
c = newDict(nil)
|
||||
func (dict *DictType) Clone() kern.Cloner {
|
||||
c := newDict(nil)
|
||||
for k, v := range *dict {
|
||||
(*c)[k] = kern.Clone(v)
|
||||
}
|
||||
return
|
||||
return c
|
||||
}
|
||||
|
||||
func (dict *DictType) Merge(second *DictType) {
|
||||
@@ -176,6 +176,13 @@ func (dict *DictType) Merge(second *DictType) {
|
||||
}
|
||||
}
|
||||
|
||||
func (dict *DictType) EqualTo(other kern.Equaler) (answer bool) {
|
||||
if dict2, ok := other.(*DictType); ok {
|
||||
answer = dict.Equals(*dict2)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (dict *DictType) Equals(dict2 DictType) (answer bool) {
|
||||
if dict2 != nil && len(*dict) == len(dict2) {
|
||||
answer = true
|
||||
|
||||
@@ -98,6 +98,13 @@ func (ls *LinkedList) TypeName() string {
|
||||
// return
|
||||
// }
|
||||
|
||||
func (ls1 *LinkedList) EqualTo(other kern.Equaler) (answer bool) {
|
||||
if ls2, ok := other.(*LinkedList); ok {
|
||||
answer = ls1.Equals(ls2)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ls1 *LinkedList) Equals(ls2 *LinkedList) (answer bool) {
|
||||
if ls2 != nil && ls1.Len() == ls2.Len() {
|
||||
answer = true
|
||||
|
||||
Reference in New Issue
Block a user