2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
|
|
|
|
2012-01-03 22:49:13 +00:00
|
|
|
from django.utils import regex_helper
|
|
|
|
|
|
|
|
|
|
|
|
class NormalizeTests(unittest.TestCase):
|
|
|
|
def test_empty(self):
|
|
|
|
pattern = r""
|
2012-06-07 16:08:47 +00:00
|
|
|
expected = [('', [])]
|
2012-01-03 22:49:13 +00:00
|
|
|
result = regex_helper.normalize(pattern)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
def test_escape(self):
|
|
|
|
pattern = r"\\\^\$\.\|\?\*\+\(\)\["
|
2012-06-07 16:08:47 +00:00
|
|
|
expected = [('\\^$.|?*+()[', [])]
|
2012-01-03 22:49:13 +00:00
|
|
|
result = regex_helper.normalize(pattern)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
def test_group_positional(self):
|
|
|
|
pattern = r"(.*)-(.+)"
|
2012-06-07 16:08:47 +00:00
|
|
|
expected = [('%(_0)s-%(_1)s', ['_0', '_1'])]
|
2012-01-03 22:49:13 +00:00
|
|
|
result = regex_helper.normalize(pattern)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
def test_group_noncapturing(self):
|
|
|
|
pattern = r"(?:non-capturing)"
|
2012-06-07 16:08:47 +00:00
|
|
|
expected = [('non-capturing', [])]
|
2012-01-03 22:49:13 +00:00
|
|
|
result = regex_helper.normalize(pattern)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
def test_group_named(self):
|
|
|
|
pattern = r"(?P<first_group_name>.*)-(?P<second_group_name>.*)"
|
2012-06-07 16:08:47 +00:00
|
|
|
expected = [('%(first_group_name)s-%(second_group_name)s',
|
2012-01-03 22:49:13 +00:00
|
|
|
['first_group_name', 'second_group_name'])]
|
|
|
|
result = regex_helper.normalize(pattern)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
def test_group_backreference(self):
|
|
|
|
pattern = r"(?P<first_group_name>.*)-(?P=first_group_name)"
|
2012-06-07 16:08:47 +00:00
|
|
|
expected = [('%(first_group_name)s-%(first_group_name)s',
|
2012-01-03 22:49:13 +00:00
|
|
|
['first_group_name'])]
|
|
|
|
result = regex_helper.normalize(pattern)
|
|
|
|
self.assertEqual(result, expected)
|