File tree Expand file tree Collapse file tree
main/java/dev/cel/testing/testrunner
test/java/dev/cel/testing/testrunner Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -79,6 +79,13 @@ public abstract class CelTestContext {
7979 */
8080 public abstract ResultMatcher resultMatcher ();
8181
82+ /**
83+ * The CEL expression to be tested. Could be a raw CEL expression or a policy/cel file path.
84+ *
85+ * <p>This expression is used incase the expression is not provided via the build macro.
86+ */
87+ public abstract Optional <String > celExpression ();
88+
8289 /** Returns a builder for {@link CelTestContext} with the current instance's values. */
8390 public abstract Builder toBuilder ();
8491
@@ -107,6 +114,8 @@ public abstract Builder setCelLateFunctionBindings(
107114
108115 public abstract Builder setResultMatcher (ResultMatcher resultMatcher );
109116
117+ public abstract Builder setCelExpression (String celExpression );
118+
110119 public abstract CelTestContext build ();
111120 }
112121}
Original file line number Diff line number Diff line change @@ -77,7 +77,20 @@ public final class TestRunnerLibrary {
7777 */
7878 public static void runTest (CelTestCase testCase , CelTestContext celTestContext ) throws Exception {
7979 String celExpression = System .getProperty (CEL_EXPR_SYSTEM_PROPERTY );
80- CelExprFileSource celExprFileSource = CelExprFileSource .fromFile (celExpression );
80+ CelExprFileSource celExprFileSource ;
81+ if (celExpression == null ) {
82+ celExprFileSource =
83+ celTestContext
84+ .celExpression ()
85+ .map (CelExprFileSource ::fromFile )
86+ .orElseThrow (
87+ () ->
88+ new IllegalArgumentException (
89+ "No cel expression provided in the test case or via cel_test build"
90+ + " macro." ));
91+ } else {
92+ celExprFileSource = CelExprFileSource .fromFile (celExpression );
93+ }
8194 evaluateTestCase (testCase , celTestContext , celExprFileSource );
8295 }
8396
Original file line number Diff line number Diff line change @@ -55,6 +55,25 @@ public void runPolicyTest_simpleBooleanOutput() throws Exception {
5555 simpleOutputTestCase , CelTestContext .newBuilder ().build (), celExprFileSource );
5656 }
5757
58+ @ Test
59+ public void evaluateTestCase_simpleBooleanOutput () throws Exception {
60+ CelTestCase simpleOutputTestCase =
61+ CelTestCase .newBuilder ()
62+ .setName ("simple_output_test_case" )
63+ .setDescription ("simple_output_test_case_description" )
64+ .setInput (CelTestSuite .CelTestSection .CelTestCase .Input .ofBindings (ImmutableMap .of ()))
65+ .setOutput (CelTestSuite .CelTestSection .CelTestCase .Output .ofResultValue (false ))
66+ .build ();
67+
68+ TestRunnerLibrary .runTest (
69+ simpleOutputTestCase ,
70+ CelTestContext .newBuilder ()
71+ .setCelExpression (
72+ TestUtil .getSrcDir ()
73+ + "/google3/third_party/java/cel/testing/src/test/java/dev/cel/testing/testrunner/resources/empty_policy.yaml" )
74+ .build ());
75+ }
76+
5877 @ Test
5978 public void runPolicyTest_outputMismatch_failureAssertion () throws Exception {
6079 CelTestCase simpleOutputTestCase =
You can’t perform that action at this time.
0 commit comments