Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/template_tests/filter_tests/test_slice.py
blob: 6d67de86618d1a5cb28cdbf91a955568ac39e997 (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
from django.template.defaultfilters import slice_filter
from django.test import SimpleTestCase
from django.utils.safestring import mark_safe

from ..utils import setup


class SliceTests(SimpleTestCase):

    @setup({'slice01': '{{ a|slice:"1:3" }} {{ b|slice:"1:3" }}'})
    def test_slice01(self):
        output = self.engine.render_to_string('slice01', {'a': 'a&b', 'b': mark_safe('a&b')})
        self.assertEqual(output, '&b &b')

    @setup({'slice02': '{% autoescape off %}{{ a|slice:"1:3" }} {{ b|slice:"1:3" }}{% endautoescape %}'})
    def test_slice02(self):
        output = self.engine.render_to_string('slice02', {'a': 'a&b', 'b': mark_safe('a&b')})
        self.assertEqual(output, '&b &b')


class FunctionTests(SimpleTestCase):

    def test_zero_length(self):
        self.assertEqual(slice_filter('abcdefg', '0'), '')

    def test_index(self):
        self.assertEqual(slice_filter('abcdefg', '1'), 'a')

    def test_negative_index(self):
        self.assertEqual(slice_filter('abcdefg', '-1'), 'abcdef')

    def test_range(self):
        self.assertEqual(slice_filter('abcdefg', '1:2'), 'b')

    def test_range_multiple(self):
        self.assertEqual(slice_filter('abcdefg', '1:3'), 'bc')

    def test_range_step(self):
        self.assertEqual(slice_filter('abcdefg', '0::2'), 'aceg')