|
18 | 18 | from __future__ import print_function |
19 | 19 | from __future__ import unicode_literals |
20 | 20 |
|
| 21 | +import os |
21 | 22 | import unittest |
22 | 23 |
|
23 | 24 | from testing_utils import get_test_loc |
|
28 | 29 |
|
29 | 30 | class AttribTest(unittest.TestCase): |
30 | 31 |
|
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') |
38 | 35 |
|
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) |
43 | 69 |
|
44 | 70 | def test_generate(self): |
45 | | - expected = (u'Apache HTTP Server: 2.4.3\n' |
46 | | - u'resource: httpd-2.4.3.tar.gz\n') |
47 | 71 | test_file = get_test_loc('attrib_gen/attrib.ABOUT') |
| 72 | + errors, abouts = model.collect_inventory(test_file) |
| 73 | + |
48 | 74 | with open(get_test_loc('attrib_gen/test.template')) as tmpl: |
49 | 75 | 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 | + |
51 | 83 | result = attrib.generate(abouts, template) |
52 | 84 | self.assertEqual(expected, result) |
53 | 85 |
|
|
0 commit comments