|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import importlib.util |
15 | 16 | import unittest |
16 | 17 |
|
17 | 18 | import celpy |
18 | 19 | from celpy import celtypes |
19 | 20 |
|
20 | | -from protovalidate.internal.extra_func import cel_matches_re2, cel_matches_re |
| 21 | +from protovalidate.internal.extra_func import cel_matches_re, cel_matches_re2 |
21 | 22 |
|
22 | 23 | _USE_RE2 = True |
23 | | -try: |
24 | | - import re2 # type: ignore |
25 | | -except ImportError: |
| 24 | +spec = importlib.util.find_spec("re2") |
| 25 | +if spec is None: |
26 | 26 | _USE_RE2 = False |
27 | 27 |
|
28 | | -class TestCollectViolations(unittest.TestCase): |
29 | 28 |
|
| 29 | +class TestCollectViolations(unittest.TestCase): |
30 | 30 | @unittest.skipUnless(_USE_RE2, "Requires 're2'") |
31 | 31 | def test_function_matches_re2(self): |
32 | 32 | empty_string = celtypes.StringType("") |
33 | 33 | # \z is valid re2 syntax for end of text |
34 | | - assert cel_matches_re2(empty_string, "^\\z") |
| 34 | + self.assertTrue(cel_matches_re2(empty_string, "^\\z")) |
35 | 35 | # \Z is invalid re2 syntax |
36 | | - assert isinstance(cel_matches_re2(empty_string, "^\\Z"), celpy.CELEvalError) |
| 36 | + self.assertIsInstance(cel_matches_re2(empty_string, "^\\Z"), celpy.CELEvalError) |
37 | 37 |
|
38 | 38 | @unittest.skipUnless(_USE_RE2 is False, "Requires 're'") |
39 | 39 | def test_function_matches_re(self): |
40 | 40 | empty_string = celtypes.StringType("") |
41 | 41 | # \z is invalid re syntax |
42 | | - assert isinstance(cel_matches_re(empty_string, "^\\z"), celpy.CELEvalError) |
| 42 | + self.assertIsInstance(cel_matches_re(empty_string, "^\\z"), celpy.CELEvalError) |
43 | 43 | # \Z is valid re syntax for end of text |
44 | | - assert cel_matches_re(empty_string, "^\\Z") |
| 44 | + self.assertTrue(cel_matches_re(empty_string, "^\\Z")) |
0 commit comments