-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathcel_function_registry_test.cc
More file actions
303 lines (264 loc) · 11.8 KB
/
Copy pathcel_function_registry_test.cc
File metadata and controls
303 lines (264 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "eval/public/cel_function_registry.h"
#include <memory>
#include <tuple>
#include <vector>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "base/kind.h"
#include "eval/internal/adapter_activation_impl.h"
#include "eval/public/activation.h"
#include "eval/public/cel_function.h"
#include "internal/testing.h"
#include "runtime/function_overload_reference.h"
namespace google::api::expr::runtime {
namespace {
using testing::ElementsAre;
using testing::Eq;
using testing::HasSubstr;
using testing::Property;
using testing::SizeIs;
using testing::Truly;
using cel::internal::StatusIs;
class ConstCelFunction : public CelFunction {
public:
ConstCelFunction() : CelFunction(MakeDescriptor()) {}
explicit ConstCelFunction(const CelFunctionDescriptor& desc)
: CelFunction(desc) {}
static CelFunctionDescriptor MakeDescriptor() {
return {"ConstFunction", false, {}};
}
absl::Status Evaluate(absl::Span<const CelValue> args, CelValue* output,
google::protobuf::Arena* arena) const override {
*output = CelValue::CreateInt64(42);
return absl::OkStatus();
}
};
TEST(CelFunctionRegistryTest, InsertAndRetrieveLazyFunction) {
CelFunctionDescriptor lazy_function_desc{"LazyFunction", false, {}};
CelFunctionRegistry registry;
Activation activation;
ASSERT_OK(registry.RegisterLazyFunction(lazy_function_desc));
const auto descriptors =
registry.FindLazyOverloads("LazyFunction", false, {});
EXPECT_THAT(descriptors, testing::SizeIs(1));
}
// Confirm that lazy and static functions share the same descriptor space:
// i.e. you can't insert both a lazy function and a static function for the same
// descriptors.
TEST(CelFunctionRegistryTest, LazyAndStaticFunctionShareDescriptorSpace) {
CelFunctionRegistry registry;
CelFunctionDescriptor desc = ConstCelFunction::MakeDescriptor();
ASSERT_OK(registry.RegisterLazyFunction(desc));
absl::Status status = registry.Register(ConstCelFunction::MakeDescriptor(),
std::make_unique<ConstCelFunction>());
EXPECT_FALSE(status.ok());
}
// Confirm that lazy and static functions share the same descriptor space:
// i.e. you can't insert both a lazy function and a static function for the same
// descriptors.
TEST(CelFunctionRegistryTest, FindStaticOverloadsReturns) {
CelFunctionRegistry registry;
CelFunctionDescriptor desc = ConstCelFunction::MakeDescriptor();
ASSERT_OK(registry.Register(desc, std::make_unique<ConstCelFunction>(desc)));
std::vector<cel::FunctionOverloadReference> overloads =
registry.FindStaticOverloads(desc.name(), false, {});
EXPECT_THAT(overloads,
ElementsAre(Truly(
[](const cel::FunctionOverloadReference& overload) -> bool {
return overload.descriptor.name() == "ConstFunction";
})))
<< "Expected single ConstFunction()";
}
TEST(CelFunctionRegistryTest, ListFunctions) {
CelFunctionDescriptor lazy_function_desc{"LazyFunction", false, {}};
CelFunctionRegistry registry;
ASSERT_OK(registry.RegisterLazyFunction(lazy_function_desc));
EXPECT_OK(registry.Register(ConstCelFunction::MakeDescriptor(),
std::make_unique<ConstCelFunction>()));
auto registered_functions = registry.ListFunctions();
EXPECT_THAT(registered_functions, SizeIs(2));
EXPECT_THAT(registered_functions["LazyFunction"], SizeIs(1));
EXPECT_THAT(registered_functions["ConstFunction"], SizeIs(1));
}
TEST(CelFunctionRegistryTest, LegacyFindLazyOverloads) {
CelFunctionDescriptor lazy_function_desc{"LazyFunction", false, {}};
CelFunctionRegistry registry;
ASSERT_OK(registry.RegisterLazyFunction(lazy_function_desc));
ASSERT_OK(registry.Register(ConstCelFunction::MakeDescriptor(),
std::make_unique<ConstCelFunction>()));
EXPECT_THAT(registry.FindLazyOverloads("LazyFunction", false, {}),
ElementsAre(Truly([](const CelFunctionDescriptor* descriptor) {
return descriptor->name() == "LazyFunction";
})))
<< "Expected single lazy overload for LazyFunction()";
}
TEST(CelFunctionRegistryTest, DefaultLazyProvider) {
CelFunctionDescriptor lazy_function_desc{"LazyFunction", false, {}};
CelFunctionRegistry registry;
Activation activation;
cel::interop_internal::AdapterActivationImpl modern_activation(activation);
EXPECT_OK(registry.RegisterLazyFunction(lazy_function_desc));
EXPECT_OK(activation.InsertFunction(
std::make_unique<ConstCelFunction>(lazy_function_desc)));
auto providers = registry.ModernFindLazyOverloads("LazyFunction", false, {});
EXPECT_THAT(providers, testing::SizeIs(1));
ASSERT_OK_AND_ASSIGN(auto func, providers[0].provider.GetFunction(
lazy_function_desc, modern_activation));
ASSERT_TRUE(func.has_value());
EXPECT_THAT(func->descriptor,
Property(&cel::FunctionDescriptor::name, Eq("LazyFunction")));
}
TEST(CelFunctionRegistryTest, DefaultLazyProviderNoOverloadFound) {
CelFunctionRegistry registry;
Activation legacy_activation;
cel::interop_internal::AdapterActivationImpl activation(legacy_activation);
CelFunctionDescriptor lazy_function_desc{"LazyFunction", false, {}};
EXPECT_OK(registry.RegisterLazyFunction(lazy_function_desc));
EXPECT_OK(legacy_activation.InsertFunction(
std::make_unique<ConstCelFunction>(lazy_function_desc)));
const auto providers =
registry.ModernFindLazyOverloads("LazyFunction", false, {});
ASSERT_THAT(providers, testing::SizeIs(1));
const auto& provider = providers[0].provider;
auto func = provider.GetFunction({"LazyFunc", false, {cel::Kind::kInt64}},
activation);
ASSERT_OK(func.status());
EXPECT_EQ(*func, absl::nullopt);
}
TEST(CelFunctionRegistryTest, DefaultLazyProviderAmbiguousLookup) {
CelFunctionRegistry registry;
Activation legacy_activation;
cel::interop_internal::AdapterActivationImpl activation(legacy_activation);
CelFunctionDescriptor desc1{"LazyFunc", false, {CelValue::Type::kInt64}};
CelFunctionDescriptor desc2{"LazyFunc", false, {CelValue::Type::kUint64}};
CelFunctionDescriptor match_desc{"LazyFunc", false, {CelValue::Type::kAny}};
ASSERT_OK(registry.RegisterLazyFunction(match_desc));
ASSERT_OK(legacy_activation.InsertFunction(
std::make_unique<ConstCelFunction>(desc1)));
ASSERT_OK(legacy_activation.InsertFunction(
std::make_unique<ConstCelFunction>(desc2)));
auto providers =
registry.ModernFindLazyOverloads("LazyFunc", false, {cel::Kind::kAny});
ASSERT_THAT(providers, testing::SizeIs(1));
const auto& provider = providers[0].provider;
auto func = provider.GetFunction(match_desc, activation);
EXPECT_THAT(std::string(func.status().message()),
HasSubstr("Couldn't resolve function"));
}
TEST(CelFunctionRegistryTest, CanRegisterNonStrictFunction) {
{
CelFunctionRegistry registry;
CelFunctionDescriptor descriptor("NonStrictFunction",
/*receiver_style=*/false,
{CelValue::Type::kAny},
/*is_strict=*/false);
ASSERT_OK(registry.Register(
descriptor, std::make_unique<ConstCelFunction>(descriptor)));
EXPECT_THAT(registry.FindStaticOverloads("NonStrictFunction", false,
{CelValue::Type::kAny}),
SizeIs(1));
}
{
CelFunctionRegistry registry;
CelFunctionDescriptor descriptor("NonStrictLazyFunction",
/*receiver_style=*/false,
{CelValue::Type::kAny},
/*is_strict=*/false);
EXPECT_OK(registry.RegisterLazyFunction(descriptor));
EXPECT_THAT(registry.FindLazyOverloads("NonStrictLazyFunction", false,
{CelValue::Type::kAny}),
SizeIs(1));
}
}
using NonStrictTestCase = std::tuple<bool, bool>;
using NonStrictRegistrationFailTest = testing::TestWithParam<NonStrictTestCase>;
TEST_P(NonStrictRegistrationFailTest,
IfOtherOverloadExistsRegisteringNonStrictFails) {
bool existing_function_is_lazy, new_function_is_lazy;
std::tie(existing_function_is_lazy, new_function_is_lazy) = GetParam();
CelFunctionRegistry registry;
CelFunctionDescriptor descriptor("OverloadedFunction",
/*receiver_style=*/false,
{CelValue::Type::kAny},
/*is_strict=*/true);
if (existing_function_is_lazy) {
ASSERT_OK(registry.RegisterLazyFunction(descriptor));
} else {
ASSERT_OK(registry.Register(
descriptor, std::make_unique<ConstCelFunction>(descriptor)));
}
CelFunctionDescriptor new_descriptor(
"OverloadedFunction",
/*receiver_style=*/false, {CelValue::Type::kAny, CelValue::Type::kAny},
/*is_strict=*/false);
absl::Status status;
if (new_function_is_lazy) {
status = registry.RegisterLazyFunction(new_descriptor);
} else {
status = registry.Register(
new_descriptor, std::make_unique<ConstCelFunction>(new_descriptor));
}
EXPECT_THAT(status, StatusIs(absl::StatusCode::kAlreadyExists,
HasSubstr("Only one overload")));
}
TEST_P(NonStrictRegistrationFailTest,
IfOtherNonStrictExistsRegisteringStrictFails) {
bool existing_function_is_lazy, new_function_is_lazy;
std::tie(existing_function_is_lazy, new_function_is_lazy) = GetParam();
CelFunctionRegistry registry;
CelFunctionDescriptor descriptor("OverloadedFunction",
/*receiver_style=*/false,
{CelValue::Type::kAny},
/*is_strict=*/false);
if (existing_function_is_lazy) {
ASSERT_OK(registry.RegisterLazyFunction(descriptor));
} else {
ASSERT_OK(registry.Register(
descriptor, std::make_unique<ConstCelFunction>(descriptor)));
}
CelFunctionDescriptor new_descriptor(
"OverloadedFunction",
/*receiver_style=*/false, {CelValue::Type::kAny, CelValue::Type::kAny},
/*is_strict=*/true);
absl::Status status;
if (new_function_is_lazy) {
status = registry.RegisterLazyFunction(new_descriptor);
} else {
status = registry.Register(
new_descriptor, std::make_unique<ConstCelFunction>(new_descriptor));
}
EXPECT_THAT(status, StatusIs(absl::StatusCode::kAlreadyExists,
HasSubstr("Only one overload")));
}
TEST_P(NonStrictRegistrationFailTest, CanRegisterStrictFunctionsWithoutLimit) {
bool existing_function_is_lazy, new_function_is_lazy;
std::tie(existing_function_is_lazy, new_function_is_lazy) = GetParam();
CelFunctionRegistry registry;
CelFunctionDescriptor descriptor("OverloadedFunction",
/*receiver_style=*/false,
{CelValue::Type::kAny},
/*is_strict=*/true);
if (existing_function_is_lazy) {
ASSERT_OK(registry.RegisterLazyFunction(descriptor));
} else {
ASSERT_OK(registry.Register(
descriptor, std::make_unique<ConstCelFunction>(descriptor)));
}
CelFunctionDescriptor new_descriptor(
"OverloadedFunction",
/*receiver_style=*/false, {CelValue::Type::kAny, CelValue::Type::kAny},
/*is_strict=*/true);
absl::Status status;
if (new_function_is_lazy) {
status = registry.RegisterLazyFunction(new_descriptor);
} else {
status = registry.Register(
new_descriptor, std::make_unique<ConstCelFunction>(new_descriptor));
}
EXPECT_OK(status);
}
INSTANTIATE_TEST_SUITE_P(NonStrictRegistrationFailTest,
NonStrictRegistrationFailTest,
testing::Combine(testing::Bool(), testing::Bool()));
} // namespace
} // namespace google::api::expr::runtime