forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex_precompilation_test.cc
More file actions
192 lines (161 loc) · 6.57 KB
/
Copy pathregex_precompilation_test.cc
File metadata and controls
192 lines (161 loc) · 6.57 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
// 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/regex_precompilation.h"
#include <string>
#include <utility>
#include <vector>
#include "cel/expr/syntax.pb.h"
#include "absl/status/status.h"
#include "absl/status/status_matchers.h"
#include "absl/status/statusor.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "base/function_adapter.h"
#include "common/value.h"
#include "extensions/protobuf/runtime_adapter.h"
#include "internal/testing.h"
#include "internal/testing_descriptor_pool.h"
#include "parser/parser.h"
#include "runtime/activation.h"
#include "runtime/constant_folding.h"
#include "runtime/register_function_helper.h"
#include "runtime/runtime_builder.h"
#include "runtime/runtime_options.h"
#include "runtime/standard_runtime_builder_factory.h"
#include "google/protobuf/arena.h"
namespace cel::extensions {
namespace {
using ::absl_testing::IsOk;
using ::absl_testing::StatusIs;
using ::cel::expr::ParsedExpr;
using ::google::api::expr::parser::Parse;
using ::testing::_;
using ::testing::HasSubstr;
using ValueMatcher = testing::Matcher<Value>;
struct TestCase {
std::string name;
std::string expression;
ValueMatcher result_matcher;
absl::Status create_status;
};
MATCHER_P(IsIntValue, expected, "") {
const Value& value = arg;
return value->Is<IntValue>() && value.GetInt().NativeValue() == expected;
}
MATCHER_P(IsBoolValue, expected, "") {
const Value& value = arg;
return value->Is<BoolValue>() && value.GetBool().NativeValue() == expected;
}
MATCHER_P(IsErrorValue, expected_substr, "") {
const Value& value = arg;
return value->Is<ErrorValue>() &&
absl::StrContains(value.GetError().NativeValue().message(),
expected_substr);
}
class RegexPrecompilationTest : public testing::TestWithParam<TestCase> {};
TEST_P(RegexPrecompilationTest, Basic) {
RuntimeOptions options;
const TestCase& test_case = GetParam();
ASSERT_OK_AND_ASSIGN(cel::RuntimeBuilder builder,
CreateStandardRuntimeBuilder(
internal::GetTestingDescriptorPool(), options));
auto status = RegisterHelper<BinaryFunctionAdapter<
absl::StatusOr<Value>, const StringValue&, const StringValue&>>::
RegisterGlobalOverload(
"prepend",
[](const StringValue& value, const StringValue& prefix) {
return StringValue(
absl::StrCat(prefix.ToString(), value.ToString()));
},
builder.function_registry());
ASSERT_THAT(status, IsOk());
ASSERT_THAT(EnableRegexPrecompilation(builder), IsOk());
ASSERT_OK_AND_ASSIGN(auto runtime, std::move(builder).Build());
ASSERT_OK_AND_ASSIGN(ParsedExpr parsed_expr, Parse(test_case.expression));
auto program_or =
ProtobufRuntimeAdapter::CreateProgram(*runtime, parsed_expr);
if (!test_case.create_status.ok()) {
ASSERT_THAT(program_or.status(),
StatusIs(test_case.create_status.code(),
HasSubstr(test_case.create_status.message())));
return;
}
ASSERT_OK_AND_ASSIGN(auto program, std::move(program_or));
google::protobuf::Arena arena;
Activation activation;
activation.InsertOrAssignValue("string_var",
StringValue(&arena, "string_var"));
ASSERT_OK_AND_ASSIGN(Value value, program->Evaluate(&arena, activation));
EXPECT_THAT(value, test_case.result_matcher);
}
TEST_P(RegexPrecompilationTest, WithConstantFolding) {
RuntimeOptions options;
const TestCase& test_case = GetParam();
ASSERT_OK_AND_ASSIGN(cel::RuntimeBuilder builder,
CreateStandardRuntimeBuilder(
internal::GetTestingDescriptorPool(), options));
auto status = RegisterHelper<BinaryFunctionAdapter<
absl::StatusOr<Value>, const StringValue&, const StringValue&>>::
RegisterGlobalOverload(
"prepend",
[](const StringValue& value, const StringValue& prefix) {
return StringValue(
absl::StrCat(prefix.ToString(), value.ToString()));
},
builder.function_registry());
ASSERT_THAT(status, IsOk());
ASSERT_THAT(EnableConstantFolding(builder), IsOk());
ASSERT_THAT(EnableRegexPrecompilation(builder), IsOk());
ASSERT_OK_AND_ASSIGN(auto runtime, std::move(builder).Build());
ASSERT_OK_AND_ASSIGN(ParsedExpr parsed_expr, Parse(test_case.expression));
auto program_or =
ProtobufRuntimeAdapter::CreateProgram(*runtime, parsed_expr);
if (!test_case.create_status.ok()) {
ASSERT_THAT(program_or.status(),
StatusIs(test_case.create_status.code(),
HasSubstr(test_case.create_status.message())));
return;
}
ASSERT_OK_AND_ASSIGN(auto program, std::move(program_or));
google::protobuf::Arena arena;
Activation activation;
activation.InsertOrAssignValue("string_var",
StringValue(&arena, "string_var"));
ASSERT_OK_AND_ASSIGN(Value value, program->Evaluate(&arena, activation));
EXPECT_THAT(value, test_case.result_matcher);
}
INSTANTIATE_TEST_SUITE_P(
Cases, RegexPrecompilationTest,
testing::ValuesIn(std::vector<TestCase>{
{"matches_receiver", R"(string_var.matches(r's\w+_var'))",
IsBoolValue(true)},
{"matches_receiver_false", R"(string_var.matches(r'string_var\d+'))",
IsBoolValue(false)},
{"matches_global_true", R"(matches(string_var, r's\w+_var'))",
IsBoolValue(true)},
{"matches_global_false", R"(matches(string_var, r'string_var\d+'))",
IsBoolValue(false)},
{"matches_bad_re2_expression", "matches('123', r'(?<!a)123')", _,
absl::InvalidArgumentError("invalid regular expression")},
{"matches_unsupported_call_signature",
"matches('123', r'(?<!a)123', 'gi')", _,
absl::InvalidArgumentError("No overloads")},
{"constant_computation",
"matches(string_var, r'string' + '_' + r'var')", IsBoolValue(true)},
}),
[](const testing::TestParamInfo<TestCase>& info) {
return info.param.name;
});
} // namespace
} // namespace cel::extensions