1414package dev .cel .testing .testrunner ;
1515
1616import static com .google .common .truth .Truth .assertThat ;
17+ import static java .nio .charset .StandardCharsets .UTF_8 ;
1718
1819import com .google .common .collect .ImmutableMap ;
20+ import com .google .common .io .Files ;
1921import dev .cel .bundle .Cel ;
2022import dev .cel .bundle .CelFactory ;
2123import dev .cel .common .CelAbstractSyntaxTree ;
2729import dev .cel .runtime .CelEvaluationListener ;
2830import dev .cel .runtime .CelRuntime ;
2931import dev .cel .testing .testrunner .CelCoverageIndex .CoverageReport ;
30- import org . junit . Before ;
32+ import java . io . File ;
3133import org .junit .Test ;
3234import org .junit .runner .RunWith ;
3335import org .junit .runners .JUnit4 ;
3436
3537@ RunWith (JUnit4 .class )
3638public final class CelCoverageIndexTest {
3739
38- private Cel cel ;
39- private CelAbstractSyntaxTree ast ;
40- private CelRuntime .Program program ;
41-
42- @ Before
43- public void setUp () throws Exception {
44- cel =
40+ @ Test
41+ public void getCoverageReport_fullCoverage () throws Exception {
42+ Cel cel =
4543 CelFactory .standardCelBuilder ()
4644 .addVar ("x" , SimpleType .INT )
4745 .addVar ("y" , SimpleType .INT )
4846 .build ();
49- ast = cel .compile ("x > 1 && y > 1" ).getAst ();
50- program = cel .createProgram (ast );
51- }
52-
53- @ Test
54- public void getCoverageReport_fullCoverage () throws Exception {
47+ CelAbstractSyntaxTree ast = cel .compile ("x > 1 && y > 1" ).getAst ();
48+ CelRuntime .Program program = cel .createProgram (ast );
5549 CelCoverageIndex coverageIndex = new CelCoverageIndex ();
5650 coverageIndex .init (ast );
5751 CelEvaluationListener listener = coverageIndex .newEvaluationListener ();
@@ -74,6 +68,13 @@ public void getCoverageReport_fullCoverage() throws Exception {
7468
7569 @ Test
7670 public void getCoverageReport_partialCoverage_shortCircuit () throws Exception {
71+ Cel cel =
72+ CelFactory .standardCelBuilder ()
73+ .addVar ("x" , SimpleType .INT )
74+ .addVar ("y" , SimpleType .INT )
75+ .build ();
76+ CelAbstractSyntaxTree ast = cel .compile ("x > 1 && y > 1" ).getAst ();
77+ CelRuntime .Program program = cel .createProgram (ast );
7778 CelCoverageIndex coverageIndex = new CelCoverageIndex ();
7879 coverageIndex .init (ast );
7980 CelEvaluationListener listener = coverageIndex .newEvaluationListener ();
@@ -97,15 +98,15 @@ public void getCoverageReport_partialCoverage_shortCircuit() throws Exception {
9798
9899 @ Test
99100 public void getCoverageReport_comprehension_generatesDotGraph () throws Exception {
100- cel = CelFactory .standardCelBuilder ().build ();
101+ Cel cel = CelFactory .standardCelBuilder ().build ();
101102 CelCompiler compiler =
102103 cel .toCompilerBuilder ()
103104 .setOptions (CelOptions .newBuilder ().populateMacroCalls (true ).build ())
104105 .setStandardMacros (CelStandardMacro .STANDARD_MACROS )
105106 .addLibraries (CelExtensions .comprehensions ())
106107 .build ();
107- ast = compiler .compile ("[1, 2, 3].all(i, i % 2 != 0)" ).getAst ();
108- program = cel .createProgram (ast );
108+ CelAbstractSyntaxTree ast = compiler .compile ("[1, 2, 3].all(i, i % 2 != 0)" ).getAst ();
109+ CelRuntime . Program program = cel .createProgram (ast );
109110 CelCoverageIndex coverageIndex = new CelCoverageIndex ();
110111 coverageIndex .init (ast );
111112 CelEvaluationListener listener = coverageIndex .newEvaluationListener ();
@@ -125,4 +126,32 @@ public void getCoverageReport_comprehension_generatesDotGraph() throws Exception
125126 .contains ("label=\" {<1> exprID: 16 | <2> LoopStep} | <3> @result && i % 2 != 0\" " );
126127 assertThat (report .dotGraph ()).contains ("label=\" {<1> exprID: 17 | <2> Result} | <3> @result\" " );
127128 }
129+
130+ @ Test
131+ public void getCoverageReport_fullCoverage_writesToUndeclaredOutputs () throws Exception {
132+ // Setup for a more complex graph to write.
133+ Cel cel = CelFactory .standardCelBuilder ().build ();
134+ CelCompiler compiler =
135+ cel .toCompilerBuilder ()
136+ .setOptions (CelOptions .newBuilder ().populateMacroCalls (true ).build ())
137+ .setStandardMacros (CelStandardMacro .STANDARD_MACROS )
138+ .addLibraries (CelExtensions .comprehensions ())
139+ .build ();
140+ CelAbstractSyntaxTree ast = compiler .compile ("[1, 2, 3].all(i, i % 2 != 0)" ).getAst ();
141+ CelRuntime .Program program = cel .createProgram (ast );
142+ CelCoverageIndex coverageIndex = new CelCoverageIndex ();
143+ coverageIndex .init (ast );
144+ CelEvaluationListener listener = coverageIndex .newEvaluationListener ();
145+ program .trace (ImmutableMap .of (), listener );
146+
147+ CoverageReport report = coverageIndex .generateCoverageReport ();
148+
149+ String undeclaredOutputsDir = System .getenv ("TEST_UNDECLARED_OUTPUTS_DIR" );
150+ assertThat (undeclaredOutputsDir ).isNotNull ();
151+
152+ File outputFile = new File (undeclaredOutputsDir , "cel_test_coverage/coverage_graph.txt" );
153+
154+ String fileContent = Files .asCharSource (outputFile , UTF_8 ).read ();
155+ assertThat (fileContent ).isEqualTo (report .dotGraph ());
156+ }
128157}
0 commit comments