Support for error message assertion. by copybara-service[bot] · Pull Request #1661 · cel-expr/cel-cpp · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions testing/testrunner/BUILD
19 changes: 18 additions & 1 deletion testing/testrunner/runner_lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <memory>
#include <utility>

#include "cel/expr/eval.pb.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
Expand Down Expand Up @@ -226,13 +227,29 @@ void TestRunner::AssertValue(const cel::Value& computed,
EXPECT_THAT(expected_value_proto, MatchesValue(computed_expr_value));
}

void TestRunner::AssertError(const cel::Value& computed,
const TestOutput& output) {
if (!computed.IsError()) {
ADD_FAILURE() << "Expected error but got value: " << computed.DebugString();
return;
}
absl::Status computed_status = computed.AsError()->ToStatus();
// We selected the first error in the set for comparison because there is only
// one runtime error that is reported even if there are multiple errors in the
// critical path.
ASSERT_TRUE(output.eval_error().errors_size() == 1)
<< "Expected exactly one error but got: "
<< output.eval_error().errors_size();
ASSERT_EQ(computed_status.message(), output.eval_error().errors(0).message());
}

void TestRunner::Assert(const cel::Value& computed, const TestCase& test_case,
google::protobuf::Arena* arena) {
TestOutput output = test_case.output();
if (output.has_result_value() || output.has_result_expr()) {
AssertValue(computed, output, arena);
} else if (output.has_eval_error()) {
ADD_FAILURE() << "Error assertion not implemented yet.";
AssertError(computed, output);
} else if (output.has_unknown()) {
ADD_FAILURE() << "Unknown assertions not implemented yet.";
} else {
Expand Down
3 changes: 3 additions & 0 deletions testing/testrunner/runner_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class TestRunner {
const cel::expr::conformance::test::TestOutput& output,
google::protobuf::Arena* arena);

void AssertError(const cel::Value& computed,
const cel::expr::conformance::test::TestOutput& output);

std::unique_ptr<cel::test::CelTestContext> test_context_;
};

Expand Down
49 changes: 49 additions & 0 deletions testing/testrunner/runner_lib_test.cc