Skip to content

Commit 37733a1

Browse files
committed
added tests for raw CEL expression and CEL expression file
1 parent 2109c3e commit 37733a1

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

tools/celtest/test_runner_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ func setupTests() []*testCase {
5858
testSuitePath: "testdata/custom_policy_tests.yaml",
5959
opts: []any{customPolicyParserOption(), compiler.PolicyMetadataEnvOption(ParsePolicyVariables)},
6060
},
61+
{
62+
name: "raw expression file test",
63+
celExpression: "testdata/raw_expr.cel",
64+
testSuitePath: "testdata/raw_expr_tests",
65+
configPath: "testdata/config.yaml",
66+
opts: []any{fnEnvOption()},
67+
},
68+
{
69+
name: "raw expression test",
70+
celExpression: "'i + fn(j) == 42'",
71+
testSuitePath: "testdata/raw_expr_tests",
72+
configPath: "testdata/config.yaml",
73+
opts: []any{fnEnvOption()},
74+
},
6175
}
6276
return testCases
6377
}
@@ -161,6 +175,15 @@ func (customTagHandler) PolicyTag(ctx policy.ParserContext, id int64, tagName st
161175
}
162176
}
163177

178+
func fnEnvOption() cel.EnvOption {
179+
return cel.Function("fn",
180+
cel.Overload("fn_int", []*cel.Type{cel.IntType}, cel.IntType,
181+
cel.UnaryBinding(func(in ref.Val) ref.Val {
182+
i := in.(types.Int)
183+
return i / types.Int(2)
184+
})))
185+
}
186+
164187
// TestTriggerTests tests different scenarios of the TriggerTestsFromCompiler function.
165188
func TestTriggerTests(t *testing.T) {
166189
for _, tc := range setupTests() {

tools/celtest/testdata/config.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2025 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: "simple expression config"
16+
variables:
17+
- name: "i"
18+
type_name: "int"
19+
- name: "j"
20+
type_name: "int"

tools/celtest/testdata/raw_expr.cel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"'i + fn(j) == 42'"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2025 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+
description: "simple expression tests"
16+
section:
17+
- name: "valid"
18+
tests:
19+
- name: "true"
20+
input:
21+
i:
22+
value: 21
23+
j:
24+
value: 42
25+
output:
26+
value: true
27+
- name: "false"
28+
input:
29+
i:
30+
value: 22
31+
j:
32+
value:42
33+
output:
34+
value: false

0 commit comments

Comments
 (0)