forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcel_function_adapter_test.cc
More file actions
173 lines (120 loc) · 5.3 KB
/
Copy pathcel_function_adapter_test.cc
File metadata and controls
173 lines (120 loc) · 5.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
#include "eval/public/cel_function_adapter.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "base/status_macros.h"
namespace google {
namespace api {
namespace expr {
namespace runtime {
namespace {
TEST(CelFunctionAdapterTest, TestAdapterNoArg) {
auto func = [](google::protobuf::Arena*) -> int64_t { return 100; };
auto func_status = FunctionAdapter<int64_t>::Create("const", false, func);
ASSERT_OK(func_status);
auto cel_func = std::move(func_status.value());
absl::Span<CelValue> args;
CelValue result = CelValue::CreateNull();
google::protobuf::Arena arena;
auto eval_status = cel_func->Evaluate(args, &result, &arena);
ASSERT_OK(eval_status);
ASSERT_TRUE(
result.IsInt64()); // Obvious failure, for educational purposes only.
}
TEST(CelFunctionAdapterTest, TestAdapterOneArg) {
std::function<int64_t(google::protobuf::Arena*, int64_t)> func =
[](google::protobuf::Arena* arena, int64_t i) -> int64_t { return i + 1; };
auto func_status = FunctionAdapter<int64_t, int64_t>::Create("_++_", false, func);
ASSERT_OK(func_status);
auto cel_func = std::move(func_status.value());
std::vector<CelValue> args_vec;
args_vec.push_back(CelValue::CreateInt64(99));
CelValue result = CelValue::CreateNull();
google::protobuf::Arena arena;
absl::Span<CelValue> args(&args_vec[0], args_vec.size());
auto eval_status = cel_func->Evaluate(args, &result, &arena);
ASSERT_OK(eval_status);
ASSERT_TRUE(result.IsInt64());
EXPECT_EQ(result.Int64OrDie(), 100);
}
TEST(CelFunctionAdapterTest, TestAdapterTwoArgs) {
auto func = [](google::protobuf::Arena* arena, int64_t i, int64_t j) -> int64_t {
return i + j;
};
auto func_status =
FunctionAdapter<int64_t, int64_t, int64_t>::Create("_++_", false, func);
ASSERT_OK(func_status);
auto cel_func = std::move(func_status.value());
std::vector<CelValue> args_vec;
args_vec.push_back(CelValue::CreateInt64(20));
args_vec.push_back(CelValue::CreateInt64(22));
CelValue result = CelValue::CreateNull();
google::protobuf::Arena arena;
absl::Span<CelValue> args(&args_vec[0], args_vec.size());
auto eval_status = cel_func->Evaluate(args, &result, &arena);
ASSERT_OK(eval_status);
ASSERT_TRUE(result.IsInt64());
EXPECT_EQ(result.Int64OrDie(), 42);
}
using StringHolder = CelValue::StringHolder;
TEST(CelFunctionAdapterTest, TestAdapterThreeArgs) {
auto func = [](google::protobuf::Arena* arena, StringHolder s1, StringHolder s2,
StringHolder s3) -> StringHolder {
std::string value = absl::StrCat(s1.value(), s2.value(), s3.value());
return StringHolder(
google::protobuf::Arena::Create<std::string>(arena, std::move(value)));
};
auto func_status =
FunctionAdapter<StringHolder, StringHolder, StringHolder,
StringHolder>::Create("concat", false, func);
ASSERT_OK(func_status);
auto cel_func = std::move(func_status.value());
std::string test1 = "1";
std::string test2 = "2";
std::string test3 = "3";
std::vector<CelValue> args_vec;
args_vec.push_back(CelValue::CreateString(&test1));
args_vec.push_back(CelValue::CreateString(&test2));
args_vec.push_back(CelValue::CreateString(&test3));
CelValue result = CelValue::CreateNull();
google::protobuf::Arena arena;
absl::Span<CelValue> args(&args_vec[0], args_vec.size());
auto eval_status = cel_func->Evaluate(args, &result, &arena);
ASSERT_OK(eval_status);
ASSERT_TRUE(result.IsString());
EXPECT_EQ(result.StringOrDie().value(), "123");
}
TEST(CelFunctionAdapterTest, TestTypeDeductionForCelValueBasicTypes) {
auto func = [](google::protobuf::Arena* arena, bool, int64_t, uint64_t, double,
CelValue::StringHolder, CelValue::BytesHolder,
const google::protobuf::Message*, absl::Duration, absl::Time,
const CelList*, const CelMap*,
const CelError*) -> bool { return false; };
auto func_status =
FunctionAdapter<bool, bool, int64_t, uint64_t, double, CelValue::StringHolder,
CelValue::BytesHolder, const google::protobuf::Message*,
absl::Duration, absl::Time, const CelList*, const CelMap*,
const CelError*>::Create("dummy_func", false, func);
ASSERT_OK(func_status);
auto cel_func = std::move(func_status.value());
auto descriptor = cel_func->descriptor();
EXPECT_EQ(descriptor.receiver_style(), false);
EXPECT_EQ(descriptor.name(), "dummy_func");
int pos = 0;
ASSERT_EQ(descriptor.types()[pos++], CelValue::Type::kBool);
ASSERT_EQ(descriptor.types()[pos++], CelValue::Type::kInt64);
ASSERT_EQ(descriptor.types()[pos++], CelValue::Type::kUint64);
ASSERT_EQ(descriptor.types()[pos++], CelValue::Type::kDouble);
ASSERT_EQ(descriptor.types()[pos++], CelValue::Type::kString);
ASSERT_EQ(descriptor.types()[pos++], CelValue::Type::kBytes);
ASSERT_EQ(descriptor.types()[pos++], CelValue::Type::kMessage);
ASSERT_EQ(descriptor.types()[pos++], CelValue::Type::kDuration);
ASSERT_EQ(descriptor.types()[pos++], CelValue::Type::kTimestamp);
ASSERT_EQ(descriptor.types()[pos++], CelValue::Type::kList);
ASSERT_EQ(descriptor.types()[pos++], CelValue::Type::kMap);
ASSERT_EQ(descriptor.types()[pos++], CelValue::Type::kError);
}
} // namespace
} // namespace runtime
} // namespace expr
} // namespace api
} // namespace google