|
| 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 | +#include "testing/testrunner/coverage_index.h" |
| 15 | + |
| 16 | +#include <memory> |
| 17 | +#include <utility> |
| 18 | + |
| 19 | +#include "cel/expr/syntax.pb.h" |
| 20 | +#include "absl/status/status_matchers.h" |
| 21 | +#include "absl/status/statusor.h" |
| 22 | +#include "absl/strings/string_view.h" |
| 23 | +#include "checker/type_checker_builder.h" |
| 24 | +#include "checker/validation_result.h" |
| 25 | +#include "common/ast.h" |
| 26 | +#include "common/ast_proto.h" |
| 27 | +#include "common/decl.h" |
| 28 | +#include "common/type.h" |
| 29 | +#include "common/value.h" |
| 30 | +#include "compiler/compiler.h" |
| 31 | +#include "compiler/compiler_factory.h" |
| 32 | +#include "compiler/standard_library.h" |
| 33 | +#include "internal/status_macros.h" |
| 34 | +#include "internal/testing.h" |
| 35 | +#include "internal/testing_descriptor_pool.h" |
| 36 | +#include "runtime/activation.h" |
| 37 | +#include "runtime/runtime.h" |
| 38 | +#include "runtime/runtime_builder.h" |
| 39 | +#include "runtime/standard_runtime_builder_factory.h" |
| 40 | +#include "google/protobuf/arena.h" |
| 41 | + |
| 42 | +namespace cel::test { |
| 43 | +namespace { |
| 44 | + |
| 45 | +using ::absl_testing::IsOk; |
| 46 | +using ::cel::expr::CheckedExpr; |
| 47 | + |
| 48 | +absl::StatusOr<std::unique_ptr<const cel::Runtime>> CreateTestRuntime() { |
| 49 | + CEL_ASSIGN_OR_RETURN(cel::RuntimeBuilder standard_runtime_builder, |
| 50 | + cel::CreateStandardRuntimeBuilder( |
| 51 | + cel::internal::GetTestingDescriptorPool(), {})); |
| 52 | + return std::move(standard_runtime_builder).Build(); |
| 53 | +} |
| 54 | + |
| 55 | +TEST(CoverageIndexTest, RecordCoverageWithErrorDoesNotCrash) { |
| 56 | + ASSERT_OK_AND_ASSIGN( |
| 57 | + std::unique_ptr<cel::CompilerBuilder> compiler_builder, |
| 58 | + cel::NewCompilerBuilder(cel::internal::GetTestingDescriptorPool())); |
| 59 | + ASSERT_THAT(compiler_builder->AddLibrary(cel::StandardCompilerLibrary()), |
| 60 | + IsOk()); |
| 61 | + ASSERT_THAT(compiler_builder->GetCheckerBuilder().AddVariable( |
| 62 | + cel::MakeVariableDecl("x", cel::IntType())), |
| 63 | + IsOk()); |
| 64 | + ASSERT_OK_AND_ASSIGN(std::unique_ptr<cel::Compiler> compiler, |
| 65 | + std::move(compiler_builder)->Build()); |
| 66 | + ASSERT_OK_AND_ASSIGN(cel::ValidationResult validation_result, |
| 67 | + compiler->Compile("1/x > 1")); |
| 68 | + CheckedExpr checked_expr; |
| 69 | + ASSERT_THAT(cel::AstToCheckedExpr(*validation_result.GetAst(), &checked_expr), |
| 70 | + IsOk()); |
| 71 | + |
| 72 | + CoverageIndex coverage_index; |
| 73 | + coverage_index.Init(checked_expr); |
| 74 | + |
| 75 | + ASSERT_OK_AND_ASSIGN(std::unique_ptr<const cel::Runtime> runtime, |
| 76 | + CreateTestRuntime()); |
| 77 | + ASSERT_THAT(EnableCoverageInRuntime(*const_cast<cel::Runtime*>(runtime.get()), |
| 78 | + coverage_index), |
| 79 | + IsOk()); |
| 80 | + ASSERT_OK_AND_ASSIGN(std::unique_ptr<cel::Ast> ast, |
| 81 | + cel::CreateAstFromCheckedExpr(checked_expr)); |
| 82 | + ASSERT_OK_AND_ASSIGN(auto program, runtime->CreateProgram(std::move(ast))); |
| 83 | + |
| 84 | + cel::Activation activation; |
| 85 | + activation.InsertOrAssignValue("x", cel::IntValue(0)); |
| 86 | + google::protobuf::Arena arena; |
| 87 | + ASSERT_OK_AND_ASSIGN(cel::Value result, |
| 88 | + program->Evaluate(&arena, activation)); |
| 89 | + EXPECT_TRUE(result.IsError()); |
| 90 | +} |
| 91 | + |
| 92 | +} // namespace |
| 93 | +} // namespace cel::test |
0 commit comments