2007-04-04 14:43:28 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-10-14 02:51:33 +08:00
|
|
|
|
2010-09-28 16:19:04 +08:00
|
|
|
import datetime
|
2011-12-30 22:35:29 +08:00
|
|
|
import decimal
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2011-10-14 02:51:33 +08:00
|
|
|
|
|
|
|
from django.template.defaultfilters import *
|
2011-05-06 21:29:44 +08:00
|
|
|
from django.test import TestCase
|
2012-07-20 20:48:51 +08:00
|
|
|
from django.utils import six
|
2013-07-01 20:22:27 +08:00
|
|
|
from django.utils import translation
|
2012-04-07 23:16:11 +08:00
|
|
|
from django.utils.safestring import SafeData
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2005-10-25 09:44:14 +08:00
|
|
|
|
2013-05-31 02:23:14 +08:00
|
|
|
from i18n import TransRealMixin
|
|
|
|
|
2005-10-25 09:44:14 +08:00
|
|
|
|
2011-05-06 21:29:44 +08:00
|
|
|
class DefaultFiltersTests(TestCase):
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_floatformat(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(floatformat(7.7), '7.7')
|
|
|
|
self.assertEqual(floatformat(7.0), '7')
|
|
|
|
self.assertEqual(floatformat(0.7), '0.7')
|
|
|
|
self.assertEqual(floatformat(0.07), '0.1')
|
|
|
|
self.assertEqual(floatformat(0.007), '0.0')
|
|
|
|
self.assertEqual(floatformat(0.0), '0')
|
|
|
|
self.assertEqual(floatformat(7.7, 3), '7.700')
|
|
|
|
self.assertEqual(floatformat(6.000000, 3), '6.000')
|
|
|
|
self.assertEqual(floatformat(6.200000, 3), '6.200')
|
|
|
|
self.assertEqual(floatformat(6.200000, -3), '6.200')
|
|
|
|
self.assertEqual(floatformat(13.1031, -3), '13.103')
|
|
|
|
self.assertEqual(floatformat(11.1197, -2), '11.12')
|
|
|
|
self.assertEqual(floatformat(11.0000, -2), '11')
|
|
|
|
self.assertEqual(floatformat(11.000001, -2), '11.00')
|
|
|
|
self.assertEqual(floatformat(8.2798, 3), '8.280')
|
|
|
|
self.assertEqual(floatformat(5555.555, 2), '5555.56')
|
|
|
|
self.assertEqual(floatformat(001.3000, 2), '1.30')
|
|
|
|
self.assertEqual(floatformat(0.12345, 2), '0.12')
|
|
|
|
self.assertEqual(floatformat(decimal.Decimal('555.555'), 2), '555.56')
|
|
|
|
self.assertEqual(floatformat(decimal.Decimal('09.000')), '9')
|
|
|
|
self.assertEqual(floatformat('foo'), '')
|
|
|
|
self.assertEqual(floatformat(13.1031, 'bar'), '13.1031')
|
|
|
|
self.assertEqual(floatformat(18.125, 2), '18.13')
|
|
|
|
self.assertEqual(floatformat('foo', 'bar'), '')
|
|
|
|
self.assertEqual(floatformat('¿Cómo esta usted?'), '')
|
|
|
|
self.assertEqual(floatformat(None), '')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2011-03-04 04:56:46 +08:00
|
|
|
# Check that we're not converting to scientific notation.
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(floatformat(0, 6), '0.000000')
|
|
|
|
self.assertEqual(floatformat(0, 7), '0.0000000')
|
|
|
|
self.assertEqual(floatformat(0, 10), '0.0000000000')
|
2011-03-04 04:56:46 +08:00
|
|
|
self.assertEqual(floatformat(0.000000000000000000015, 20),
|
2012-06-08 00:08:47 +08:00
|
|
|
'0.00000000000000000002')
|
2011-03-04 04:56:46 +08:00
|
|
|
|
2010-09-28 16:19:04 +08:00
|
|
|
pos_inf = float(1e30000)
|
2012-07-20 20:48:51 +08:00
|
|
|
self.assertEqual(floatformat(pos_inf), six.text_type(pos_inf))
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
neg_inf = float(-1e30000)
|
2012-07-20 20:48:51 +08:00
|
|
|
self.assertEqual(floatformat(neg_inf), six.text_type(neg_inf))
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
nan = pos_inf / pos_inf
|
2012-07-20 20:48:51 +08:00
|
|
|
self.assertEqual(floatformat(nan), six.text_type(nan))
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
class FloatWrapper(object):
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
def __float__(self):
|
|
|
|
return self.value
|
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(floatformat(FloatWrapper(11.000001), -2), '11.00')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2011-12-30 22:35:29 +08:00
|
|
|
# Regression for #15789
|
|
|
|
decimal_ctx = decimal.getcontext()
|
|
|
|
old_prec, decimal_ctx.prec = decimal_ctx.prec, 2
|
|
|
|
try:
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(floatformat(1.2345, 2), '1.23')
|
|
|
|
self.assertEqual(floatformat(15.2042, -3), '15.204')
|
|
|
|
self.assertEqual(floatformat(1.2345, '2'), '1.23')
|
|
|
|
self.assertEqual(floatformat(15.2042, '-3'), '15.204')
|
|
|
|
self.assertEqual(floatformat(decimal.Decimal('1.2345'), 2), '1.23')
|
|
|
|
self.assertEqual(floatformat(decimal.Decimal('15.2042'), -3), '15.204')
|
2011-12-30 22:35:29 +08:00
|
|
|
finally:
|
|
|
|
decimal_ctx.prec = old_prec
|
|
|
|
|
|
|
|
|
2012-10-29 02:56:19 +08:00
|
|
|
def test_floatformat_py2_fail(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(floatformat(1.00000000000000015, 16), '1.0000000000000002')
|
2011-03-04 04:56:46 +08:00
|
|
|
|
2012-10-29 02:56:19 +08:00
|
|
|
# The test above fails because of Python 2's float handling. Floats with
|
|
|
|
# many zeroes after the decimal point should be passed in as another type
|
|
|
|
# such as unicode or Decimal.
|
2013-09-02 18:06:32 +08:00
|
|
|
if six.PY2:
|
2012-10-29 02:56:19 +08:00
|
|
|
test_floatformat_py2_fail = unittest.expectedFailure(test_floatformat_py2_fail)
|
|
|
|
|
|
|
|
|
2010-09-28 16:19:04 +08:00
|
|
|
def test_addslashes(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(addslashes('"double quotes" and \'single quotes\''),
|
|
|
|
'\\"double quotes\\" and \\\'single quotes\\\'')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(addslashes(r'\ : backslashes, too'),
|
|
|
|
'\\\\ : backslashes, too')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_capfirst(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(capfirst('hello world'), 'Hello world')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_escapejs(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(escapejs_filter('"double quotes" and \'single quotes\''),
|
|
|
|
'\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027')
|
|
|
|
self.assertEqual(escapejs_filter(r'\ : backslashes, too'),
|
|
|
|
'\\u005C : backslashes, too')
|
|
|
|
self.assertEqual(escapejs_filter('and lots of whitespace: \r\n\t\v\f\b'),
|
|
|
|
'and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008')
|
|
|
|
self.assertEqual(escapejs_filter(r'<script>and this</script>'),
|
|
|
|
'\\u003Cscript\\u003Eand this\\u003C/script\\u003E')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(
|
2012-06-08 00:08:47 +08:00
|
|
|
escapejs_filter('paragraph separator:\u2029and line separator:\u2028'),
|
|
|
|
'paragraph separator:\\u2029and line separator:\\u2028')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_fix_ampersands(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(fix_ampersands_filter('Jack & Jill & Jeroboam'),
|
|
|
|
'Jack & Jill & Jeroboam')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_linenumbers(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(linenumbers('line 1\nline 2'),
|
|
|
|
'1. line 1\n2. line 2')
|
|
|
|
self.assertEqual(linenumbers('\n'.join(['x'] * 10)),
|
|
|
|
'01. x\n02. x\n03. x\n04. x\n05. x\n06. x\n07. '\
|
|
|
|
'x\n08. x\n09. x\n10. x')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_lower(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(lower('TEST'), 'test')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
# uppercase E umlaut
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(lower('\xcb'), '\xeb')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_make_list(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(make_list('abc'), ['a', 'b', 'c'])
|
|
|
|
self.assertEqual(make_list(1234), ['1', '2', '3', '4'])
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_slugify(self):
|
|
|
|
self.assertEqual(slugify(' Jack & Jill like numbers 1,2,3 and 4 and'\
|
|
|
|
' silly characters ?%.$!/'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'jack-jill-like-numbers-123-and-4-and-silly-characters')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(slugify("Un \xe9l\xe9phant \xe0 l'or\xe9e du bois"),
|
|
|
|
'un-elephant-a-loree-du-bois')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_stringformat(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(stringformat(1, '03d'), '001')
|
|
|
|
self.assertEqual(stringformat(1, 'z'), '')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_title(self):
|
|
|
|
self.assertEqual(title('a nice title, isn\'t it?'),
|
2012-06-08 00:08:47 +08:00
|
|
|
"A Nice Title, Isn't It?")
|
|
|
|
self.assertEqual(title('discoth\xe8que'), 'Discoth\xe8que')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_truncatewords(self):
|
|
|
|
self.assertEqual(
|
2012-06-08 00:08:47 +08:00
|
|
|
truncatewords('A sentence with a few words in it', 1), 'A ...')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(
|
2012-06-08 00:08:47 +08:00
|
|
|
truncatewords('A sentence with a few words in it', 5),
|
|
|
|
'A sentence with a few ...')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(
|
2012-06-08 00:08:47 +08:00
|
|
|
truncatewords('A sentence with a few words in it', 100),
|
|
|
|
'A sentence with a few words in it')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(
|
2012-06-08 00:08:47 +08:00
|
|
|
truncatewords('A sentence with a few words in it',
|
|
|
|
'not a number'), 'A sentence with a few words in it')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_truncatewords_html(self):
|
|
|
|
self.assertEqual(truncatewords_html(
|
2012-06-08 00:08:47 +08:00
|
|
|
'<p>one <a href="#">two - three <br>four</a> five</p>', 0), '')
|
|
|
|
self.assertEqual(truncatewords_html('<p>one <a href="#">two - '\
|
|
|
|
'three <br>four</a> five</p>', 2),
|
|
|
|
'<p>one <a href="#">two ...</a></p>')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(truncatewords_html(
|
2012-06-08 00:08:47 +08:00
|
|
|
'<p>one <a href="#">two - three <br>four</a> five</p>', 4),
|
|
|
|
'<p>one <a href="#">two - three <br>four ...</a></p>')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(truncatewords_html(
|
2012-06-08 00:08:47 +08:00
|
|
|
'<p>one <a href="#">two - three <br>four</a> five</p>', 5),
|
|
|
|
'<p>one <a href="#">two - three <br>four</a> five</p>')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(truncatewords_html(
|
2012-06-08 00:08:47 +08:00
|
|
|
'<p>one <a href="#">two - three <br>four</a> five</p>', 100),
|
|
|
|
'<p>one <a href="#">two - three <br>four</a> five</p>')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(truncatewords_html(
|
2012-06-08 00:08:47 +08:00
|
|
|
'\xc5ngstr\xf6m was here', 1), '\xc5ngstr\xf6m ...')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_upper(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(upper('Mixed case input'), 'MIXED CASE INPUT')
|
2010-09-28 16:19:04 +08:00
|
|
|
# lowercase e umlaut
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(upper('\xeb'), '\xcb')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_urlencode(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(urlencode('fran\xe7ois & jill'),
|
|
|
|
'fran%C3%A7ois%20%26%20jill')
|
|
|
|
self.assertEqual(urlencode(1), '1')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_iriencode(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(iriencode('S\xf8r-Tr\xf8ndelag'),
|
|
|
|
'S%C3%B8r-Tr%C3%B8ndelag')
|
|
|
|
self.assertEqual(iriencode(urlencode('fran\xe7ois & jill')),
|
|
|
|
'fran%C3%A7ois%20%26%20jill')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_urlizetrunc(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(urlizetrunc('http://short.com/', 20), '<a href='\
|
|
|
|
'"http://short.com/" rel="nofollow">http://short.com/</a>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(urlizetrunc('http://www.google.co.uk/search?hl=en'\
|
|
|
|
'&q=some+long+url&btnG=Search&meta=', 20), '<a href="http://'\
|
|
|
|
'www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&'\
|
|
|
|
'meta=" rel="nofollow">http://www.google...</a>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
self.assertEqual(urlizetrunc('http://www.google.co.uk/search?hl=en'\
|
2012-06-08 00:08:47 +08:00
|
|
|
'&q=some+long+url&btnG=Search&meta=', 20), '<a href="http://'\
|
|
|
|
'www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search'\
|
|
|
|
'&meta=" rel="nofollow">http://www.google...</a>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
# Check truncating of URIs which are the exact length
|
|
|
|
uri = 'http://31characteruri.com/test/'
|
|
|
|
self.assertEqual(len(uri), 31)
|
|
|
|
|
|
|
|
self.assertEqual(urlizetrunc(uri, 31),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://31characteruri.com/test/" rel="nofollow">'\
|
|
|
|
'http://31characteruri.com/test/</a>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
self.assertEqual(urlizetrunc(uri, 30),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://31characteruri.com/test/" rel="nofollow">'\
|
|
|
|
'http://31characteruri.com/t...</a>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
self.assertEqual(urlizetrunc(uri, 2),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://31characteruri.com/test/"'\
|
|
|
|
' rel="nofollow">...</a>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_urlize(self):
|
|
|
|
# Check normal urlize
|
|
|
|
self.assertEqual(urlize('http://google.com'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://google.com" rel="nofollow">http://google.com</a>')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(urlize('http://google.com/'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://google.com/" rel="nofollow">http://google.com/</a>')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(urlize('www.google.com'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://www.google.com" rel="nofollow">www.google.com</a>')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(urlize('djangoproject.org'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://djangoproject.org" rel="nofollow">djangoproject.org</a>')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(urlize('info@djangoproject.org'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="mailto:info@djangoproject.org">info@djangoproject.org</a>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
# Check urlize with https addresses
|
|
|
|
self.assertEqual(urlize('https://google.com'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="https://google.com" rel="nofollow">https://google.com</a>')
|
2012-01-08 02:39:14 +08:00
|
|
|
|
2012-01-08 02:15:28 +08:00
|
|
|
# Check urlize doesn't overquote already quoted urls - see #9655
|
2013-07-28 16:05:39 +08:00
|
|
|
# The teststring is the urlquoted version of 'http://hi.baidu.com/重新开始'
|
|
|
|
self.assertEqual(urlize('http://hi.baidu.com/%E9%87%8D%E6%96%B0%E5%BC%80%E5%A7%8B'),
|
|
|
|
'<a href="http://hi.baidu.com/%E9%87%8D%E6%96%B0%E5%BC%80%E5%A7%8B" rel="nofollow">'
|
|
|
|
'http://hi.baidu.com/%E9%87%8D%E6%96%B0%E5%BC%80%E5%A7%8B</a>')
|
2012-01-08 02:15:28 +08:00
|
|
|
self.assertEqual(urlize('www.mystore.com/30%OffCoupons!'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://www.mystore.com/30%25OffCoupons!" rel="nofollow">'
|
|
|
|
'www.mystore.com/30%OffCoupons!</a>')
|
2012-01-08 02:15:28 +08:00
|
|
|
self.assertEqual(urlize('http://en.wikipedia.org/wiki/Caf%C3%A9'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://en.wikipedia.org/wiki/Caf%C3%A9" rel="nofollow">'
|
|
|
|
'http://en.wikipedia.org/wiki/Caf%C3%A9</a>')
|
2012-01-08 02:15:28 +08:00
|
|
|
self.assertEqual(urlize('http://en.wikipedia.org/wiki/Café'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://en.wikipedia.org/wiki/Caf%C3%A9" rel="nofollow">'
|
|
|
|
'http://en.wikipedia.org/wiki/Café</a>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2012-01-09 03:42:14 +08:00
|
|
|
# Check urlize keeps balanced parentheses - see #11911
|
|
|
|
self.assertEqual(urlize('http://en.wikipedia.org/wiki/Django_(web_framework)'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://en.wikipedia.org/wiki/Django_(web_framework)" rel="nofollow">'
|
|
|
|
'http://en.wikipedia.org/wiki/Django_(web_framework)</a>')
|
2012-01-09 03:42:14 +08:00
|
|
|
self.assertEqual(urlize('(see http://en.wikipedia.org/wiki/Django_(web_framework))'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'(see <a href="http://en.wikipedia.org/wiki/Django_(web_framework)" rel="nofollow">'
|
|
|
|
'http://en.wikipedia.org/wiki/Django_(web_framework)</a>)')
|
2012-01-09 03:42:14 +08:00
|
|
|
|
2012-01-08 17:51:36 +08:00
|
|
|
# Check urlize adds nofollow properly - see #12183
|
|
|
|
self.assertEqual(urlize('foo@bar.com or www.bar.com'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="mailto:foo@bar.com">foo@bar.com</a> or '
|
|
|
|
'<a href="http://www.bar.com" rel="nofollow">www.bar.com</a>')
|
2012-01-08 17:51:36 +08:00
|
|
|
|
2012-01-08 02:39:14 +08:00
|
|
|
# Check urlize handles IDN correctly - see #13704
|
|
|
|
self.assertEqual(urlize('http://c✶.ws'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://xn--c-lgq.ws" rel="nofollow">http://c✶.ws</a>')
|
2012-01-08 02:39:14 +08:00
|
|
|
self.assertEqual(urlize('www.c✶.ws'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://www.xn--c-lgq.ws" rel="nofollow">www.c✶.ws</a>')
|
2012-01-08 02:39:14 +08:00
|
|
|
self.assertEqual(urlize('c✶.org'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://xn--c-lgq.org" rel="nofollow">c✶.org</a>')
|
2012-01-08 02:39:14 +08:00
|
|
|
self.assertEqual(urlize('info@c✶.org'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="mailto:info@xn--c-lgq.org">info@c✶.org</a>')
|
2012-01-08 02:39:14 +08:00
|
|
|
|
2012-01-08 23:43:32 +08:00
|
|
|
# Check urlize doesn't highlight malformed URIs - see #16395
|
|
|
|
self.assertEqual(urlize('http:///www.google.com'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'http:///www.google.com')
|
2012-01-08 23:43:32 +08:00
|
|
|
self.assertEqual(urlize('http://.google.com'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'http://.google.com')
|
2012-01-08 23:43:32 +08:00
|
|
|
self.assertEqual(urlize('http://@foo.com'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'http://@foo.com')
|
2012-01-08 23:43:32 +08:00
|
|
|
|
2012-01-09 00:08:43 +08:00
|
|
|
# Check urlize accepts more TLDs - see #16656
|
|
|
|
self.assertEqual(urlize('usa.gov'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="http://usa.gov" rel="nofollow">usa.gov</a>')
|
2012-01-09 00:08:43 +08:00
|
|
|
|
2012-02-05 00:05:48 +08:00
|
|
|
# Check urlize don't crash on invalid email with dot-starting domain - see #17592
|
|
|
|
self.assertEqual(urlize('email@.stream.ru'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'email@.stream.ru')
|
2012-02-05 00:05:48 +08:00
|
|
|
|
2012-04-12 01:49:22 +08:00
|
|
|
# Check urlize accepts uppercased URL schemes - see #18071
|
|
|
|
self.assertEqual(urlize('HTTPS://github.com/'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'<a href="https://github.com/" rel="nofollow">HTTPS://github.com/</a>')
|
2012-04-12 01:49:22 +08:00
|
|
|
|
2012-07-17 22:38:04 +08:00
|
|
|
# Check urlize trims trailing period when followed by parenthesis - see #18644
|
|
|
|
self.assertEqual(urlize('(Go to http://www.example.com/foo.)'),
|
2012-10-31 18:58:14 +08:00
|
|
|
'(Go to <a href="http://www.example.com/foo" rel="nofollow">http://www.example.com/foo</a>.)')
|
|
|
|
|
2013-05-19 19:39:21 +08:00
|
|
|
# Check urlize handles brackets properly (#19070)
|
2012-10-31 18:58:14 +08:00
|
|
|
self.assertEqual(urlize('[see www.example.com]'),
|
|
|
|
'[see <a href="http://www.example.com" rel="nofollow">www.example.com</a>]' )
|
2012-12-03 20:08:22 +08:00
|
|
|
self.assertEqual(urlize('see test[at[example.com'),
|
|
|
|
'see <a href="http://test[at[example.com" rel="nofollow">test[at[example.com</a>' )
|
2013-05-19 19:39:21 +08:00
|
|
|
self.assertEqual(urlize('[http://168.192.0.1](http://168.192.0.1)'),
|
2013-05-19 22:23:14 +08:00
|
|
|
'[<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>')
|
2012-12-03 20:08:22 +08:00
|
|
|
|
2013-04-01 21:37:37 +08:00
|
|
|
# Check urlize works with IPv4/IPv6 addresses
|
|
|
|
self.assertEqual(urlize('http://192.168.0.15/api/9'),
|
|
|
|
'<a href="http://192.168.0.15/api/9" rel="nofollow">http://192.168.0.15/api/9</a>')
|
|
|
|
self.assertEqual(urlize('http://[2001:db8:cafe::2]/api/9'),
|
|
|
|
'<a href="http://[2001:db8:cafe::2]/api/9" rel="nofollow">http://[2001:db8:cafe::2]/api/9</a>')
|
2012-07-17 22:38:04 +08:00
|
|
|
|
2013-09-23 19:07:26 +08:00
|
|
|
# Check urlize correctly include quotation marks in links - #20364
|
|
|
|
self.assertEqual(urlize('before "hi@example.com" afterwards'),
|
|
|
|
u'before "<a href="mailto:hi@example.com">hi@example.com</a>" afterwards')
|
|
|
|
self.assertEqual(urlize('before hi@example.com" afterwards'),
|
|
|
|
u'before <a href="mailto:hi@example.com">hi@example.com</a>" afterwards')
|
|
|
|
self.assertEqual(urlize('before "hi@example.com afterwards'),
|
|
|
|
u'before "<a href="mailto:hi@example.com">hi@example.com</a> afterwards')
|
|
|
|
self.assertEqual(urlize('before \'hi@example.com\' afterwards'),
|
|
|
|
u'before \'<a href="mailto:hi@example.com">hi@example.com</a>\' afterwards')
|
|
|
|
self.assertEqual(urlize('before hi@example.com\' afterwards'),
|
|
|
|
u'before <a href="mailto:hi@example.com">hi@example.com</a>\' afterwards')
|
|
|
|
self.assertEqual(urlize('before \'hi@example.com afterwards'),
|
|
|
|
u'before \'<a href="mailto:hi@example.com">hi@example.com</a> afterwards')
|
|
|
|
|
|
|
|
# Check urlize copes with commas following URLs in quotes - see #20364
|
|
|
|
self.assertEqual(urlize('Email us at "hi@example.com", or phone us at +xx.yy'),
|
|
|
|
'Email us at "<a href="mailto:hi@example.com">hi@example.com</a>", or phone us at +xx.yy')
|
|
|
|
|
2010-09-28 16:19:04 +08:00
|
|
|
def test_wordcount(self):
|
|
|
|
self.assertEqual(wordcount(''), 0)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(wordcount('oneword'), 1)
|
|
|
|
self.assertEqual(wordcount('lots of words'), 3)
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(wordwrap('this is a long paragraph of text that '\
|
|
|
|
'really needs to be wrapped I\'m afraid', 14),
|
|
|
|
"this is a long\nparagraph of\ntext that\nreally needs\nto be "\
|
|
|
|
"wrapped\nI'm afraid")
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(wordwrap('this is a short paragraph of text.\n '\
|
|
|
|
'But this line should be indented', 14),
|
|
|
|
'this is a\nshort\nparagraph of\ntext.\n But this\nline '\
|
|
|
|
'should be\nindented')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(wordwrap('this is a short paragraph of text.\n '\
|
|
|
|
'But this line should be indented',15), 'this is a short\n'\
|
|
|
|
'paragraph of\ntext.\n But this line\nshould be\nindented')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_rjust(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(ljust('test', 10), 'test ')
|
|
|
|
self.assertEqual(ljust('test', 3), 'test')
|
|
|
|
self.assertEqual(rjust('test', 10), ' test')
|
|
|
|
self.assertEqual(rjust('test', 3), 'test')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_center(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(center('test', 6), ' test ')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_cut(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(cut('a string to be mangled', 'a'),
|
|
|
|
' string to be mngled')
|
|
|
|
self.assertEqual(cut('a string to be mangled', 'ng'),
|
|
|
|
'a stri to be maled')
|
|
|
|
self.assertEqual(cut('a string to be mangled', 'strings'),
|
|
|
|
'a string to be mangled')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_force_escape(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
escaped = force_escape('<some html & special characters > here')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(
|
2012-06-08 00:08:47 +08:00
|
|
|
escaped, '<some html & special characters > here')
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(escaped, SafeData)
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(
|
2012-06-08 00:08:47 +08:00
|
|
|
force_escape('<some html & special characters > here ĐÅ€£'),
|
|
|
|
'<some html & special characters > here'\
|
|
|
|
' \u0110\xc5\u20ac\xa3')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_linebreaks(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(linebreaks_filter('line 1'), '<p>line 1</p>')
|
|
|
|
self.assertEqual(linebreaks_filter('line 1\nline 2'),
|
|
|
|
'<p>line 1<br />line 2</p>')
|
|
|
|
self.assertEqual(linebreaks_filter('line 1\rline 2'),
|
|
|
|
'<p>line 1<br />line 2</p>')
|
|
|
|
self.assertEqual(linebreaks_filter('line 1\r\nline 2'),
|
|
|
|
'<p>line 1<br />line 2</p>')
|
2011-07-29 18:22:25 +08:00
|
|
|
|
|
|
|
def test_linebreaksbr(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(linebreaksbr('line 1\nline 2'),
|
|
|
|
'line 1<br />line 2')
|
|
|
|
self.assertEqual(linebreaksbr('line 1\rline 2'),
|
|
|
|
'line 1<br />line 2')
|
|
|
|
self.assertEqual(linebreaksbr('line 1\r\nline 2'),
|
|
|
|
'line 1<br />line 2')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_removetags(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(removetags('some <b>html</b> with <script>alert'\
|
|
|
|
'("You smell")</script> disallowed <img /> tags', 'script img'),
|
|
|
|
'some <b>html</b> with alert("You smell") disallowed tags')
|
|
|
|
self.assertEqual(striptags('some <b>html</b> with <script>alert'\
|
|
|
|
'("You smell")</script> disallowed <img /> tags'),
|
|
|
|
'some html with alert("You smell") disallowed tags')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_dictsort(self):
|
|
|
|
sorted_dicts = dictsort([{'age': 23, 'name': 'Barbara-Ann'},
|
|
|
|
{'age': 63, 'name': 'Ra Ra Rasputin'},
|
|
|
|
{'name': 'Jonny B Goode', 'age': 18}], 'age')
|
|
|
|
|
|
|
|
self.assertEqual([sorted(dict.items()) for dict in sorted_dicts],
|
|
|
|
[[('age', 18), ('name', 'Jonny B Goode')],
|
|
|
|
[('age', 23), ('name', 'Barbara-Ann')],
|
|
|
|
[('age', 63), ('name', 'Ra Ra Rasputin')]])
|
|
|
|
|
2012-01-15 10:01:21 +08:00
|
|
|
# If it gets passed a list of something else different from
|
|
|
|
# dictionaries it should fail silently
|
|
|
|
self.assertEqual(dictsort([1, 2, 3], 'age'), '')
|
|
|
|
self.assertEqual(dictsort('Hello!', 'age'), '')
|
|
|
|
self.assertEqual(dictsort({'a': 1}, 'age'), '')
|
|
|
|
self.assertEqual(dictsort(1, 'age'), '')
|
|
|
|
|
2010-09-28 16:19:04 +08:00
|
|
|
def test_dictsortreversed(self):
|
|
|
|
sorted_dicts = dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},
|
|
|
|
{'age': 63, 'name': 'Ra Ra Rasputin'},
|
|
|
|
{'name': 'Jonny B Goode', 'age': 18}],
|
|
|
|
'age')
|
|
|
|
|
|
|
|
self.assertEqual([sorted(dict.items()) for dict in sorted_dicts],
|
|
|
|
[[('age', 63), ('name', 'Ra Ra Rasputin')],
|
|
|
|
[('age', 23), ('name', 'Barbara-Ann')],
|
|
|
|
[('age', 18), ('name', 'Jonny B Goode')]])
|
|
|
|
|
2012-01-15 10:01:21 +08:00
|
|
|
# If it gets passed a list of something else different from
|
|
|
|
# dictionaries it should fail silently
|
|
|
|
self.assertEqual(dictsortreversed([1, 2, 3], 'age'), '')
|
|
|
|
self.assertEqual(dictsortreversed('Hello!', 'age'), '')
|
|
|
|
self.assertEqual(dictsortreversed({'a': 1}, 'age'), '')
|
|
|
|
self.assertEqual(dictsortreversed(1, 'age'), '')
|
|
|
|
|
2010-09-28 16:19:04 +08:00
|
|
|
def test_first(self):
|
|
|
|
self.assertEqual(first([0,1,2]), 0)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(first(''), '')
|
|
|
|
self.assertEqual(first('test'), 't')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_join(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(join([0,1,2], 'glue'), '0glue1glue2')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_length(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(length('1234'), 4)
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(length([1,2,3,4]), 4)
|
|
|
|
self.assertEqual(length_is([], 0), True)
|
|
|
|
self.assertEqual(length_is([], 1), False)
|
|
|
|
self.assertEqual(length_is('a', 1), True)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(length_is('a', 10), False)
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_slice(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(slice_filter('abcdefg', '0'), '')
|
|
|
|
self.assertEqual(slice_filter('abcdefg', '1'), 'a')
|
|
|
|
self.assertEqual(slice_filter('abcdefg', '-1'), 'abcdef')
|
|
|
|
self.assertEqual(slice_filter('abcdefg', '1:2'), 'b')
|
|
|
|
self.assertEqual(slice_filter('abcdefg', '1:3'), 'bc')
|
|
|
|
self.assertEqual(slice_filter('abcdefg', '0::2'), 'aceg')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_unordered_list(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(unordered_list(['item 1', 'item 2']),
|
|
|
|
'\t<li>item 1</li>\n\t<li>item 2</li>')
|
|
|
|
self.assertEqual(unordered_list(['item 1', ['item 1.1']]),
|
|
|
|
'\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1</li>\n\t</ul>\n\t</li>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
self.assertEqual(
|
2012-06-08 00:08:47 +08:00
|
|
|
unordered_list(['item 1', ['item 1.1', 'item1.2'], 'item 2']),
|
|
|
|
'\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1</li>\n\t\t<li>item1.2'\
|
|
|
|
'</li>\n\t</ul>\n\t</li>\n\t<li>item 2</li>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
self.assertEqual(
|
2012-06-08 00:08:47 +08:00
|
|
|
unordered_list(['item 1', ['item 1.1', ['item 1.1.1',
|
|
|
|
['item 1.1.1.1']]]]),
|
|
|
|
'\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1\n\t\t<ul>\n\t\t\t<li>'\
|
|
|
|
'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'\
|
|
|
|
'</ul>\n\t\t\t</li>\n\t\t</ul>\n\t\t</li>\n\t</ul>\n\t</li>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
self.assertEqual(unordered_list(
|
|
|
|
['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]),
|
2012-06-08 00:08:47 +08:00
|
|
|
'\t<li>States\n\t<ul>\n\t\t<li>Kansas\n\t\t<ul>\n\t\t\t<li>'\
|
|
|
|
'Lawrence</li>\n\t\t\t<li>Topeka</li>\n\t\t</ul>\n\t\t</li>'\
|
|
|
|
'\n\t\t<li>Illinois</li>\n\t</ul>\n\t</li>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2010-09-28 16:19:04 +08:00
|
|
|
class ULItem(object):
|
|
|
|
def __init__(self, title):
|
2013-09-04 02:22:21 +08:00
|
|
|
self.title = title
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return 'ulitem-%s' % str(self.title)
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
a = ULItem('a')
|
|
|
|
b = ULItem('b')
|
|
|
|
self.assertEqual(unordered_list([a,b]),
|
2012-06-08 00:08:47 +08:00
|
|
|
'\t<li>ulitem-a</li>\n\t<li>ulitem-b</li>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
# Old format for unordered lists should still work
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(unordered_list(['item 1', []]), '\t<li>item 1</li>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(unordered_list(['item 1', [['item 1.1', []]]]),
|
|
|
|
'\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1</li>\n\t</ul>\n\t</li>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(unordered_list(['item 1', [['item 1.1', []],
|
|
|
|
['item 1.2', []]]]), '\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1'\
|
|
|
|
'</li>\n\t\t<li>item 1.2</li>\n\t</ul>\n\t</li>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
self.assertEqual(unordered_list(['States', [['Kansas', [['Lawrence',
|
2012-06-08 00:08:47 +08:00
|
|
|
[]], ['Topeka', []]]], ['Illinois', []]]]), '\t<li>States\n\t'\
|
|
|
|
'<ul>\n\t\t<li>Kansas\n\t\t<ul>\n\t\t\t<li>Lawrence</li>'\
|
|
|
|
'\n\t\t\t<li>Topeka</li>\n\t\t</ul>\n\t\t</li>\n\t\t<li>'\
|
|
|
|
'Illinois</li>\n\t</ul>\n\t</li>')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_add(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(add('1', '2'), 3)
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_get_digit(self):
|
|
|
|
self.assertEqual(get_digit(123, 1), 3)
|
|
|
|
self.assertEqual(get_digit(123, 2), 2)
|
|
|
|
self.assertEqual(get_digit(123, 3), 1)
|
|
|
|
self.assertEqual(get_digit(123, 4), 0)
|
|
|
|
self.assertEqual(get_digit(123, 0), 123)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(get_digit('xyz', 0), 'xyz')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_date(self):
|
|
|
|
# real testing of date() is in dateformat.py
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(date(datetime.datetime(2005, 12, 29), "d F Y"),
|
|
|
|
'29 December 2005')
|
|
|
|
self.assertEqual(date(datetime.datetime(2005, 12, 29), r'jS \o\f F'),
|
|
|
|
'29th of December')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_time(self):
|
|
|
|
# real testing of time() is done in dateformat.py
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(time(datetime.time(13), "h"), '01')
|
|
|
|
self.assertEqual(time(datetime.time(0), "h"), '12')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_timesince(self):
|
|
|
|
# real testing is done in timesince.py, where we can provide our own 'now'
|
2013-05-18 19:58:45 +08:00
|
|
|
# NOTE: \xa0 avoids wrapping between value and unit
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(
|
2011-10-29 23:28:44 +08:00
|
|
|
timesince_filter(datetime.datetime.now() - datetime.timedelta(1)),
|
2013-05-18 19:58:45 +08:00
|
|
|
'1\xa0day')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
self.assertEqual(
|
2011-10-29 23:28:44 +08:00
|
|
|
timesince_filter(datetime.datetime(2005, 12, 29),
|
|
|
|
datetime.datetime(2005, 12, 30)),
|
2013-05-18 19:58:45 +08:00
|
|
|
'1\xa0day')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_timeuntil(self):
|
2013-05-18 19:58:45 +08:00
|
|
|
# NOTE: \xa0 avoids wrapping between value and unit
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(
|
2011-11-18 21:01:06 +08:00
|
|
|
timeuntil_filter(datetime.datetime.now() + datetime.timedelta(1, 1)),
|
2013-05-18 19:58:45 +08:00
|
|
|
'1\xa0day')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2011-10-29 23:28:44 +08:00
|
|
|
self.assertEqual(
|
|
|
|
timeuntil_filter(datetime.datetime(2005, 12, 30),
|
|
|
|
datetime.datetime(2005, 12, 29)),
|
2013-05-18 19:58:45 +08:00
|
|
|
'1\xa0day')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_default(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(default("val", "default"), 'val')
|
|
|
|
self.assertEqual(default(None, "default"), 'default')
|
|
|
|
self.assertEqual(default('', "default"), 'default')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_if_none(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(default_if_none("val", "default"), 'val')
|
|
|
|
self.assertEqual(default_if_none(None, "default"), 'default')
|
|
|
|
self.assertEqual(default_if_none('', "default"), '')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_divisibleby(self):
|
|
|
|
self.assertEqual(divisibleby(4, 2), True)
|
|
|
|
self.assertEqual(divisibleby(4, 3), False)
|
|
|
|
|
|
|
|
def test_yesno(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(yesno(True), 'yes')
|
|
|
|
self.assertEqual(yesno(False), 'no')
|
|
|
|
self.assertEqual(yesno(None), 'maybe')
|
|
|
|
self.assertEqual(yesno(True, 'certainly,get out of town,perhaps'),
|
|
|
|
'certainly')
|
|
|
|
self.assertEqual(yesno(False, 'certainly,get out of town,perhaps'),
|
|
|
|
'get out of town')
|
|
|
|
self.assertEqual(yesno(None, 'certainly,get out of town,perhaps'),
|
|
|
|
'perhaps')
|
|
|
|
self.assertEqual(yesno(None, 'certainly,get out of town'),
|
|
|
|
'get out of town')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_filesizeformat(self):
|
2013-05-18 19:58:45 +08:00
|
|
|
# NOTE: \xa0 avoids wrapping between value and unit
|
|
|
|
self.assertEqual(filesizeformat(1023), '1023\xa0bytes')
|
|
|
|
self.assertEqual(filesizeformat(1024), '1.0\xa0KB')
|
|
|
|
self.assertEqual(filesizeformat(10*1024), '10.0\xa0KB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024-1), '1024.0\xa0KB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024), '1.0\xa0MB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024*50), '50.0\xa0MB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024*1024-1), '1024.0\xa0MB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024*1024), '1.0\xa0GB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024*1024*1024), '1.0\xa0TB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024*1024*1024*1024), '1.0\xa0PB')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(filesizeformat(1024*1024*1024*1024*1024*2000),
|
2013-05-18 19:58:45 +08:00
|
|
|
'2000.0\xa0PB')
|
|
|
|
self.assertEqual(filesizeformat(complex(1,-1)), '0\xa0bytes')
|
|
|
|
self.assertEqual(filesizeformat(""), '0\xa0bytes')
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(filesizeformat("\N{GREEK SMALL LETTER ALPHA}"),
|
2013-05-18 19:58:45 +08:00
|
|
|
'0\xa0bytes')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_pluralize(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(pluralize(1), '')
|
|
|
|
self.assertEqual(pluralize(0), 's')
|
|
|
|
self.assertEqual(pluralize(2), 's')
|
|
|
|
self.assertEqual(pluralize([1]), '')
|
|
|
|
self.assertEqual(pluralize([]), 's')
|
|
|
|
self.assertEqual(pluralize([1,2,3]), 's')
|
|
|
|
self.assertEqual(pluralize(1,'es'), '')
|
|
|
|
self.assertEqual(pluralize(0,'es'), 'es')
|
|
|
|
self.assertEqual(pluralize(2,'es'), 'es')
|
|
|
|
self.assertEqual(pluralize(1,'y,ies'), 'y')
|
|
|
|
self.assertEqual(pluralize(0,'y,ies'), 'ies')
|
|
|
|
self.assertEqual(pluralize(2,'y,ies'), 'ies')
|
|
|
|
self.assertEqual(pluralize(0,'y,ies,error'), '')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_phone2numeric(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(phone2numeric_filter('0800 flowers'), '0800 3569377')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
|
|
|
def test_non_string_input(self):
|
|
|
|
# Filters shouldn't break if passed non-strings
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(addslashes(123), '123')
|
|
|
|
self.assertEqual(linenumbers(123), '1. 123')
|
|
|
|
self.assertEqual(lower(123), '123')
|
|
|
|
self.assertEqual(make_list(123), ['1', '2', '3'])
|
|
|
|
self.assertEqual(slugify(123), '123')
|
|
|
|
self.assertEqual(title(123), '123')
|
|
|
|
self.assertEqual(truncatewords(123, 2), '123')
|
|
|
|
self.assertEqual(upper(123), '123')
|
|
|
|
self.assertEqual(urlencode(123), '123')
|
|
|
|
self.assertEqual(urlize(123), '123')
|
|
|
|
self.assertEqual(urlizetrunc(123, 1), '123')
|
2010-09-28 16:19:04 +08:00
|
|
|
self.assertEqual(wordcount(123), 1)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(wordwrap(123, 2), '123')
|
|
|
|
self.assertEqual(ljust('123', 4), '123 ')
|
|
|
|
self.assertEqual(rjust('123', 4), ' 123')
|
|
|
|
self.assertEqual(center('123', 5), ' 123 ')
|
|
|
|
self.assertEqual(center('123', 6), ' 123 ')
|
|
|
|
self.assertEqual(cut(123, '2'), '13')
|
|
|
|
self.assertEqual(escape(123), '123')
|
|
|
|
self.assertEqual(linebreaks_filter(123), '<p>123</p>')
|
|
|
|
self.assertEqual(linebreaksbr(123), '123')
|
|
|
|
self.assertEqual(removetags(123, 'a'), '123')
|
|
|
|
self.assertEqual(striptags(123), '123')
|
2010-09-28 16:19:04 +08:00
|
|
|
|
2013-05-31 02:23:14 +08:00
|
|
|
|
|
|
|
class DefaultFiltersI18NTests(TransRealMixin, TestCase):
|
|
|
|
|
|
|
|
def test_localized_filesizeformat(self):
|
|
|
|
# NOTE: \xa0 avoids wrapping between value and unit
|
2013-08-17 02:12:10 +08:00
|
|
|
with self.settings(USE_L10N=True), translation.override('de', deactivate=True):
|
|
|
|
self.assertEqual(filesizeformat(1023), '1023\xa0Bytes')
|
|
|
|
self.assertEqual(filesizeformat(1024), '1,0\xa0KB')
|
|
|
|
self.assertEqual(filesizeformat(10*1024), '10,0\xa0KB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024-1), '1024,0\xa0KB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024), '1,0\xa0MB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024*50), '50,0\xa0MB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024*1024-1), '1024,0\xa0MB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024*1024), '1,0\xa0GB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024*1024*1024), '1,0\xa0TB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024*1024*1024*1024),
|
|
|
|
'1,0\xa0PB')
|
|
|
|
self.assertEqual(filesizeformat(1024*1024*1024*1024*1024*2000),
|
|
|
|
'2000,0\xa0PB')
|
|
|
|
self.assertEqual(filesizeformat(complex(1,-1)), '0\xa0Bytes')
|
|
|
|
self.assertEqual(filesizeformat(""), '0\xa0Bytes')
|
|
|
|
self.assertEqual(filesizeformat("\N{GREEK SMALL LETTER ALPHA}"),
|
|
|
|
'0\xa0Bytes')
|