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;
4243using ::testing::Contains;
4344using ::testing::HasSubstr;
4445using ::testing::Property;
46+ using ::testing::Truly;
4547
4648TEST (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+
165204TEST (CompilerFactoryTest, FailsIfLibraryAddedTwice) {
166205 ASSERT_OK_AND_ASSIGN (
167206 auto builder,
0 commit comments