summaryrefslogtreecommitdiff
path: root/tests/test_utils.py
blob: 2400fb8bd3b7207db2e5ee80e5f5ad22e7b1fb3b (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-
"""
    jinja2.testsuite.utils
    ~~~~~~~~~~~~~~~~~~~~~~

    Tests utilities jinja uses.

    :copyright: (c) 2017 by the Jinja Team.
    :license: BSD, see LICENSE for more details.
"""
import gc

import pytest

import pickle

from jinja2.utils import LRUCache, escape, object_type_repr, urlize, \
     select_autoescape


@pytest.mark.utils
@pytest.mark.lrucache
class TestLRUCache(object):

    def test_simple(self):
        d = LRUCache(3)
        d["a"] = 1
        d["b"] = 2
        d["c"] = 3
        d["a"]
        d["d"] = 4
        assert len(d) == 3
        assert 'a' in d and 'c' in d and 'd' in d and 'b' not in d

    def test_pickleable(self):
        cache = LRUCache(2)
        cache["foo"] = 42
        cache["bar"] = 23
        cache["foo"]

        for protocol in range(3):
            copy = pickle.loads(pickle.dumps(cache, protocol))
            assert copy.capacity == cache.capacity
            assert copy._mapping == cache._mapping
            assert copy._queue == cache._queue


@pytest.mark.utils
@pytest.mark.helpers
class TestHelpers(object):

    def test_object_type_repr(self):
        class X(object):
            pass
        assert object_type_repr(42) == 'int object'
        assert object_type_repr([]) == 'list object'
        assert object_type_repr(X()) == 'test_utils.X object'
        assert object_type_repr(None) == 'None'
        assert object_type_repr(Ellipsis) == 'Ellipsis'

    def test_autoescape_select(self):
        func = select_autoescape(
            enabled_extensions=('html', '.htm'),
            disabled_extensions=('txt',),
            default_for_string='STRING',
            default='NONE',
        )

        assert func(None) == 'STRING'
        assert func('unknown.foo') == 'NONE'
        assert func('foo.html') == True
        assert func('foo.htm') == True
        assert func('foo.txt') == False
        assert func('FOO.HTML') == True
        assert func('FOO.TXT') == False


@pytest.mark.utils
@pytest.mark.markupleak
@pytest.mark.skipif(hasattr(escape, 'func_code'),
                    reason='this test only tests the c extension')
class TestMarkupLeak(object):

    def test_markup_leaks(self):
        counts = set()
        for count in range(20):
            for item in range(1000):
                escape("foo")
                escape("<foo>")
                escape(u"foo")
                escape(u"<foo>")
            counts.add(len(gc.get_objects()))
        assert len(counts) == 1, 'ouch, c extension seems to leak objects'


@pytest.mark.utils
@pytest.mark.escapeUrlizeTarget
class TestEscapeUrlizeTarget(object):
    def test_escape_urlize_target(self):
        url = "http://example.org"
        target = "<script>"
        assert urlize(url, target=target) == ('<a href="http://example.org"'
                                              ' target="&lt;script&gt;">'
                                              'http://example.org</a>')