summaryrefslogtreecommitdiff
path: root/tests/template_tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests/template_tests')
-rw-r--r--tests/template_tests/test_partials.py203
1 files changed, 158 insertions, 45 deletions
diff --git a/tests/template_tests/test_partials.py b/tests/template_tests/test_partials.py
index cc3ef7fb25..9762436fdc 100644
--- a/tests/template_tests/test_partials.py
+++ b/tests/template_tests/test_partials.py
@@ -32,33 +32,6 @@ class PartialTagsTests(TestCase):
):
engine.get_template(template_name)
- def test_template_source_is_correct(self):
- partial = engine.get_template("partial_examples.html#test-partial")
- msg = (
- "PartialTemplate.source is only available when "
- "template debugging is enabled."
- )
- with self.assertRaisesMessage(RuntimeWarning, msg):
- self.assertEqual(
- partial.template.source,
- "{% partialdef test-partial %}\n"
- "TEST-PARTIAL-CONTENT\n"
- "{% endpartialdef %}",
- )
-
- def test_template_source_inline_is_correct(self):
- partial = engine.get_template("partial_examples.html#inline-partial")
- msg = (
- "PartialTemplate.source is only available when "
- "template debugging is enabled."
- )
- with self.assertRaisesMessage(RuntimeWarning, msg):
- self.assertEqual(
- partial.template.source,
- "{% partialdef inline-partial inline %}\nINLINE-CONTENT\n"
- "{% endpartialdef %}",
- )
-
def test_full_template_from_loader(self):
template = engine.get_template("partial_examples.html")
rendered = template.render({})
@@ -172,12 +145,7 @@ class PartialTagsTests(TestCase):
"PartialTemplate.source is only available when template "
"debugging is enabled.",
):
- self.assertEqual(
- partial.template.source,
- "{% partialdef test-partial %}\n"
- "TEST-PARTIAL-CONTENT\n"
- "{% endpartialdef %}",
- )
+ self.assertEqual(partial.template.source, "")
class RobustPartialHandlingTests(TestCase):
@@ -287,6 +255,20 @@ INLINE-CONTENT
{% endpartialdef %}"""
self.assertEqual(partial_proxy.source.strip(), expected.strip())
+ def test_find_partial_source_fallback_cases(self):
+ cases = {"None offsets": (None, None), "Out of bounds offsets": (10, 20)}
+ for name, (source_start, source_end) in cases.items():
+ with self.subTest(name):
+ partial = PartialTemplate(
+ NodeList(),
+ Origin("test"),
+ "test",
+ source_start=source_start,
+ source_end=source_end,
+ )
+ result = partial.find_partial_source("nonexistent-partial")
+ self.assertEqual(result, "")
+
@setup(
{
"empty_partial_template": ("{% partialdef empty %}{% endpartialdef %}"),
@@ -297,7 +279,7 @@ INLINE-CONTENT
template = self.engine.get_template("empty_partial_template")
partial_proxy = template.extra_data["partials"]["empty"]
- result = partial_proxy.find_partial_source(template.source, "empty")
+ result = partial_proxy.find_partial_source(template.source)
self.assertEqual(result, "{% partialdef empty %}{% endpartialdef %}")
@setup(
@@ -315,10 +297,10 @@ INLINE-CONTENT
empty_proxy = template.extra_data["partials"]["empty"]
other_proxy = template.extra_data["partials"]["other"]
- empty_result = empty_proxy.find_partial_source(template.source, "empty")
+ empty_result = empty_proxy.find_partial_source(template.source)
self.assertEqual(empty_result, "{% partialdef empty %}{% endpartialdef %}")
- other_result = other_proxy.find_partial_source(template.source, "other")
+ other_result = other_proxy.find_partial_source(template.source)
self.assertEqual(other_result, "{% partialdef other %}...{% endpartialdef %}")
def test_partials_with_duplicate_names(self):
@@ -368,7 +350,7 @@ INLINE-CONTENT
template = self.engine.get_template("named_end_tag_template")
partial_proxy = template.extra_data["partials"]["thing"]
- result = partial_proxy.find_partial_source(template.source, "thing")
+ result = partial_proxy.find_partial_source(template.source)
self.assertEqual(
result, "{% partialdef thing %}CONTENT{% endpartialdef thing %}"
)
@@ -389,7 +371,7 @@ INLINE-CONTENT
empty_proxy = template.extra_data["partials"]["outer"]
other_proxy = template.extra_data["partials"]["inner"]
- outer_result = empty_proxy.find_partial_source(template.source, "outer")
+ outer_result = empty_proxy.find_partial_source(template.source)
self.assertEqual(
outer_result,
(
@@ -398,7 +380,7 @@ INLINE-CONTENT
),
)
- inner_result = other_proxy.find_partial_source(template.source, "inner")
+ inner_result = other_proxy.find_partial_source(template.source)
self.assertEqual(inner_result, "{% partialdef inner %}...{% endpartialdef %}")
@setup(
@@ -417,7 +399,7 @@ INLINE-CONTENT
empty_proxy = template.extra_data["partials"]["outer"]
other_proxy = template.extra_data["partials"]["inner"]
- outer_result = empty_proxy.find_partial_source(template.source, "outer")
+ outer_result = empty_proxy.find_partial_source(template.source)
self.assertEqual(
outer_result,
(
@@ -426,7 +408,7 @@ INLINE-CONTENT
),
)
- inner_result = other_proxy.find_partial_source(template.source, "inner")
+ inner_result = other_proxy.find_partial_source(template.source)
self.assertEqual(
inner_result, "{% partialdef inner %}...{% endpartialdef inner %}"
)
@@ -447,7 +429,7 @@ INLINE-CONTENT
empty_proxy = template.extra_data["partials"]["outer"]
other_proxy = template.extra_data["partials"]["inner"]
- outer_result = empty_proxy.find_partial_source(template.source, "outer")
+ outer_result = empty_proxy.find_partial_source(template.source)
self.assertEqual(
outer_result,
(
@@ -456,7 +438,7 @@ INLINE-CONTENT
),
)
- inner_result = other_proxy.find_partial_source(template.source, "inner")
+ inner_result = other_proxy.find_partial_source(template.source)
self.assertEqual(inner_result, "{% partialdef inner %}...{% endpartialdef %}")
@setup(
@@ -475,7 +457,7 @@ INLINE-CONTENT
empty_proxy = template.extra_data["partials"]["outer"]
other_proxy = template.extra_data["partials"]["inner"]
- outer_result = empty_proxy.find_partial_source(template.source, "outer")
+ outer_result = empty_proxy.find_partial_source(template.source)
self.assertEqual(
outer_result,
(
@@ -484,7 +466,138 @@ INLINE-CONTENT
),
)
- inner_result = other_proxy.find_partial_source(template.source, "inner")
+ inner_result = other_proxy.find_partial_source(template.source)
self.assertEqual(
inner_result, "{% partialdef inner %}...{% endpartialdef inner %}"
)
+
+ @setup(
+ {
+ "partial_embedded_in_verbatim": (
+ "{% verbatim %}\n"
+ "{% partialdef testing-name %}\n"
+ "<p>Should be ignored</p>"
+ "{% endpartialdef testing-name %}\n"
+ "{% endverbatim %}\n"
+ "{% partialdef testing-name %}\n"
+ "<p>Content</p>\n"
+ "{% endpartialdef %}\n"
+ ),
+ },
+ debug_only=True,
+ )
+ def test_partial_template_embedded_in_verbatim(self):
+ template = self.engine.get_template("partial_embedded_in_verbatim")
+ partial_template = template.extra_data["partials"]["testing-name"]
+ self.assertEqual(
+ partial_template.source,
+ "{% partialdef testing-name %}\n<p>Content</p>\n{% endpartialdef %}",
+ )
+
+ @setup(
+ {
+ "partial_debug_source": (
+ "{% partialdef testing-name %}\n"
+ "<p>Content</p>\n"
+ "{% endpartialdef %}\n"
+ ),
+ },
+ debug_only=True,
+ )
+ def test_partial_source_uses_offsets_in_debug(self):
+ template = self.engine.get_template("partial_debug_source")
+ partial_template = template.extra_data["partials"]["testing-name"]
+
+ self.assertEqual(partial_template._source_start, 0)
+ self.assertEqual(partial_template._source_end, 64)
+ expected = template.source[
+ partial_template._source_start : partial_template._source_end
+ ]
+ self.assertEqual(partial_template.source, expected)
+
+ @setup(
+ {
+ "partial_embedded_in_named_verbatim": (
+ "{% verbatim block1 %}\n"
+ "{% partialdef testing-name %}\n"
+ "{% endverbatim block1 %}\n"
+ "{% partialdef testing-name %}\n"
+ "<p>Named Content</p>\n"
+ "{% endpartialdef %}\n"
+ ),
+ },
+ debug_only=True,
+ )
+ def test_partial_template_embedded_in_named_verbatim(self):
+ template = self.engine.get_template("partial_embedded_in_named_verbatim")
+ partial_template = template.extra_data["partials"]["testing-name"]
+ self.assertEqual(
+ "{% partialdef testing-name %}\n<p>Named Content</p>\n{% endpartialdef %}",
+ partial_template.source,
+ )
+
+ @setup(
+ {
+ "partial_embedded_in_comment_block": (
+ "{% comment %}\n"
+ "{% partialdef testing-name %}\n"
+ "{% endcomment %}\n"
+ "{% partialdef testing-name %}\n"
+ "<p>Comment Content</p>\n"
+ "{% endpartialdef %}\n"
+ ),
+ },
+ debug_only=True,
+ )
+ def test_partial_template_embedded_in_comment_block(self):
+ template = self.engine.get_template("partial_embedded_in_comment_block")
+ partial_template = template.extra_data["partials"]["testing-name"]
+ self.assertEqual(
+ partial_template.source,
+ "{% partialdef testing-name %}\n"
+ "<p>Comment Content</p>\n"
+ "{% endpartialdef %}",
+ )
+
+ @setup(
+ {
+ "partial_embedded_in_inline_comment": (
+ "{# {% partialdef testing-name %} #}\n"
+ "{% partialdef testing-name %}\n"
+ "<p>Inline Comment Content</p>\n"
+ "{% endpartialdef %}\n"
+ ),
+ },
+ debug_only=True,
+ )
+ def test_partial_template_embedded_in_inline_comment(self):
+ template = self.engine.get_template("partial_embedded_in_inline_comment")
+ partial_template = template.extra_data["partials"]["testing-name"]
+ self.assertEqual(
+ partial_template.source,
+ "{% partialdef testing-name %}\n"
+ "<p>Inline Comment Content</p>\n"
+ "{% endpartialdef %}",
+ )
+
+ @setup(
+ {
+ "partial_contains_fake_end_inside_verbatim": (
+ "{% partialdef testing-name %}\n"
+ "{% verbatim %}{% endpartialdef %}{% endverbatim %}\n"
+ "<p>Body</p>\n"
+ "{% endpartialdef %}\n"
+ ),
+ },
+ debug_only=True,
+ )
+ def test_partial_template_contains_fake_end_inside_verbatim(self):
+ template = self.engine.get_template("partial_contains_fake_end_inside_verbatim")
+ partial_template = template.extra_data["partials"]["testing-name"]
+ self.assertEqual(
+ partial_template.source,
+ "{% partialdef testing-name %}\n"
+ "{% verbatim %}{% endpartialdef %}{% endverbatim %}\n"
+ "<p>Body</p>\n"
+ "{% endpartialdef %}",
+ )