tests: add inline syntax highlight, inline template files

This commit is contained in:
Juro Oravec 2024-05-02 22:33:02 +02:00
parent e566d8ecbb
commit 8c4a55901c
40 changed files with 1309 additions and 1168 deletions

View file

@ -4,6 +4,7 @@ from django.template import Context, Template
from django.test import override_settings
from django_components import component
from django_components import types
from .django_test_setup import * # NOQA
from .testutils import BaseTestCase
@ -14,7 +15,9 @@ from .testutils import BaseTestCase
class SimpleComponent(component.Component):
template_name = "simple_template.html"
template: types.django_html = """
Variable: <strong>{{ variable }}</strong>
"""
def get_context_data(self, variable=None):
return {"variable": variable} if variable is not None else {}
@ -24,22 +27,12 @@ class SimpleComponent(component.Component):
return "Variable: < strong > {} < / strong >".format(variable_value)
class ParentComponent(component.Component):
template_name = "parent_template.html"
def get_context_data(self):
return {"shadowing_variable": "NOT SHADOWED"}
class ParentComponentWithArgs(component.Component):
template_name = "parent_with_args_template.html"
def get_context_data(self, parent_value):
return {"inner_parent_value": parent_value}
class VariableDisplay(component.Component):
template_name = "variable_display.html"
template: types.django_html = """
{% load component_tags %}
<h1>Shadowing variable = {{ shadowing_variable }}</h1>
<h1>Uniquely named variable = {{ unique_variable }}</h1>
"""
def get_context_data(self, shadowing_variable=None, new_variable=None):
context = {}
@ -51,7 +44,11 @@ class VariableDisplay(component.Component):
class IncrementerComponent(component.Component):
template_name = "incrementer.html"
template: types.django_html = """
{% load component_tags %}
<p class="incrementer">value={{ value }};calls={{ calls }}</p>
{% slot 'content' %}{% endslot %}
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@ -66,32 +63,46 @@ class IncrementerComponent(component.Component):
return {"value": value + 1, "calls": self.call_count}
class OuterContextComponent(component.Component):
template_name = "simple_template.html"
def get_context_data(self):
return self.outer_context.flatten()
#########################
# TESTS
#########################
class ContextTests(BaseTestCase):
class ParentComponent(component.Component):
template: types.django_html = """
{% load component_tags %}
<div>
<h1>Parent content</h1>
{% component name="variable_display" shadowing_variable='override' new_variable='unique_val' %}
{% endcomponent %}
</div>
<div>
{% slot 'content' %}
<h2>Slot content</h2>
{% component name="variable_display" shadowing_variable='slot_default_override' new_variable='slot_default_unique' %}
{% endcomponent %}
{% endslot %}
</div>
""" # noqa
def get_context_data(self):
return {"shadowing_variable": "NOT SHADOWED"}
@classmethod
def setUpClass(cls):
super().setUpClass()
component.registry.register(name="variable_display", component=VariableDisplay)
component.registry.register(name="parent_component", component=ParentComponent)
component.registry.register(name="parent_component", component=cls.ParentComponent)
def test_nested_component_context_shadows_parent_with_unfilled_slots_and_component_tag(
self,
):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component 'parent_component' %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'parent_component' %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context())
self.assertIn("<h1>Shadowing variable = override</h1>", rendered, rendered)
@ -105,10 +116,12 @@ class ContextTests(BaseTestCase):
def test_nested_component_instances_have_unique_context_with_unfilled_slots_and_component_tag(
self,
):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component name='parent_component' %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}
{% component_dependencies %}
{% component name='parent_component' %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context())
self.assertIn("<h1>Uniquely named variable = unique_val</h1>", rendered, rendered)
@ -119,8 +132,7 @@ class ContextTests(BaseTestCase):
)
def test_nested_component_context_shadows_parent_with_filled_slots(self):
template = Template(
"""
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'parent_component' %}
{% fill 'content' %}
@ -129,7 +141,7 @@ class ContextTests(BaseTestCase):
{% endfill %}
{% endcomponent %}
""" # NOQA
)
template = Template(template_str)
rendered = template.render(Context())
self.assertIn("<h1>Shadowing variable = override</h1>", rendered, rendered)
@ -140,12 +152,10 @@ class ContextTests(BaseTestCase):
)
self.assertNotIn("<h1>Shadowing variable = NOT SHADOWED</h1>", rendered, rendered)
def test_nested_component_instances_have_unique_context_with_filled_slots(
self,
):
template = Template(
"""
{% load component_tags %}{% component_dependencies %}
def test_nested_component_instances_have_unique_context_with_filled_slots(self):
template_str: types.django_html = """
{% load component_tags %}
{% component_dependencies %}
{% component 'parent_component' %}
{% fill 'content' %}
{% component name='variable_display' shadowing_variable='shadow_from_slot' new_variable='unique_from_slot' %}
@ -153,7 +163,7 @@ class ContextTests(BaseTestCase):
{% endfill %}
{% endcomponent %}
""" # NOQA
)
template = Template(template_str)
rendered = template.render(Context())
self.assertIn("<h1>Uniquely named variable = unique_val</h1>", rendered, rendered)
@ -166,10 +176,12 @@ class ContextTests(BaseTestCase):
def test_nested_component_context_shadows_outer_context_with_unfilled_slots_and_component_tag(
self,
):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component name='parent_component' %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}
{% component_dependencies %}
{% component name='parent_component' %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context({"shadowing_variable": "NOT SHADOWED"}))
self.assertIn("<h1>Shadowing variable = override</h1>", rendered, rendered)
@ -183,8 +195,7 @@ class ContextTests(BaseTestCase):
def test_nested_component_context_shadows_outer_context_with_filled_slots(
self,
):
template = Template(
"""
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'parent_component' %}
{% fill 'content' %}
@ -193,7 +204,7 @@ class ContextTests(BaseTestCase):
{% endfill %}
{% endcomponent %}
""" # NOQA
)
template = Template(template_str)
rendered = template.render(Context({"shadowing_variable": "NOT SHADOWED"}))
self.assertIn("<h1>Shadowing variable = override</h1>", rendered, rendered)
@ -206,11 +217,31 @@ class ContextTests(BaseTestCase):
class ParentArgsTests(BaseTestCase):
class ParentComponentWithArgs(component.Component):
template: types.django_html = """
{% load component_tags %}
<div>
<h1>Parent content</h1>
{% component name="variable_display" shadowing_variable=inner_parent_value new_variable='unique_val' %}
{% endcomponent %}
</div>
<div>
{% slot 'content' %}
<h2>Slot content</h2>
{% component name="variable_display" shadowing_variable='slot_default_override' new_variable=inner_parent_value %}
{% endcomponent %}
{% endslot %}
</div>
""" # noqa
def get_context_data(self, parent_value):
return {"inner_parent_value": parent_value}
@classmethod
def setUpClass(cls):
super().setUpClass()
component.registry.register(name="incrementer", component=IncrementerComponent)
component.registry.register(name="parent_with_args", component=ParentComponentWithArgs)
component.registry.register(name="parent_with_args", component=cls.ParentComponentWithArgs)
component.registry.register(name="variable_display", component=VariableDisplay)
@override_settings(
@ -219,13 +250,12 @@ class ParentArgsTests(BaseTestCase):
}
)
def test_parent_args_can_be_drawn_from_context__django(self):
template = Template(
"""
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'parent_with_args' parent_value=parent_value %}
{% endcomponent %}
"""
)
template = Template(template_str)
rendered = template.render(Context({"parent_value": "passed_in"}))
self.assertHTMLEqual(
@ -250,13 +280,12 @@ class ParentArgsTests(BaseTestCase):
}
)
def test_parent_args_can_be_drawn_from_context__isolated(self):
template = Template(
"""
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'parent_with_args' parent_value=parent_value %}
{% endcomponent %}
"""
)
template = Template(template_str)
rendered = template.render(Context({"parent_value": "passed_in"}))
self.assertHTMLEqual(
@ -276,10 +305,11 @@ class ParentArgsTests(BaseTestCase):
)
def test_parent_args_available_outside_slots(self):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component 'parent_with_args' parent_value='passed_in' %}{%endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'parent_with_args' parent_value='passed_in' %}{%endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context())
self.assertIn("<h1>Shadowing variable = passed_in</h1>", rendered, rendered)
@ -292,8 +322,7 @@ class ParentArgsTests(BaseTestCase):
}
)
def test_parent_args_available_in_slots__django(self):
template = Template(
"""
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'parent_with_args' parent_value='passed_in' %}
{% fill 'content' %}
@ -302,7 +331,7 @@ class ParentArgsTests(BaseTestCase):
{% endfill %}
{% endcomponent %}
""" # noqa: E501
)
template = Template(template_str)
rendered = template.render(Context())
self.assertHTMLEqual(
rendered,
@ -325,8 +354,7 @@ class ParentArgsTests(BaseTestCase):
}
)
def test_parent_args_not_available_in_slots__isolated(self):
template = Template(
"""
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'parent_with_args' parent_value='passed_in' %}
{% fill 'content' %}
@ -334,8 +362,8 @@ class ParentArgsTests(BaseTestCase):
{% endcomponent %}
{% endfill %}
{% endcomponent %}
""" # noqa: E501
)
""" # noqa: E501
template = Template(template_str)
rendered = template.render(Context())
self.assertHTMLEqual(
rendered,
@ -360,10 +388,11 @@ class ContextCalledOnceTests(BaseTestCase):
component.registry.register(name="incrementer", component=IncrementerComponent)
def test_one_context_call_with_simple_component(self):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component name='incrementer' %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component name='incrementer' %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context()).strip().replace("\n", "")
self.assertHTMLEqual(
rendered,
@ -371,46 +400,66 @@ class ContextCalledOnceTests(BaseTestCase):
)
def test_one_context_call_with_simple_component_and_arg(self):
template = Template("{% load component_tags %}{% component name='incrementer' value='2' %}{% endcomponent %}")
template_str: types.django_html = """
{% load component_tags %}
{% component name='incrementer' value='2' %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context()).strip()
self.assertEqual(rendered, '<p class="incrementer">value=3;calls=1</p>', rendered)
self.assertHTMLEqual(rendered, '<p class="incrementer">value=3;calls=1</p>', rendered)
def test_one_context_call_with_component(self):
template = Template("{% load component_tags %}" "{% component 'incrementer' %}{% endcomponent %}")
template_str: types.django_html = """
{% load component_tags %}
{% component 'incrementer' %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context()).strip()
self.assertEqual(rendered, '<p class="incrementer">value=1;calls=1</p>', rendered)
self.assertHTMLEqual(rendered, '<p class="incrementer">value=1;calls=1</p>', rendered)
def test_one_context_call_with_component_and_arg(self):
template = Template("{% load component_tags %}" "{% component 'incrementer' value='3' %}{% endcomponent %}")
template_str: types.django_html = """
{% load component_tags %}
{% component 'incrementer' value='3' %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context()).strip()
self.assertEqual(rendered, '<p class="incrementer">value=4;calls=1</p>', rendered)
self.assertHTMLEqual(rendered, '<p class="incrementer">value=4;calls=1</p>', rendered)
def test_one_context_call_with_slot(self):
template = Template(
"{% load component_tags %}"
"{% component 'incrementer' %}{% fill 'content' %}"
"<p>slot</p>{% endfill %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}
{% component 'incrementer' %}
{% fill 'content' %}
<p>slot</p>
{% endfill %}
{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context()).strip()
self.assertEqual(
self.assertHTMLEqual(
rendered,
'<p class="incrementer">value=1;calls=1</p>\n<p>slot</p>',
rendered,
)
def test_one_context_call_with_slot_and_arg(self):
template = Template(
"{% load component_tags %}"
"{% component 'incrementer' value='3' %}{% fill 'content' %}"
"<p>slot</p>{% endfill %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}
{% component 'incrementer' value='3' %}
{% fill 'content' %}
<p>slot</p>
{% endfill %}
{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context()).strip()
self.assertEqual(
self.assertHTMLEqual(
rendered,
'<p class="incrementer">value=4;calls=1</p>\n<p>slot</p>',
rendered,
@ -427,10 +476,11 @@ class ComponentsCanAccessOuterContext(BaseTestCase):
COMPONENTS={"context_behavior": "django"},
)
def test_simple_component_can_use_outer_context__django(self):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component 'simple_component' %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'simple_component' %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context({"variable": "outer_value"}))
self.assertHTMLEqual(
rendered,
@ -443,10 +493,11 @@ class ComponentsCanAccessOuterContext(BaseTestCase):
COMPONENTS={"context_behavior": "isolated"},
)
def test_simple_component_cannot_use_outer_context__isolated(self):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component 'simple_component' %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'simple_component' %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context({"variable": "outer_value"}))
self.assertHTMLEqual(
rendered,
@ -463,18 +514,20 @@ class IsolatedContextTests(BaseTestCase):
component.registry.register(name="simple_component", component=SimpleComponent)
def test_simple_component_can_pass_outer_context_in_args(self):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component 'simple_component' variable only %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'simple_component' variable only %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context({"variable": "outer_value"})).strip()
self.assertIn("outer_value", rendered, rendered)
def test_simple_component_cannot_use_outer_context(self):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component 'simple_component' only %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'simple_component' only %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context({"variable": "outer_value"})).strip()
self.assertNotIn("outer_value", rendered, rendered)
@ -499,60 +552,73 @@ class IsolatedContextSettingTests(BaseTestCase):
def test_component_tag_includes_variable_with_isolated_context_from_settings(
self,
):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component 'simple_component' variable %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'simple_component' variable %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context({"variable": "outer_value"}))
self.assertIn("outer_value", rendered, rendered)
def test_component_tag_excludes_variable_with_isolated_context_from_settings(
self,
):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component 'simple_component' %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'simple_component' %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context({"variable": "outer_value"}))
self.assertNotIn("outer_value", rendered, rendered)
def test_component_includes_variable_with_isolated_context_from_settings(
self,
):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component 'simple_component' variable %}"
"{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'simple_component' variable %}
{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context({"variable": "outer_value"}))
self.assertIn("outer_value", rendered, rendered)
def test_component_excludes_variable_with_isolated_context_from_settings(
self,
):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component 'simple_component' %}"
"{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'simple_component' %}
{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context({"variable": "outer_value"}))
self.assertNotIn("outer_value", rendered, rendered)
class OuterContextPropertyTests(BaseTestCase):
class OuterContextComponent(component.Component):
template: types.django_html = """
Variable: <strong>{{ variable }}</strong>
"""
def get_context_data(self):
return self.outer_context.flatten()
@classmethod
def setUpClass(cls):
super().setUpClass()
component.registry.register(name="outer_context_component", component=OuterContextComponent)
component.registry.register(name="outer_context_component", component=cls.OuterContextComponent)
@override_settings(
COMPONENTS={"context_behavior": "django"},
)
def test_outer_context_property_with_component__django(self):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component 'outer_context_component' only %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'outer_context_component' only %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context({"variable": "outer_value"})).strip()
self.assertIn("outer_value", rendered, rendered)
@ -560,9 +626,10 @@ class OuterContextPropertyTests(BaseTestCase):
COMPONENTS={"context_behavior": "isolated"},
)
def test_outer_context_property_with_component__isolated(self):
template = Template(
"{% load component_tags %}{% component_dependencies %}"
"{% component 'outer_context_component' only %}{% endcomponent %}"
)
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'outer_context_component' only %}{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context({"variable": "outer_value"})).strip()
self.assertIn("outer_value", rendered, rendered)