The function parameter model has been modified to support the passing of named parameters

This commit is contained in:
2024-07-28 18:49:08 +02:00
parent ab06702e5e
commit 9070b5c9cc
34 changed files with 520 additions and 347 deletions
+10 -5
View File
@@ -24,12 +24,17 @@ func TestFuncs(t *testing.T) {
/* 11 */ {`f=func(@y){g=func(){@x=5}; g(); @z=x; @y=@y+@z}; f(2); y+z`, int64(12), nil},
/* 12 */ {`f=func(@y){g=func(){@x=5}; g(); @z=x; @x=@y+@z}; f(2); y+x`, int64(9), nil},
/* 13 */ {`two=func(){2}; four=func(f){f()+f()}; four(two)`, int64(4), nil},
/* 14 */ {`two=func(){2}; two(123)`, nil, `two(): too much params -- expected 0, got 1`},
/* 14 */ {`two=func(){2}; two(123)`, nil, `two(): too many params -- expected 0, got 1`},
/* 15 */ {`f=func(x,n=2){x+n}; f(3)`, int64(5), nil},
/* 16 */ {`f=func(x,n=2,y){x+n}`, nil, `[1:16] can't mix default and non-default parameters`},
/* 17 */ {`f=func(x,n){1}; f(3,4,)`, nil, "[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},
/* 19 */ {`f=func(a,y=1,z="sos"){}; string(f)`, `f(a, y=1, z="sos"):any{}`, nil},
/* 20 */ {`f=func(a,b){a*2+b}; f(1,10)`, int64(12), nil},
/* 21 */ {`f=func(a,b){a*2+b}; f(a=2,b=1)`, int64(5), nil},
/* 22 */ {`f=func(a,b){a*2+b}; f(b=2,a=1)`, int64(4), nil},
/* 23 */ {`f=func(a=10,b=10){a*2+b}; f(b=1)`, int64(21), nil},
/* 24 */ {`f=func(a,b){a*2+b}; f(a=1,2)`, nil, `f(): positional param nr 2 not allowed after named params`},
// /* 20 */ {`a=[func(){3}]; a[0]()`, int64(3), nil},
// /* 20 */ {`m={}; m["f"]=func(){3}; m["f"]()`, int64(3), nil},
// /* 18 */ {`f=func(a){a*2}`, nil, errors.New(`[1:24] expected "function-param-value", got ")"`)},
@@ -37,16 +42,16 @@ func TestFuncs(t *testing.T) {
// t.Setenv("EXPR_PATH", ".")
//runTestSuiteSpec(t, section, inputs, 20)
//runTestSuiteSpec(t, section, inputs, 19)
runTestSuite(t, section, inputs)
}
func dummy(ctx ExprContext, name string, args []any) (result any, err error) {
func dummy(ctx ExprContext, name string, args map[string]any) (result any, err error) {
return
}
func TestFunctionToStringSimple(t *testing.T) {
source := NewGolangFunctor(dummy)
source := NewGolangFunctor2(dummy)
want := "func(){}"
got := source.ToString(0)
if got != want {
@@ -56,7 +61,7 @@ func TestFunctionToStringSimple(t *testing.T) {
func TestFunctionGetFunc(t *testing.T) {
source := NewGolangFunctor(dummy)
source := NewGolangFunctor2(dummy)
want := ExprFunc(nil)
got := source.GetFunc()
if got != want {