- fraction type renamed as FractionType and moved from operator-fraction.go to fraction-type.go - ListType moved from operator-list.go to list-type.go - all test file were renamed adding the "t_" prefix - defined a test template in file t_temple_test.go - new test file t_relational_test.go where relational tests are collected - lists can now compared as set using operators <, <=, >, and >= (IMPORTANT: here = menas same content, not same list)
117 lines
6.1 KiB
Go
117 lines
6.1 KiB
Go
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// t_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(`too much params -- expected 1, got 2`)},
|
|
/* 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},
|
|
/* 28 */ {`builtin "string"; joinStr("-", "one", "two", "three")`, "one-two-three", nil},
|
|
/* 29 */ {`builtin "string"; joinStr("-", ["one", "two", "three"])`, "one-two-three", nil},
|
|
/* 30 */ {`builtin "string"; ls= ["one", "two", "three"]; joinStr("-", ls)`, "one-two-three", nil},
|
|
/* 31 */ {`builtin "string"; ls= ["one", "two", "three"]; joinStr(1, ls)`, nil, errors.New(`joinStr() the "separator" parameter must be a string, got a int64 (1)`)},
|
|
/* 32 */ {`builtin "string"; ls= ["one", 2, "three"]; joinStr("-", ls)`, nil, errors.New(`joinStr() expected string, got int64 (2)`)},
|
|
/* 33 */ {`builtin "string"; "<"+trimStr(" bye bye ")+">"`, "<bye bye>", nil},
|
|
/* 34 */ {`builtin "string"; subStr("0123456789", 1,2)`, "12", nil},
|
|
/* 35 */ {`builtin "string"; subStr("0123456789", -3,2)`, "78", nil},
|
|
/* 36 */ {`builtin "string"; subStr("0123456789", -3)`, "789", nil},
|
|
/* 37 */ {`builtin "string"; subStr("0123456789")`, "0123456789", nil},
|
|
/* 38 */ {`builtin "string"; startsWithStr("0123456789", "xyz", "012")`, true, nil},
|
|
/* 39 */ {`builtin "string"; startsWithStr("0123456789", "xyz", "0125")`, false, nil},
|
|
/* 40 */ {`builtin "string"; startsWithStr("0123456789")`, nil, errors.New(`too few params -- expected 2 or more, got 1`)},
|
|
/* 41 */ {`builtin "string"; endsWithStr("0123456789", "xyz", "789")`, true, nil},
|
|
/* 42 */ {`builtin "string"; endsWithStr("0123456789", "xyz", "0125")`, false, nil},
|
|
/* 43 */ {`builtin "string"; endsWithStr("0123456789")`, nil, errors.New(`too few params -- expected 2 or more, got 1`)},
|
|
/* 44 */ {`builtin "string"; splitStr("one-two-three", "-", )`, newListA("one", "two", "three"), nil},
|
|
/* 45 */ {`isInt(2+1)`, true, nil},
|
|
/* 46 */ {`isInt(3.1)`, false, nil},
|
|
/* 47 */ {`isFloat(3.1)`, true, nil},
|
|
/* 48 */ {`isString("3.1")`, true, nil},
|
|
/* 49 */ {`isString("3" + 1)`, true, nil},
|
|
/* 50 */ {`isList(["3", 1])`, true, nil},
|
|
/* 51 */ {`isDict({"a":"3", "b":1})`, true, nil},
|
|
/* 52 */ {`isFract(1|3)`, true, nil},
|
|
/* 53 */ {`isFract(3|1)`, false, nil},
|
|
/* 54 */ {`isRational(3|1)`, true, nil},
|
|
/* 55 */ {`builtin "math.arith"; add(1,2)`, int64(3), nil},
|
|
/* 56 */ {`fract("2.2(3)")`, newFraction(67, 30), nil},
|
|
/* 57 */ {`fract("1.21(3)")`, newFraction(91, 75), nil},
|
|
/* 58 */ {`fract(1.21(3))`, newFraction(91, 75), nil},
|
|
/* 59 */ {`fract(1.21)`, newFraction(121, 100), nil},
|
|
/* 60 */ {`dec(2)`, float64(2), nil},
|
|
/* 61 */ {`dec(2.0)`, float64(2), nil},
|
|
/* 62 */ {`dec("2.0")`, float64(2), nil},
|
|
/* 63 */ {`dec(true)`, float64(1), nil},
|
|
/* 64 */ {`dec(true")`, nil, errors.New("[1:11] missing string termination \"")},
|
|
/* 65 */ {`builtin "string"; joinStr("-", [1, "two", "three"])`, nil, errors.New(`joinStr() expected string, got int64 (1)`)},
|
|
/* 66 */ {`dec()`, nil, errors.New(`too few params -- expected 1, got 0`)},
|
|
/* 67 */ {`dec(1,2,3)`, nil, errors.New(`too much params -- expected 1, got 3`)},
|
|
/* 68 */ {`builtin "string"; joinStr()`, nil, errors.New(`too few params -- expected 1 or more, got 0`)},
|
|
/* 69 */ /*{`builtin "string"; $$global`, `vars: {
|
|
|
|
}
|
|
funcs: {
|
|
add(any=0 ...) -> number,
|
|
dec(any) -> decimal,
|
|
endsWithStr(source, suffix) -> boolean,
|
|
fract(any, denominator=1) -> fraction,
|
|
import( ...) -> any,
|
|
importAll( ...) -> any,
|
|
int(any) -> integer,
|
|
isBool(any) -> boolean,
|
|
isDec(any) -> boolean,
|
|
isDict(any) -> boolean,
|
|
isFloat(any) -> boolean,
|
|
isFract(any) -> boolean,
|
|
isInt(any) -> boolean,
|
|
isList(any) -> boolean,
|
|
isNil(any) -> boolean,
|
|
isString(any) -> boolean,
|
|
joinStr(separator, item="" ...) -> string,
|
|
mul(any=1 ...) -> number,
|
|
splitStr(source, separator="", count=-1) -> list of string,
|
|
startsWithStr(source, prefix) -> boolean,
|
|
subStr(source, start=0, count=-1) -> string,
|
|
trimStr(source) -> string
|
|
}
|
|
`, nil},*/
|
|
}
|
|
|
|
t.Setenv("EXPR_PATH", ".")
|
|
|
|
// parserTestSpec(t, "Func", inputs, 69)
|
|
parserTest(t, "Func", inputs)
|
|
}
|