| 59 | using RuntimeStdExtensionTest = testing::TestWithParam<TestCase>; |
| 60 | |
| 61 | TEST_P(RuntimeStdExtensionTest, RegisterStandardExtensions) { |
| 62 | const TestCase& param = GetParam(); |
| 63 | Env env; |
| 64 | env.SetDescriptorPool(cel::internal::GetSharedTestingDescriptorPool()); |
| 65 | RegisterStandardExtensions(env); |
| 66 | |
| 67 | Config compiler_config; |
| 68 | // For the compilation step, assume latest version of the extension to ensure |
| 69 | // a successful compilation. Later, we will test the runtime with different |
| 70 | // extension versions. |
| 71 | ASSERT_THAT(compiler_config.AddExtensionConfig( |
| 72 | param.extension_name, Config::ExtensionConfig::kLatest), |
| 73 | IsOk()); |
| 74 | env.SetConfig(compiler_config); |
| 75 | ASSERT_OK_AND_ASSIGN(std::unique_ptr<Compiler> compiler, env.NewCompiler()); |
| 76 | ASSERT_OK_AND_ASSIGN(ValidationResult result, compiler->Compile(param.expr)); |
| 77 | EXPECT_THAT(result.GetIssues(), IsEmpty()) << result.FormatError(); |
| 78 | ASSERT_OK_AND_ASSIGN(std::unique_ptr<Ast> ast, result.ReleaseAst()); |
| 79 | |
| 80 | for (int version = 0; version <= param.latest_extension_version; ++version) { |
| 81 | Config runtime_config; |
| 82 | // Request a specific version of the extension to be configured in the |
| 83 | // runtime. |
| 84 | ASSERT_THAT( |
| 85 | runtime_config.AddExtensionConfig(param.extension_name, version), |
| 86 | IsOk()); |
| 87 | if (param.requires_optional_extension) { |
| 88 | ASSERT_THAT(runtime_config.AddExtensionConfig("optional"), IsOk()); |
| 89 | } |
| 90 | |
| 91 | EnvRuntime env_runtime; |
| 92 | env_runtime.SetDescriptorPool( |
| 93 | cel::internal::GetSharedTestingDescriptorPool()); |
| 94 | RegisterStandardExtensions(env_runtime); |
| 95 | env_runtime.SetConfig(runtime_config); |
| 96 | ASSERT_OK_AND_ASSIGN(std::unique_ptr<Runtime> runtime, |
| 97 | env_runtime.NewRuntime()); |
| 98 | absl::StatusOr<std::unique_ptr<Program>> program_or = |
| 99 | runtime->CreateProgram(std::make_unique<Ast>(*ast)); |
| 100 | |
| 101 | // If the function is not supported in this extension version, check that |
| 102 | // the program creation returned an error. |
| 103 | if (!absl::c_contains(param.extension_versions, version)) { |
| 104 | EXPECT_THAT(program_or, StatusIs(absl::StatusCode::kInvalidArgument)) |
| 105 | << " expr: " << param.expr << " version: " << version; |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | ASSERT_THAT(program_or, IsOk()) |
| 110 | << " expr: " << param.expr << " version: " << version; |
| 111 | std::unique_ptr<Program> program = *std::move(program_or); |
| 112 | google::protobuf::Arena arena; |
| 113 | Activation activation; |
| 114 | ASSERT_OK_AND_ASSIGN(Value value, program->Evaluate(&arena, activation)); |
| 115 | EXPECT_TRUE(value.GetBool()) |
| 116 | << " expr: " << param.expr << " version: " << version; |
| 117 | } |
| 118 | } |
nothing calls this directly
no test coverage detected