Skip to content

Commit 5d6a7ef

Browse files
authored
tests: --fail-on-template-vars with default_if_none (#702)
2 parents 8d31d0f + e77fe4f commit 5d6a7ef

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tests/test_environment.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,48 @@ def test_ignore(client):
113113
)
114114

115115

116+
@pytest.mark.django_project(
117+
extra_settings="""
118+
TEMPLATE_LOADERS = (
119+
'django.template.loaders.filesystem.Loader',
120+
'django.template.loaders.app_directories.Loader',
121+
)
122+
ROOT_URLCONF = 'tpkg.app.urls'
123+
"""
124+
)
125+
def test_invalid_template_with_default_if_none(django_testdir):
126+
django_testdir.create_app_file(
127+
"""
128+
<div>{{ data.empty|default:'d' }}</div>
129+
<div>{{ data.none|default:'d' }}</div>
130+
<div>{{ data.empty|default_if_none:'d' }}</div>
131+
<div>{{ data.none|default_if_none:'d' }}</div>
132+
<div>{{ data.missing|default_if_none:'d' }}</div>
133+
""",
134+
"templates/the_template.html",
135+
)
136+
django_testdir.create_test_module(
137+
"""
138+
def test_for_invalid_template():
139+
from django.shortcuts import render
140+
141+
142+
render(
143+
request=None,
144+
template_name='the_template.html',
145+
context={'data': {'empty': '', 'none': None}},
146+
)
147+
"""
148+
)
149+
result = django_testdir.runpytest_subprocess("--fail-on-template-vars")
150+
result.stdout.fnmatch_lines(
151+
[
152+
"tpkg/test_the_test.py F",
153+
"E * Failed: Undefined template variable 'data.missing' in *the_template.html'",
154+
]
155+
)
156+
157+
116158
@pytest.mark.django_project(
117159
extra_settings="""
118160
TEMPLATE_LOADERS = (

0 commit comments

Comments
 (0)