@@ -86,24 +86,63 @@ public void standardDeclaration_moreThanOneIdentifierFilterSet_throws(
8686 }
8787
8888 @ Test
89- public void compiler_standardEnvironmentEnabled_throwsWhenOverridingDeclarations () {
90- IllegalArgumentException e =
91- assertThrows (
92- IllegalArgumentException .class ,
93- () ->
94- CelCompilerFactory .standardCelCompilerBuilder ()
95- .setStandardEnvironmentEnabled (true )
96- .setStandardDeclarations (
97- CelStandardDeclarations .newBuilder ()
98- .includeFunctions (StandardFunction .ADD , StandardFunction .SUBTRACT )
99- .build ())
100- .build ());
89+ public void compiler_setStandardDeclarations_overridesDefaultStandardEnvironment ()
90+ throws Exception {
91+ CelCompiler compiler =
92+ CelCompilerFactory .standardCelCompilerBuilder ()
93+ .setStandardDeclarations (
94+ CelStandardDeclarations .newBuilder ()
95+ .includeFunctions (StandardFunction .ADD )
96+ .build ())
97+ .build ();
10198
102- assertThat (e )
103- .hasMessageThat ()
104- .contains (
105- "setStandardEnvironmentEnabled must be set to false to override standard"
106- + " declarations." );
99+ assertThat (compiler .compile ("1 + 1" ).hasError ()).isFalse ();
100+ assertThat (compiler .compile ("1 - 1" ).hasError ()).isTrue ();
101+ }
102+
103+ @ Test
104+ public void compiler_setStandardDeclarations_withStandardEnvironmentExplicitlyEnabled ()
105+ throws Exception {
106+ CelCompiler compiler =
107+ CelCompilerFactory .standardCelCompilerBuilder ()
108+ .setStandardEnvironmentEnabled (true )
109+ .setStandardDeclarations (
110+ CelStandardDeclarations .newBuilder ()
111+ .includeFunctions (StandardFunction .ADD )
112+ .build ())
113+ .build ();
114+
115+ assertThat (compiler .compile ("1 + 1" ).hasError ()).isFalse ();
116+ assertThat (compiler .compile ("1 - 1" ).hasError ()).isTrue ();
117+ }
118+
119+ @ Test
120+ public void compiler_setStandardDeclarations_withStandardEnvironmentExplicitlyDisabled ()
121+ throws Exception {
122+ CelCompiler compiler =
123+ CelCompilerFactory .standardCelCompilerBuilder ()
124+ .setStandardEnvironmentEnabled (false )
125+ .setStandardDeclarations (
126+ CelStandardDeclarations .newBuilder ()
127+ .includeFunctions (StandardFunction .ADD )
128+ .build ())
129+ .build ();
130+
131+ assertThat (compiler .compile ("1 + 1" ).hasError ()).isFalse ();
132+ assertThat (compiler .compile ("1 - 1" ).hasError ()).isTrue ();
133+ }
134+
135+ @ Test
136+ public void compiler_setStandardDeclarations_emptyDisablesAllStandardDeclarations ()
137+ throws Exception {
138+ CelCompiler compiler =
139+ CelCompilerFactory .standardCelCompilerBuilder ()
140+ .setStandardDeclarations (CelStandardDeclarations .EMPTY )
141+ .build ();
142+
143+ assertThat (compiler .compile ("1 + 1" ).hasError ()).isTrue ();
144+ assertThat (compiler .compile ("1 - 1" ).hasError ()).isTrue ();
145+ assertThat (compiler .compile ("size([1])" ).hasError ()).isTrue ();
107146 }
108147
109148 @ Test
0 commit comments