1616
1717import static com .google .common .truth .Truth .assertThat ;
1818
19+ import com .google .common .collect .ImmutableCollection ;
20+ import com .google .common .collect .ImmutableList ;
21+ import dev .cel .checker .CelStandardDeclarations .StandardFunction ;
22+ import dev .cel .common .CelContainer ;
1923import dev .cel .common .CelFunctionDecl ;
24+ import dev .cel .common .CelOptions ;
2025import dev .cel .common .CelOverloadDecl ;
2126import dev .cel .common .CelVarDecl ;
27+ import dev .cel .common .types .CelType ;
28+ import dev .cel .common .types .CelTypeProvider ;
2229import dev .cel .common .types .SimpleType ;
2330import dev .cel .compiler .CelCompilerFactory ;
2431import dev .cel .expr .conformance .proto3 .TestAllTypes ;
32+ import java .util .Optional ;
2533import org .junit .Test ;
2634import org .junit .runner .RunWith ;
2735import org .junit .runners .JUnit4 ;
@@ -49,7 +57,45 @@ public void toCheckerBuilder_isImmutable() {
4957 CelCheckerLegacyImpl .Builder newCheckerBuilder =
5058 (CelCheckerLegacyImpl .Builder ) celChecker .toCheckerBuilder ();
5159
52- assertThat (newCheckerBuilder .getCheckerLibraries ().build ()).isEmpty ();
60+ assertThat (newCheckerBuilder .checkerLibraries ().build ()).isEmpty ();
61+ }
62+
63+ @ Test
64+ public void toCheckerBuilder_singularFields_copied () {
65+ CelStandardDeclarations subsetDecls =
66+ CelStandardDeclarations .newBuilder ().includeFunctions (StandardFunction .BOOL ).build ();
67+ CelOptions celOptions = CelOptions .current ().enableTimestampEpoch (true ).build ();
68+ CelContainer celContainer = CelContainer .ofName ("foo" );
69+ CelType expectedResultType = SimpleType .BOOL ;
70+ CelTypeProvider customTypeProvider =
71+ new CelTypeProvider () {
72+ @ Override
73+ public ImmutableCollection <CelType > types () {
74+ return ImmutableList .of ();
75+ }
76+
77+ @ Override
78+ public Optional <CelType > findType (String typeName ) {
79+ return Optional .empty ();
80+ }
81+ };
82+ CelCheckerBuilder celCheckerBuilder =
83+ CelCompilerFactory .standardCelCheckerBuilder ()
84+ .setOptions (celOptions )
85+ .setContainer (celContainer )
86+ .setResultType (expectedResultType )
87+ .setTypeProvider (customTypeProvider )
88+ .setStandardEnvironmentEnabled (false )
89+ .setStandardDeclarations (subsetDecls );
90+ CelCheckerLegacyImpl celChecker = (CelCheckerLegacyImpl ) celCheckerBuilder .build ();
91+
92+ CelCheckerLegacyImpl .Builder newCheckerBuilder =
93+ (CelCheckerLegacyImpl .Builder ) celChecker .toCheckerBuilder ();
94+
95+ assertThat (newCheckerBuilder .standardDeclarations ()).isEqualTo (subsetDecls );
96+ assertThat (newCheckerBuilder .options ()).isEqualTo (celOptions );
97+ assertThat (newCheckerBuilder .container ()).isEqualTo (celContainer );
98+ assertThat (newCheckerBuilder .celTypeProvider ()).isEqualTo (customTypeProvider );
5399 }
54100
55101 @ Test
@@ -70,12 +116,12 @@ public void toCheckerBuilder_collectionProperties_copied() {
70116 CelCheckerLegacyImpl .Builder newCheckerBuilder =
71117 (CelCheckerLegacyImpl .Builder ) celChecker .toCheckerBuilder ();
72118
73- assertThat (newCheckerBuilder .getFunctionDecls ().build ()).hasSize (1 );
74- assertThat (newCheckerBuilder .getIdentDecls ().build ()).hasSize (1 );
75- assertThat (newCheckerBuilder .getProtoTypeMasks ().build ()).hasSize (1 );
76- assertThat (newCheckerBuilder .getFileTypes ().build ())
119+ assertThat (newCheckerBuilder .functionDecls ().build ()).hasSize (1 );
120+ assertThat (newCheckerBuilder .identDecls ().build ()).hasSize (1 );
121+ assertThat (newCheckerBuilder .protoTypeMasks ().build ()).hasSize (1 );
122+ assertThat (newCheckerBuilder .fileTypes ().build ())
77123 .hasSize (1 ); // MessageTypes and FileTypes deduped into the same file descriptor
78- assertThat (newCheckerBuilder .getCheckerLibraries ().build ()).hasSize (1 );
124+ assertThat (newCheckerBuilder .checkerLibraries ().build ()).hasSize (1 );
79125 }
80126
81127 @ Test
@@ -96,11 +142,11 @@ public void toCheckerBuilder_collectionProperties_areImmutable() {
96142 ProtoTypeMask .ofAllFields ("cel.expr.conformance.proto3.TestAllTypes" ));
97143 celCheckerBuilder .addLibraries (new CelCheckerLibrary () {});
98144
99- assertThat (newCheckerBuilder .getFunctionDecls ().build ()).isEmpty ();
100- assertThat (newCheckerBuilder .getIdentDecls ().build ()).isEmpty ();
101- assertThat (newCheckerBuilder .getProtoTypeMasks ().build ()).isEmpty ();
102- assertThat (newCheckerBuilder .getMessageTypes ().build ()).isEmpty ();
103- assertThat (newCheckerBuilder .getFileTypes ().build ()).isEmpty ();
104- assertThat (newCheckerBuilder .getCheckerLibraries ().build ()).isEmpty ();
145+ assertThat (newCheckerBuilder .functionDecls ().build ()).isEmpty ();
146+ assertThat (newCheckerBuilder .identDecls ().build ()).isEmpty ();
147+ assertThat (newCheckerBuilder .protoTypeMasks ().build ()).isEmpty ();
148+ assertThat (newCheckerBuilder .messageTypes ().build ()).isEmpty ();
149+ assertThat (newCheckerBuilder .fileTypes ().build ()).isEmpty ();
150+ assertThat (newCheckerBuilder .checkerLibraries ().build ()).isEmpty ();
105151 }
106152}
0 commit comments