tests.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime
  4. import decimal
  5. import unittest
  6. from django.template.defaultfilters import (
  7. add, addslashes, capfirst, center, cut, date, default, default_if_none,
  8. dictsort, dictsortreversed, divisibleby, escape, escapejs_filter,
  9. filesizeformat, first, fix_ampersands_filter, floatformat, force_escape,
  10. get_digit, iriencode, join, length, length_is, linebreaksbr,
  11. linebreaks_filter, linenumbers, ljust, lower, make_list,
  12. phone2numeric_filter, pluralize, removetags, rjust, slice_filter, slugify,
  13. stringformat, striptags, time, timesince_filter, timeuntil_filter, title,
  14. truncatewords, truncatewords_html, unordered_list, upper, urlencode,
  15. urlize, urlizetrunc, wordcount, wordwrap, yesno,
  16. )
  17. from django.test import TestCase
  18. from django.utils import six
  19. from django.utils import translation
  20. from django.utils.safestring import SafeData
  21. from django.utils.encoding import python_2_unicode_compatible
  22. class DefaultFiltersTests(TestCase):
  23. def test_floatformat(self):
  24. self.assertEqual(floatformat(7.7), '7.7')
  25. self.assertEqual(floatformat(7.0), '7')
  26. self.assertEqual(floatformat(0.7), '0.7')
  27. self.assertEqual(floatformat(0.07), '0.1')
  28. self.assertEqual(floatformat(0.007), '0.0')
  29. self.assertEqual(floatformat(0.0), '0')
  30. self.assertEqual(floatformat(7.7, 3), '7.700')
  31. self.assertEqual(floatformat(6.000000, 3), '6.000')
  32. self.assertEqual(floatformat(6.200000, 3), '6.200')
  33. self.assertEqual(floatformat(6.200000, -3), '6.200')
  34. self.assertEqual(floatformat(13.1031, -3), '13.103')
  35. self.assertEqual(floatformat(11.1197, -2), '11.12')
  36. self.assertEqual(floatformat(11.0000, -2), '11')
  37. self.assertEqual(floatformat(11.000001, -2), '11.00')
  38. self.assertEqual(floatformat(8.2798, 3), '8.280')
  39. self.assertEqual(floatformat(5555.555, 2), '5555.56')
  40. self.assertEqual(floatformat(001.3000, 2), '1.30')
  41. self.assertEqual(floatformat(0.12345, 2), '0.12')
  42. self.assertEqual(floatformat(decimal.Decimal('555.555'), 2), '555.56')
  43. self.assertEqual(floatformat(decimal.Decimal('09.000')), '9')
  44. self.assertEqual(floatformat('foo'), '')
  45. self.assertEqual(floatformat(13.1031, 'bar'), '13.1031')
  46. self.assertEqual(floatformat(18.125, 2), '18.13')
  47. self.assertEqual(floatformat('foo', 'bar'), '')
  48. self.assertEqual(floatformat('¿Cómo esta usted?'), '')
  49. self.assertEqual(floatformat(None), '')
  50. # Check that we're not converting to scientific notation.
  51. self.assertEqual(floatformat(0, 6), '0.000000')
  52. self.assertEqual(floatformat(0, 7), '0.0000000')
  53. self.assertEqual(floatformat(0, 10), '0.0000000000')
  54. self.assertEqual(floatformat(0.000000000000000000015, 20),
  55. '0.00000000000000000002')
  56. pos_inf = float(1e30000)
  57. self.assertEqual(floatformat(pos_inf), six.text_type(pos_inf))
  58. neg_inf = float(-1e30000)
  59. self.assertEqual(floatformat(neg_inf), six.text_type(neg_inf))
  60. nan = pos_inf / pos_inf
  61. self.assertEqual(floatformat(nan), six.text_type(nan))
  62. class FloatWrapper(object):
  63. def __init__(self, value):
  64. self.value = value
  65. def __float__(self):
  66. return self.value
  67. self.assertEqual(floatformat(FloatWrapper(11.000001), -2), '11.00')
  68. # Regression for #15789
  69. decimal_ctx = decimal.getcontext()
  70. old_prec, decimal_ctx.prec = decimal_ctx.prec, 2
  71. try:
  72. self.assertEqual(floatformat(1.2345, 2), '1.23')
  73. self.assertEqual(floatformat(15.2042, -3), '15.204')
  74. self.assertEqual(floatformat(1.2345, '2'), '1.23')
  75. self.assertEqual(floatformat(15.2042, '-3'), '15.204')
  76. self.assertEqual(floatformat(decimal.Decimal('1.2345'), 2), '1.23')
  77. self.assertEqual(floatformat(decimal.Decimal('15.2042'), -3), '15.204')
  78. finally:
  79. decimal_ctx.prec = old_prec
  80. def test_floatformat_py2_fail(self):
  81. self.assertEqual(floatformat(1.00000000000000015, 16), '1.0000000000000002')
  82. # The test above fails because of Python 2's float handling. Floats with
  83. # many zeroes after the decimal point should be passed in as another type
  84. # such as unicode or Decimal.
  85. if six.PY2:
  86. test_floatformat_py2_fail = unittest.expectedFailure(test_floatformat_py2_fail)
  87. def test_addslashes(self):
  88. self.assertEqual(addslashes('"double quotes" and \'single quotes\''),
  89. '\\"double quotes\\" and \\\'single quotes\\\'')
  90. self.assertEqual(addslashes(r'\ : backslashes, too'),
  91. '\\\\ : backslashes, too')
  92. def test_capfirst(self):
  93. self.assertEqual(capfirst('hello world'), 'Hello world')
  94. def test_escapejs(self):
  95. self.assertEqual(escapejs_filter('"double quotes" and \'single quotes\''),
  96. '\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027')
  97. self.assertEqual(escapejs_filter(r'\ : backslashes, too'),
  98. '\\u005C : backslashes, too')
  99. self.assertEqual(escapejs_filter('and lots of whitespace: \r\n\t\v\f\b'),
  100. 'and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008')
  101. self.assertEqual(escapejs_filter(r'<script>and this</script>'),
  102. '\\u003Cscript\\u003Eand this\\u003C/script\\u003E')
  103. self.assertEqual(
  104. escapejs_filter('paragraph separator:\u2029and line separator:\u2028'),
  105. 'paragraph separator:\\u2029and line separator:\\u2028')
  106. def test_fix_ampersands(self):
  107. self.assertEqual(fix_ampersands_filter('Jack & Jill & Jeroboam'),
  108. 'Jack &amp; Jill &amp; Jeroboam')
  109. def test_linenumbers(self):
  110. self.assertEqual(linenumbers('line 1\nline 2'),
  111. '1. line 1\n2. line 2')
  112. self.assertEqual(linenumbers('\n'.join(['x'] * 10)),
  113. '01. x\n02. x\n03. x\n04. x\n05. x\n06. x\n07. '
  114. 'x\n08. x\n09. x\n10. x')
  115. def test_lower(self):
  116. self.assertEqual(lower('TEST'), 'test')
  117. # uppercase E umlaut
  118. self.assertEqual(lower('\xcb'), '\xeb')
  119. def test_make_list(self):
  120. self.assertEqual(make_list('abc'), ['a', 'b', 'c'])
  121. self.assertEqual(make_list(1234), ['1', '2', '3', '4'])
  122. def test_slugify(self):
  123. self.assertEqual(slugify(' Jack & Jill like numbers 1,2,3 and 4 and'
  124. ' silly characters ?%.$!/'),
  125. 'jack-jill-like-numbers-123-and-4-and-silly-characters')
  126. self.assertEqual(slugify("Un \xe9l\xe9phant \xe0 l'or\xe9e du bois"),
  127. 'un-elephant-a-loree-du-bois')
  128. def test_stringformat(self):
  129. self.assertEqual(stringformat(1, '03d'), '001')
  130. self.assertEqual(stringformat(1, 'z'), '')
  131. def test_title(self):
  132. self.assertEqual(title('a nice title, isn\'t it?'),
  133. "A Nice Title, Isn't It?")
  134. self.assertEqual(title('discoth\xe8que'), 'Discoth\xe8que')
  135. def test_truncatewords(self):
  136. self.assertEqual(
  137. truncatewords('A sentence with a few words in it', 1), 'A ...')
  138. self.assertEqual(
  139. truncatewords('A sentence with a few words in it', 5),
  140. 'A sentence with a few ...')
  141. self.assertEqual(
  142. truncatewords('A sentence with a few words in it', 100),
  143. 'A sentence with a few words in it')
  144. self.assertEqual(
  145. truncatewords('A sentence with a few words in it',
  146. 'not a number'), 'A sentence with a few words in it')
  147. def test_truncatewords_html(self):
  148. self.assertEqual(truncatewords_html(
  149. '<p>one <a href="#">two - three <br>four</a> five</p>', 0), '')
  150. self.assertEqual(truncatewords_html('<p>one <a href="#">two - '
  151. 'three <br>four</a> five</p>', 2),
  152. '<p>one <a href="#">two ...</a></p>')
  153. self.assertEqual(truncatewords_html(
  154. '<p>one <a href="#">two - three <br>four</a> five</p>', 4),
  155. '<p>one <a href="#">two - three <br>four ...</a></p>')
  156. self.assertEqual(truncatewords_html(
  157. '<p>one <a href="#">two - three <br>four</a> five</p>', 5),
  158. '<p>one <a href="#">two - three <br>four</a> five</p>')
  159. self.assertEqual(truncatewords_html(
  160. '<p>one <a href="#">two - three <br>four</a> five</p>', 100),
  161. '<p>one <a href="#">two - three <br>four</a> five</p>')
  162. self.assertEqual(truncatewords_html(
  163. '\xc5ngstr\xf6m was here', 1), '\xc5ngstr\xf6m ...')
  164. self.assertEqual(truncatewords_html('<i>Buenos d&iacute;as! '
  165. '&#x00bf;C&oacute;mo est&aacute;?</i>', 3),
  166. '<i>Buenos d&iacute;as! &#x00bf;C&oacute;mo ...</i>')
  167. def test_upper(self):
  168. self.assertEqual(upper('Mixed case input'), 'MIXED CASE INPUT')
  169. # lowercase e umlaut
  170. self.assertEqual(upper('\xeb'), '\xcb')
  171. def test_urlencode(self):
  172. self.assertEqual(urlencode('fran\xe7ois & jill'),
  173. 'fran%C3%A7ois%20%26%20jill')
  174. self.assertEqual(urlencode(1), '1')
  175. def test_iriencode(self):
  176. self.assertEqual(iriencode('S\xf8r-Tr\xf8ndelag'),
  177. 'S%C3%B8r-Tr%C3%B8ndelag')
  178. self.assertEqual(iriencode(urlencode('fran\xe7ois & jill')),
  179. 'fran%C3%A7ois%20%26%20jill')
  180. def test_urlizetrunc(self):
  181. self.assertEqual(urlizetrunc('http://short.com/', 20), '<a href='
  182. '"http://short.com/" rel="nofollow">http://short.com/</a>')
  183. self.assertEqual(urlizetrunc('http://www.google.co.uk/search?hl=en'
  184. '&q=some+long+url&btnG=Search&meta=', 20), '<a href="http://'
  185. 'www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&'
  186. 'meta=" rel="nofollow">http://www.google...</a>')
  187. self.assertEqual(urlizetrunc('http://www.google.co.uk/search?hl=en'
  188. '&q=some+long+url&btnG=Search&meta=', 20), '<a href="http://'
  189. 'www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search'
  190. '&meta=" rel="nofollow">http://www.google...</a>')
  191. # Check truncating of URIs which are the exact length
  192. uri = 'http://31characteruri.com/test/'
  193. self.assertEqual(len(uri), 31)
  194. self.assertEqual(urlizetrunc(uri, 31),
  195. '<a href="http://31characteruri.com/test/" rel="nofollow">'
  196. 'http://31characteruri.com/test/</a>')
  197. self.assertEqual(urlizetrunc(uri, 30),
  198. '<a href="http://31characteruri.com/test/" rel="nofollow">'
  199. 'http://31characteruri.com/t...</a>')
  200. self.assertEqual(urlizetrunc(uri, 2),
  201. '<a href="http://31characteruri.com/test/"'
  202. ' rel="nofollow">...</a>')
  203. def test_urlize(self):
  204. # Check normal urlize
  205. self.assertEqual(urlize('http://google.com'),
  206. '<a href="http://google.com" rel="nofollow">http://google.com</a>')
  207. self.assertEqual(urlize('http://google.com/'),
  208. '<a href="http://google.com/" rel="nofollow">http://google.com/</a>')
  209. self.assertEqual(urlize('www.google.com'),
  210. '<a href="http://www.google.com" rel="nofollow">www.google.com</a>')
  211. self.assertEqual(urlize('djangoproject.org'),
  212. '<a href="http://djangoproject.org" rel="nofollow">djangoproject.org</a>')
  213. self.assertEqual(urlize('info@djangoproject.org'),
  214. '<a href="mailto:info@djangoproject.org">info@djangoproject.org</a>')
  215. # Check urlize with https addresses
  216. self.assertEqual(urlize('https://google.com'),
  217. '<a href="https://google.com" rel="nofollow">https://google.com</a>')
  218. # Check urlize doesn't overquote already quoted urls - see #9655
  219. # The teststring is the urlquoted version of 'http://hi.baidu.com/重新开始'
  220. self.assertEqual(urlize('http://hi.baidu.com/%E9%87%8D%E6%96%B0%E5%BC%80%E5%A7%8B'),
  221. '<a href="http://hi.baidu.com/%E9%87%8D%E6%96%B0%E5%BC%80%E5%A7%8B" rel="nofollow">'
  222. 'http://hi.baidu.com/%E9%87%8D%E6%96%B0%E5%BC%80%E5%A7%8B</a>')
  223. self.assertEqual(urlize('www.mystore.com/30%OffCoupons!'),
  224. '<a href="http://www.mystore.com/30%25OffCoupons!" rel="nofollow">'
  225. 'www.mystore.com/30%OffCoupons!</a>')
  226. self.assertEqual(urlize('http://en.wikipedia.org/wiki/Caf%C3%A9'),
  227. '<a href="http://en.wikipedia.org/wiki/Caf%C3%A9" rel="nofollow">'
  228. 'http://en.wikipedia.org/wiki/Caf%C3%A9</a>')
  229. self.assertEqual(urlize('http://en.wikipedia.org/wiki/Café'),
  230. '<a href="http://en.wikipedia.org/wiki/Caf%C3%A9" rel="nofollow">'
  231. 'http://en.wikipedia.org/wiki/Café</a>')
  232. # Check urlize keeps balanced parentheses - see #11911
  233. self.assertEqual(urlize('http://en.wikipedia.org/wiki/Django_(web_framework)'),
  234. '<a href="http://en.wikipedia.org/wiki/Django_(web_framework)" rel="nofollow">'
  235. 'http://en.wikipedia.org/wiki/Django_(web_framework)</a>')
  236. self.assertEqual(urlize('(see http://en.wikipedia.org/wiki/Django_(web_framework))'),
  237. '(see <a href="http://en.wikipedia.org/wiki/Django_(web_framework)" rel="nofollow">'
  238. 'http://en.wikipedia.org/wiki/Django_(web_framework)</a>)')
  239. # Check urlize adds nofollow properly - see #12183
  240. self.assertEqual(urlize('foo@bar.com or www.bar.com'),
  241. '<a href="mailto:foo@bar.com">foo@bar.com</a> or '
  242. '<a href="http://www.bar.com" rel="nofollow">www.bar.com</a>')
  243. # Check urlize handles IDN correctly - see #13704
  244. self.assertEqual(urlize('http://c✶.ws'),
  245. '<a href="http://xn--c-lgq.ws" rel="nofollow">http://c✶.ws</a>')
  246. self.assertEqual(urlize('www.c✶.ws'),
  247. '<a href="http://www.xn--c-lgq.ws" rel="nofollow">www.c✶.ws</a>')
  248. self.assertEqual(urlize('c✶.org'),
  249. '<a href="http://xn--c-lgq.org" rel="nofollow">c✶.org</a>')
  250. self.assertEqual(urlize('info@c✶.org'),
  251. '<a href="mailto:info@xn--c-lgq.org">info@c✶.org</a>')
  252. # Check urlize doesn't highlight malformed URIs - see #16395
  253. self.assertEqual(urlize('http:///www.google.com'),
  254. 'http:///www.google.com')
  255. self.assertEqual(urlize('http://.google.com'),
  256. 'http://.google.com')
  257. self.assertEqual(urlize('http://@foo.com'),
  258. 'http://@foo.com')
  259. # Check urlize accepts more TLDs - see #16656
  260. self.assertEqual(urlize('usa.gov'),
  261. '<a href="http://usa.gov" rel="nofollow">usa.gov</a>')
  262. # Check urlize don't crash on invalid email with dot-starting domain - see #17592
  263. self.assertEqual(urlize('email@.stream.ru'),
  264. 'email@.stream.ru')
  265. # Check urlize accepts uppercased URL schemes - see #18071
  266. self.assertEqual(urlize('HTTPS://github.com/'),
  267. '<a href="https://github.com/" rel="nofollow">HTTPS://github.com/</a>')
  268. # Check urlize trims trailing period when followed by parenthesis - see #18644
  269. self.assertEqual(urlize('(Go to http://www.example.com/foo.)'),
  270. '(Go to <a href="http://www.example.com/foo" rel="nofollow">http://www.example.com/foo</a>.)')
  271. # Check urlize handles brackets properly (#19070)
  272. self.assertEqual(urlize('[see www.example.com]'),
  273. '[see <a href="http://www.example.com" rel="nofollow">www.example.com</a>]')
  274. self.assertEqual(urlize('see test[at[example.com'),
  275. 'see <a href="http://test[at[example.com" rel="nofollow">test[at[example.com</a>')
  276. self.assertEqual(urlize('[http://168.192.0.1](http://168.192.0.1)'),
  277. '[<a href="http://168.192.0.1](http://168.192.0.1)" rel="nofollow">http://168.192.0.1](http://168.192.0.1)</a>')
  278. # Check urlize works with IPv4/IPv6 addresses
  279. self.assertEqual(urlize('http://192.168.0.15/api/9'),
  280. '<a href="http://192.168.0.15/api/9" rel="nofollow">http://192.168.0.15/api/9</a>')
  281. self.assertEqual(urlize('http://[2001:db8:cafe::2]/api/9'),
  282. '<a href="http://[2001:db8:cafe::2]/api/9" rel="nofollow">http://[2001:db8:cafe::2]/api/9</a>')
  283. # Check urlize correctly include quotation marks in links - #20364
  284. self.assertEqual(urlize('before "hi@example.com" afterwards'),
  285. 'before "<a href="mailto:hi@example.com">hi@example.com</a>" afterwards')
  286. self.assertEqual(urlize('before hi@example.com" afterwards'),
  287. 'before <a href="mailto:hi@example.com">hi@example.com</a>" afterwards')
  288. self.assertEqual(urlize('before "hi@example.com afterwards'),
  289. 'before "<a href="mailto:hi@example.com">hi@example.com</a> afterwards')
  290. self.assertEqual(urlize('before \'hi@example.com\' afterwards'),
  291. 'before \'<a href="mailto:hi@example.com">hi@example.com</a>\' afterwards')
  292. self.assertEqual(urlize('before hi@example.com\' afterwards'),
  293. 'before <a href="mailto:hi@example.com">hi@example.com</a>\' afterwards')
  294. self.assertEqual(urlize('before \'hi@example.com afterwards'),
  295. 'before \'<a href="mailto:hi@example.com">hi@example.com</a> afterwards')
  296. # Check urlize copes with commas following URLs in quotes - see #20364
  297. self.assertEqual(urlize('Email us at "hi@example.com", or phone us at +xx.yy'),
  298. 'Email us at "<a href="mailto:hi@example.com">hi@example.com</a>", or phone us at +xx.yy')
  299. def test_wordcount(self):
  300. self.assertEqual(wordcount(''), 0)
  301. self.assertEqual(wordcount('oneword'), 1)
  302. self.assertEqual(wordcount('lots of words'), 3)
  303. self.assertEqual(wordwrap('this is a long paragraph of text that '
  304. 'really needs to be wrapped I\'m afraid', 14),
  305. "this is a long\nparagraph of\ntext that\nreally needs\nto be "
  306. "wrapped\nI'm afraid")
  307. self.assertEqual(wordwrap('this is a short paragraph of text.\n '
  308. 'But this line should be indented', 14),
  309. 'this is a\nshort\nparagraph of\ntext.\n But this\nline '
  310. 'should be\nindented')
  311. self.assertEqual(wordwrap('this is a short paragraph of text.\n '
  312. 'But this line should be indented', 15), 'this is a short\n'
  313. 'paragraph of\ntext.\n But this line\nshould be\nindented')
  314. def test_rjust(self):
  315. self.assertEqual(ljust('test', 10), 'test ')
  316. self.assertEqual(ljust('test', 3), 'test')
  317. self.assertEqual(rjust('test', 10), ' test')
  318. self.assertEqual(rjust('test', 3), 'test')
  319. def test_center(self):
  320. self.assertEqual(center('test', 6), ' test ')
  321. def test_cut(self):
  322. self.assertEqual(cut('a string to be mangled', 'a'),
  323. ' string to be mngled')
  324. self.assertEqual(cut('a string to be mangled', 'ng'),
  325. 'a stri to be maled')
  326. self.assertEqual(cut('a string to be mangled', 'strings'),
  327. 'a string to be mangled')
  328. def test_force_escape(self):
  329. escaped = force_escape('<some html & special characters > here')
  330. self.assertEqual(
  331. escaped, '&lt;some html &amp; special characters &gt; here')
  332. self.assertIsInstance(escaped, SafeData)
  333. self.assertEqual(
  334. force_escape('<some html & special characters > here ĐÅ€£'),
  335. '&lt;some html &amp; special characters &gt; here'
  336. ' \u0110\xc5\u20ac\xa3')
  337. def test_linebreaks(self):
  338. self.assertEqual(linebreaks_filter('line 1'), '<p>line 1</p>')
  339. self.assertEqual(linebreaks_filter('line 1\nline 2'),
  340. '<p>line 1<br />line 2</p>')
  341. self.assertEqual(linebreaks_filter('line 1\rline 2'),
  342. '<p>line 1<br />line 2</p>')
  343. self.assertEqual(linebreaks_filter('line 1\r\nline 2'),
  344. '<p>line 1<br />line 2</p>')
  345. def test_linebreaksbr(self):
  346. self.assertEqual(linebreaksbr('line 1\nline 2'),
  347. 'line 1<br />line 2')
  348. self.assertEqual(linebreaksbr('line 1\rline 2'),
  349. 'line 1<br />line 2')
  350. self.assertEqual(linebreaksbr('line 1\r\nline 2'),
  351. 'line 1<br />line 2')
  352. def test_removetags(self):
  353. self.assertEqual(removetags('some <b>html</b> with <script>alert'
  354. '("You smell")</script> disallowed <img /> tags', 'script img'),
  355. 'some <b>html</b> with alert("You smell") disallowed tags')
  356. self.assertEqual(striptags('some <b>html</b> with <script>alert'
  357. '("You smell")</script> disallowed <img /> tags'),
  358. 'some html with alert("You smell") disallowed tags')
  359. def test_dictsort(self):
  360. sorted_dicts = dictsort([{'age': 23, 'name': 'Barbara-Ann'},
  361. {'age': 63, 'name': 'Ra Ra Rasputin'},
  362. {'name': 'Jonny B Goode', 'age': 18}], 'age')
  363. self.assertEqual([sorted(dict.items()) for dict in sorted_dicts],
  364. [[('age', 18), ('name', 'Jonny B Goode')],
  365. [('age', 23), ('name', 'Barbara-Ann')],
  366. [('age', 63), ('name', 'Ra Ra Rasputin')]])
  367. # If it gets passed a list of something else different from
  368. # dictionaries it should fail silently
  369. self.assertEqual(dictsort([1, 2, 3], 'age'), '')
  370. self.assertEqual(dictsort('Hello!', 'age'), '')
  371. self.assertEqual(dictsort({'a': 1}, 'age'), '')
  372. self.assertEqual(dictsort(1, 'age'), '')
  373. def test_dictsort_complex_sorting_key(self):
  374. """
  375. Since dictsort uses template.Variable under the hood, it can sort
  376. on keys like 'foo.bar'.
  377. """
  378. data = [
  379. {'foo': {'bar': 1, 'baz': 'c'}},
  380. {'foo': {'bar': 2, 'baz': 'b'}},
  381. {'foo': {'bar': 3, 'baz': 'a'}},
  382. ]
  383. sorted_data = dictsort(data, 'foo.baz')
  384. self.assertEqual([d['foo']['bar'] for d in sorted_data], [3, 2, 1])
  385. def test_dictsortreversed(self):
  386. sorted_dicts = dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},
  387. {'age': 63, 'name': 'Ra Ra Rasputin'},
  388. {'name': 'Jonny B Goode', 'age': 18}],
  389. 'age')
  390. self.assertEqual([sorted(dict.items()) for dict in sorted_dicts],
  391. [[('age', 63), ('name', 'Ra Ra Rasputin')],
  392. [('age', 23), ('name', 'Barbara-Ann')],
  393. [('age', 18), ('name', 'Jonny B Goode')]])
  394. # If it gets passed a list of something else different from
  395. # dictionaries it should fail silently
  396. self.assertEqual(dictsortreversed([1, 2, 3], 'age'), '')
  397. self.assertEqual(dictsortreversed('Hello!', 'age'), '')
  398. self.assertEqual(dictsortreversed({'a': 1}, 'age'), '')
  399. self.assertEqual(dictsortreversed(1, 'age'), '')
  400. def test_first(self):
  401. self.assertEqual(first([0, 1, 2]), 0)
  402. self.assertEqual(first(''), '')
  403. self.assertEqual(first('test'), 't')
  404. def test_join(self):
  405. self.assertEqual(join([0, 1, 2], 'glue'), '0glue1glue2')
  406. def test_length(self):
  407. self.assertEqual(length('1234'), 4)
  408. self.assertEqual(length([1, 2, 3, 4]), 4)
  409. self.assertEqual(length_is([], 0), True)
  410. self.assertEqual(length_is([], 1), False)
  411. self.assertEqual(length_is('a', 1), True)
  412. self.assertEqual(length_is('a', 10), False)
  413. def test_slice(self):
  414. self.assertEqual(slice_filter('abcdefg', '0'), '')
  415. self.assertEqual(slice_filter('abcdefg', '1'), 'a')
  416. self.assertEqual(slice_filter('abcdefg', '-1'), 'abcdef')
  417. self.assertEqual(slice_filter('abcdefg', '1:2'), 'b')
  418. self.assertEqual(slice_filter('abcdefg', '1:3'), 'bc')
  419. self.assertEqual(slice_filter('abcdefg', '0::2'), 'aceg')
  420. def test_unordered_list(self):
  421. self.assertEqual(unordered_list(['item 1', 'item 2']),
  422. '\t<li>item 1</li>\n\t<li>item 2</li>')
  423. self.assertEqual(unordered_list(['item 1', ['item 1.1']]),
  424. '\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1</li>\n\t</ul>\n\t</li>')
  425. self.assertEqual(
  426. unordered_list(['item 1', ['item 1.1', 'item1.2'], 'item 2']),
  427. '\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1</li>\n\t\t<li>item1.2'
  428. '</li>\n\t</ul>\n\t</li>\n\t<li>item 2</li>')
  429. self.assertEqual(
  430. unordered_list(['item 1', ['item 1.1', ['item 1.1.1', ['item 1.1.1.1']]]]),
  431. '\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1\n\t\t<ul>\n\t\t\t<li>'
  432. 'item 1.1.1\n\t\t\t<ul>\n\t\t\t\t<li>item 1.1.1.1</li>\n\t\t\t'
  433. '</ul>\n\t\t\t</li>\n\t\t</ul>\n\t\t</li>\n\t</ul>\n\t</li>')
  434. self.assertEqual(unordered_list(
  435. ['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]),
  436. '\t<li>States\n\t<ul>\n\t\t<li>Kansas\n\t\t<ul>\n\t\t\t<li>'
  437. 'Lawrence</li>\n\t\t\t<li>Topeka</li>\n\t\t</ul>\n\t\t</li>'
  438. '\n\t\t<li>Illinois</li>\n\t</ul>\n\t</li>')
  439. @python_2_unicode_compatible
  440. class ULItem(object):
  441. def __init__(self, title):
  442. self.title = title
  443. def __str__(self):
  444. return 'ulitem-%s' % str(self.title)
  445. a = ULItem('a')
  446. b = ULItem('b')
  447. self.assertEqual(unordered_list([a, b]), '\t<li>ulitem-a</li>\n\t<li>ulitem-b</li>')
  448. # Old format for unordered lists should still work
  449. self.assertEqual(unordered_list(['item 1', []]), '\t<li>item 1</li>')
  450. self.assertEqual(unordered_list(['item 1', [['item 1.1', []]]]),
  451. '\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1</li>\n\t</ul>\n\t</li>')
  452. self.assertEqual(unordered_list(['item 1', [['item 1.1', []],
  453. ['item 1.2', []]]]), '\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1'
  454. '</li>\n\t\t<li>item 1.2</li>\n\t</ul>\n\t</li>')
  455. self.assertEqual(unordered_list(['States', [['Kansas', [['Lawrence',
  456. []], ['Topeka', []]]], ['Illinois', []]]]), '\t<li>States\n\t'
  457. '<ul>\n\t\t<li>Kansas\n\t\t<ul>\n\t\t\t<li>Lawrence</li>'
  458. '\n\t\t\t<li>Topeka</li>\n\t\t</ul>\n\t\t</li>\n\t\t<li>'
  459. 'Illinois</li>\n\t</ul>\n\t</li>')
  460. def test_add(self):
  461. self.assertEqual(add('1', '2'), 3)
  462. def test_get_digit(self):
  463. self.assertEqual(get_digit(123, 1), 3)
  464. self.assertEqual(get_digit(123, 2), 2)
  465. self.assertEqual(get_digit(123, 3), 1)
  466. self.assertEqual(get_digit(123, 4), 0)
  467. self.assertEqual(get_digit(123, 0), 123)
  468. self.assertEqual(get_digit('xyz', 0), 'xyz')
  469. def test_date(self):
  470. # real testing of date() is in dateformat.py
  471. self.assertEqual(date(datetime.datetime(2005, 12, 29), "d F Y"),
  472. '29 December 2005')
  473. self.assertEqual(date(datetime.datetime(2005, 12, 29), r'jS \o\f F'),
  474. '29th of December')
  475. def test_time(self):
  476. # real testing of time() is done in dateformat.py
  477. self.assertEqual(time(datetime.time(13), "h"), '01')
  478. self.assertEqual(time(datetime.time(0), "h"), '12')
  479. def test_timesince(self):
  480. # real testing is done in timesince.py, where we can provide our own 'now'
  481. # NOTE: \xa0 avoids wrapping between value and unit
  482. self.assertEqual(
  483. timesince_filter(datetime.datetime.now() - datetime.timedelta(1)),
  484. '1\xa0day')
  485. self.assertEqual(
  486. timesince_filter(datetime.datetime(2005, 12, 29),
  487. datetime.datetime(2005, 12, 30)),
  488. '1\xa0day')
  489. def test_timeuntil(self):
  490. # NOTE: \xa0 avoids wrapping between value and unit
  491. self.assertEqual(
  492. timeuntil_filter(datetime.datetime.now() + datetime.timedelta(1, 1)),
  493. '1\xa0day')
  494. self.assertEqual(
  495. timeuntil_filter(datetime.datetime(2005, 12, 30),
  496. datetime.datetime(2005, 12, 29)),
  497. '1\xa0day')
  498. def test_default(self):
  499. self.assertEqual(default("val", "default"), 'val')
  500. self.assertEqual(default(None, "default"), 'default')
  501. self.assertEqual(default('', "default"), 'default')
  502. def test_if_none(self):
  503. self.assertEqual(default_if_none("val", "default"), 'val')
  504. self.assertEqual(default_if_none(None, "default"), 'default')
  505. self.assertEqual(default_if_none('', "default"), '')
  506. def test_divisibleby(self):
  507. self.assertEqual(divisibleby(4, 2), True)
  508. self.assertEqual(divisibleby(4, 3), False)
  509. def test_yesno(self):
  510. self.assertEqual(yesno(True), 'yes')
  511. self.assertEqual(yesno(False), 'no')
  512. self.assertEqual(yesno(None), 'maybe')
  513. self.assertEqual(yesno(True, 'certainly,get out of town,perhaps'),
  514. 'certainly')
  515. self.assertEqual(yesno(False, 'certainly,get out of town,perhaps'),
  516. 'get out of town')
  517. self.assertEqual(yesno(None, 'certainly,get out of town,perhaps'),
  518. 'perhaps')
  519. self.assertEqual(yesno(None, 'certainly,get out of town'),
  520. 'get out of town')
  521. def test_filesizeformat(self):
  522. # NOTE: \xa0 avoids wrapping between value and unit
  523. self.assertEqual(filesizeformat(1023), '1023\xa0bytes')
  524. self.assertEqual(filesizeformat(1024), '1.0\xa0KB')
  525. self.assertEqual(filesizeformat(10 * 1024), '10.0\xa0KB')
  526. self.assertEqual(filesizeformat(1024 * 1024 - 1), '1024.0\xa0KB')
  527. self.assertEqual(filesizeformat(1024 * 1024), '1.0\xa0MB')
  528. self.assertEqual(filesizeformat(1024 * 1024 * 50), '50.0\xa0MB')
  529. self.assertEqual(filesizeformat(1024 * 1024 * 1024 - 1), '1024.0\xa0MB')
  530. self.assertEqual(filesizeformat(1024 * 1024 * 1024), '1.0\xa0GB')
  531. self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024), '1.0\xa0TB')
  532. self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024 * 1024), '1.0\xa0PB')
  533. self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024 * 1024 * 2000), '2000.0\xa0PB')
  534. self.assertEqual(filesizeformat(complex(1, -1)), '0\xa0bytes')
  535. self.assertEqual(filesizeformat(""), '0\xa0bytes')
  536. self.assertEqual(filesizeformat("\N{GREEK SMALL LETTER ALPHA}"), '0\xa0bytes')
  537. def test_pluralize(self):
  538. self.assertEqual(pluralize(1), '')
  539. self.assertEqual(pluralize(0), 's')
  540. self.assertEqual(pluralize(2), 's')
  541. self.assertEqual(pluralize([1]), '')
  542. self.assertEqual(pluralize([]), 's')
  543. self.assertEqual(pluralize([1, 2, 3]), 's')
  544. self.assertEqual(pluralize(1, 'es'), '')
  545. self.assertEqual(pluralize(0, 'es'), 'es')
  546. self.assertEqual(pluralize(2, 'es'), 'es')
  547. self.assertEqual(pluralize(1, 'y,ies'), 'y')
  548. self.assertEqual(pluralize(0, 'y,ies'), 'ies')
  549. self.assertEqual(pluralize(2, 'y,ies'), 'ies')
  550. self.assertEqual(pluralize(0, 'y,ies,error'), '')
  551. def test_phone2numeric(self):
  552. self.assertEqual(phone2numeric_filter('0800 flowers'), '0800 3569377')
  553. def test_non_string_input(self):
  554. # Filters shouldn't break if passed non-strings
  555. self.assertEqual(addslashes(123), '123')
  556. self.assertEqual(linenumbers(123), '1. 123')
  557. self.assertEqual(lower(123), '123')
  558. self.assertEqual(make_list(123), ['1', '2', '3'])
  559. self.assertEqual(slugify(123), '123')
  560. self.assertEqual(title(123), '123')
  561. self.assertEqual(truncatewords(123, 2), '123')
  562. self.assertEqual(upper(123), '123')
  563. self.assertEqual(urlencode(123), '123')
  564. self.assertEqual(urlize(123), '123')
  565. self.assertEqual(urlizetrunc(123, 1), '123')
  566. self.assertEqual(wordcount(123), 1)
  567. self.assertEqual(wordwrap(123, 2), '123')
  568. self.assertEqual(ljust('123', 4), '123 ')
  569. self.assertEqual(rjust('123', 4), ' 123')
  570. self.assertEqual(center('123', 5), ' 123 ')
  571. self.assertEqual(center('123', 6), ' 123 ')
  572. self.assertEqual(cut(123, '2'), '13')
  573. self.assertEqual(escape(123), '123')
  574. self.assertEqual(linebreaks_filter(123), '<p>123</p>')
  575. self.assertEqual(linebreaksbr(123), '123')
  576. self.assertEqual(removetags(123, 'a'), '123')
  577. self.assertEqual(striptags(123), '123')
  578. class DefaultFiltersI18NTests(TestCase):
  579. def test_localized_filesizeformat(self):
  580. # NOTE: \xa0 avoids wrapping between value and unit
  581. with self.settings(USE_L10N=True), translation.override('de'):
  582. self.assertEqual(filesizeformat(1023), '1023\xa0Bytes')
  583. self.assertEqual(filesizeformat(1024), '1,0\xa0KB')
  584. self.assertEqual(filesizeformat(10 * 1024), '10,0\xa0KB')
  585. self.assertEqual(filesizeformat(1024 * 1024 - 1), '1024,0\xa0KB')
  586. self.assertEqual(filesizeformat(1024 * 1024), '1,0\xa0MB')
  587. self.assertEqual(filesizeformat(1024 * 1024 * 50), '50,0\xa0MB')
  588. self.assertEqual(filesizeformat(1024 * 1024 * 1024 - 1), '1024,0\xa0MB')
  589. self.assertEqual(filesizeformat(1024 * 1024 * 1024), '1,0\xa0GB')
  590. self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024), '1,0\xa0TB')
  591. self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024 * 1024), '1,0\xa0PB')
  592. self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024 * 1024 * 2000), '2000,0\xa0PB')
  593. self.assertEqual(filesizeformat(complex(1, -1)), '0\xa0Bytes')
  594. self.assertEqual(filesizeformat(""), '0\xa0Bytes')
  595. self.assertEqual(filesizeformat("\N{GREEK SMALL LETTER ALPHA}"), '0\xa0Bytes')