|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <memory> |
| 16 | + |
| 17 | +#include "cel/expr/eval.pb.h" |
| 18 | +#include "absl/status/status.h" |
| 19 | +#include "absl/strings/string_view.h" |
| 20 | +#include "common/internal/value_conversion.h" |
| 21 | +#include "common/value.h" |
| 22 | +#include "internal/testing.h" |
| 23 | +#include "testing/testrunner/cel_test_context.h" |
| 24 | +#include "testing/testrunner/result_matcher.h" |
| 25 | +#include "cel/expr/conformance/test/suite.pb.h" |
| 26 | +#include "google/protobuf/arena.h" |
| 27 | +#include "google/protobuf/descriptor.h" |
| 28 | +#include "google/protobuf/message.h" |
| 29 | +#include "google/protobuf/util/field_comparator.h" |
| 30 | +#include "google/protobuf/util/message_differencer.h" |
| 31 | + |
| 32 | +namespace cel::test { |
| 33 | + |
| 34 | +namespace { |
| 35 | + |
| 36 | +using ValueProto = ::cel::expr::Value; |
| 37 | +using ::cel::expr::conformance::test::TestOutput; |
| 38 | + |
| 39 | +bool IsEqual(const ValueProto& expected, const ValueProto& actual) { |
| 40 | + static auto* kFieldComparator = []() { |
| 41 | + auto* field_comparator = new google::protobuf::util::DefaultFieldComparator(); |
| 42 | + field_comparator->set_treat_nan_as_equal(true); |
| 43 | + return field_comparator; |
| 44 | + }(); |
| 45 | + static auto* kDifferencer = []() { |
| 46 | + auto* differencer = new google::protobuf::util::MessageDifferencer(); |
| 47 | + differencer->set_message_field_comparison( |
| 48 | + google::protobuf::util::MessageDifferencer::EQUIVALENT); |
| 49 | + differencer->set_field_comparator(kFieldComparator); |
| 50 | + const auto* descriptor = cel::expr::MapValue::descriptor(); |
| 51 | + const auto* entries_field = descriptor->FindFieldByName("entries"); |
| 52 | + const auto* key_field = |
| 53 | + entries_field->message_type()->FindFieldByName("key"); |
| 54 | + differencer->TreatAsMap(entries_field, key_field); |
| 55 | + return differencer; |
| 56 | + }(); |
| 57 | + return kDifferencer->Compare(expected, actual); |
| 58 | +} |
| 59 | + |
| 60 | +MATCHER_P(MatchesValue, expected, "") { return IsEqual(arg, expected); } |
| 61 | + |
| 62 | +class DefaultResultMatcher : public cel::test::ResultMatcher { |
| 63 | + public: |
| 64 | + void Match(const ResultMatcherParams& params) const override { |
| 65 | + const TestOutput& output = params.expected_output; |
| 66 | + const auto& computed_output = params.computed_output; |
| 67 | + google::protobuf::Arena* arena = params.arena; |
| 68 | + |
| 69 | + if (output.has_result_value()) { |
| 70 | + AssertValue(computed_output, output, params.test_context, arena); |
| 71 | + } else if (output.has_eval_error()) { |
| 72 | + AssertError(computed_output, output); |
| 73 | + } else if (output.has_unknown()) { |
| 74 | + ADD_FAILURE() << "Unknown assertions not implemented yet."; |
| 75 | + } else { |
| 76 | + ADD_FAILURE() << "Unexpected output kind."; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private: |
| 81 | + void AssertValue(const cel::Value& computed, const TestOutput& output, |
| 82 | + const CelTestContext& test_context, |
| 83 | + google::protobuf::Arena* arena) const { |
| 84 | + ValueProto expected_value_proto; |
| 85 | + const auto* descriptor_pool = |
| 86 | + test_context.runtime() != nullptr |
| 87 | + ? test_context.runtime()->GetDescriptorPool() |
| 88 | + : google::protobuf::DescriptorPool::generated_pool(); |
| 89 | + auto* message_factory = test_context.runtime() != nullptr |
| 90 | + ? test_context.runtime()->GetMessageFactory() |
| 91 | + : google::protobuf::MessageFactory::generated_factory(); |
| 92 | + |
| 93 | + ValueProto computed_expr_value; |
| 94 | + ASSERT_OK_AND_ASSIGN( |
| 95 | + computed_expr_value, |
| 96 | + ToExprValue(computed, descriptor_pool, message_factory, arena)); |
| 97 | + EXPECT_THAT(output.result_value(), MatchesValue(computed_expr_value)); |
| 98 | + } |
| 99 | + |
| 100 | + void AssertError(const cel::Value& computed, const TestOutput& output) const { |
| 101 | + if (!computed.IsError()) { |
| 102 | + ADD_FAILURE() << "Expected error but got value: " |
| 103 | + << computed.DebugString(); |
| 104 | + return; |
| 105 | + } |
| 106 | + absl::Status computed_status = computed.AsError()->ToStatus(); |
| 107 | + // We selected the first error in the set for comparison because there is |
| 108 | + // only one runtime error that is reported even if there are multiple errors |
| 109 | + // in the critical path. |
| 110 | + ASSERT_TRUE(output.eval_error().errors_size() == 1) |
| 111 | + << "Expected exactly one error but got: " |
| 112 | + << output.eval_error().errors_size(); |
| 113 | + ASSERT_EQ(computed_status.message(), |
| 114 | + output.eval_error().errors(0).message()); |
| 115 | + } |
| 116 | +}; |
| 117 | +} // namespace |
| 118 | + |
| 119 | +std::unique_ptr<ResultMatcher> CreateDefaultResultMatcher() { |
| 120 | + return std::make_unique<DefaultResultMatcher>(); |
| 121 | +} |
| 122 | +} // namespace cel::test |
0 commit comments