Ticket #5657: urlize_with_ascii_plus_unittests.patch

File urlize_with_ascii_plus_unittests.patch, 3.1 KB (added by shaleh, 16 years ago)

updated patch, adds unittests

  • django/utils/html.py

     
    100100            if safe_input:
    101101                middle = mark_safe(middle)
    102102            if middle.startswith('www.') or ('@' not in middle and not middle.startswith('http://') and \
    103                     len(middle) > 0 and middle[0] in string.letters + string.digits and \
     103                    len(middle) > 0 and middle[0] in string.ascii_letters + string.digits and \
    104104                    (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))):
    105105                middle = '<a href="http://%s"%s>%s</a>' % (
    106106                        urlquote(middle, safe='/&=:;#?+'),  nofollow_attr,
  • tests/regressiontests/utils/tests.py

     
    123123        for value, output in items:
    124124            self.check_output(f, value, output)
    125125
     126    def test_urlize(self):
     127        cases = (('abc', u'abc', {}),
     128                 # simple case
     129                 ('www.example.com',
     130                  u'<a href="http://www.example.com">www.example.com</a>',
     131                  {}),
     132                 # simple case with nofollow
     133                 ('www.example.com',
     134                  u'<a href="http://www.example.com" rel="nofollow">www.example.com</a>',
     135                  {'nofollow': True}),
     136                 # wrapped in parens
     137                 ('(www.example.com)',
     138                  u'(<a href="http://www.example.com">www.example.com</a>)',
     139                  {}),
     140                 # text plus url
     141                 ('Check out this link: http://www.example.com/foo',
     142                  u'Check out this link: <a href="http://www.example.com/foo">http://www.example.com/foo</a>',
     143                  {}),
     144                 # trailing punctuation
     145                 ('link http://www.example.com/foo, updated weekly',
     146                  u'link <a href="http://www.example.com/foo">http://www.example.com/foo</a>, updated weekly',
     147                  {}),
     148                 ('link http://www.example.com/foo. Updated weekly.',
     149                  u'link <a href="http://www.example.com/foo">http://www.example.com/foo</a>. Updated weekly.',
     150                  {}),
     151                 # mailto
     152                 ('mail me user@example.com, we can discuss things',
     153                  u'mail me <a href="mailto:user@example.com">user@example.com</a>, we can discuss things', {}),
     154                 )
     155        for input, expected, arguments in cases:
     156            output = html.urlize(input, **arguments)
     157            self.assertEquals(output, expected)
     158
     159        # now validate that changing locales does not break anything
     160        import locale
     161        locale.setlocale(locale.LC_ALL, 'de_DE')
     162        output = html.urlize('abc')
     163        self.assertEquals(output, u'abc')
     164
    126165class TestUtilsChecksums(TestCase):
    127166
    128167    def check_output(self, function, value, output=None):
Back to Top