Debian Codegraph @

test/units/_internal/templating/test_template_utilities.py

raw
1 # (c) 2015 Toshio Kuratomi <[email protected]>
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 import unittest
21
22 from ansible._internal._datatag._tags import TrustedAsTemplate
23 from ansible._internal._templating._jinja_bits import AnsibleEnvironment
24 from ansible._internal._templating._engine import TemplateEngine
25
26
27 # These are internal utility functions only needed for templating. They're
28 # algorithmic so good candidates for unit testing by themselves
29
30
31 class TestBackslashEscape(unittest.TestCase):
32
33 test_data = (
34 # Test backslashes in a filter arg are double escaped
35 dict(
36 template=u"{{ 'test2 %s' | format('\\1') }}",
37 expectation=u"test2 \\1",
38 args=dict()
39 ),
40 # Test backslashes inside the jinja2 var itself are double
41 # escaped
42 dict(
43 template=u"Test 2\\3: {{ '\\1 %s' | format('\\2') }}",
44 expectation=u"Test 2\\3: \\1 \\2",
45 args=dict()
46 ),
47 # Test backslashes outside of the jinja2 var are not double
48 # escaped
49 dict(
50 template=u"Test 2\\3: {{ 'test2 %s' | format('\\1') }}; \\done",
51 expectation=u"Test 2\\3: test2 \\1; \\done",
52 args=dict()
53 ),
54 # Test backslashes in a variable sent to a filter are handled
55 dict(
56 template=u"{{ 'test2 %s' | format(var1) }}",
57 expectation=u"test2 \\1",
58 args=dict(var1=u'\\1')
59 ),
60 # Test backslashes in a variable expanded by jinja2 are double
61 # escaped
62 dict(
63 template=u"Test 2\\3: {{ var1 | format('\\2') }}",
64 expectation=u"Test 2\\3: \\1 \\2",
65 args=dict(var1=u'\\1 %s')
66 ),
67 )
68
69 def setUp(self):
70 self.env = AnsibleEnvironment()
71
72 def test_backslash_escaping(self):
73
74 for test in self.test_data:
75 templar = TemplateEngine(None, test['args'])
76 self.assertEqual(templar.template(TrustedAsTemplate().tag(test['template'])), test['expectation'])
77
78
79 class TestCountNewlines(unittest.TestCase):
80
81 def test_zero_length_string(self):
82 self.assertEqual(TemplateEngine._count_newlines_from_end(u''), 0)
83
84 def test_short_string(self):
85 self.assertEqual(TemplateEngine._count_newlines_from_end(u'The quick\n'), 1)
86
87 def test_one_newline(self):
88 self.assertEqual(TemplateEngine._count_newlines_from_end(u'The quick brown fox jumped over the lazy dog' * 1000 + u'\n'), 1)
89
90 def test_multiple_newlines(self):
91 self.assertEqual(TemplateEngine._count_newlines_from_end(u'The quick brown fox jumped over the lazy dog' * 1000 + u'\n\n\n'), 3)
92
93 def test_zero_newlines(self):
94 self.assertEqual(TemplateEngine._count_newlines_from_end(u'The quick brown fox jumped over the lazy dog' * 1000), 0)
95
96 def test_all_newlines(self):
97 self.assertEqual(TemplateEngine._count_newlines_from_end(u'\n' * 10), 10)
98
99 def test_mostly_newlines(self):
100 self.assertEqual(TemplateEngine._count_newlines_from_end(u'The quick brown fox jumped over the lazy dog' + u'\n' * 1000), 1000)