2024-04-20 07:29:42 +02:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
2024-05-28 07:26:05 +02:00
|
|
|
// t_funcs_test.go
|
2024-04-20 07:29:42 +02:00
|
|
|
package expr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFuncs(t *testing.T) {
|
2024-06-01 16:31:50 +02:00
|
|
|
section := "Funcs"
|
2024-04-20 07:29:42 +02:00
|
|
|
inputs := []inputType{
|
2024-06-01 16:31:50 +02:00
|
|
|
/* 1 */ {`two=func(){2}; two()`, int64(2), nil},
|
|
|
|
/* 2 */ {`double=func(x) {2*x}; (double(3))`, int64(6), nil},
|
|
|
|
/* 3 */ {`double=func(x){2*x}; double(3)`, int64(6), nil},
|
|
|
|
/* 4 */ {`double=func(x){2*x}; a=5; double(3+a) + 1`, int64(17), nil},
|
|
|
|
/* 5 */ {`double=func(x){2*x}; a=5; two=func() {2}; (double(3+a) + 1) * two()`, int64(34), nil},
|
2024-07-06 16:43:13 +02:00
|
|
|
/* 6 */ {`@x="hello"; @x`, nil, `[1:3] variable references are not allowed in top level expressions: "@x"`},
|
2024-06-01 16:31:50 +02:00
|
|
|
/* 7 */ {`f=func(){@x="hello"}; f(); x`, "hello", nil},
|
|
|
|
/* 8 */ {`f=func(@y){@y=@y+1}; f(2); y`, int64(3), nil},
|
2024-07-06 16:43:13 +02:00
|
|
|
/* 9 */ {`f=func(@y){g=func(){@x=5}; @y=@y+g()}; f(2); y+x`, nil, `undefined variable or function "x"`},
|
2024-06-01 16:31:50 +02:00
|
|
|
/* 10 */ {`f=func(@y){g=func(){@x=5}; @z=g(); @y=@y+@z}; f(2); y+z`, int64(12), nil},
|
|
|
|
/* 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},
|
2024-07-28 18:49:08 +02:00
|
|
|
/* 14 */ {`two=func(){2}; two(123)`, nil, `two(): too many params -- expected 0, got 1`},
|
2024-06-01 19:56:40 +02:00
|
|
|
/* 15 */ {`f=func(x,n=2){x+n}; f(3)`, int64(5), nil},
|
2024-07-06 16:43:13 +02:00
|
|
|
/* 16 */ {`f=func(x,n=2,y){x+n}`, nil, `[1:16] can't mix default and non-default parameters`},
|
2024-07-21 05:33:06 +02:00
|
|
|
/* 17 */ {`f=func(x,n){1}; f(3,4,)`, nil, "[1:24] expected `function-param-value`, got `)`"},
|
2024-06-25 10:59:03 +02:00
|
|
|
/* 18 */ {`factory=func(base){func(){@base=base+1}}; inc10=factory(10); inc5=factory(5); inc10(); inc5(); inc10()`, int64(12), nil},
|
2024-06-26 04:29:40 +02:00
|
|
|
/* 19 */ {`f=func(a,y=1,z="sos"){}; string(f)`, `f(a, y=1, z="sos"):any{}`, nil},
|
2024-07-28 18:49:08 +02:00
|
|
|
/* 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`},
|
2024-07-31 09:11:57 +02:00
|
|
|
// /* 20 */ {`a=[func(){3}]; a[0]()`, int64(3), nil},
|
|
|
|
// /* 20 */ {`m={}; m["f"]=func(){3}; m["f"]()`, int64(3), nil},
|
2024-06-02 12:33:32 +02:00
|
|
|
// /* 18 */ {`f=func(a){a*2}`, nil, errors.New(`[1:24] expected "function-param-value", got ")"`)},
|
2024-04-20 07:29:42 +02:00
|
|
|
}
|
|
|
|
|
2024-06-01 19:56:40 +02:00
|
|
|
// t.Setenv("EXPR_PATH", ".")
|
2024-05-06 10:38:45 +02:00
|
|
|
|
2024-07-28 18:49:08 +02:00
|
|
|
//runTestSuiteSpec(t, section, inputs, 19)
|
2024-06-25 10:59:03 +02:00
|
|
|
runTestSuite(t, section, inputs)
|
2024-06-01 16:31:50 +02:00
|
|
|
}
|
|
|
|
|
2024-07-28 18:49:08 +02:00
|
|
|
func dummy(ctx ExprContext, name string, args map[string]any) (result any, err error) {
|
2024-06-01 16:31:50 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFunctionToStringSimple(t *testing.T) {
|
2024-07-31 09:11:57 +02:00
|
|
|
source := NewGolangFunctor(dummy)
|
2024-06-24 07:20:17 +02:00
|
|
|
want := "func(){}"
|
2024-06-01 16:31:50 +02:00
|
|
|
got := source.ToString(0)
|
|
|
|
if got != want {
|
|
|
|
t.Errorf(`(func() -> result = %v [%T], want = %v [%T]`, got, got, want, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFunctionGetFunc(t *testing.T) {
|
2024-07-31 09:11:57 +02:00
|
|
|
source := NewGolangFunctor(dummy)
|
2024-06-01 16:31:50 +02:00
|
|
|
want := ExprFunc(nil)
|
|
|
|
got := source.GetFunc()
|
|
|
|
if got != want {
|
|
|
|
t.Errorf(`(func() -> result = %v [%T], want = %v [%T]`, got, got, want, want)
|
|
|
|
}
|
2024-04-20 07:29:42 +02:00
|
|
|
}
|