48 lines
2.2 KiB
Go
48 lines
2.2 KiB
Go
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// funcs_test.go
|
|
package expr
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestFuncs(t *testing.T) {
|
|
inputs := []inputType{
|
|
/* 1 */ {`isNil(nil)`, true, nil},
|
|
/* 2 */ {`v=nil; isNil(v)`, true, nil},
|
|
/* 3 */ {`v=5; isNil(v)`, false, nil},
|
|
/* 4 */ {`int(true)`, int64(1), nil},
|
|
/* 5 */ {`int(false)`, int64(0), nil},
|
|
/* 6 */ {`int(3.1)`, int64(3), nil},
|
|
/* 7 */ {`int(3.9)`, int64(3), nil},
|
|
/* 8 */ {`int("432")`, int64(432), nil},
|
|
/* 9 */ {`int("1.5")`, nil, errors.New(`strconv.Atoi: parsing "1.5": invalid syntax`)},
|
|
/* 10 */ {`int("432", 4)`, nil, errors.New(`int() requires exactly one param`)},
|
|
/* 11 */ {`int(nil)`, nil, errors.New(`int() can't convert <nil> to int`)},
|
|
/* 12 */ {`two=func(){2}; two()`, int64(2), nil},
|
|
/* 13 */ {`double=func(x) {2*x}; (double(3))`, int64(6), nil},
|
|
/* 14 */ {`double=func(x){2*x}; double(3)`, int64(6), nil},
|
|
/* 15 */ {`double=func(x){2*x}; a=5; double(3+a) + 1`, int64(17), nil},
|
|
/* 16 */ {`double=func(x){2*x}; a=5; two=func() {2}; (double(3+a) + 1) * two()`, int64(34), nil},
|
|
/* 17 */ {`builtin "import"; import("./test-funcs.expr"); (double(3+a) + 1) * two()`, int64(34), nil},
|
|
/* 18 */ {`builtin "import"; import("test-funcs.expr"); (double(3+a) + 1) * two()`, int64(34), nil},
|
|
/* 19 */ {`@x="hello"; @x`, nil, errors.New(`[1:3] variable references are not allowed in top level expressions: "@x"`)},
|
|
/* 20 */ {`f=func(){@x="hello"}; f(); x`, "hello", nil},
|
|
/* 21 */ {`f=func(@y){@y=@y+1}; f(2); y`, int64(3), nil},
|
|
/* 22 */ {`f=func(@y){g=func(){@x=5}; @y=@y+g()}; f(2); y+x`, nil, errors.New(`undefined variable or function "x"`)},
|
|
/* 23 */ {`f=func(@y){g=func(){@x=5}; @z=g(); @y=@y+@z}; f(2); y+z`, int64(12), nil},
|
|
/* 24 */ {`f=func(@y){g=func(){@x=5}; g(); @z=x; @y=@y+@z}; f(2); y+z`, int64(12), nil},
|
|
/* 25 */ {`f=func(@y){g=func(){@x=5}; g(); @z=x; @x=@y+@z}; f(2); y+x`, int64(9), nil},
|
|
/* 26 */ {`builtin "import"; importAll("./test-funcs.expr"); six()`, int64(6), nil},
|
|
/* 27 */ {`builtin "import"; import("./sample-export-all.expr"); six()`, int64(6), nil},
|
|
}
|
|
|
|
t.Setenv("EXPR_PATH", ".")
|
|
|
|
// parserTest(t, "Func", inputs[25:26])
|
|
parserTest(t, "Func", inputs)
|
|
}
|