diff --git a/builtin-base.go b/builtin-base.go index acedefa..91a27a8 100644 --- a/builtin-base.go +++ b/builtin-base.go @@ -254,18 +254,18 @@ func setFunc(ctx kern.ExprContext, name string, args map[string]any) (result any return } -func unsetFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) { - var varName string - var ok bool +// func unsetFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) { +// var varName string +// var ok bool - if varName, ok = args[kern.ParamName].(string); !ok { - return nil, kern.ErrWrongParamType(name, kern.ParamName, kern.TypeString, args[kern.ParamName]) - } else { - ctx.GetParent().DeleteVar(varName) - result = nil - } - return -} +// if varName, ok = args[kern.ParamName].(string); !ok { +// return nil, kern.ErrWrongParamType(name, kern.ParamName, kern.TypeString, args[kern.ParamName]) +// } else { +// ctx.GetParent().DeleteVar(varName) +// result = nil +// } +// return +// } //// import @@ -307,10 +307,10 @@ func ImportBuiltinsFuncs(ctx kern.ExprContext) { NewFuncParam(kern.ParamValue), }) - ctx.RegisterFunc("unset", kern.NewGolangFunctor(unsetFunc), kern.TypeAny, []kern.ExprFuncParam{ - NewFuncParam(kern.ParamName), - NewFuncParam(kern.ParamValue), - }) + // ctx.RegisterFunc("unset", kern.NewGolangFunctor(unsetFunc), kern.TypeAny, []kern.ExprFuncParam{ + // NewFuncParam(kern.ParamName), + // NewFuncParam(kern.ParamValue), + // }) } func init() { diff --git a/t_builtin-base_test.go b/t_builtin-base_test.go index fa49f7b..92805cb 100644 --- a/t_builtin-base_test.go +++ b/t_builtin-base_test.go @@ -72,3 +72,56 @@ func TestFuncBase(t *testing.T) { // runTestSuiteSpec(t, section, inputs, 49) runTestSuite(t, section, inputs) } + +func TestFuncBaseString(t *testing.T) { + section := "Builtin-Base-String" + + inputs := []inputType{ + /* 1 */ {`string(3)`, "3", nil}, + /* 2 */ {`string(3.5)`, "3.5", nil}, + /* 3 */ {`string(true)`, "true", nil}, + /* 4 */ {`string(false)`, "false", nil}, + /* 5 */ {`string("123")`, "123", nil}, + /* 6 */ {`string(1:2)`, "1:2", nil}, + } + + // t.Setenv("EXPR_PATH", ".") + + // runTestSuiteSpec(t, section, inputs, 49) + runTestSuite(t, section, inputs) +} + +func TestFuncBaseFraction(t *testing.T) { + section := "Builtin-Base-Fraction" + + inputs := []inputType{ + /* 1 */ {`fract(-0.5)`, kern.NewFraction(-1, 2), nil}, + /* 2 */ {`fract("")`, (*kern.FractionType)(nil), `bad syntax`}, + /* 3 */ {`fract("-1")`, kern.NewFraction(-1, 1), nil}, + /* 4 */ {`fract("+1")`, kern.NewFraction(1, 1), nil}, + /* 5 */ {`fract("1a")`, (*kern.FractionType)(nil), `strconv.ParseInt: parsing "1a": invalid syntax`}, + /* 6 */ {`fract(1,0)`, nil, `fract(): division by zero`}, + /* 7 */ {`fract(5, "1")`, nil, `fract(): expected integer, got string ("1")`}, + /* 8 */ {`fract(true)`, kern.NewFraction(1, 1), nil}, + /* 9 */ {`fract(false)`, kern.NewFraction(0, 1), nil}, + } + + // t.Setenv("EXPR_PATH", ".") + + // runTestSuiteSpec(t, section, inputs, 8) + runTestSuite(t, section, inputs) +} + +func TestFuncBaseOthers(t *testing.T) { + section := "Builtin-Base-Others" + + inputs := []inputType{ + /* 1 */ {`set("a", 3); a`, int64(3), nil}, + /* 2 */ {`set(true, 3)`, nil, `set(): the "name" parameter must be a string, got a bool (true)`}, + // /* 3 */ {`a=3; unset("a"); a`, nil, `undefined variable or function "a"`}, + // /* 4 */ {`unset("a")`, nil, `undefined variable or function "a"`}, + } + + // runTestSuiteSpec(t, section, inputs, 4) + runTestSuite(t, section, inputs) +} diff --git a/t_common_test.go b/t_common_test.go index 4d74b6f..bd5668d 100644 --- a/t_common_test.go +++ b/t_common_test.go @@ -92,23 +92,6 @@ func doTest(t *testing.T, ctx kern.ExprContext, section string, input *inputType } eq := kern.Equal(gotResult, input.wantResult) - // if input.wantResult != nil && gotResult != nil { - // if ls1, ok := input.wantResult.(*kern.ListType); ok { - // if ls2, ok := gotResult.(*kern.ListType); ok { - // eq = ls1.Equals(*ls2) - // eqDone = true - // } - // } else if dict1, ok := input.wantResult.(*kern.DictType); ok { - // if dict2, ok := gotResult.(*kern.DictType); ok { - // eq = dict1.Equals(*dict2) - // eqDone = true - // } - // } - // } - - // if !eqDone { - // eq = reflect.DeepEqual(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)) diff --git a/t_fractions_test.go b/t_fractions_test.go index fe6ba81..174c945 100644 --- a/t_fractions_test.go +++ b/t_fractions_test.go @@ -28,17 +28,10 @@ func TestFractionsParser(t *testing.T) { /* 13 */ {`builtin "math.arith"; mul(1:2, 2:3)`, kern.NewFraction(2, 6), nil}, /* 14 */ {`builtin "math.arith"; mul(1:2, 1.0, 2)`, float64(1.0), nil}, /* 15 */ {`1:0`, nil, `[1:3] division by zero`}, - /* 16 */ {`fract(-0.5)`, kern.NewFraction(-1, 2), nil}, - /* 17 */ {`fract("")`, (*kern.FractionType)(nil), `bad syntax`}, - /* 18 */ {`fract("-1")`, kern.NewFraction(-1, 1), nil}, - /* 19 */ {`fract("+1")`, kern.NewFraction(1, 1), nil}, - /* 20 */ {`fract("1a")`, (*kern.FractionType)(nil), `strconv.ParseInt: parsing "1a": invalid syntax`}, - /* 21 */ {`fract(1,0)`, nil, `fract(): division by zero`}, - /* 22 */ {`string(1:2)`, "1:2", nil}, - /* 23 */ {`1+1:2+0.5`, float64(2), nil}, - /* 24 */ {`1:(2-2)`, nil, `[1:3] division by zero`}, - /* 25 */ {`[0,1][1-1]:1`, kern.NewFraction(0, 1), nil}, - /* 26 */ {`1:2 == 0.5`, true, nil}, + /* 16 */ {`1+1:2+0.5`, float64(2), nil}, + /* 17 */ {`1:(2-2)`, nil, `[1:3] division by zero`}, + /* 18 */ {`[0,1][1-1]:1`, kern.NewFraction(0, 1), nil}, + /* 19 */ {`1:2 == 0.5`, true, nil}, } // runTestSuiteSpec(t, section, inputs, 26) runTestSuite(t, section, inputs) diff --git a/t_funcs_test.go b/t_funcs_test.go index e3118df..3f90ff7 100644 --- a/t_funcs_test.go +++ b/t_funcs_test.go @@ -49,6 +49,18 @@ func TestFuncs(t *testing.T) { runTestSuite(t, section, inputs) } +func TestFuncs2(t *testing.T) { + section := "Funcs2" + inputs := []inputType{ + /* 1 */ {`sum=func(a,b){a+b}; sum(1,2)`, int64(3), nil}, + /* 2 */ {`sum=func(a,b){a+b}; sum(1)`, nil, `sum(): too few params -- expected 2, got 1`}, + /* 3 */ {`["1", "2", "3"] map int()`, nil, `int(): too few params -- expected 1, got 0`}, + /* 4 */ {`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) +} + func dummy(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) { return }