Debian Codegraph @

tests/utils_tests/test_regex_helper.py

raw
1 import re
2 import unittest
3
4 from django.test import SimpleTestCase
5 from django.utils import regex_helper
6
7
8 class NormalizeTests(unittest.TestCase):
9 def test_empty(self):
10 pattern = r""
11 expected = [("", [])]
12 result = regex_helper.normalize(pattern)
13 self.assertEqual(result, expected)
14
15 def test_escape(self):
16 pattern = r"\\\^\$\.\|\?\*\+\(\)\["
17 expected = [("\\^$.|?*+()[", [])]
18 result = regex_helper.normalize(pattern)
19 self.assertEqual(result, expected)
20
21 def test_group_positional(self):
22 pattern = r"(.*)-(.+)"
23 expected = [("%(_0)s-%(_1)s", ["_0", "_1"])]
24 result = regex_helper.normalize(pattern)
25 self.assertEqual(result, expected)
26
27 def test_group_noncapturing(self):
28 pattern = r"(?:non-capturing)"
29 expected = [("non-capturing", [])]
30 result = regex_helper.normalize(pattern)
31 self.assertEqual(result, expected)
32
33 def test_group_named(self):
34 pattern = r"(?P<first_group_name>.*)-(?P<second_group_name>.*)"
35 expected = [
36 (
37 "%(first_group_name)s-%(second_group_name)s",
38 ["first_group_name", "second_group_name"],
39 )
40 ]
41 result = regex_helper.normalize(pattern)
42 self.assertEqual(result, expected)
43
44 def test_group_backreference(self):
45 pattern = r"(?P<first_group_name>.*)-(?P=first_group_name)"
46 expected = [("%(first_group_name)s-%(first_group_name)s", ["first_group_name"])]
47 result = regex_helper.normalize(pattern)
48 self.assertEqual(result, expected)
49
50
51 class LazyReCompileTests(SimpleTestCase):
52 def test_flags_with_pre_compiled_regex(self):
53 test_pattern = re.compile("test")
54 lazy_test_pattern = regex_helper._lazy_re_compile(test_pattern, re.I)
55 msg = "flags must be empty if regex is passed pre-compiled"
56 with self.assertRaisesMessage(AssertionError, msg):
57 lazy_test_pattern.match("TEST")