Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion common/src/main/java/dev/cel/common/formats/YamlHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@

package dev.cel.common.formats;

import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import java.io.StringReader;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
Expand All @@ -42,9 +45,13 @@ public enum YamlNodeType {
LIST("tag:yaml.org,2002:seq"),
;

public static final ImmutableMap<String, YamlNodeType> TAG_TO_NODE_TYPE =
stream(YamlNodeType.values())
.collect(toImmutableMap(YamlNodeType::tag, Function.identity()));

private final String tag;

String tag() {
public String tag() {
return tag;
}

Expand Down Expand Up @@ -97,6 +104,15 @@ public static boolean validateYamlType(Node node, YamlNodeType... expectedNodeTy
return false;
}

public static Double newDouble(ParserContext<Node> ctx, Node node) {
long id = ctx.collectMetadata(node);
if (!assertYamlType(ctx, id, node, YamlNodeType.DOUBLE)) {
return 0.0;
}

return Double.parseDouble(((ScalarNode) node).getValue());
}

public static Integer newInteger(ParserContext<Node> ctx, Node node) {
long id = ctx.collectMetadata(node);
if (!assertYamlType(ctx, id, node, YamlNodeType.INTEGER)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

name: "custom_variable_bindings"
description: "Tests for custom variable bindings."
section:
sections:
- name: "custom_variable_bindings"
description: "Tests for custom variable bindings."
tests:
- name: "true_by_default"
description: "Tests that the custom variable bindings are set to true by default."
# The input for this test is configured programmatically in the test
# class with a value of 1 (see CustomVariableBindingsUserTest.java).
output: "true"
output:
expr: "true"
16 changes: 11 additions & 5 deletions testing/src/test/resources/policy/late_function_binding/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

name: "late_function_binding_tests"
description: "Tests for late function binding."
section:
- name: "late_function_binding"
sections:
- name: "late_function_binding_tests_section"
description: "Tests for late function binding."
tests:
- name: "true_by_default"
description: "Test that the default value of a late function binding is true."
input:
a:
value: "foo"
output: "true"
expr: "'foo'"
output:
value: true
- name: "false_by_default"
description: "Test that the default value of a late function binding is false."
input:
a:
value: "baz"
output: "false"
output:
value: false
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ rule:
match:
- condition: |
resource.origin in variables.banned_regions &&
!(resource.origin in variables.permitted_regions) && foo(resource.origin)
!(resource.origin in variables.permitted_regions)
output: "{'banned': true}"
- condition: resource.origin in variables.permitted_regions && foo(resource.origin)
- condition: foo(resource.origin) && resource.origin in variables.permitted_regions
output: "{'banned': false}"
- output: "{'banned': true}"
explanation: "'resource is in the banned region ' + resource.origin"
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: "eval_error"
description: evaluation error tests
sections:
- name: "eval_error"
description: "Tests for evaluation errors"
tests:
- name: "eval_error_no_matching_overload"
description: "No matching overload for function"
input:
resource:
value:
origin: "uk"
output:
error_set:
- "evaluation error: No matching overload for function 'foo'. Overload candidates: foo_id"
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: "nested_rule"
description: Nested rule conformance tests
sections:
- name: "banned"
description: "Tests for the banned section."
tests:
- name: "restricted_origin"
description: "Tests that the ir origin is restricted."
input:
resource:
value:
origin: "ir"
output:
expr: "{'banned': true}"
- name: "by_default"
description: "Tests that the de origin is restricted."
input:
resource:
value:
origin: "de"
output:
expr: "{'banned': true}"
- name: "permitted"
description: "Tests for the permitted section."
tests:
- name: "valid_origin"
description: "Tests that the valid origin is permitted."
input:
resource:
value:
origin: "uk"
output:
expr: "{'banned': false}"