|
| 1 | +# Copyright 2023-2025 Buf Technologies, Inc. |
| 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 | +# http://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 | +import importlib.util |
| 16 | +import unittest |
| 17 | + |
| 18 | +import celpy |
| 19 | +from celpy import celtypes |
| 20 | + |
| 21 | +from protovalidate.internal.extra_func import cel_matches_re, cel_matches_re2 |
| 22 | + |
| 23 | +_USE_RE2 = True |
| 24 | +spec = importlib.util.find_spec("re2") |
| 25 | +if spec is None: |
| 26 | + _USE_RE2 = False |
| 27 | + |
| 28 | + |
| 29 | +class TestCollectViolations(unittest.TestCase): |
| 30 | + @unittest.skipUnless(_USE_RE2, "Requires 're2'") |
| 31 | + def test_function_matches_re2(self): |
| 32 | + empty_string = celtypes.StringType("") |
| 33 | + # \z is valid re2 syntax for end of text |
| 34 | + self.assertTrue(cel_matches_re2(empty_string, "^\\z")) |
| 35 | + # \Z is invalid re2 syntax |
| 36 | + self.assertIsInstance(cel_matches_re2(empty_string, "^\\Z"), celpy.CELEvalError) |
| 37 | + |
| 38 | + @unittest.skipUnless(_USE_RE2 is False, "Requires 're'") |
| 39 | + def test_function_matches_re(self): |
| 40 | + empty_string = celtypes.StringType("") |
| 41 | + # \z is invalid re syntax |
| 42 | + self.assertIsInstance(cel_matches_re(empty_string, "^\\z"), celpy.CELEvalError) |
| 43 | + # \Z is valid re syntax for end of text |
| 44 | + self.assertTrue(cel_matches_re(empty_string, "^\\Z")) |
0 commit comments