|
14 | 14 |
|
15 | 15 | #include "runtime/constant_folding.h" |
16 | 16 |
|
| 17 | +#include <memory> |
17 | 18 | #include <string> |
18 | 19 | #include <utility> |
19 | 20 | #include <vector> |
|
25 | 26 | #include "absl/strings/match.h" |
26 | 27 | #include "absl/strings/str_cat.h" |
27 | 28 | #include "base/function_adapter.h" |
| 29 | +#include "common/function_descriptor.h" |
28 | 30 | #include "common/value.h" |
29 | 31 | #include "extensions/protobuf/runtime_adapter.h" |
30 | 32 | #include "internal/testing.h" |
31 | 33 | #include "internal/testing_descriptor_pool.h" |
32 | 34 | #include "parser/parser.h" |
33 | 35 | #include "runtime/activation.h" |
34 | | -#include "runtime/register_function_helper.h" |
35 | 36 | #include "runtime/runtime_builder.h" |
36 | 37 | #include "runtime/runtime_options.h" |
37 | 38 | #include "runtime/standard_runtime_builder_factory.h" |
@@ -82,8 +83,8 @@ TEST_P(ConstantFoldingExtTest, Runner) { |
82 | 83 | CreateStandardRuntimeBuilder( |
83 | 84 | internal::GetTestingDescriptorPool(), options)); |
84 | 85 |
|
85 | | - auto status = RegisterHelper<BinaryFunctionAdapter< |
86 | | - absl::StatusOr<Value>, const StringValue&, const StringValue&>>:: |
| 86 | + auto status = BinaryFunctionAdapter<absl::StatusOr<Value>, const StringValue&, |
| 87 | + const StringValue&>:: |
87 | 88 | RegisterGlobalOverload( |
88 | 89 | "prepend", |
89 | 90 | [](const StringValue& value, const StringValue& prefix) { |
@@ -129,14 +130,99 @@ INSTANTIATE_TEST_SUITE_P( |
129 | 130 | IsBoolValue(true)}, |
130 | 131 | {"runtime_error", "[1, 2, 3, 4].exists(x, ['4'].all(y, y <= x))", |
131 | 132 | IsErrorValue("No matching overloads")}, |
132 | | - // TODO(uncreated-issue/32): Depends on map creation |
133 | | - // {"map_create", "{'abc': 'def', 'abd': 'deg'}.size()", 2}, |
| 133 | + {"map_create", "{'abc': 'def', 'abd': 'deg'}.size()", IsIntValue(2)}, |
134 | 134 | {"custom_function", "prepend('def', 'abc') == 'abcdef'", |
135 | 135 | IsBoolValue(true)}}), |
136 | 136 |
|
137 | 137 | [](const testing::TestParamInfo<TestCase>& info) { |
138 | 138 | return info.param.name; |
139 | 139 | }); |
140 | 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; |
| 186 | + RuntimeOptions options; |
| 187 | + ASSERT_OK_AND_ASSIGN(cel::RuntimeBuilder builder, |
| 188 | + CreateStandardRuntimeBuilder( |
| 189 | + internal::GetTestingDescriptorPool(), options)); |
| 190 | + int call_count = 0; |
| 191 | + |
| 192 | + auto status = BinaryFunctionAdapter< |
| 193 | + absl::StatusOr<Value>, const StringValue&, |
| 194 | + const StringValue&>::Register("contextual_prepend", |
| 195 | + /*receiver_style=*/false, |
| 196 | + [&call_count](const StringValue& value, |
| 197 | + const StringValue& prefix) { |
| 198 | + call_count++; |
| 199 | + return StringValue(absl::StrCat( |
| 200 | + prefix.ToString(), value.ToString())); |
| 201 | + }, |
| 202 | + builder.function_registry(), |
| 203 | + {/*.is_strict=*/true, |
| 204 | + /*is_contextual=*/true}); |
| 205 | + ASSERT_THAT(status, IsOk()); |
| 206 | + |
| 207 | + ASSERT_THAT(EnableConstantFolding(builder), IsOk()); |
| 208 | + |
| 209 | + ASSERT_OK_AND_ASSIGN(auto runtime, std::move(builder).Build()); |
| 210 | + |
| 211 | + ASSERT_OK_AND_ASSIGN(ParsedExpr parsed_expr, |
| 212 | + Parse("contextual_prepend('def', 'abc') == 'abcdef'")); |
| 213 | + |
| 214 | + ASSERT_OK_AND_ASSIGN(auto program, ProtobufRuntimeAdapter::CreateProgram( |
| 215 | + *runtime, parsed_expr)); |
| 216 | + EXPECT_EQ(call_count, 0); |
| 217 | + Activation activation; |
| 218 | + ASSERT_OK_AND_ASSIGN(Value value, program->Evaluate(&arena, activation)); |
| 219 | + EXPECT_EQ(call_count, 1); |
| 220 | + EXPECT_THAT(value, IsBoolValue(true)); |
| 221 | + |
| 222 | + ASSERT_OK_AND_ASSIGN(value, program->Evaluate(&arena, activation)); |
| 223 | + EXPECT_EQ(call_count, 2); |
| 224 | + EXPECT_THAT(value, IsBoolValue(true)); |
| 225 | +} |
| 226 | + |
141 | 227 | } // namespace |
142 | 228 | } // namespace cel::extensions |
0 commit comments