mirror of
https://github.com/django/django.git
synced 2025-06-02 18:19:11 +00:00
Refs #26621 -- Added tests for admindocs.views.simplify_regex().
This commit is contained in:
parent
995d09ead4
commit
16a842b379
@ -394,9 +394,9 @@ non_named_group_matcher = re.compile(r'\(.*?\)')
|
|||||||
|
|
||||||
def simplify_regex(pattern):
|
def simplify_regex(pattern):
|
||||||
"""
|
"""
|
||||||
Clean up urlpattern regexes into something somewhat readable by Mere Humans:
|
Clean up urlpattern regexes into something more readable by humans. For
|
||||||
turns something like "^(?P<sport_slug>\w+)/athletes/(?P<athlete_slug>\w+)/$"
|
example, turn "^(?P<sport_slug>\w+)/athletes/(?P<athlete_slug>\w+)/$"
|
||||||
into "<sport_slug>/athletes/<athlete_slug>/"
|
into "/<sport_slug>/athletes/<athlete_slug>/".
|
||||||
"""
|
"""
|
||||||
# handle named groups first
|
# handle named groups first
|
||||||
pattern = named_group_matcher.sub(lambda m: m.group(1), pattern)
|
pattern = named_group_matcher.sub(lambda m: m.group(1), pattern)
|
||||||
|
@ -3,7 +3,7 @@ import unittest
|
|||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.admindocs import utils
|
from django.contrib.admindocs import utils
|
||||||
from django.contrib.admindocs.views import get_return_data_type
|
from django.contrib.admindocs.views import get_return_data_type, simplify_regex
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.contrib.sites.models import Site
|
from django.contrib.sites.models import Site
|
||||||
from django.test import TestCase, modify_settings, override_settings
|
from django.test import TestCase, modify_settings, override_settings
|
||||||
@ -123,6 +123,15 @@ class AdminDocViewTests(TestDataMixin, AdminDocsTestCase):
|
|||||||
finally:
|
finally:
|
||||||
utils.docutils_is_available = True
|
utils.docutils_is_available = True
|
||||||
|
|
||||||
|
def test_simplify_regex(self):
|
||||||
|
tests = (
|
||||||
|
('^a', '/a'),
|
||||||
|
('^(?P<a>\w+)/b/(?P<c>\w+)/$', '/<a>/b/<c>/'),
|
||||||
|
('^(?P<a>\w+)/b/(?P<c>\w+)$', '/<a>/b/<c>'),
|
||||||
|
)
|
||||||
|
for pattern, output in tests:
|
||||||
|
self.assertEqual(simplify_regex(pattern), output)
|
||||||
|
|
||||||
|
|
||||||
@override_settings(TEMPLATES=[{
|
@override_settings(TEMPLATES=[{
|
||||||
'NAME': 'ONE',
|
'NAME': 'ONE',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user