Skip to content

Commit 1472228

Browse files
CEL Dev Teamcopybara-github
authored andcommitted
Introduce ResultMatcher to the Java CEL Test Runner for custom assertions on the expected output and the computed output.
PiperOrigin-RevId: 738059491
1 parent 108a156 commit 1472228

File tree

6 files changed

+112
-10
lines changed

6 files changed

+112
-10
lines changed

common/src/main/java/dev/cel/common/formats/YamlHelper.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414

1515
package dev.cel.common.formats;
1616

17+
import static com.google.common.collect.ImmutableMap.toImmutableMap;
1718
import static java.util.Arrays.stream;
1819
import static java.util.stream.Collectors.joining;
1920

2021
import com.google.common.base.Joiner;
22+
import com.google.common.collect.ImmutableMap;
2123
import java.io.StringReader;
2224
import java.util.List;
2325
import java.util.Optional;
26+
import java.util.function.Function;
2427
import org.yaml.snakeyaml.LoaderOptions;
2528
import org.yaml.snakeyaml.Yaml;
2629
import org.yaml.snakeyaml.constructor.SafeConstructor;
@@ -42,9 +45,13 @@ public enum YamlNodeType {
4245
LIST("tag:yaml.org,2002:seq"),
4346
;
4447

48+
public static final ImmutableMap<String, YamlNodeType> TAG_TO_NODE_TYPE =
49+
stream(YamlNodeType.values())
50+
.collect(toImmutableMap(YamlNodeType::tag, Function.identity()));
51+
4552
private final String tag;
4653

47-
String tag() {
54+
public String tag() {
4855
return tag;
4956
}
5057

@@ -97,6 +104,15 @@ public static boolean validateYamlType(Node node, YamlNodeType... expectedNodeTy
97104
return false;
98105
}
99106

107+
public static Double newDouble(ParserContext<Node> ctx, Node node) {
108+
long id = ctx.collectMetadata(node);
109+
if (!assertYamlType(ctx, id, node, YamlNodeType.DOUBLE)) {
110+
return 0.0;
111+
}
112+
113+
return Double.parseDouble(((ScalarNode) node).getValue());
114+
}
115+
100116
public static Integer newInteger(ParserContext<Node> ctx, Node node) {
101117
long id = ctx.collectMetadata(node);
102118
if (!assertYamlType(ctx, id, node, YamlNodeType.INTEGER)) {

testing/src/test/resources/policy/custom_variable_bindings/tests.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
name: "custom_variable_bindings"
1516
description: "Tests for custom variable bindings."
16-
section:
17+
sections:
1718
- name: "custom_variable_bindings"
19+
description: "Tests for custom variable bindings."
1820
tests:
1921
- name: "true_by_default"
22+
description: "Tests that the custom variable bindings are set to true by default."
2023
# The input for this test is configured programmatically in the test
2124
# class with a value of 1 (see CustomVariableBindingsUserTest.java).
22-
output: "true"
25+
output:
26+
expr: "true"

testing/src/test/resources/policy/late_function_binding/tests.yaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,23 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
name: "late_function_binding_tests"
1516
description: "Tests for late function binding."
16-
section:
17-
- name: "late_function_binding"
17+
sections:
18+
- name: "late_function_binding_tests_section"
19+
description: "Tests for late function binding."
1820
tests:
1921
- name: "true_by_default"
22+
description: "Test that the default value of a late function binding is true."
2023
input:
2124
a:
22-
value: "foo"
23-
output: "true"
25+
expr: "'foo'"
26+
output:
27+
value: true
2428
- name: "false_by_default"
29+
description: "Test that the default value of a late function binding is false."
2530
input:
2631
a:
2732
value: "baz"
28-
output: "false"
33+
output:
34+
value: false

testing/src/test/resources/policy/nested_rule/eval_error_policy.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ rule:
3030
match:
3131
- condition: |
3232
resource.origin in variables.banned_regions &&
33-
!(resource.origin in variables.permitted_regions) && foo(resource.origin)
33+
!(resource.origin in variables.permitted_regions)
3434
output: "{'banned': true}"
35-
- condition: resource.origin in variables.permitted_regions && foo(resource.origin)
35+
- condition: foo(resource.origin) && resource.origin in variables.permitted_regions
3636
output: "{'banned': false}"
3737
- output: "{'banned': true}"
3838
explanation: "'resource is in the banned region ' + resource.origin"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: "eval_error"
16+
description: evaluation error tests
17+
sections:
18+
- name: "eval_error"
19+
description: "Tests for evaluation errors"
20+
tests:
21+
- name: "eval_error_no_matching_overload"
22+
description: "No matching overload for function"
23+
input:
24+
resource:
25+
value:
26+
origin: "uk"
27+
output:
28+
error_set:
29+
- "evaluation error: No matching overload for function 'foo'. Overload candidates: foo_id"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: "nested_rule"
16+
description: Nested rule conformance tests
17+
sections:
18+
- name: "banned"
19+
description: "Tests for the banned section."
20+
tests:
21+
- name: "restricted_origin"
22+
description: "Tests that the ir origin is restricted."
23+
input:
24+
resource:
25+
value:
26+
origin: "ir"
27+
output:
28+
expr: "{'banned': true}"
29+
- name: "by_default"
30+
description: "Tests that the de origin is restricted."
31+
input:
32+
resource:
33+
value:
34+
origin: "de"
35+
output:
36+
expr: "{'banned': true}"
37+
- name: "permitted"
38+
description: "Tests for the permitted section."
39+
tests:
40+
- name: "valid_origin"
41+
description: "Tests that the valid origin is permitted."
42+
input:
43+
resource:
44+
value:
45+
origin: "uk"
46+
output:
47+
expr: "{'banned': false}"

0 commit comments

Comments
 (0)