|
18 | 18 | #include <utility> |
19 | 19 |
|
20 | 20 | #include "gtest/gtest-spi.h" |
| 21 | +#include "absl/container/flat_hash_map.h" |
21 | 22 | #include "absl/flags/flag.h" |
22 | 23 | #include "absl/log/absl_check.h" |
23 | 24 | #include "absl/status/status_matchers.h" |
@@ -57,6 +58,7 @@ using ::cel::expr::conformance::proto3::TestAllTypes; |
57 | 58 | using ::cel::expr::conformance::test::TestCase; |
58 | 59 | using ::cel::expr::CheckedExpr; |
59 | 60 | using ::google::api::expr::runtime::CelExpressionBuilder; |
| 61 | +using ValueProto = ::cel::expr::Value; |
60 | 62 |
|
61 | 63 | template <typename T> |
62 | 64 | T ParseTextProtoOrDie(absl::string_view text_proto) { |
@@ -190,7 +192,8 @@ TEST_P(TestRunnerParamTest, BasicTestReportsFailure) { |
190 | 192 | CelExpressionSource::FromCheckedExpr( |
191 | 193 | std::move(checked_expr))})); |
192 | 194 | TestRunner test_runner(std::move(context)); |
193 | | - EXPECT_NONFATAL_FAILURE(test_runner.RunTest(test_case), "bool_value: true"); |
| 195 | + EXPECT_NONFATAL_FAILURE(test_runner.RunTest(test_case), |
| 196 | + "bool_value: true"); // expected true got false |
194 | 197 | } |
195 | 198 |
|
196 | 199 | TEST_P(TestRunnerParamTest, DynamicInputAndOutputReportsSuccess) { |
@@ -248,7 +251,8 @@ TEST_P(TestRunnerParamTest, DynamicInputAndOutputReportsFailure) { |
248 | 251 | std::move(checked_expr)), |
249 | 252 | .compiler = std::move(compiler)})); |
250 | 253 | TestRunner test_runner(std::move(context)); |
251 | | - EXPECT_NONFATAL_FAILURE(test_runner.RunTest(test_case), "int64_value: 5"); |
| 254 | + EXPECT_NONFATAL_FAILURE(test_runner.RunTest(test_case), |
| 255 | + "int64_value: 5"); // expected 5 got 10 |
252 | 256 | } |
253 | 257 |
|
254 | 258 | TEST_P(TestRunnerParamTest, RawExpressionWithCompilerReportsSuccess) { |
@@ -296,7 +300,8 @@ TEST_P(TestRunnerParamTest, RawExpressionWithCompilerReportsFailure) { |
296 | 300 | CelExpressionSource::FromRawExpression("x - y"), |
297 | 301 | .compiler = std::move(compiler)})); |
298 | 302 | TestRunner test_runner(std::move(context)); |
299 | | - EXPECT_NONFATAL_FAILURE(test_runner.RunTest(test_case), "int64_value: 7"); |
| 303 | + EXPECT_NONFATAL_FAILURE(test_runner.RunTest(test_case), |
| 304 | + "int64_value: 7"); // expected 7 got 100 |
300 | 305 | } |
301 | 306 |
|
302 | 307 | TEST_P(TestRunnerParamTest, CelFileWithCompilerReportsSuccess) { |
@@ -350,7 +355,67 @@ TEST_P(TestRunnerParamTest, CelFileWithCompilerReportsFailure) { |
350 | 355 | CelExpressionSource::FromCelFile(cel_file_path), |
351 | 356 | .compiler = std::move(compiler)})); |
352 | 357 | TestRunner test_runner(std::move(context)); |
353 | | - EXPECT_NONFATAL_FAILURE(test_runner.RunTest(test_case), "int64_value: 7"); |
| 358 | + EXPECT_NONFATAL_FAILURE(test_runner.RunTest(test_case), |
| 359 | + "int64_value: 7"); // expected 7 got 123 |
| 360 | +} |
| 361 | + |
| 362 | +TEST_P(TestRunnerParamTest, BasicTestWithCustomBindingsSucceeds) { |
| 363 | + ASSERT_OK_AND_ASSIGN(cel::ValidationResult validation_result, |
| 364 | + DefaultCompiler().Compile("x + y")); |
| 365 | + CheckedExpr checked_expr; |
| 366 | + ASSERT_THAT(cel::AstToCheckedExpr(*validation_result.GetAst(), &checked_expr), |
| 367 | + absl_testing::IsOk()); |
| 368 | + |
| 369 | + TestCase test_case = ParseTextProtoOrDie<TestCase>(R"pb( |
| 370 | + input { |
| 371 | + key: "x" |
| 372 | + value { value { int64_value: 10 } } |
| 373 | + } |
| 374 | + output { result_value { int64_value: 15 } } |
| 375 | + )pb"); |
| 376 | + |
| 377 | + absl::flat_hash_map<std::string, ValueProto> bindings; |
| 378 | + bindings["y"] = ParseTextProtoOrDie<ValueProto>(R"pb(int64_value: 5)pb"); |
| 379 | + |
| 380 | + ASSERT_OK_AND_ASSIGN( |
| 381 | + auto context, CreateTestContext( |
| 382 | + /*options=*/{.expression_source = |
| 383 | + CelExpressionSource::FromCheckedExpr( |
| 384 | + std::move(checked_expr)), |
| 385 | + .custom_bindings = std::move(bindings)})); |
| 386 | + TestRunner test_runner(std::move(context)); |
| 387 | + |
| 388 | + EXPECT_NO_FATAL_FAILURE(test_runner.RunTest(test_case)); |
| 389 | +} |
| 390 | + |
| 391 | +TEST_P(TestRunnerParamTest, BasicTestWithCustomBindingsReportsFailure) { |
| 392 | + ASSERT_OK_AND_ASSIGN(cel::ValidationResult validation_result, |
| 393 | + DefaultCompiler().Compile("x + y")); |
| 394 | + CheckedExpr checked_expr; |
| 395 | + ASSERT_THAT(cel::AstToCheckedExpr(*validation_result.GetAst(), &checked_expr), |
| 396 | + absl_testing::IsOk()); |
| 397 | + |
| 398 | + TestCase test_case = ParseTextProtoOrDie<TestCase>(R"pb( |
| 399 | + input { |
| 400 | + key: "x" |
| 401 | + value { value { int64_value: 10 } } |
| 402 | + } |
| 403 | + output { result_value { int64_value: 999 } } |
| 404 | + )pb"); |
| 405 | + |
| 406 | + absl::flat_hash_map<std::string, ValueProto> bindings; |
| 407 | + bindings["y"] = ParseTextProtoOrDie<ValueProto>(R"pb(int64_value: 5)pb"); |
| 408 | + |
| 409 | + ASSERT_OK_AND_ASSIGN( |
| 410 | + auto context, CreateTestContext( |
| 411 | + /*options=*/{.expression_source = |
| 412 | + CelExpressionSource::FromCheckedExpr( |
| 413 | + std::move(checked_expr)), |
| 414 | + .custom_bindings = std::move(bindings)})); |
| 415 | + TestRunner test_runner(std::move(context)); |
| 416 | + |
| 417 | + EXPECT_NONFATAL_FAILURE(test_runner.RunTest(test_case), |
| 418 | + "int64_value: 15"); // expected 15 got 999. |
354 | 419 | } |
355 | 420 |
|
356 | 421 | INSTANTIATE_TEST_SUITE_P(TestRunnerTests, TestRunnerParamTest, |
|
0 commit comments