summaryrefslogtreecommitdiff
path: root/pecan/tests/test_templating.py
blob: 567a6b821f51121bbce840d05b9a508b47b073d1 (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
from unittest import TestCase

from pecan.templating import RendererFactory, format_line_context

import tempfile


class TestTemplate(TestCase):
    def setUp(self):
        self.rf = RendererFactory()

    def test_available(self):
        self.assertTrue(self.rf.available('json'))
        self.assertFalse(self.rf.available('badrenderer'))

    def test_create_bad(self):
        self.assertEqual(self.rf.get('doesnotexist', '/'), None)

    def test_extra_vars(self):
        extra_vars = self.rf.extra_vars
        self.assertEqual(extra_vars.make_ns({}), {})

        extra_vars.update({'foo': 1})
        self.assertEqual(extra_vars.make_ns({}), {'foo': 1})

    def test_update_extra_vars(self):
        extra_vars = self.rf.extra_vars
        extra_vars.update({'foo': 1})

        self.assertEqual(extra_vars.make_ns({'bar': 2}), {'foo': 1, 'bar': 2})
        self.assertEqual(extra_vars.make_ns({'foo': 2}), {'foo': 2})


class TestTemplateLineFormat(TestCase):

    def setUp(self):
        self.f = tempfile.NamedTemporaryFile()

    def tearDown(self):
        del self.f

    def test_format_line_context(self):
        for i in range(11):
            self.f.write('Testing Line %d\n' % i)
        self.f.flush()

        assert format_line_context(self.f.name, 0).count('Testing Line') == 10