11package builtin_test
22
33import (
4+ "fmt"
5+ "reflect"
46 "testing"
57 "time"
68
79 "github.com/antonmedv/expr"
10+ "github.com/antonmedv/expr/checker"
11+ "github.com/antonmedv/expr/conf"
12+ "github.com/antonmedv/expr/parser"
813 "github.com/stretchr/testify/assert"
914 "github.com/stretchr/testify/require"
1015)
@@ -96,32 +101,31 @@ func TestBuiltin(t *testing.T) {
96101 }
97102}
98103
99- var errorTests = []struct {
100- input string
101- err string
102- }{
103- {`len()` , `invalid number of arguments for len (expected 1, got 0)` },
104- {`len(1)` , `invalid argument for len (type int)` },
105- {`abs()` , `invalid number of arguments for abs (expected 1, got 0)` },
106- {`abs(1, 2)` , `invalid number of arguments for abs (expected 1, got 2)` },
107- {`abs("foo")` , `invalid argument for abs (type string)` },
108- {`int()` , `invalid number of arguments for int (expected 1, got 0)` },
109- {`int(1, 2)` , `invalid number of arguments for int (expected 1, got 2)` },
110- {`float()` , `invalid number of arguments for float (expected 1, got 0)` },
111- {`float(1, 2)` , `invalid number of arguments for float (expected 1, got 2)` },
112- {`string(1, 2)` , `too many arguments to call string` },
113- {`trim()` , `not enough arguments to call trim` },
114- {`max()` , `not enough arguments to call max` },
115- {`max(1, "2")` , `invalid argument for max (type string)` },
116- {`min()` , `not enough arguments to call min` },
117- {`min(1, "2")` , `invalid argument for min (type string)` },
118- {`duration("error")` , `invalid duration` },
119- {`date("error")` , `invalid date` },
120- {`get()` , `invalid number of arguments for get (expected 2, got 0)` },
121- {`get(1, 2)` , `cannot get int from int` },
122- }
123-
124- func TestBuiltinErrors (t * testing.T ) {
104+ func TestBuiltin_errors (t * testing.T ) {
105+ var errorTests = []struct {
106+ input string
107+ err string
108+ }{
109+ {`len()` , `invalid number of arguments (expected 1, got 0)` },
110+ {`len(1)` , `invalid argument for len (type int)` },
111+ {`abs()` , `invalid number of arguments (expected 1, got 0)` },
112+ {`abs(1, 2)` , `invalid number of arguments (expected 1, got 2)` },
113+ {`abs("foo")` , `invalid argument for abs (type string)` },
114+ {`int()` , `invalid number of arguments (expected 1, got 0)` },
115+ {`int(1, 2)` , `invalid number of arguments (expected 1, got 2)` },
116+ {`float()` , `invalid number of arguments (expected 1, got 0)` },
117+ {`float(1, 2)` , `invalid number of arguments (expected 1, got 2)` },
118+ {`string(1, 2)` , `too many arguments to call string` },
119+ {`trim()` , `not enough arguments to call trim` },
120+ {`max()` , `not enough arguments to call max` },
121+ {`max(1, "2")` , `invalid argument for max (type string)` },
122+ {`min()` , `not enough arguments to call min` },
123+ {`min(1, "2")` , `invalid argument for min (type string)` },
124+ {`duration("error")` , `invalid duration` },
125+ {`date("error")` , `invalid date` },
126+ {`get()` , `invalid number of arguments (expected 2, got 0)` },
127+ {`get(1, 2)` , `type int does not support indexing` },
128+ }
125129 for _ , test := range errorTests {
126130 t .Run (test .input , func (t * testing.T ) {
127131 _ , err := expr .Eval (test .input , nil )
@@ -130,3 +134,38 @@ func TestBuiltinErrors(t *testing.T) {
130134 })
131135 }
132136}
137+
138+ func TestBuiltin_types (t * testing.T ) {
139+ env := map [string ]interface {}{
140+ "num" : 42 ,
141+ "str" : "foo" ,
142+ "ArrayOfString" : []string {"foo" , "bar" , "baz" },
143+ "ArrayOfInt" : []int {1 , 2 , 3 },
144+ }
145+
146+ tests := []struct {
147+ input string
148+ want reflect.Kind
149+ }{
150+ {`get(ArrayOfString, 0)` , reflect .String },
151+ {`get(ArrayOfInt, 0)` , reflect .Int },
152+ {`first(ArrayOfString)` , reflect .String },
153+ {`first(ArrayOfInt)` , reflect .Int },
154+ {`last(ArrayOfString)` , reflect .String },
155+ {`last(ArrayOfInt)` , reflect .Int },
156+ {`get(env, 'str')` , reflect .String },
157+ {`get(env, 'num')` , reflect .Int },
158+ {`get(env, 'ArrayOfString')` , reflect .Slice },
159+ }
160+
161+ for _ , test := range tests {
162+ t .Run (test .input , func (t * testing.T ) {
163+ tree , err := parser .Parse (test .input )
164+ require .NoError (t , err )
165+
166+ rtype , err := checker .Check (tree , conf .New (env ))
167+ require .NoError (t , err )
168+ require .True (t , rtype .Kind () == test .want , fmt .Sprintf ("expected %s, got %s" , test .want , rtype .Kind ()))
169+ })
170+ }
171+ }
0 commit comments