Skip to content

Commit 7975fdd

Browse files
ChinmayMadeshicopybara-github
authored andcommitted
Support for error message assertion.
PiperOrigin-RevId: 797132225
1 parent c88cedf commit 7975fdd

4 files changed

Lines changed: 71 additions & 1 deletion

File tree

testing/testrunner/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ cc_library(
4242
"@com_google_absl//absl/status",
4343
"@com_google_absl//absl/status:statusor",
4444
"@com_google_absl//absl/strings:string_view",
45+
"@com_google_cel_spec//proto/cel/expr:value_cc_proto",
4546
"@com_google_cel_spec//proto/cel/expr/conformance/test:suite_cc_proto",
4647
"@com_google_protobuf//:differencer",
4748
"@com_google_protobuf//:protobuf",

testing/testrunner/runner_lib.cc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <memory>
1717
#include <utility>
1818

19+
#include "cel/expr/eval.pb.h"
1920
#include "absl/status/status.h"
2021
#include "absl/status/statusor.h"
2122
#include "absl/strings/string_view.h"
@@ -226,13 +227,29 @@ void TestRunner::AssertValue(const cel::Value& computed,
226227
EXPECT_THAT(expected_value_proto, MatchesValue(computed_expr_value));
227228
}
228229

230+
void TestRunner::AssertError(const cel::Value& computed,
231+
const TestOutput& output) {
232+
if (!computed.IsError()) {
233+
ADD_FAILURE() << "Expected error but got value: " << computed.DebugString();
234+
return;
235+
}
236+
absl::Status computed_status = computed.AsError()->ToStatus();
237+
// We selected the first error in the set for comparison because there is only
238+
// one runtime error that is reported even if there are multiple errors in the
239+
// critical path.
240+
ASSERT_TRUE(output.eval_error().errors_size() == 1)
241+
<< "Expected exactly one error but got: "
242+
<< output.eval_error().errors_size();
243+
ASSERT_EQ(computed_status.message(), output.eval_error().errors(0).message());
244+
}
245+
229246
void TestRunner::Assert(const cel::Value& computed, const TestCase& test_case,
230247
google::protobuf::Arena* arena) {
231248
TestOutput output = test_case.output();
232249
if (output.has_result_value() || output.has_result_expr()) {
233250
AssertValue(computed, output, arena);
234251
} else if (output.has_eval_error()) {
235-
ADD_FAILURE() << "Error assertion not implemented yet.";
252+
AssertError(computed, output);
236253
} else if (output.has_unknown()) {
237254
ADD_FAILURE() << "Unknown assertions not implemented yet.";
238255
} else {

testing/testrunner/runner_lib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class TestRunner {
5353
const cel::expr::conformance::test::TestOutput& output,
5454
google::protobuf::Arena* arena);
5555

56+
void AssertError(const cel::Value& computed,
57+
const cel::expr::conformance::test::TestOutput& output);
58+
5659
std::unique_ptr<cel::test::CelTestContext> test_context_;
5760
};
5861

testing/testrunner/runner_lib_test.cc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,5 +450,54 @@ TEST(TestRunnerCustomCompilerTest,
450450
std::move(runtime), /*options=*/{.checked_expr = checked_expr}));
451451
EXPECT_NO_FATAL_FAILURE(test_runner.RunTest(test_case));
452452
}
453+
454+
TEST_F(TestRunnerTest, BasicTestWithErrorAssertion) {
455+
// Compile the expression.
456+
ASSERT_OK_AND_ASSIGN(cel::ValidationResult validation_result,
457+
compiler_->Compile("x + y"));
458+
CheckedExpr checked_expr;
459+
ASSERT_THAT(cel::AstToCheckedExpr(*validation_result.GetAst(), &checked_expr),
460+
absl_testing::IsOk());
461+
// Create a runtime.
462+
ASSERT_OK_AND_ASSIGN(std::unique_ptr<const cel::Runtime> runtime,
463+
CreateTestRuntime());
464+
TestCase test_case = ParseTextProtoOrDie<TestCase>(R"pb(
465+
input {
466+
key: "x"
467+
value { value { int64_value: 1 } }
468+
}
469+
output {
470+
eval_error {
471+
errors { message: "No value with name \"y\" found in Activation" }
472+
}
473+
}
474+
)pb");
475+
TestRunner test_runner(CelTestContext::CreateFromRuntime(
476+
std::move(runtime), /*options=*/{.checked_expr = checked_expr}));
477+
EXPECT_NO_FATAL_FAILURE(test_runner.RunTest(test_case));
478+
}
479+
480+
TEST_F(TestRunnerTest, BasicTestFailsWhenExpectingErrorButGotValue) {
481+
// Compile the expression.
482+
ASSERT_OK_AND_ASSIGN(cel::ValidationResult validation_result,
483+
compiler_->Compile("1 + 1"));
484+
CheckedExpr checked_expr;
485+
ASSERT_THAT(cel::AstToCheckedExpr(*validation_result.GetAst(), &checked_expr),
486+
absl_testing::IsOk());
487+
// Create a runtime.
488+
ASSERT_OK_AND_ASSIGN(std::unique_ptr<const cel::Runtime> runtime,
489+
CreateTestRuntime());
490+
TestCase test_case = ParseTextProtoOrDie<TestCase>(R"pb(
491+
output {
492+
eval_error {
493+
errors { message: "No value with name \"y\" found in Activation" }
494+
}
495+
}
496+
)pb");
497+
TestRunner test_runner(CelTestContext::CreateFromRuntime(
498+
std::move(runtime), /*options=*/{.checked_expr = checked_expr}));
499+
EXPECT_NONFATAL_FAILURE(test_runner.RunTest(test_case),
500+
"Expected error but got value");
501+
}
453502
} // namespace
454503
} // namespace cel::test

0 commit comments

Comments
 (0)