2024-04-20 07:29:42 +02:00
// 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 {
2024-04-20 07:41:58 +02:00
/* 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 ` ) } ,
2024-05-10 04:44:08 +02:00
/* 10 */ { ` int("432", 4) ` , nil , errors . New ( ` too much params -- expected 1, got 2 ` ) } ,
2024-04-20 07:41:58 +02:00
/* 11 */ { ` int(nil) ` , nil , errors . New ( ` int() can't convert <nil> to int ` ) } ,
2024-05-06 05:52:25 +02:00
/* 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 } ,
2024-05-06 15:32:44 +02:00
/* 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 ` ) } ,
2024-05-10 04:44:08 +02:00
/* 44 */ { ` builtin "string"; splitStr("one-two-three", "-", ) ` , newListA ( "one" , "two" , "three" ) , nil } ,
/* 45 */ { ` isInt(2+1) ` , true , nil } ,
/* 46 */ { ` isInt(3.1) ` , false , nil } ,
2024-05-10 09:18:32 +02:00
/* 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 } ,
2024-05-11 10:45:38 +02:00
/* 55 */ { ` builtin "math.arith"; add(1,2) ` , int64 ( 3 ) , nil } ,
2024-04-20 07:29:42 +02:00
}
2024-05-06 10:38:45 +02:00
t . Setenv ( "EXPR_PATH" , "." )
2024-05-11 10:45:38 +02:00
//parserTest(t, "Func", inputs[54:55])
2024-05-06 05:52:25 +02:00
parserTest ( t , "Func" , inputs )
2024-04-20 07:29:42 +02:00
}