Ticket #17492: 17492.diff
File 17492.diff, 5.9 KB (added by , 13 years ago) |
---|
-
django/utils/regex_helper.py
diff --git a/django/utils/regex_helper.py b/django/utils/regex_helper.py index b11fe96..6ca92f5 100644
a b def normalize(pattern): 134 134 raise ValueError("Non-reversible reg-exp portion: '(?%s'" % ch) 135 135 else: 136 136 ch, escaped = pattern_iter.next() 137 if ch != '<':137 if ch not in ('<', '='): 138 138 raise ValueError("Non-reversible reg-exp portion: '(?P%s'" % ch) 139 139 # We are in a named capturing group. Extra the name and 140 140 # then skip to the end. 141 if ch == '<': 142 terminal_char = '>' 143 # We are in a named backreference. 144 else: 145 terminal_char = ')' 141 146 name = [] 142 147 ch, escaped = pattern_iter.next() 143 while ch != '>':148 while ch != terminal_char: 144 149 name.append(ch) 145 150 ch, escaped = pattern_iter.next() 146 151 param = ''.join(name) 147 152 result.append(Group(((u"%%(%s)s" % param), param))) 148 walk_to_end(ch, pattern_iter) 153 # Named backreferences have already consumed the 154 # parenthesis. 155 if terminal_char != ')': 156 walk_to_end(ch, pattern_iter) 149 157 elif ch in "*?+{": 150 158 # Quanitifers affect the previous item in the result list. 151 159 count, ch = get_quantifier(ch, pattern_iter) -
tests/regressiontests/urlpatterns_reverse/tests.py
diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py index a5df26f..7219c1b 100644
a b test_data = ( 76 76 ('people', NoReverseMatch, [], {'name': 'name with spaces'}), 77 77 ('people2', '/people/name/', [], {}), 78 78 ('people2a', '/people/name/fred/', ['fred'], {}), 79 # ('people_backref', '/people/nate-nate/', ['nate'], {}), 80 ('people_backref', '/people/nate-nate/', [], {'name': 'nate'}), 79 81 ('optional', '/optional/fred/', [], {'name': 'fred'}), 80 82 ('optional', '/optional/fred/', ['fred'], {}), 81 83 ('hardcoded', '/hardcoded/', [], {}), -
tests/regressiontests/urlpatterns_reverse/urls.py
diff --git a/tests/regressiontests/urlpatterns_reverse/urls.py b/tests/regressiontests/urlpatterns_reverse/urls.py index 5bde2b0..1d4ae73 100644
a b urlpatterns = patterns('', 22 22 url(r'^people/(?P<name>\w+)/$', empty_view, name="people"), 23 23 url(r'^people/(?:name/)', empty_view, name="people2"), 24 24 url(r'^people/(?:name/(\w+)/)?', empty_view, name="people2a"), 25 url(r'^people/(?P<name>\w+)-(?P=name)/$', empty_view, name="people_backref"), 25 26 url(r'^optional/(?P<name>.*)/(?:.+/)?', empty_view, name="optional"), 26 27 url(r'^hardcoded/$', empty_view, name="hardcoded"), 27 28 url(r'^hardcoded/doc\.pdf$', empty_view, name="hardcoded2"), … … urlpatterns = patterns('', 65 66 (r'defaults_view2/(?P<arg1>\d+)/', 'defaults_view', {'arg2': 2}, 'defaults'), 66 67 67 68 url('^includes/', include(other_patterns)), 68 69 69 ) 70 71 -
new file tests/regressiontests/utils/regex_helper.py
diff --git a/tests/regressiontests/utils/regex_helper.py b/tests/regressiontests/utils/regex_helper.py new file mode 100644 index 0000000..8dc712f
- + 1 from django.utils import regex_helper 2 from django.utils import unittest 3 4 5 class NormalizeTests(unittest.TestCase): 6 def test_empty(self): 7 pattern = r"" 8 expected = [(u'', [])] 9 result = regex_helper.normalize(pattern) 10 self.assertEqual(result, expected) 11 12 def test_escape(self): 13 pattern = r"\\\^\$\.\|\?\*\+\(\)\[" 14 expected = [(u'\\^$.|?*+()[', [])] 15 result = regex_helper.normalize(pattern) 16 self.assertEqual(result, expected) 17 18 def test_group_positional(self): 19 pattern = r"(.*)-(.+)" 20 expected = [(u'%(_0)s-%(_1)s', ['_0', '_1'])] 21 result = regex_helper.normalize(pattern) 22 self.assertEqual(result, expected) 23 24 def test_group_ignored(self): 25 pattern = r"(?i)(?L)(?m)(?s)(?u)(?#)" 26 expected = [(u'', [])] 27 result = regex_helper.normalize(pattern) 28 self.assertEqual(result, expected) 29 30 def test_group_noncapturing(self): 31 pattern = r"(?:non-capturing)" 32 expected = [(u'non-capturing', [])] 33 result = regex_helper.normalize(pattern) 34 self.assertEqual(result, expected) 35 36 def test_group_named(self): 37 pattern = r"(?P<first_group_name>.*)-(?P<second_group_name>.*)" 38 expected = [(u'%(first_group_name)s-%(second_group_name)s', 39 ['first_group_name', 'second_group_name'])] 40 result = regex_helper.normalize(pattern) 41 self.assertEqual(result, expected) 42 43 def test_group_backreference(self): 44 pattern = r"(?P<first_group_name>.*)-(?P=first_group_name)" 45 expected = [(u'%(first_group_name)s-%(first_group_name)s', 46 ['first_group_name'])] 47 result = regex_helper.normalize(pattern) 48 self.assertEqual(result, expected) -
tests/regressiontests/utils/tests.py
diff --git a/tests/regressiontests/utils/tests.py b/tests/regressiontests/utils/tests.py index f48a4ad..f5ca06e 100644
a b from .ipv6 import TestUtilsIPv6 25 25 from .timezone import TimezoneTests 26 26 from .crypto import TestUtilsCryptoPBKDF2 27 27 from .archive import TestZip, TestTar, TestGzipTar, TestBzip2Tar 28 from .regex_helper import NormalizeTests