MCPcopy Create free account
hub / github.com/cel-expr/cel-cpp / TEST

Function TEST

runtime/constant_folding_test.cc:141–182  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

139 });
140
141TEST(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
184TEST(ConstantFoldingExtTest, ContextualFunctionNotFolded) {
185 google::protobuf::Arena arena;

Callers

nothing calls this directly

Calls 8

CreateDescriptorFunction · 0.85
EnableConstantFoldingFunction · 0.85
StringValueClass · 0.50
ASSERT_OK_AND_ASSIGNFunction · 0.50
ToStringMethod · 0.45
RegisterLazyFunctionMethod · 0.45
InsertFunctionMethod · 0.45
EvaluateMethod · 0.45

Tested by

no test coverage detected