|
20 | 20 | #include <vector> |
21 | 21 |
|
22 | 22 | #include "cel/expr/syntax.pb.h" |
| 23 | +#include "absl/algorithm/container.h" |
23 | 24 | #include "absl/status/status.h" |
24 | 25 | #include "absl/status/status_matchers.h" |
25 | 26 | #include "absl/strings/string_view.h" |
| 27 | +#include "checker/type_check_issue.h" |
26 | 28 | #include "checker/validation_result.h" |
27 | 29 | #include "common/source.h" |
28 | 30 | #include "common/value.h" |
@@ -54,7 +56,9 @@ using ::cel::test::ErrorValueIs; |
54 | 56 | using ::cel::expr::Expr; |
55 | 57 | using ::cel::expr::ParsedExpr; |
56 | 58 | using ::cel::expr::SourceInfo; |
| 59 | +using ::testing::Contains; |
57 | 60 | using ::testing::HasSubstr; |
| 61 | +using ::testing::IsEmpty; |
58 | 62 | using ::testing::ValuesIn; |
59 | 63 |
|
60 | 64 | struct TestInfo { |
@@ -377,5 +381,81 @@ std::vector<ListCheckerTestCase> createListsCheckerParams() { |
377 | 381 | INSTANTIATE_TEST_SUITE_P(ListsCheckerLibraryTest, ListsCheckerLibraryTest, |
378 | 382 | ValuesIn(createListsCheckerParams())); |
379 | 383 |
|
| 384 | +struct ListsExtensionVersionTestCase { |
| 385 | + std::string expr; |
| 386 | + std::vector<int> expected_supported_versions; |
| 387 | +}; |
| 388 | + |
| 389 | +class ListsExtensionVersionTest |
| 390 | + : public ::testing::TestWithParam<ListsExtensionVersionTestCase> {}; |
| 391 | + |
| 392 | +TEST_P(ListsExtensionVersionTest, ListsExtensionVersions) { |
| 393 | + const ListsExtensionVersionTestCase& test_case = GetParam(); |
| 394 | + for (int version = 0; |
| 395 | + version <= cel::extensions::kListsExtensionLatestVersion; ++version) { |
| 396 | + CompilerLibrary compiler_library = ListsCompilerLibrary(version); |
| 397 | + |
| 398 | + ASSERT_OK_AND_ASSIGN( |
| 399 | + std::unique_ptr<CompilerBuilder> builder, |
| 400 | + cel::NewCompilerBuilder(internal::GetTestingDescriptorPool(), |
| 401 | + CompilerOptions())); |
| 402 | + ASSERT_THAT(builder->AddLibrary(StandardCompilerLibrary()), IsOk()); |
| 403 | + ASSERT_THAT(builder->AddLibrary(std::move(compiler_library)), IsOk()); |
| 404 | + |
| 405 | + ASSERT_OK_AND_ASSIGN(std::unique_ptr<Compiler> compiler, builder->Build()); |
| 406 | + ASSERT_OK_AND_ASSIGN(ValidationResult result, |
| 407 | + compiler->Compile(test_case.expr)); |
| 408 | + if (absl::c_contains(test_case.expected_supported_versions, version)) { |
| 409 | + EXPECT_THAT(result.GetIssues(), IsEmpty()) |
| 410 | + << "Expected no issues for expr: " << test_case.expr |
| 411 | + << " at version: " << version << " but got: " << result.FormatError(); |
| 412 | + } else { |
| 413 | + EXPECT_THAT(result.GetIssues(), |
| 414 | + Contains(Property(&TypeCheckIssue::message, |
| 415 | + HasSubstr("undeclared reference")))); |
| 416 | + } |
| 417 | + } |
| 418 | +}; |
| 419 | + |
| 420 | +std::vector<ListsExtensionVersionTestCase> CreateListsExtensionVersionParams() { |
| 421 | + return { |
| 422 | + ListsExtensionVersionTestCase{ |
| 423 | + .expr = "[0,1,2,3].slice(0, 2)", |
| 424 | + .expected_supported_versions = {0, 1, 2}, |
| 425 | + }, |
| 426 | + ListsExtensionVersionTestCase{ |
| 427 | + .expr = "[[0]].flatten()", |
| 428 | + .expected_supported_versions = {1, 2}, |
| 429 | + }, |
| 430 | + ListsExtensionVersionTestCase{ |
| 431 | + .expr = "[[0]].flatten(1)", |
| 432 | + .expected_supported_versions = {1, 2}, |
| 433 | + }, |
| 434 | + ListsExtensionVersionTestCase{ |
| 435 | + .expr = "[1,2,3,4].sort()", |
| 436 | + .expected_supported_versions = {2}, |
| 437 | + }, |
| 438 | + ListsExtensionVersionTestCase{ |
| 439 | + .expr = "[1,2,3,4].sortBy(x, x)", |
| 440 | + .expected_supported_versions = {2}, |
| 441 | + }, |
| 442 | + ListsExtensionVersionTestCase{ |
| 443 | + .expr = "[1,2,3,4].distinct()", |
| 444 | + .expected_supported_versions = {2}, |
| 445 | + }, |
| 446 | + ListsExtensionVersionTestCase{ |
| 447 | + .expr = "lists.range(4)", |
| 448 | + .expected_supported_versions = {2}, |
| 449 | + }, |
| 450 | + ListsExtensionVersionTestCase{ |
| 451 | + .expr = "[1,2,3,4].reverse()", |
| 452 | + .expected_supported_versions = {2}, |
| 453 | + }, |
| 454 | + }; |
| 455 | +} |
| 456 | + |
| 457 | +INSTANTIATE_TEST_SUITE_P(ListsExtensionVersionTest, ListsExtensionVersionTest, |
| 458 | + ValuesIn(CreateListsExtensionVersionParams())); |
| 459 | + |
380 | 460 | } // namespace |
381 | 461 | } // namespace cel::extensions |
0 commit comments