| 367 | } |
| 368 | |
| 369 | func TestStringsVersions(t *testing.T) { |
| 370 | versionCases := []struct { |
| 371 | version uint32 |
| 372 | supportedFunctions map[string]string |
| 373 | }{ |
| 374 | { |
| 375 | version: 0, |
| 376 | supportedFunctions: map[string]string{ |
| 377 | "chatAt": "''.charAt(0) == ''", |
| 378 | "indexOf": "'a'.indexOf('a') == 0", |
| 379 | "lastIndexOf": "'a'.lastIndexOf('a') == 0", |
| 380 | "join": "['a', 'b'].join() == 'ab'", |
| 381 | "joinSep": "['a', 'b'].join('-') == 'a-b'", |
| 382 | "lowerAscii": "'a'.lowerAscii() == 'a'", |
| 383 | "replace": "'hello hello'.replace('he', 'we') == 'wello wello'", |
| 384 | "split": "'hello hello hello'.split(' ') == ['hello', 'hello', 'hello']", |
| 385 | "substring": "'tacocat'.substring(4) == 'cat'", |
| 386 | "trim": "' \\ttrim\\n '.trim() == 'trim'", |
| 387 | "upperAscii": "'TacoCat'.upperAscii() == 'TACOCAT'", |
| 388 | }, |
| 389 | }, |
| 390 | { |
| 391 | version: 1, |
| 392 | supportedFunctions: map[string]string{ |
| 393 | "format": "'a %d'.format([1]) == 'a 1'", |
| 394 | "quote": `strings.quote('\a \b "double quotes"') == '"\\a \\b \\"double quotes\\""'`, |
| 395 | }, |
| 396 | }, |
| 397 | { |
| 398 | version: 3, |
| 399 | supportedFunctions: map[string]string{ |
| 400 | "reverse": "'taco'.reverse() == 'ocat'", |
| 401 | }, |
| 402 | }, |
| 403 | } |
| 404 | for _, lib := range versionCases { |
| 405 | env, err := cel.NewEnv(Strings(StringsVersion(lib.version))) |
| 406 | if err != nil { |
| 407 | t.Fatalf("cel.NewEnv(Strings(StringsVersion(%d))) failed: %v", lib.version, err) |
| 408 | } |
| 409 | t.Run(fmt.Sprintf("version=%d", lib.version), func(t *testing.T) { |
| 410 | for _, tc := range versionCases { |
| 411 | for name, expr := range tc.supportedFunctions { |
| 412 | supported := lib.version >= tc.version |
| 413 | t.Run(fmt.Sprintf("%s-supported=%t", name, supported), func(t *testing.T) { |
| 414 | ast, iss := env.Compile(expr) |
| 415 | if supported { |
| 416 | if iss.Err() != nil { |
| 417 | t.Errorf("unexpected error: %v", iss.Err()) |
| 418 | } |
| 419 | } else { |
| 420 | if iss.Err() == nil || !strings.Contains(iss.Err().Error(), "undeclared reference") { |
| 421 | t.Errorf("got error %v, wanted error %s for expr: %s, version: %d", iss.Err(), "undeclared reference", expr, tc.version) |
| 422 | } |
| 423 | return |
| 424 | } |
| 425 | prg, err := env.Program(ast) |
| 426 | if err != nil { |