|
14 | 14 |
|
15 | 15 | package dev.cel.policy; |
16 | 16 |
|
17 | | -import static com.google.common.collect.ImmutableMap.toImmutableMap; |
| 17 | +import static com.google.common.base.Strings.isNullOrEmpty; |
18 | 18 | import static com.google.common.truth.Truth.assertThat; |
19 | 19 | import static dev.cel.policy.PolicyTestHelper.readFromYaml; |
20 | 20 | import static org.junit.Assert.assertThrows; |
|
36 | 36 | import dev.cel.policy.PolicyTestHelper.PolicyTestSuite; |
37 | 37 | import dev.cel.policy.PolicyTestHelper.PolicyTestSuite.PolicyTestSection; |
38 | 38 | import dev.cel.policy.PolicyTestHelper.PolicyTestSuite.PolicyTestSection.PolicyTestCase; |
| 39 | +import dev.cel.policy.PolicyTestHelper.PolicyTestSuite.PolicyTestSection.PolicyTestCase.PolicyTestInput; |
39 | 40 | import dev.cel.policy.PolicyTestHelper.TestYamlPolicy; |
40 | 41 | import dev.cel.runtime.CelRuntime.CelFunctionBinding; |
41 | | -import java.util.Map.Entry; |
| 42 | +import dev.cel.testing.testdata.proto3.TestAllTypesProto.TestAllTypes; |
| 43 | +import java.util.Map; |
42 | 44 | import java.util.Optional; |
43 | 45 | import org.junit.Test; |
44 | 46 | import org.junit.runner.RunWith; |
@@ -138,10 +140,17 @@ public void evaluateYamlPolicy_withCanonicalTestData( |
138 | 140 | // Compile then evaluate the policy |
139 | 141 | CelAbstractSyntaxTree compiledPolicyAst = |
140 | 142 | CelPolicyCompilerFactory.newPolicyCompiler(cel).build().compile(policy); |
141 | | - ImmutableMap<String, Object> input = |
142 | | - testData.testCase.getInput().entrySet().stream() |
143 | | - .collect(toImmutableMap(Entry::getKey, e -> e.getValue().getValue())); |
144 | | - Object evalResult = cel.createProgram(compiledPolicyAst).eval(input); |
| 143 | + ImmutableMap.Builder<String, Object> inputBuilder = ImmutableMap.builder(); |
| 144 | + for (Map.Entry<String, PolicyTestInput> entry : testData.testCase.getInput().entrySet()) { |
| 145 | + String exprInput = entry.getValue().getExpr(); |
| 146 | + if (isNullOrEmpty(exprInput)) { |
| 147 | + inputBuilder.put(entry.getKey(), entry.getValue().getValue()); |
| 148 | + } else { |
| 149 | + CelAbstractSyntaxTree exprInputAst = cel.compile(exprInput).getAst(); |
| 150 | + inputBuilder.put(entry.getKey(), cel.createProgram(exprInputAst).eval()); |
| 151 | + } |
| 152 | + } |
| 153 | + Object evalResult = cel.createProgram(compiledPolicyAst).eval(inputBuilder.buildOrThrow()); |
145 | 154 |
|
146 | 155 | // Assert |
147 | 156 | // Note that policies may either produce an optional or a non-optional result, |
@@ -219,6 +228,7 @@ private static Cel newCel() { |
219 | 228 | .setStandardMacros(CelStandardMacro.STANDARD_MACROS) |
220 | 229 | .addCompilerLibraries(CelOptionalLibrary.INSTANCE) |
221 | 230 | .addRuntimeLibraries(CelOptionalLibrary.INSTANCE) |
| 231 | + .addMessageTypes(TestAllTypes.getDescriptor()) |
222 | 232 | .setOptions(CEL_OPTIONS) |
223 | 233 | .addFunctionBindings( |
224 | 234 | CelFunctionBinding.from( |
|
0 commit comments