-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathfunction_registry_test.cc
More file actions
307 lines (271 loc) · 11.7 KB
/
function_registry_test.cc
File metadata and controls
307 lines (271 loc) · 11.7 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
304
305
306
307
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "runtime/function_registry.h"
#include <cstdint>
#include <memory>
#include <tuple>
#include <vector>
#include "absl/base/nullability.h"
#include "absl/status/status.h"
#include "common/function_descriptor.h"
#include "common/kind.h"
#include "internal/testing.h"
#include "runtime/activation.h"
#include "runtime/function.h"
#include "runtime/function_adapter.h"
#include "runtime/function_overload_reference.h"
#include "runtime/function_provider.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
namespace cel {
namespace {
using ::absl_testing::StatusIs;
using ::cel::runtime_internal::FunctionProvider;
using ::testing::ElementsAre;
using ::testing::HasSubstr;
using ::testing::SizeIs;
using ::testing::Truly;
class ConstIntFunction : public cel::Function {
public:
static cel::FunctionDescriptor MakeDescriptor() {
return {"ConstFunction", false, {}};
}
absl::StatusOr<Value> Invoke(
absl::Span<const Value> args,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena,
absl::Span<const std::string> overload_id) const override {
return IntValue(42);
}
};
TEST(FunctionRegistryTest, InsertAndRetrieveLazyFunction) {
cel::FunctionDescriptor lazy_function_desc{"LazyFunction", false, {}};
FunctionRegistry registry;
Activation activation;
ASSERT_OK(registry.RegisterLazyFunction(lazy_function_desc));
const auto descriptors =
registry.FindLazyOverloads("LazyFunction", false, {});
EXPECT_THAT(descriptors, 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(FunctionRegistryTest, LazyAndStaticFunctionShareDescriptorSpace) {
FunctionRegistry registry;
cel::FunctionDescriptor desc = ConstIntFunction::MakeDescriptor();
ASSERT_OK(registry.RegisterLazyFunction(desc));
absl::Status status = registry.Register(ConstIntFunction::MakeDescriptor(),
std::make_unique<ConstIntFunction>());
EXPECT_FALSE(status.ok());
}
TEST(FunctionRegistryTest, FindStaticOverloadsReturns) {
FunctionRegistry registry;
cel::FunctionDescriptor desc = ConstIntFunction::MakeDescriptor();
ASSERT_OK(registry.Register(desc, std::make_unique<ConstIntFunction>()));
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(FunctionRegistryTest, ListFunctions) {
cel::FunctionDescriptor lazy_function_desc{"LazyFunction", false, {}};
FunctionRegistry registry;
ASSERT_OK(registry.RegisterLazyFunction(lazy_function_desc));
EXPECT_OK(registry.Register(ConstIntFunction::MakeDescriptor(),
std::make_unique<ConstIntFunction>()));
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(FunctionRegistryTest, DefaultLazyProviderNoOverloadFound) {
FunctionRegistry registry;
Activation activation;
cel::FunctionDescriptor lazy_function_desc{"LazyFunction", false, {}};
EXPECT_OK(registry.RegisterLazyFunction(lazy_function_desc));
auto providers = registry.FindLazyOverloads("LazyFunction", false, {});
ASSERT_THAT(providers, SizeIs(1));
const FunctionProvider& provider = providers[0].provider;
ASSERT_OK_AND_ASSIGN(
absl::optional<FunctionOverloadReference> func,
provider.GetFunction({"LazyFunc", false, {cel::Kind::kInt64}},
activation));
EXPECT_EQ(func, absl::nullopt);
}
TEST(FunctionRegistryTest, DefaultLazyProviderReturnsImpl) {
FunctionRegistry registry;
Activation activation;
EXPECT_OK(registry.RegisterLazyFunction(
FunctionDescriptor("LazyFunction", false, {Kind::kAny})));
EXPECT_TRUE(activation.InsertFunction(
FunctionDescriptor("LazyFunction", false, {Kind::kInt}),
UnaryFunctionAdapter<int64_t, int64_t>::WrapFunction(
[](int64_t x) { return 2 * x; })));
EXPECT_TRUE(activation.InsertFunction(
FunctionDescriptor("LazyFunction", false, {Kind::kDouble}),
UnaryFunctionAdapter<double, double>::WrapFunction(
[](double x) { return 2 * x; })));
auto providers =
registry.FindLazyOverloads("LazyFunction", false, {Kind::kInt});
ASSERT_THAT(providers, SizeIs(1));
const FunctionProvider& provider = providers[0].provider;
ASSERT_OK_AND_ASSIGN(
absl::optional<FunctionOverloadReference> func,
provider.GetFunction(
FunctionDescriptor("LazyFunction", false, {Kind::kInt}), activation));
ASSERT_TRUE(func.has_value());
EXPECT_EQ(func->descriptor.name(), "LazyFunction");
EXPECT_EQ(func->descriptor.types(), std::vector{cel::Kind::kInt64});
}
TEST(FunctionRegistryTest, DefaultLazyProviderAmbiguousOverload) {
FunctionRegistry registry;
Activation activation;
EXPECT_OK(registry.RegisterLazyFunction(
FunctionDescriptor("LazyFunction", false, {Kind::kAny})));
EXPECT_TRUE(activation.InsertFunction(
FunctionDescriptor("LazyFunction", false, {Kind::kInt}),
UnaryFunctionAdapter<int64_t, int64_t>::WrapFunction(
[](int64_t x) { return 2 * x; })));
EXPECT_TRUE(activation.InsertFunction(
FunctionDescriptor("LazyFunction", false, {Kind::kDouble}),
UnaryFunctionAdapter<double, double>::WrapFunction(
[](double x) { return 2 * x; })));
auto providers =
registry.FindLazyOverloads("LazyFunction", false, {Kind::kInt});
ASSERT_THAT(providers, SizeIs(1));
const FunctionProvider& provider = providers[0].provider;
EXPECT_THAT(
provider.GetFunction(
FunctionDescriptor("LazyFunction", false, {Kind::kAny}), activation),
StatusIs(absl::StatusCode::kInvalidArgument,
HasSubstr("Couldn't resolve function")));
}
TEST(FunctionRegistryTest, CanRegisterNonStrictFunction) {
{
FunctionRegistry registry;
cel::FunctionDescriptor descriptor("NonStrictFunction",
/*receiver_style=*/false, {Kind::kAny},
/*is_strict=*/false);
ASSERT_OK(
registry.Register(descriptor, std::make_unique<ConstIntFunction>()));
EXPECT_THAT(
registry.FindStaticOverloads("NonStrictFunction", false, {Kind::kAny}),
SizeIs(1));
}
{
FunctionRegistry registry;
cel::FunctionDescriptor descriptor("NonStrictLazyFunction",
/*receiver_style=*/false, {Kind::kAny},
/*is_strict=*/false);
EXPECT_OK(registry.RegisterLazyFunction(descriptor));
EXPECT_THAT(registry.FindLazyOverloads("NonStrictLazyFunction", false,
{Kind::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();
FunctionRegistry registry;
cel::FunctionDescriptor descriptor("OverloadedFunction",
/*receiver_style=*/false, {Kind::kAny},
/*is_strict=*/true);
if (existing_function_is_lazy) {
ASSERT_OK(registry.RegisterLazyFunction(descriptor));
} else {
ASSERT_OK(
registry.Register(descriptor, std::make_unique<ConstIntFunction>()));
}
cel::FunctionDescriptor new_descriptor("OverloadedFunction",
/*receiver_style=*/false,
{Kind::kAny, Kind::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<ConstIntFunction>());
}
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();
FunctionRegistry registry;
cel::FunctionDescriptor descriptor("OverloadedFunction",
/*receiver_style=*/false, {Kind::kAny},
/*is_strict=*/false);
if (existing_function_is_lazy) {
ASSERT_OK(registry.RegisterLazyFunction(descriptor));
} else {
ASSERT_OK(
registry.Register(descriptor, std::make_unique<ConstIntFunction>()));
}
cel::FunctionDescriptor new_descriptor("OverloadedFunction",
/*receiver_style=*/false,
{Kind::kAny, Kind::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<ConstIntFunction>());
}
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();
FunctionRegistry registry;
cel::FunctionDescriptor descriptor("OverloadedFunction",
/*receiver_style=*/false, {Kind::kAny},
/*is_strict=*/true);
if (existing_function_is_lazy) {
ASSERT_OK(registry.RegisterLazyFunction(descriptor));
} else {
ASSERT_OK(
registry.Register(descriptor, std::make_unique<ConstIntFunction>()));
}
cel::FunctionDescriptor new_descriptor("OverloadedFunction",
/*receiver_style=*/false,
{Kind::kAny, Kind::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<ConstIntFunction>());
}
EXPECT_OK(status);
}
INSTANTIATE_TEST_SUITE_P(NonStrictRegistrationFailTest,
NonStrictRegistrationFailTest,
testing::Combine(testing::Bool(), testing::Bool()));
} // namespace
} // namespace cel