Skip to content

Commit 9a45d28

Browse files
committed
Improve attribution template validation tests
* add more template tests for corner cases * add validation for all builtin templates Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent c4cc41e commit 9a45d28

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

tests/test_attrib.py

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from __future__ import print_function
1919
from __future__ import unicode_literals
2020

21+
import os
2122
import unittest
2223

2324
from testing_utils import get_test_loc
@@ -28,26 +29,57 @@
2829

2930
class AttribTest(unittest.TestCase):
3031

31-
def test_check_template(self):
32-
assert attrib.check_template('template_string') == None
33-
assert attrib.check_template('{{template_string') == (1,
34-
"unexpected end of template, expected 'end of print statement'.",)
35-
with open(get_test_loc('attrib_gen/test.template')) as tmpl:
36-
template = tmpl.read()
37-
assert attrib.check_template(template) == None
32+
def test_check_template_simple_valid_returns_None(self):
33+
expected = None
34+
assert expected == attrib.check_template('template_string')
3835

39-
def test_check_template_default_is_valid(self):
40-
with open(attrib.default_template) as tmpl:
41-
template = tmpl.read()
42-
assert attrib.check_template(template) == None
36+
def test_check_template_complex_valid_returns_None(self):
37+
template = '''
38+
{% for about in abouts -%}
39+
{{ about.name.value }}: {{ about.version.value }}
40+
{% for res in about.about_resource.value -%}
41+
resource: {{ res }}
42+
{% endfor -%}
43+
{% endfor -%}'''
44+
expected = None
45+
assert expected == attrib.check_template(template)
46+
47+
def test_check_template_complex_invalid_returns_error(self):
48+
template = '''
49+
{% for about in abouts -%}
50+
{{ about.name.value }}: {{ about.version.value }}
51+
{% for res in about.about_ressdsdsdsdsdsdource.value -%}
52+
resource: {{] res }}
53+
{% endfor -%}
54+
{% endfor -%}'''
55+
expected = (5, "unexpected ']'")
56+
assert expected == attrib.check_template(template)
57+
58+
def test_check_template_invalid_return_error_lineno_and_message(self):
59+
expected = 1, "unexpected end of template, expected 'end of print statement'."
60+
assert expected == attrib.check_template('{{template_string')
61+
62+
def test_check_template_all_builtin_templates_are_valid(self):
63+
builtin_templates_dir = os.path.dirname(attrib.default_template)
64+
for template in os.listdir(builtin_templates_dir):
65+
template = os.path.join(builtin_templates_dir, template)
66+
with open(template) as tmpl:
67+
template = tmpl.read()
68+
assert None == attrib.check_template(template)
4369

4470
def test_generate(self):
45-
expected = (u'Apache HTTP Server: 2.4.3\n'
46-
u'resource: httpd-2.4.3.tar.gz\n')
4771
test_file = get_test_loc('attrib_gen/attrib.ABOUT')
72+
errors, abouts = model.collect_inventory(test_file)
73+
4874
with open(get_test_loc('attrib_gen/test.template')) as tmpl:
4975
template = tmpl.read()
50-
_errors, abouts = model.collect_inventory(test_file)
76+
77+
assert not errors
78+
79+
expected = (
80+
'Apache HTTP Server: 2.4.3\n'
81+
'resource: httpd-2.4.3.tar.gz\n')
82+
5183
result = attrib.generate(abouts, template)
5284
self.assertEqual(expected, result)
5385

0 commit comments

Comments
 (0)