forked from nedbat/django_coverage_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_simple.py
272 lines (232 loc) · 8.23 KB
/
test_simple.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# coding: utf8
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/django_coverage_plugin/blob/master/NOTICE.txt
"""Simple tests for django_coverage_plugin."""
from .plugin_test import DjangoPluginTestCase
# 200 Unicode chars: snowman + poo.
UNIUNI = u"\u26C4\U0001F4A9"*100
if isinstance(UNIUNI, str):
UNISTR = UNIUNI
else:
UNISTR = UNIUNI.encode("utf8")
class SimpleTemplateTest(DjangoPluginTestCase):
def test_one_line(self):
self.make_template('Hello')
text = self.run_django_coverage()
self.assertEqual(text, 'Hello')
self.assert_analysis([1])
def test_plain_text(self):
self.make_template('Hello\nWorld\n\nGoodbye')
text = self.run_django_coverage()
self.assertEqual(text, 'Hello\nWorld\n\nGoodbye')
self.assert_analysis([1, 2, 3, 4])
def test_variable(self):
self.make_template("""\
Hello, {{name}}
""")
text = self.run_django_coverage(context={'name': 'John'})
self.assertEqual(text, "Hello, John\n")
self.assert_analysis([1])
def test_variable_on_second_line(self):
self.make_template("""\
Hello,
{{name}}
""")
text = self.run_django_coverage(context={'name': 'John'})
self.assertEqual(text, "Hello,\nJohn\n")
self.assert_analysis([1, 2])
def test_lone_variable(self):
self.make_template("""\
{{name}}
""")
text = self.run_django_coverage(context={'name': 'John'})
self.assertEqual(text, "John\n")
self.assert_analysis([1])
def test_long_text(self):
self.make_template("line\n"*50)
text = self.run_django_coverage()
self.assertEqual(text, "line\n"*50)
self.assert_analysis(list(range(1, 51)))
def test_non_ascii(self):
self.make_template("""\
υηιcσɗє ιѕ тяιcку
{{more}}!
""")
text = self.run_django_coverage(context={'more': u'ɘboɔinU'})
self.assertEqual(text, u'υηιcσɗє ιѕ тяιcку\nɘboɔinU!\n')
self.assert_analysis([1, 2])
self.assertEqual(self.get_html_report(), 100)
self.assertEqual(self.get_xml_report(), 100)
class CommentTest(DjangoPluginTestCase):
def test_simple(self):
self.make_template("""\
First
{% comment %}
ignore this
{% endcomment %}
Last
""")
text = self.run_django_coverage()
self.assertEqual(text, "First\n\nLast\n")
self.assert_analysis([1, 2, 5])
def test_with_stuff_inside(self):
self.make_template("""\
First
{% comment %}
{% if foo %}
{{ foo }}
{% endif %}
{% endcomment %}
Last
""")
text = self.run_django_coverage()
self.assertEqual(text, "First\n\nLast\n")
self.assert_analysis([1, 2, 7])
def test_inline_comment(self):
self.make_template("""\
First
{# disregard all of this #}
Last
""")
text = self.run_django_coverage()
self.assertEqual(text, "First\n\nLast\n")
self.assert_analysis([1, 3])
class OtherTest(DjangoPluginTestCase):
"""Tests of miscellaneous tags."""
def test_autoescape(self):
self.make_template("""\
First
{% autoescape on %}
{{ body }}
{% endautoescape %}
{% autoescape off %}
{{ body }}
{% endautoescape %}
Last
""")
text = self.run_django_coverage(context={'body': '<Hello>'})
self.assertEqual(text, "First\n\n<Hello>\n\n\n<Hello>\n\nLast\n")
self.assert_analysis([1, 2, 3, 5, 6, 8])
def test_filter(self):
self.make_template("""\
First
{% filter force_escape|lower %}
LOOK: 1 < 2
{% endfilter %}
Last
""")
text = self.run_django_coverage()
self.assertEqual(text, "First\n\n look: 1 < 2\n\nLast\n")
self.assert_analysis([1, 2, 3, 5])
def test_firstof(self):
self.make_template("""\
{% firstof var1 var2 var3 "xyzzy" %}
{% firstof var2 var3 "plugh" %}
{% firstof var3 "quux" %}
""")
text = self.run_django_coverage(context={'var1': 'A'})
self.assertEqual(text, "A\nplugh\nquux\n")
self.assert_analysis([1, 2, 3])
text = self.run_django_coverage(context={'var2': 'B'})
self.assertEqual(text, "B\nB\nquux\n")
self.assert_analysis([1, 2, 3])
def test_lorem(self):
self.make_template("""\
First
{% lorem 3 w %}
Last
""")
text = self.run_django_coverage()
self.assertEqual(text, "First\nlorem ipsum dolor\nLast\n")
self.assert_analysis([1, 2, 3])
def test_now(self):
self.make_template("""\
Now:
{% now "\\n\\o\\w" %}
.
""")
text = self.run_django_coverage()
self.assertEqual(text, "Now:\nnow\n.\n")
self.assert_analysis([1, 2, 3])
def test_now_as(self):
self.make_template("""\
{% now "\\n\\o\\w" as right_now %}
Now it's {{ right_now }}.
""")
text = self.run_django_coverage()
self.assertEqual(text, "\nNow it's now.\n")
self.assert_analysis([1, 2])
def test_templatetag(self):
self.make_template("""\
{% templatetag openblock %}
url 'entry_list'
{% templatetag closeblock %}
""")
text = self.run_django_coverage()
self.assertEqual(text, "{%\nurl 'entry_list'\n%}\n")
self.assert_analysis([1, 2, 3])
def test_url(self):
self.make_template("""\
{% url 'index' %}
nice.
""")
text = self.run_django_coverage()
self.assertEqual(text, "/home\nnice.\n")
self.assert_analysis([1, 2])
def test_verbatim(self):
self.make_template("""\
1
{% verbatim %}
{{if dying}}Alive.{{/if}}
second.
{%third%}.UNISTR
{% endverbatim %}
7
""".replace("UNISTR", UNISTR))
text = self.run_django_coverage()
self.assertEqual(
text,
u"1\n\n{{if dying}}Alive.{{/if}}\nsecond.\n"
u"{%third%}.UNIUNI\n\n7\n".replace(u"UNIUNI", UNIUNI)
)
self.assert_analysis([1, 2, 3, 4, 5, 7])
def test_widthratio(self):
self.make_template("""\
{% widthratio 175 200 100 %}
== 88
""")
text = self.run_django_coverage()
self.assertEqual(text, "88\n== 88\n")
self.assert_analysis([1, 2])
def test_with(self):
self.make_template("""\
{% with alpha=1 beta=2 %}
alpha = {{ alpha }}, beta = {{ beta }}.
{% endwith %}
""")
text = self.run_django_coverage()
self.assertEqual(text, "\nalpha = 1, beta = 2.\n\n")
self.assert_analysis([1, 2])
class StringTemplateTest(DjangoPluginTestCase):
run_in_temp_dir = False
def test_string_template(self):
text = self.run_django_coverage(
text="Hello, {{name}}!",
context={'name': 'World'},
options={},
)
self.assertEqual(text, "Hello, World!")
class BranchTest(DjangoPluginTestCase):
def test_with_branch_enabled(self):
self.make_template('Hello\nWorld\n\nGoodbye')
text = self.run_django_coverage(
options={'source': ["."], 'branch': True}
)
self.assertEqual(text, 'Hello\nWorld\n\nGoodbye')
self.assert_analysis([1, 2, 3, 4])
class TestNonUTF8StaticFile(DjangoPluginTestCase):
def test_non_utf8_static_file(self):
# Non-template file containing a word encoded in CP-1252
self.make_file("static/german.txt", bytes=b"sh\xf6n")
self.run_django_coverage(text='Hello')
self.assertEqual(self.get_html_report('../static/german.txt'), 0)