|
14 | 14 | package dev.cel.testing.testrunner; |
15 | 15 |
|
16 | 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | +import static java.nio.charset.StandardCharsets.UTF_8; |
17 | 18 |
|
18 | 19 | import com.google.common.collect.ImmutableMap; |
| 20 | +import com.google.common.io.Files; |
19 | 21 | import dev.cel.bundle.Cel; |
20 | 22 | import dev.cel.bundle.CelFactory; |
21 | 23 | import dev.cel.common.CelAbstractSyntaxTree; |
|
27 | 29 | import dev.cel.runtime.CelEvaluationListener; |
28 | 30 | import dev.cel.runtime.CelRuntime; |
29 | 31 | import dev.cel.testing.testrunner.CelCoverageIndex.CoverageReport; |
| 32 | +import java.io.File; |
30 | 33 | import org.junit.Before; |
31 | 34 | import org.junit.Test; |
32 | 35 | import org.junit.runner.RunWith; |
@@ -125,4 +128,32 @@ public void getCoverageReport_comprehension_generatesDotGraph() throws Exception |
125 | 128 | .contains("label=\"{<1> exprID: 16 | <2> LoopStep} | <3> @result && i % 2 != 0\""); |
126 | 129 | assertThat(report.dotGraph()).contains("label=\"{<1> exprID: 17 | <2> Result} | <3> @result\""); |
127 | 130 | } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void getCoverageReport_fullCoverage_writesToUndeclaredOutputs() throws Exception { |
| 134 | + // Setup for a more complex graph to write. |
| 135 | + cel = CelFactory.standardCelBuilder().build(); |
| 136 | + CelCompiler compiler = |
| 137 | + cel.toCompilerBuilder() |
| 138 | + .setOptions(CelOptions.newBuilder().populateMacroCalls(true).build()) |
| 139 | + .setStandardMacros(CelStandardMacro.STANDARD_MACROS) |
| 140 | + .addLibraries(CelExtensions.comprehensions()) |
| 141 | + .build(); |
| 142 | + ast = compiler.compile("[1, 2, 3].all(i, i % 2 != 0)").getAst(); |
| 143 | + program = cel.createProgram(ast); |
| 144 | + CelCoverageIndex coverageIndex = new CelCoverageIndex(); |
| 145 | + coverageIndex.init(ast); |
| 146 | + CelEvaluationListener listener = coverageIndex.newEvaluationListener(); |
| 147 | + program.trace(ImmutableMap.of(), listener); |
| 148 | + |
| 149 | + CoverageReport report = coverageIndex.generateCoverageReport(); |
| 150 | + |
| 151 | + String undeclaredOutputsDir = System.getenv("TEST_UNDECLARED_OUTPUTS_DIR"); |
| 152 | + assertThat(undeclaredOutputsDir).isNotNull(); |
| 153 | + |
| 154 | + File outputFile = new File(undeclaredOutputsDir, "cel_test_coverage/coverage_graph.txt"); |
| 155 | + |
| 156 | + String fileContent = Files.asCharSource(outputFile, UTF_8).read(); |
| 157 | + assertThat(fileContent).isEqualTo(report.dotGraph()); |
| 158 | + } |
128 | 159 | } |
0 commit comments