-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathevaluator_core_test.cc
More file actions
282 lines (223 loc) · 9.38 KB
/
Copy pathevaluator_core_test.cc
File metadata and controls
282 lines (223 loc) · 9.38 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
#include "eval/eval/evaluator_core.h"
#include <memory>
#include <string>
#include <utility>
#include "google/api/expr/v1alpha1/syntax.pb.h"
#include "google/protobuf/descriptor.h"
#include "eval/compiler/flat_expr_builder.h"
#include "eval/eval/attribute_trail.h"
#include "eval/eval/test_type_registry.h"
#include "eval/internal/interop.h"
#include "eval/public/activation.h"
#include "eval/public/builtin_func_registrar.h"
#include "eval/public/cel_attribute.h"
#include "eval/public/cel_value.h"
#include "extensions/protobuf/memory_manager.h"
#include "internal/testing.h"
#include "runtime/runtime_options.h"
namespace google::api::expr::runtime {
using ::cel::extensions::ProtoMemoryManager;
using ::cel::interop_internal::CreateIntValue;
using ::google::api::expr::v1alpha1::Expr;
using ::google::api::expr::runtime::RegisterBuiltinFunctions;
using testing::_;
using testing::Eq;
// 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(CreateIntValue(0));
return absl::OkStatus();
}
int64_t id() const override { return 0; }
bool ComesFromAst() const override { return true; }
cel::internal::TypeInfo TypeId() const override {
return cel::internal::TypeInfo();
}
};
// Fake expression implementation
// Increments argument on top of the stack.
class FakeIncrementExpressionStep : public ExpressionStep {
public:
absl::Status Evaluate(ExecutionFrame* frame) const override {
CelValue value = cel::interop_internal::ModernValueToLegacyValueOrDie(
frame->memory_manager(), frame->value_stack().Peek());
frame->value_stack().Pop(1);
EXPECT_TRUE(value.IsInt64());
int64_t val = value.Int64OrDie();
frame->value_stack().Push(CreateIntValue(val + 1));
return absl::OkStatus();
}
int64_t id() const override { return 0; }
bool ComesFromAst() const override { return true; }
cel::internal::TypeInfo TypeId() const override {
return cel::internal::TypeInfo();
}
};
TEST(EvaluatorCoreTest, ExecutionFrameNext) {
ExecutionPath path;
auto const_step = std::make_unique<const FakeConstExpressionStep>();
auto incr_step1 = std::make_unique<const FakeIncrementExpressionStep>();
auto incr_step2 = std::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 = std::make_unique<Expr>();
cel::RuntimeOptions options;
options.unknown_processing = cel::UnknownProcessingOptions::kDisabled;
Activation activation;
CelExpressionFlatEvaluationState state(path.size(), nullptr);
ExecutionFrame frame(path, activation, &TestTypeRegistry(), options, &state);
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_iter_var = "test_iter_var";
const std::string test_accu_var = "test_accu_var";
const int64_t test_value = 0xF00F00;
Activation activation;
google::protobuf::Arena arena;
ProtoMemoryManager manager(&arena);
ExecutionPath path;
CelExpressionFlatEvaluationState state(path.size(), nullptr);
cel::RuntimeOptions options;
options.unknown_processing = cel::UnknownProcessingOptions::kDisabled;
ExecutionFrame frame(path, activation, &TestTypeRegistry(), options, &state);
auto original = cel::interop_internal::CreateIntValue(test_value);
Expr ident;
ident.mutable_ident_expr()->set_name("var");
AttributeTrail original_trail =
AttributeTrail(ident, manager)
.Step(CreateCelAttributeQualifier(CelValue::CreateInt64(1)), manager);
cel::Handle<cel::Value> result;
AttributeTrail trail;
ASSERT_OK(frame.PushIterFrame(test_iter_var, test_accu_var));
// Nothing is there yet
ASSERT_FALSE(frame.GetIterVar(test_iter_var, &result, nullptr));
ASSERT_OK(frame.SetIterVar(original, original_trail));
// Nothing is there yet
ASSERT_FALSE(frame.GetIterVar(test_accu_var, &result, nullptr));
ASSERT_OK(frame.SetAccuVar(cel::interop_internal::CreateBoolValue(true)));
ASSERT_TRUE(frame.GetIterVar(test_accu_var, &result, nullptr));
ASSERT_TRUE(result->Is<cel::BoolValue>());
EXPECT_EQ(result.As<cel::BoolValue>()->value(), true);
// Make sure its now there
ASSERT_TRUE(frame.GetIterVar(test_iter_var, &result, &trail));
int64_t result_value = result.As<cel::IntValue>()->value();
EXPECT_EQ(test_value, result_value);
ASSERT_TRUE(trail.attribute().has_variable_name());
ASSERT_EQ(trail.attribute().variable_name(), "var");
// Test that it goes away properly
ASSERT_OK(frame.ClearIterVar());
ASSERT_FALSE(frame.GetIterVar(test_iter_var, &result, &trail));
ASSERT_OK(frame.PopIterFrame());
// Access on empty stack ok, but no value.
ASSERT_FALSE(frame.GetIterVar(test_iter_var, &result, nullptr));
// Pop empty stack
ASSERT_FALSE(frame.PopIterFrame().ok());
// Updates on empty stack not ok.
ASSERT_FALSE(frame.SetIterVar(original).ok());
}
TEST(EvaluatorCoreTest, SimpleEvaluatorTest) {
ExecutionPath path;
auto const_step = std::make_unique<FakeConstExpressionStep>();
auto incr_step1 = std::make_unique<FakeIncrementExpressionStep>();
auto incr_step2 = std::make_unique<FakeIncrementExpressionStep>();
path.push_back(std::move(const_step));
path.push_back(std::move(incr_step1));
path.push_back(std::move(incr_step2));
CelExpressionFlatImpl impl(std::move(path), &TestTypeRegistry(),
cel::RuntimeOptions{});
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);
cel::RuntimeOptions options;
options.short_circuiting = false;
FlatExprBuilder builder(options);
ASSERT_OK(RegisterBuiltinFunctions(builder.GetRegistry()));
ASSERT_OK_AND_ASSIGN(auto cel_expr,
builder.CreateExpression(&expr, &source_info));
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 google::api::expr::runtime