|
| 1 | +# Copyright 2026 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 | +"""Tests for Env/Config. |
| 16 | +
|
| 17 | +This module contains tests for the `cel.EnvConfig` class, focusing on its |
| 18 | +ability to be created from and serialized to YAML format. |
| 19 | +""" |
| 20 | + |
| 21 | +from absl.testing import absltest |
| 22 | +from cel_expr_python import cel |
| 23 | + |
| 24 | + |
| 25 | +class CelEnvTest(absltest.TestCase): |
| 26 | + |
| 27 | + def test_env_config_from_and_to_yaml(self): |
| 28 | + config = cel.NewEnvConfigFromYaml(""" |
| 29 | + name: foo |
| 30 | + container: test.container |
| 31 | + stdlib: |
| 32 | + exclude_macros: |
| 33 | + - map |
| 34 | + - filter |
| 35 | + exclude_functions: |
| 36 | + - name: "_+_" |
| 37 | + extensions: |
| 38 | + - name: math |
| 39 | + variables: |
| 40 | + - name: one |
| 41 | + type_name: int |
| 42 | + value: 1 |
| 43 | + functions: |
| 44 | + - name: add |
| 45 | + overloads: |
| 46 | + - id: "add_int_int" |
| 47 | + args: |
| 48 | + - type_name: int |
| 49 | + - type_name: int |
| 50 | + return: |
| 51 | + type_name: int |
| 52 | + """) |
| 53 | + yaml = config.to_yaml() |
| 54 | + self.assertEqual( |
| 55 | + normalize_yaml(yaml), |
| 56 | + normalize_yaml(""" |
| 57 | + name: "foo" |
| 58 | + container: "test.container" |
| 59 | + extensions: |
| 60 | + - name: "math" |
| 61 | + stdlib: |
| 62 | + exclude_macros: |
| 63 | + - "filter" |
| 64 | + - "map" |
| 65 | + exclude_functions: |
| 66 | + - name: "_+_" |
| 67 | + variables: |
| 68 | + - name: "one" |
| 69 | + type_name: "int" |
| 70 | + value: 1 |
| 71 | + functions: |
| 72 | + - name: "add" |
| 73 | + overloads: |
| 74 | + - id: "add_int_int" |
| 75 | + args: |
| 76 | + - type_name: "int" |
| 77 | + - type_name: "int" |
| 78 | + return: |
| 79 | + type_name: "int" |
| 80 | + """), |
| 81 | + ) |
| 82 | + |
| 83 | + def test_invalid_yaml(self): |
| 84 | + with self.assertRaises(Exception) as e: |
| 85 | + cel.NewEnvConfigFromYaml(" invalid yaml") |
| 86 | + self.assertIn( |
| 87 | + "1:2: Invalid CEL environment config YAML\n" |
| 88 | + "| invalid yaml\n" |
| 89 | + "| ^", |
| 90 | + str(e.exception), |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +def normalize_yaml(yaml: str) -> str: |
| 95 | + lines = yaml.split("\n") |
| 96 | + indent = -1 |
| 97 | + unindented_lines = [] |
| 98 | + for line in lines: |
| 99 | + pos = -1 |
| 100 | + for i, char in enumerate(line): |
| 101 | + if char != " " and char != "\t": |
| 102 | + pos = i |
| 103 | + break |
| 104 | + if pos == -1: |
| 105 | + # Skip blank lines. |
| 106 | + continue |
| 107 | + if indent == -1: |
| 108 | + indent = pos |
| 109 | + if pos >= indent: |
| 110 | + unindented_lines.append(line[indent:]) |
| 111 | + else: |
| 112 | + unindented_lines.append(line) |
| 113 | + return "\n".join(unindented_lines) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + absltest.main() |
0 commit comments