Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/template_tests/filter_tests/test_length_is.py
blob: 9a67a866d4fc484eb774ce788dfd4c8ea7695c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from django.template.defaultfilters import length_is
from django.test import SimpleTestCase

from ..utils import setup


class LengthIsTests(SimpleTestCase):

    @setup({'length_is01': '{% if some_list|length_is:"4" %}Four{% endif %}'})
    def test_length_is01(self):
        output = self.engine.render_to_string('length_is01', {'some_list': ['4', None, True, {}]})
        self.assertEqual(output, 'Four')

    @setup({'length_is02': '{% if some_list|length_is:"4" %}Four{% else %}Not Four{% endif %}'})
    def test_length_is02(self):
        output = self.engine.render_to_string('length_is02', {'some_list': ['4', None, True, {}, 17]})
        self.assertEqual(output, 'Not Four')

    @setup({'length_is03': '{% if mystring|length_is:"4" %}Four{% endif %}'})
    def test_length_is03(self):
        output = self.engine.render_to_string('length_is03', {'mystring': 'word'})
        self.assertEqual(output, 'Four')

    @setup({'length_is04': '{% if mystring|length_is:"4" %}Four{% else %}Not Four{% endif %}'})
    def test_length_is04(self):
        output = self.engine.render_to_string('length_is04', {'mystring': 'Python'})
        self.assertEqual(output, 'Not Four')

    @setup({'length_is05': '{% if mystring|length_is:"4" %}Four{% else %}Not Four{% endif %}'})
    def test_length_is05(self):
        output = self.engine.render_to_string('length_is05', {'mystring': ''})
        self.assertEqual(output, 'Not Four')

    @setup({'length_is06': '{% with var|length as my_length %}{{ my_length }}{% endwith %}'})
    def test_length_is06(self):
        output = self.engine.render_to_string('length_is06', {'var': 'django'})
        self.assertEqual(output, '6')

    # Boolean return value from length_is should not be coerced to a string
    @setup({'length_is07': '{% if "X"|length_is:0 %}Length is 0{% else %}Length not 0{% endif %}'})
    def test_length_is07(self):
        output = self.engine.render_to_string('length_is07', {})
        self.assertEqual(output, 'Length not 0')

    @setup({'length_is08': '{% if "X"|length_is:1 %}Length is 1{% else %}Length not 1{% endif %}'})
    def test_length_is08(self):
        output = self.engine.render_to_string('length_is08', {})
        self.assertEqual(output, 'Length is 1')

    # Invalid uses that should fail silently.
    @setup({'length_is09': '{{ var|length_is:"fish" }}'})
    def test_length_is09(self):
        output = self.engine.render_to_string('length_is09', {'var': 'django'})
        self.assertEqual(output, '')

    @setup({'length_is10': '{{ int|length_is:"1" }}'})
    def test_length_is10(self):
        output = self.engine.render_to_string('length_is10', {'int': 7})
        self.assertEqual(output, '')

    @setup({'length_is11': '{{ none|length_is:"1" }}'})
    def test_length_is11(self):
        output = self.engine.render_to_string('length_is11', {'none': None})
        self.assertEqual(output, '')


class FunctionTests(SimpleTestCase):

    def test_empty_list(self):
        self.assertEqual(length_is([], 0), True)
        self.assertEqual(length_is([], 1), False)

    def test_string(self):
        self.assertEqual(length_is('a', 1), True)
        self.assertEqual(length_is('a', 10), False)