| 244 | } |
| 245 | |
| 246 | func TestBuiltin_errors(t *testing.T) { |
| 247 | var errorTests = []struct { |
| 248 | input string |
| 249 | err string |
| 250 | }{ |
| 251 | {`len()`, `invalid number of arguments (expected 1, got 0)`}, |
| 252 | {`len(1)`, `invalid argument for len (type int)`}, |
| 253 | {`abs()`, `invalid number of arguments (expected 1, got 0)`}, |
| 254 | {`abs(1, 2)`, `invalid number of arguments (expected 1, got 2)`}, |
| 255 | {`abs("foo")`, `invalid argument for abs (type string)`}, |
| 256 | {`int()`, `invalid number of arguments (expected 1, got 0)`}, |
| 257 | {`int(1, 2)`, `invalid number of arguments (expected 1, got 2)`}, |
| 258 | {`float()`, `invalid number of arguments (expected 1, got 0)`}, |
| 259 | {`float(1, 2)`, `invalid number of arguments (expected 1, got 2)`}, |
| 260 | {`string(1, 2)`, `too many arguments to call string`}, |
| 261 | {`trim()`, `not enough arguments to call trim`}, |
| 262 | {`max()`, `not enough arguments to call max`}, |
| 263 | {`max(1, "2")`, `invalid argument for max (type string)`}, |
| 264 | {`max([1, "2"])`, `invalid argument for max (type string)`}, |
| 265 | {`min()`, `not enough arguments to call min`}, |
| 266 | {`min(1, "2")`, `invalid argument for min (type string)`}, |
| 267 | {`min([1, "2"])`, `invalid argument for min (type string)`}, |
| 268 | {`median(1..9, "t")`, "invalid argument for median (type string)"}, |
| 269 | {`mean("s", 1..9)`, "invalid argument for mean (type string)"}, |
| 270 | {`duration("error")`, `invalid duration`}, |
| 271 | {`date("error")`, `invalid date`}, |
| 272 | {`get()`, `invalid number of arguments (expected 2, got 0)`}, |
| 273 | {`get(1, 2)`, `type int does not support indexing`}, |
| 274 | {`bitnot("1")`, "cannot use string as argument (type int) to call bitnot (1:8)"}, |
| 275 | {`bitand("1", 1)`, "cannot use string as argument (type int) to call bitand (1:8)"}, |
| 276 | {`"10" | bitor(1)`, "cannot use string as argument (type int) to call bitor (1:1)"}, |
| 277 | {`bitshr("5", 1)`, "cannot use string as argument (type int) to call bitshr (1:8)"}, |
| 278 | {`bitshr(-5, -2)`, "invalid operation: negative shift count -2 (type int) (1:1)"}, |
| 279 | {`bitshl(1, -1)`, "invalid operation: negative shift count -1 (type int) (1:1)"}, |
| 280 | {`bitushr(-5, -2)`, "invalid operation: negative shift count -2 (type int) (1:1)"}, |
| 281 | {`now(nil)`, "invalid number of arguments (expected 0, got 1)"}, |
| 282 | {`date(nil)`, "interface {} is nil, not string (1:1)"}, |
| 283 | {`timezone(nil)`, "cannot use nil as argument (type string) to call timezone (1:10)"}, |
| 284 | {`flatten([1, 2], [3, 4])`, "invalid number of arguments (expected 1, got 2)"}, |
| 285 | {`flatten(1)`, "cannot flatten int"}, |
| 286 | {`fromJSON("5e2482")`, "cannot unmarshal number"}, |
| 287 | } |
| 288 | for _, test := range errorTests { |
| 289 | t.Run(test.input, func(t *testing.T) { |
| 290 | program, err := expr.Compile(test.input) |
| 291 | if err != nil { |
| 292 | assert.Error(t, err) |
| 293 | assert.Contains(t, err.Error(), test.err) |
| 294 | } else { |
| 295 | _, err = expr.Run(program, nil) |
| 296 | assert.Error(t, err) |
| 297 | assert.Contains(t, err.Error(), test.err) |
| 298 | } |
| 299 | }) |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | func TestBuiltin_env_not_callable(t *testing.T) { |