| 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 | // Package test provides a YAML-serializable test suite for CEL tests. |
| 16 | package test |
| 17 | |
| 18 | // Suite is a collection of tests designed to evaluate the correctness of |
| 19 | // a CEL policy or a CEL expression |
| 20 | type Suite struct { |
| 21 | Name string `yaml:"name"` |
| 22 | Description string `yaml:"description"` |
| 23 | Sections []*Section `yaml:"section"` |
| 24 | } |
| 25 | |
| 26 | // Section is a collection of related test cases. |
| 27 | type Section struct { |
| 28 | Name string `yaml:"name"` |
| 29 | Tests []*Case `yaml:"tests"` |
| 30 | } |
| 31 | |
| 32 | // Case is a test case to validate a CEL policy or expression. The test case |
| 33 | // encompasses evaluation of the compiled expression using the provided input |
| 34 | // bindings and asserting the result against the expected result. |
| 35 | type Case struct { |
| 36 | Name string `yaml:"name"` |
| 37 | Description string `yaml:"description"` |
| 38 | Input map[string]*InputValue `yaml:"input,omitempty"` |
| 39 | *InputContext `yaml:",inline,omitempty"` |
| 40 | Output *Output `yaml:"output"` |
| 41 | } |
| 42 | |
| 43 | // InputContext represents an optional context expression. |
| 44 | type InputContext struct { |
| 45 | ContextExpr string `yaml:"context_expr"` |
| 46 | } |
| 47 | |
| 48 | // InputValue represents an input value for a binding which can be either a simple literal value or |
| 49 | // an expression. |
| 50 | type InputValue struct { |
| 51 | Value any `yaml:"value"` |
| 52 | Expr string `yaml:"expr"` |
| 53 | } |
| 54 | |
| 55 | // Output represents the expected result of a test case. |
| 56 | type Output struct { |
| 57 | Value any `yaml:"value"` |
| 58 | Expr string `yaml:"expr"` |
| 59 | ErrorSet []string `yaml:"error_set"` |
| 60 | UnknownSet []int64 `yaml:"unknown_set"` |
| 61 | } |