Skip to content

Commit 4d19eab

Browse files
jnthntatumcopybara-github
authored andcommitted
Add example for configuring macros with cel::CompilerBuilder.
PiperOrigin-RevId: 735803661
1 parent 6e1eba6 commit 4d19eab

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

compiler/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ cc_test(
7474
"//testutil:baseline_tests",
7575
"@com_google_absl//absl/status",
7676
"@com_google_absl//absl/status:status_matchers",
77+
"@com_google_absl//absl/strings",
7778
"@com_google_protobuf//:protobuf",
7879
],
7980
)

compiler/compiler_factory_test.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "absl/status/status.h"
2121
#include "absl/status/status_matchers.h"
22+
#include "absl/strings/match.h"
2223
#include "checker/optional.h"
2324
#include "checker/standard_library.h"
2425
#include "checker/type_check_issue.h"
@@ -42,6 +43,7 @@ using ::cel::test::FormatBaselineAst;
4243
using ::testing::Contains;
4344
using ::testing::HasSubstr;
4445
using ::testing::Property;
46+
using ::testing::Truly;
4547

4648
TEST(CompilerFactoryTest, Works) {
4749
ASSERT_OK_AND_ASSIGN(
@@ -162,6 +164,43 @@ TEST(CompilerFactoryTest, ParserOptions) {
162164
ASSERT_THAT(compiler->Compile("a.?b.orValue('foo')"), IsOk());
163165
}
164166

167+
TEST(CompilerFactoryTest, DisableStandarMacros) {
168+
CompilerOptions options;
169+
options.parser_options.disable_standard_macros = true;
170+
171+
ASSERT_OK_AND_ASSIGN(
172+
auto builder,
173+
NewCompilerBuilder(cel::internal::GetSharedTestingDescriptorPool(),
174+
options));
175+
176+
ASSERT_THAT(builder->AddLibrary(StandardCheckerLibrary()), IsOk());
177+
ASSERT_THAT(builder->GetParserBuilder().AddMacro(cel::ExistsMacro()), IsOk());
178+
179+
// a: map(dyn, dyn)
180+
ASSERT_THAT(builder->GetCheckerBuilder().AddVariable(
181+
MakeVariableDecl("a", MapType())),
182+
IsOk());
183+
184+
ASSERT_OK_AND_ASSIGN(auto compiler, std::move(*builder).Build());
185+
186+
ASSERT_OK_AND_ASSIGN(ValidationResult result, compiler->Compile("a.b"));
187+
188+
EXPECT_TRUE(result.IsValid());
189+
190+
// The has macro is disabled, so looks like a function call.
191+
ASSERT_OK_AND_ASSIGN(result, compiler->Compile("has(a.b)"));
192+
193+
EXPECT_FALSE(result.IsValid());
194+
EXPECT_THAT(result.GetIssues(),
195+
Contains(Truly([](const TypeCheckIssue& issue) {
196+
return absl::StrContains(issue.message(),
197+
"undeclared reference to 'has'");
198+
})));
199+
200+
ASSERT_OK_AND_ASSIGN(result, compiler->Compile("a.exists(x, x == 'foo')"));
201+
EXPECT_TRUE(result.IsValid());
202+
}
203+
165204
TEST(CompilerFactoryTest, FailsIfLibraryAddedTwice) {
166205
ASSERT_OK_AND_ASSIGN(
167206
auto builder,

0 commit comments

Comments
 (0)