| 139 | }); |
| 140 | |
| 141 | TEST(ConstantFoldingExtTest, LazyFunctionNotFolded) { |
| 142 | google::protobuf::Arena arena; |
| 143 | RuntimeOptions options; |
| 144 | |
| 145 | ASSERT_OK_AND_ASSIGN(cel::RuntimeBuilder builder, |
| 146 | CreateStandardRuntimeBuilder( |
| 147 | internal::GetTestingDescriptorPool(), options)); |
| 148 | int call_count = 0; |
| 149 | using FunctionAdapter = |
| 150 | BinaryFunctionAdapter<absl::StatusOr<Value>, const StringValue&, |
| 151 | const StringValue&>; |
| 152 | auto fn = FunctionAdapter::WrapFunction( |
| 153 | [&call_count](const StringValue& value, const StringValue& prefix) { |
| 154 | call_count++; |
| 155 | return StringValue(absl::StrCat(prefix.ToString(), value.ToString())); |
| 156 | }); |
| 157 | FunctionDescriptor descriptor = FunctionAdapter::CreateDescriptor( |
| 158 | "lazy_prepend", /*receiver_style=*/false); |
| 159 | ASSERT_THAT(builder.function_registry().RegisterLazyFunction(descriptor), |
| 160 | IsOk()); |
| 161 | |
| 162 | ASSERT_THAT(EnableConstantFolding(builder), IsOk()); |
| 163 | |
| 164 | ASSERT_OK_AND_ASSIGN(auto runtime, std::move(builder).Build()); |
| 165 | |
| 166 | ASSERT_OK_AND_ASSIGN(ParsedExpr parsed_expr, |
| 167 | Parse("lazy_prepend('def', 'abc') == 'abcdef'")); |
| 168 | |
| 169 | ASSERT_OK_AND_ASSIGN(auto program, ProtobufRuntimeAdapter::CreateProgram( |
| 170 | *runtime, parsed_expr)); |
| 171 | EXPECT_EQ(call_count, 0); |
| 172 | Activation activation; |
| 173 | activation.InsertFunction(descriptor, std::move(fn)); |
| 174 | |
| 175 | ASSERT_OK_AND_ASSIGN(Value result, program->Evaluate(&arena, activation)); |
| 176 | EXPECT_EQ(call_count, 1); |
| 177 | EXPECT_THAT(result, IsBoolValue(true)); |
| 178 | |
| 179 | ASSERT_OK_AND_ASSIGN(result, program->Evaluate(&arena, activation)); |
| 180 | EXPECT_EQ(call_count, 2); |
| 181 | EXPECT_THAT(result, IsBoolValue(true)); |
| 182 | } |
| 183 | |
| 184 | TEST(ConstantFoldingExtTest, ContextualFunctionNotFolded) { |
| 185 | google::protobuf::Arena arena; |
nothing calls this directly
no test coverage detected