forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluator_core_test.cc
More file actions
317 lines (249 loc) · 10.3 KB
/
Copy pathevaluator_core_test.cc
File metadata and controls
317 lines (249 loc) · 10.3 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
308
309
310
311
312
313
314
315
316
317
#include "eval/eval/evaluator_core.h"
#include "google/api/expr/v1alpha1/syntax.pb.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "eval/compiler/flat_expr_builder.h"
#include "eval/eval/attribute_trail.h"
#include "eval/public/builtin_func_registrar.h"
#include "eval/public/cel_attribute.h"
#include "eval/public/cel_value.h"
#include "base/status_macros.h"
namespace google {
namespace api {
namespace expr {
namespace runtime {
using google::api::expr::v1alpha1::Expr;
using ::google::api::expr::runtime::RegisterBuiltinFunctions;
using testing::_;
using testing::Eq;
using testing::NotNull;
// Fake expression implementation
// Pushes int64_t(0) on top of value stack.
class FakeConstExpressionStep : public ExpressionStep {
public:
absl::Status Evaluate(ExecutionFrame* frame) const override {
frame->value_stack().Push(CelValue::CreateInt64(0));
return absl::OkStatus();
}
int64_t id() const override { return 0; }
bool ComesFromAst() const override { return true; }
};
// Fake expression implementation
// Increments argument on top of the stack.
class FakeIncrementExpressionStep : public ExpressionStep {
public:
absl::Status Evaluate(ExecutionFrame* frame) const override {
CelValue value = frame->value_stack().Peek();
frame->value_stack().Pop(1);
EXPECT_TRUE(value.IsInt64());
int64_t val = value.Int64OrDie();
frame->value_stack().Push(CelValue::CreateInt64(val + 1));
return absl::OkStatus();
}
int64_t id() const override { return 0; }
bool ComesFromAst() const override { return true; }
};
// Test Value Stack Push/Pop operation
TEST(EvaluatorCoreTest, ValueStackPushPop) {
google::protobuf::Arena arena;
google::api::expr::v1alpha1::Expr expr;
expr.mutable_ident_expr()->set_name("name");
CelAttribute attribute(expr, {});
ValueStack stack(10);
stack.Push(CelValue::CreateInt64(1));
stack.Push(CelValue::CreateInt64(2), AttributeTrail());
stack.Push(CelValue::CreateInt64(3), AttributeTrail(expr, &arena));
ASSERT_EQ(stack.Peek().Int64OrDie(), 3);
ASSERT_THAT(stack.PeekAttribute().attribute(), NotNull());
ASSERT_EQ(*stack.PeekAttribute().attribute(), attribute);
stack.Pop(1);
ASSERT_EQ(stack.Peek().Int64OrDie(), 2);
ASSERT_EQ(stack.PeekAttribute().attribute(), nullptr);
stack.Pop(1);
ASSERT_EQ(stack.Peek().Int64OrDie(), 1);
ASSERT_EQ(stack.PeekAttribute().attribute(), nullptr);
}
// Test that inner stacks within value stack retain the equality of their sizes.
TEST(EvaluatorCoreTest, ValueStackBalanced) {
ValueStack stack(10);
ASSERT_EQ(stack.size(), stack.attribute_size());
stack.Push(CelValue::CreateInt64(1));
ASSERT_EQ(stack.size(), stack.attribute_size());
stack.Push(CelValue::CreateInt64(2), AttributeTrail());
stack.Push(CelValue::CreateInt64(3), AttributeTrail());
ASSERT_EQ(stack.size(), stack.attribute_size());
stack.PopAndPush(CelValue::CreateInt64(4), AttributeTrail());
ASSERT_EQ(stack.size(), stack.attribute_size());
stack.PopAndPush(CelValue::CreateInt64(5));
ASSERT_EQ(stack.size(), stack.attribute_size());
stack.Pop(3);
ASSERT_EQ(stack.size(), stack.attribute_size());
}
TEST(EvaluatorCoreTest, ExecutionFrameNext) {
ExecutionPath path;
auto const_step = absl::make_unique<const FakeConstExpressionStep>();
auto incr_step1 = absl::make_unique<const FakeIncrementExpressionStep>();
auto incr_step2 = absl::make_unique<const FakeIncrementExpressionStep>();
path.push_back(std::move(const_step));
path.push_back(std::move(incr_step1));
path.push_back(std::move(incr_step2));
auto dummy_expr = absl::make_unique<Expr>();
Activation activation;
CelExpressionFlatEvaluationState state(path.size(), {}, nullptr);
ExecutionFrame frame(path, activation, 0, &state, false, false, false);
EXPECT_THAT(frame.Next(), Eq(path[0].get()));
EXPECT_THAT(frame.Next(), Eq(path[1].get()));
EXPECT_THAT(frame.Next(), Eq(path[2].get()));
EXPECT_THAT(frame.Next(), Eq(nullptr));
}
// Test the set, get, and clear functions for "IterVar" on ExecutionFrame
TEST(EvaluatorCoreTest, ExecutionFrameSetGetClearVar) {
const std::string test_key = "test_key";
const int64_t test_value = 0xF00F00;
Activation activation;
google::protobuf::Arena arena;
ExecutionPath path;
CelExpressionFlatEvaluationState state(path.size(), {test_key}, nullptr);
ExecutionFrame frame(path, activation, 0, &state, false, false, false);
CelValue original = CelValue::CreateInt64(test_value);
Expr ident;
ident.mutable_ident_expr()->set_name("var");
AttributeTrail original_trail =
AttributeTrail(ident, &arena)
.Step(CelAttributeQualifier::Create(CelValue::CreateInt64(1)),
&arena);
CelValue result;
const AttributeTrail* trail;
ASSERT_OK(frame.PushIterFrame());
// Nothing is there yet
ASSERT_FALSE(frame.GetIterVar(test_key, &result));
ASSERT_OK(frame.SetIterVar(test_key, original, original_trail));
// Make sure its now there
ASSERT_TRUE(frame.GetIterVar(test_key, &result));
ASSERT_TRUE(frame.GetIterAttr(test_key, &trail));
int64_t result_value;
ASSERT_TRUE(result.GetValue(&result_value));
EXPECT_EQ(test_value, result_value);
ASSERT_TRUE(trail->attribute()->variable().has_ident_expr());
ASSERT_EQ(trail->attribute()->variable().ident_expr().name(), "var");
// Test that it goes away properly
ASSERT_OK(frame.ClearIterVar(test_key));
ASSERT_FALSE(frame.GetIterVar(test_key, &result));
ASSERT_FALSE(frame.GetIterAttr(test_key, &trail));
// Test that bogus names return the right thing
ASSERT_FALSE(frame.SetIterVar("foo", original).ok());
ASSERT_FALSE(frame.ClearIterVar("bar").ok());
// Test error conditions for accesses outside of comprehension.
ASSERT_OK(frame.SetIterVar(test_key, original));
ASSERT_OK(frame.PopIterFrame());
// Access on empty stack ok, but no value.
ASSERT_FALSE(frame.GetIterVar(test_key, &result));
// Pop empty stack
ASSERT_FALSE(frame.PopIterFrame().ok());
// Updates on empty stack not ok.
ASSERT_FALSE(frame.SetIterVar(test_key, original).ok());
ASSERT_FALSE(frame.ClearIterVar(test_key).ok());
}
TEST(EvaluatorCoreTest, SimpleEvaluatorTest) {
ExecutionPath path;
auto const_step = absl::make_unique<FakeConstExpressionStep>();
auto incr_step1 = absl::make_unique<FakeIncrementExpressionStep>();
auto incr_step2 = absl::make_unique<FakeIncrementExpressionStep>();
path.push_back(std::move(const_step));
path.push_back(std::move(incr_step1));
path.push_back(std::move(incr_step2));
auto dummy_expr = absl::make_unique<Expr>();
CelExpressionFlatImpl impl(dummy_expr.get(), std::move(path), 0, {});
Activation activation;
google::protobuf::Arena arena;
auto status = impl.Evaluate(activation, &arena);
EXPECT_OK(status);
auto value = status.value();
EXPECT_TRUE(value.IsInt64());
EXPECT_THAT(value.Int64OrDie(), Eq(2));
}
class MockTraceCallback {
public:
MOCK_METHOD(void, Call,
(int64_t expr_id, const CelValue& value, google::protobuf::Arena*));
};
TEST(EvaluatorCoreTest, TraceTest) {
Expr expr;
google::api::expr::v1alpha1::SourceInfo source_info;
// 1 && [1,2,3].all(x, x > 0)
expr.set_id(1);
auto and_call = expr.mutable_call_expr();
and_call->set_function("_&&_");
auto true_expr = and_call->add_args();
true_expr->set_id(2);
true_expr->mutable_const_expr()->set_int64_value(1);
auto comp_expr = and_call->add_args();
comp_expr->set_id(3);
auto comp = comp_expr->mutable_comprehension_expr();
comp->set_iter_var("x");
comp->set_accu_var("accu");
auto list_expr = comp->mutable_iter_range();
list_expr->set_id(4);
auto el1_expr = list_expr->mutable_list_expr()->add_elements();
el1_expr->set_id(11);
el1_expr->mutable_const_expr()->set_int64_value(1);
auto el2_expr = list_expr->mutable_list_expr()->add_elements();
el2_expr->set_id(12);
el2_expr->mutable_const_expr()->set_int64_value(2);
auto el3_expr = list_expr->mutable_list_expr()->add_elements();
el3_expr->set_id(13);
el3_expr->mutable_const_expr()->set_int64_value(3);
auto accu_init_expr = comp->mutable_accu_init();
accu_init_expr->set_id(20);
accu_init_expr->mutable_const_expr()->set_bool_value(true);
auto loop_cond_expr = comp->mutable_loop_condition();
loop_cond_expr->set_id(21);
loop_cond_expr->mutable_const_expr()->set_bool_value(true);
auto loop_step_expr = comp->mutable_loop_step();
loop_step_expr->set_id(22);
auto condition = loop_step_expr->mutable_call_expr();
condition->set_function("_>_");
auto iter_expr = condition->add_args();
iter_expr->set_id(23);
iter_expr->mutable_ident_expr()->set_name("x");
auto zero_expr = condition->add_args();
zero_expr->set_id(24);
zero_expr->mutable_const_expr()->set_int64_value(0);
auto result_expr = comp->mutable_result();
result_expr->set_id(25);
result_expr->mutable_const_expr()->set_bool_value(true);
FlatExprBuilder builder;
auto builtin_status = RegisterBuiltinFunctions(builder.GetRegistry());
ASSERT_OK(builtin_status);
builder.set_shortcircuiting(false);
auto build_status = builder.CreateExpression(&expr, &source_info);
ASSERT_OK(build_status);
auto cel_expr = std::move(build_status.value());
Activation activation;
google::protobuf::Arena arena;
MockTraceCallback callback;
EXPECT_CALL(callback, Call(accu_init_expr->id(), _, &arena));
EXPECT_CALL(callback, Call(el1_expr->id(), _, &arena));
EXPECT_CALL(callback, Call(el2_expr->id(), _, &arena));
EXPECT_CALL(callback, Call(el3_expr->id(), _, &arena));
EXPECT_CALL(callback, Call(list_expr->id(), _, &arena));
EXPECT_CALL(callback, Call(loop_cond_expr->id(), _, &arena)).Times(3);
EXPECT_CALL(callback, Call(iter_expr->id(), _, &arena)).Times(3);
EXPECT_CALL(callback, Call(zero_expr->id(), _, &arena)).Times(3);
EXPECT_CALL(callback, Call(loop_step_expr->id(), _, &arena)).Times(3);
EXPECT_CALL(callback, Call(result_expr->id(), _, &arena));
EXPECT_CALL(callback, Call(comp_expr->id(), _, &arena));
EXPECT_CALL(callback, Call(true_expr->id(), _, &arena));
EXPECT_CALL(callback, Call(expr.id(), _, &arena));
auto eval_status = cel_expr->Trace(
activation, &arena,
[&](int64_t expr_id, const CelValue& value, google::protobuf::Arena* arena) {
callback.Call(expr_id, value, arena);
return absl::OkStatus();
});
ASSERT_OK(eval_status);
}
} // namespace runtime
} // namespace expr
} // namespace api
} // namespace google