1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
|
package checker_test
import (
"fmt"
"strings"
"testing"
"time"
"github.com/antonmedv/expr"
"github.com/antonmedv/expr/checker"
"github.com/antonmedv/expr/conf"
"github.com/antonmedv/expr/parser"
"github.com/stretchr/testify/assert"
)
func TestCheck_debug(t *testing.T) {
input := `2**3 + 1`
tree, err := parser.Parse(input)
assert.NoError(t, err)
out, err := checker.Check(tree, conf.New(&mockEnv{}))
assert.NoError(t, err)
if err == nil {
assert.Equal(t, "float64", out.Name())
}
}
func TestVisitor_FunctionNode(t *testing.T) {
var err error
env := &mockEnv{}
input := `Set(1, "tag") + Add(2) + Get() + Sub(3) + Any()`
tree, err := parser.Parse(input)
assert.NoError(t, err)
out, err := checker.Check(tree, conf.New(env))
assert.NoError(t, err)
if err == nil {
assert.Equal(t, "int64", out.Name())
}
}
func TestVisitor_MethodNode(t *testing.T) {
var err error
env := &mockEnv{}
input := `Var.Set(1, 0.5)
+ Var.Add(2)
+ Var.Any(true)
+ Var.Get()
+ Var.Sub(3)
+ (Duration.String() == "" ? 1 : 0)
+ Interface.Method(0)
+ Tickets[0].Method(0)`
tree, err := parser.Parse(input)
assert.NoError(t, err)
out, err := checker.Check(tree, conf.New(env))
assert.NoError(t, err)
if err == nil {
assert.Equal(t, "int64", out.Name())
}
}
func TestVisitor_BuiltinNode(t *testing.T) {
var typeTests = []string{
`all(Tickets, {.Price > 0}) && any(map(Tickets, {.Price}), {# < 1000})`,
`filter(map(Tickets, {.Origin}), {len(#) != 3})[0]`,
`none(Any, {#.Any < 1})`,
`none(Any, {.Thing != "awesome"})`,
}
for _, input := range typeTests {
tree, err := parser.Parse(input)
assert.NoError(t, err)
_, err = checker.Check(tree, conf.New(&mockEnv{}))
assert.NoError(t, err)
}
}
func TestCheck(t *testing.T) {
var typeTests = []string{
"!Bool",
"!BoolPtr == Bool",
"'a' == 'b' + 'c'",
"'foo' contains 'bar'",
"'foo' endsWith 'bar'",
"'foo' startsWith 'bar'",
"(1 == 1) || (String matches Any)",
"1 + 2 + Int64",
"1 + 2 == FloatPtr",
"1 + 2 + Float + 3 + 4",
"1 + Int + Float",
"1 < Float",
"1 <= Float",
"1 == 2 and true or Bool",
"1 > Float",
"1 >= Float",
"2**3 + 1",
"[1,2,3]",
"Abc == Float",
"Abc()",
"Any matches Any",
"Any.Thing.Is.Bool",
"ArrayOfAny['string'].next.goes['any thing']",
"ArrayOfFoo[0].Bar.Baz",
"ArrayOfFoo[1].Int64 + 1",
"Bool && Any",
"BoolFn() and BoolFn()",
"EmbedPtr.EmbPtrStr + String",
"EmbPtrStr == ''",
"Float == 1",
"Float == Abc",
"Fn(true, 1, 'str', Any)",
"Foo.Abc()",
"Foo.Bar == Map.id.Bar",
"Foo.Bar.Baz",
"Foo.Fn() or Foo.Fn()",
"Foo.Fn()",
"Foo2p.Bar.Baz",
"Int % Int > 1",
"Int + Int + Int",
"Int == Any",
"Int in Int..Int",
"Int64 % 1",
"IntPtr == Int",
"len([])",
"Map.id.Bar.Baz",
"Map['string'].Bar.Baz",
"Method(Foo.Bar) > 1",
"nil == IntPtr",
"nil == nil",
"nil in ArrayOfFoo",
"nil",
"String + (true ? String : String)",
"String in ArrayOfFoo",
"String in Foo",
"String matches 'ok'",
"String matches Any",
"String not in Foo2p",
"StringPtr == nil",
"Sub.Method(0) + String",
"Sub.SubString",
"SubStr + ''",
"SubString == ''",
"SubSub.SubStr",
"true == false",
"true ? Any : Any",
"{id: Foo.Bar.Baz, 'str': Bool}",
`"a" < "b"`,
"Variadic('', 1, 2) + Variadic('')",
"Foo.Variadic('', 1, 2) + Foo.Variadic('')",
"count(1..30, {# % 3 == 0}) > 0",
"map(1..3, {#}) == [1,2,3]",
"map(filter(ArrayOfFoo, {.Int64 > 0}), {.Bar})",
}
for _, test := range typeTests {
var err error
tree, err := parser.Parse(test)
assert.NoError(t, err, test)
_, err = checker.Check(tree, conf.New(mockEnv2{}))
assert.NoError(t, err, test)
}
}
const errorTests = `
Foo.Bar.Not
type checker_test.bar has no field Not (1:9)
| Foo.Bar.Not
| ........^
Noo
unknown name Noo (1:1)
| Noo
| ^
Foo()
unknown func Foo (1:1)
| Foo()
| ^
Foo['string']
invalid operation: type *checker_test.foo does not support indexing (1:4)
| Foo['string']
| ...^
Foo.Fn(Not)
too many arguments to call Fn (1:5)
| Foo.Fn(Not)
| ....^
Foo.Bar()
type *checker_test.foo has no method Bar (1:5)
| Foo.Bar()
| ....^
Foo.Bar.Not()
type checker_test.bar has no method Not (1:9)
| Foo.Bar.Not()
| ........^
ArrayOfFoo[0].Not
type *checker_test.foo has no field Not (1:15)
| ArrayOfFoo[0].Not
| ..............^
ArrayOfFoo[Not]
unknown name Not (1:12)
| ArrayOfFoo[Not]
| ...........^
Not[0]
unknown name Not (1:1)
| Not[0]
| ^
Not.Bar
unknown name Not (1:1)
| Not.Bar
| ^
ArrayOfFoo.Not
type []*checker_test.foo has no field Not (1:12)
| ArrayOfFoo.Not
| ...........^
Fn(Not)
not enough arguments to call Fn (1:1)
| Fn(Not)
| ^
Map['str'].Not
type *checker_test.foo has no field Not (1:12)
| Map['str'].Not
| ...........^
Bool && IntPtr
invalid operation: && (mismatched types bool and *int) (1:6)
| Bool && IntPtr
| .....^
No ? Any.Bool : Any.Not
unknown name No (1:1)
| No ? Any.Bool : Any.Not
| ^
Any.Cond ? No : Any.Not
unknown name No (1:12)
| Any.Cond ? No : Any.Not
| ...........^
Any.Cond ? Any.Bool : No
unknown name No (1:23)
| Any.Cond ? Any.Bool : No
| ......................^
ManOfAny ? Any : Any
non-bool expression (type map[string]interface {}) used as condition (1:1)
| ManOfAny ? Any : Any
| ^
String matches Int
invalid operation: matches (mismatched types string and int) (1:8)
| String matches Int
| .......^
Int matches String
invalid operation: matches (mismatched types int and string) (1:5)
| Int matches String
| ....^
String contains Int
invalid operation: contains (mismatched types string and int) (1:8)
| String contains Int
| .......^
Int contains String
invalid operation: contains (mismatched types int and string) (1:5)
| Int contains String
| ....^
!Not
unknown name Not (1:2)
| !Not
| .^
Not == Any
unknown name Not (1:1)
| Not == Any
| ^
[Not]
unknown name Not (1:2)
| [Not]
| .^
{id: Not}
unknown name Not (1:6)
| {id: Not}
| .....^
(nil).Foo
type <nil> has no field Foo (1:7)
| (nil).Foo
| ......^
(nil)['Foo']
invalid operation: type <nil> does not support indexing (1:6)
| (nil)['Foo']
| .....^
1 and false
invalid operation: and (mismatched types int and bool) (1:3)
| 1 and false
| ..^
true or 0
invalid operation: or (mismatched types bool and int) (1:6)
| true or 0
| .....^
not IntPtr
invalid operation: not (mismatched type *int) (1:1)
| not IntPtr
| ^
len(Not)
unknown name Not (1:5)
| len(Not)
| ....^
Int < Bool
invalid operation: < (mismatched types int and bool) (1:5)
| Int < Bool
| ....^
Int > Bool
invalid operation: > (mismatched types int and bool) (1:5)
| Int > Bool
| ....^
Int >= Bool
invalid operation: >= (mismatched types int and bool) (1:5)
| Int >= Bool
| ....^
Int <= Bool
invalid operation: <= (mismatched types int and bool) (1:5)
| Int <= Bool
| ....^
Int + Bool
invalid operation: + (mismatched types int and bool) (1:5)
| Int + Bool
| ....^
Int - Bool
invalid operation: - (mismatched types int and bool) (1:5)
| Int - Bool
| ....^
Int * Bool
invalid operation: * (mismatched types int and bool) (1:5)
| Int * Bool
| ....^
Int / Bool
invalid operation: / (mismatched types int and bool) (1:5)
| Int / Bool
| ....^
Int % Bool
invalid operation: % (mismatched types int and bool) (1:5)
| Int % Bool
| ....^
Int ** Bool
invalid operation: ** (mismatched types int and bool) (1:5)
| Int ** Bool
| ....^
Int .. Bool
invalid operation: .. (mismatched types int and bool) (1:5)
| Int .. Bool
| ....^
NilFn() and BoolFn()
func NilFn doesn't return value (1:1)
| NilFn() and BoolFn()
| ^
'str' in String
invalid operation: in (mismatched types string and string) (1:7)
| 'str' in String
| ......^
1 in Foo
invalid operation: in (mismatched types int and *checker_test.foo) (1:3)
| 1 in Foo
| ..^
1 + ''
invalid operation: + (mismatched types int and string) (1:3)
| 1 + ''
| ..^
all(ArrayOfFoo, {#.Fn() < 0})
invalid operation: < (mismatched types bool and int) (1:25)
| all(ArrayOfFoo, {#.Fn() < 0})
| ........................^
map(Any, {0})[0] + "str"
invalid operation: + (mismatched types int and string) (1:18)
| map(Any, {0})[0] + "str"
| .................^
Variadic()
not enough arguments to call Variadic (1:1)
| Variadic()
| ^
Variadic('', '')
cannot use string as argument (type int) to call Variadic (1:14)
| Variadic('', '')
| .............^
Foo.Variadic()
not enough arguments to call Variadic (1:5)
| Foo.Variadic()
| ....^
Foo.Variadic('', '')
cannot use string as argument (type int) to call Variadic (1:18)
| Foo.Variadic('', '')
| .................^
count(1, {#})
builtin count takes only array (got int) (1:7)
| count(1, {#})
| ......^
count(ArrayOfInt, {#})
closure should return boolean (got int) (1:19)
| count(ArrayOfInt, {#})
| ..................^
all(ArrayOfInt, {# + 1})
closure should return boolean (got int) (1:17)
| all(ArrayOfInt, {# + 1})
| ................^
filter(ArrayOfFoo, {.Int64})
closure should return boolean (got int64) (1:20)
| filter(ArrayOfFoo, {.Int64})
| ...................^
map(1, {2})
builtin map takes only array (got int) (1:5)
| map(1, {2})
| ....^
map(filter(ArrayOfFoo, {.Int64 > 0}), {.Var})
type *checker_test.foo has no field Var (1:41)
| map(filter(ArrayOfFoo, {.Int64 > 0}), {.Var})
| ........................................^
`
func TestCheck_error(t *testing.T) {
tests := strings.Split(strings.Trim(errorTests, "\n"), "\n\n")
for _, test := range tests {
input := strings.SplitN(test, "\n", 2)
if len(input) != 2 {
t.Errorf("syntax error in test: %q", test)
break
}
tree, err := parser.Parse(input[0])
assert.NoError(t, err)
_, err = checker.Check(tree, conf.New(mockEnv2{}))
if err == nil {
err = fmt.Errorf("<nil>")
}
assert.Equal(t, input[1], err.Error(), input[0])
}
}
func TestCheck_AsBool(t *testing.T) {
input := `1+2`
tree, err := parser.Parse(input)
assert.NoError(t, err)
config := &conf.Config{}
expr.AsBool()(config)
_, err = checker.Check(tree, config)
assert.Error(t, err)
assert.Equal(t, "expected bool, but got int", err.Error())
}
//
// Mock types
//
type mockEnv struct {
*mockEmbed
Add func(int64) int64
Any interface{}
Var *mockVar
Tickets []mockTicket
Duration time.Duration
Interface mockInterface
}
func (f *mockEnv) Set(v int64, any interface{}) int64 {
return v
}
type mockEmbed struct {
EmbedVar int64
Sub func(int64) int64
}
func (f *mockEmbed) Get() int64 {
return 0
}
type mockVar struct {
*mockEmbed
Add func(int64) int64
Any interface{}
}
func (*mockVar) Set(v int64, f float64) int64 {
return 0
}
type mockInterface interface {
Method(int) int
}
type mockTicket struct {
Price int
Origin string
}
func (t mockTicket) Method(int) int {
return 0
}
type abc interface {
Abc()
}
type bar struct {
Baz string
}
type foo struct {
Int64 int64
Bar bar
Fn func() bool
Abc abc
Variadic func(head string, xs ...int) int
}
type SubSub struct {
SubStr string
}
type Sub struct {
SubSub
SubString string
}
func (p Sub) Method(i int) string {
return ""
}
type EmbedPtr struct {
EmbPtrStr string
}
type mockEnv2 struct {
Sub
*EmbedPtr
Abc abc
Foo *foo
ArrayOfFoo []*foo
Map map[string]*foo
Any interface{}
ArrayOfAny []interface{}
ArrayOfInt []int
ManOfAny map[string]interface{}
Fn func(bool, int, string, interface{}) string
Bool bool
Float float64
Int64 int64
Int int
String string
BoolPtr *bool
FloatPtr *float64
IntPtr *int
StringPtr *string
Foo2p **foo
BoolFn func() bool
NilFn func()
Variadic func(head string, xs ...int) int
}
func (p mockEnv2) Method(_ bar) int {
return 0
}
|