tests.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from django.test import TestCase
  4. from django.utils.encoding import iri_to_uri, force_text
  5. from django.utils.functional import lazy
  6. from django.utils.http import (cookie_date, http_date,
  7. urlquote, urlquote_plus, urlunquote, urlunquote_plus)
  8. from django.utils import six
  9. from django.utils.text import get_text_list, smart_split
  10. from django.utils.translation import override
  11. lazystr = lazy(force_text, six.text_type)
  12. class TextTests(TestCase):
  13. """
  14. Tests for stuff in django.utils.text and other text munging util functions.
  15. """
  16. def test_get_text_list(self):
  17. self.assertEqual(get_text_list(['a', 'b', 'c', 'd']), 'a, b, c or d')
  18. self.assertEqual(get_text_list(['a', 'b', 'c'], 'and'), 'a, b and c')
  19. self.assertEqual(get_text_list(['a', 'b'], 'and'), 'a and b')
  20. self.assertEqual(get_text_list(['a']), 'a')
  21. self.assertEqual(get_text_list([]), '')
  22. with override('ar'):
  23. self.assertEqual(get_text_list(['a', 'b', 'c']), "a، b أو c")
  24. def test_smart_split(self):
  25. testdata = [
  26. ('This is "a person" test.',
  27. ['This', 'is', '"a person"', 'test.']),
  28. ('This is "a person\'s" test.',
  29. ['This', 'is', '"a person\'s"', 'test.']),
  30. ('This is "a person\\"s" test.',
  31. ['This', 'is', '"a person\\"s"', 'test.']),
  32. ('"a \'one',
  33. ['"a', "'one"]),
  34. ('all friends\' tests',
  35. ['all', 'friends\'', 'tests']),
  36. ('url search_page words="something else"',
  37. ['url', 'search_page', 'words="something else"']),
  38. ("url search_page words='something else'",
  39. ['url', 'search_page', "words='something else'"]),
  40. ('url search_page words "something else"',
  41. ['url', 'search_page', 'words', '"something else"']),
  42. ('url search_page words-"something else"',
  43. ['url', 'search_page', 'words-"something else"']),
  44. ('url search_page words=hello',
  45. ['url', 'search_page', 'words=hello']),
  46. ('url search_page words="something else',
  47. ['url', 'search_page', 'words="something', 'else']),
  48. ("cut:','|cut:' '",
  49. ["cut:','|cut:' '"]),
  50. (lazystr("a b c d"), # Test for #20231
  51. ['a', 'b', 'c', 'd']),
  52. ]
  53. for test, expected in testdata:
  54. self.assertEqual(list(smart_split(test)), expected)
  55. def test_urlquote(self):
  56. self.assertEqual(urlquote('Paris & Orl\xe9ans'),
  57. 'Paris%20%26%20Orl%C3%A9ans')
  58. self.assertEqual(urlquote('Paris & Orl\xe9ans', safe="&"),
  59. 'Paris%20&%20Orl%C3%A9ans')
  60. self.assertEqual(
  61. urlunquote('Paris%20%26%20Orl%C3%A9ans'),
  62. 'Paris & Orl\xe9ans')
  63. self.assertEqual(
  64. urlunquote('Paris%20&%20Orl%C3%A9ans'),
  65. 'Paris & Orl\xe9ans')
  66. self.assertEqual(urlquote_plus('Paris & Orl\xe9ans'),
  67. 'Paris+%26+Orl%C3%A9ans')
  68. self.assertEqual(urlquote_plus('Paris & Orl\xe9ans', safe="&"),
  69. 'Paris+&+Orl%C3%A9ans')
  70. self.assertEqual(
  71. urlunquote_plus('Paris+%26+Orl%C3%A9ans'),
  72. 'Paris & Orl\xe9ans')
  73. self.assertEqual(
  74. urlunquote_plus('Paris+&+Orl%C3%A9ans'),
  75. 'Paris & Orl\xe9ans')
  76. def test_cookie_date(self):
  77. t = 1167616461.0
  78. self.assertEqual(cookie_date(t), 'Mon, 01-Jan-2007 01:54:21 GMT')
  79. def test_http_date(self):
  80. t = 1167616461.0
  81. self.assertEqual(http_date(t), 'Mon, 01 Jan 2007 01:54:21 GMT')
  82. def test_iri_to_uri(self):
  83. self.assertEqual(iri_to_uri('red%09ros\xe9#red'),
  84. 'red%09ros%C3%A9#red')
  85. self.assertEqual(iri_to_uri('/blog/for/J\xfcrgen M\xfcnster/'),
  86. '/blog/for/J%C3%BCrgen%20M%C3%BCnster/')
  87. self.assertEqual(iri_to_uri('locations/%s' % urlquote_plus('Paris & Orl\xe9ans')),
  88. 'locations/Paris+%26+Orl%C3%A9ans')
  89. def test_iri_to_uri_idempotent(self):
  90. self.assertEqual(iri_to_uri(iri_to_uri('red%09ros\xe9#red')),
  91. 'red%09ros%C3%A9#red')