import jinja2 import pytest from jinjax import JinjaX VALID_DATA = ( # Simple case ( """content""", """{% call(_slot="") catalog.irender("Foo", __prefix=__prefix, **{"bar":"baz"}) -%}content{%- endcall %}""", ), # Self-closing tag ( """""", """{{ catalog.irender("Alert", __prefix=__prefix, **{"type":"success", "message":"Success!"}) }}""", ), # No attributes ( """content""", """{% call(_slot="") catalog.irender("Foo", __prefix=__prefix, **{}) -%}content{%- endcall %}""", ), # No attributes, self-closing tag ( """""", """{{ catalog.irender("Foo", __prefix=__prefix, **{}) }}""", ), # Line breaks ( """content""", """{% call(_slot="") catalog.irender("Foo", __prefix=__prefix, **{"bar":"baz", "lorem":"ipsum"}) -%}content{%- endcall %}""", ), # Line breaks, self-closing tag ( """""", """{{ catalog.irender("Foo", __prefix=__prefix, **{"bar":"baz", "lorem":"ipsum", "green":True}) }}""", ), # Subfolder in tag name ( """content""", """{% call(_slot="") catalog.irender("sub.Alert", __prefix=__prefix, **{"type":"success"}) -%}content{%- endcall %}""", ), # Python expression in attribute and boolean attributes ( """content""", """{% call(_slot="") catalog.irender("Foo", __prefix=__prefix, **{"bar":42 + 4, "green":True, "large":True}) -%}content{%- endcall %}""", ), # Prefix in tag name and `'}}'` in attribute ( """content""", """{% call(_slot="") catalog.irender("ui:Button", __prefix=__prefix, **{"lorem":'ipsum }}', "foo":"bar"}) -%}content{%- endcall %}""", ), # `>` in expression ( """ 4 }} />""", """{{ catalog.irender("CloseBtn", __prefix=__prefix, **{"disabled":num > 4}) }}""", ), # `>` in attribute value ( """""", """{{ catalog.irender("CloseBtn", __prefix=__prefix, **{"data_closer_action":"click->closer#close"}) }}""", ), ) @pytest.mark.parametrize("source, expected", VALID_DATA) def test_process_valid_tags(source, expected): # Test the process_tags method of the JinjaX extension env = jinja2.Environment() jinjax = JinjaX(env) result = jinjax.process_tags(source) print(result) assert result == expected INVALID_DATA = ( # Tag not closed ( """content aslasals ls,als,as""", jinja2.TemplateSyntaxError, "Unclosed component", ), # String attribute not closed ( """content""", jinja2.TemplateSyntaxError, "Syntax error", ), # Expression not opem ( """content""", jinja2.TemplateSyntaxError, "Syntax error", ), ) @pytest.mark.parametrize("source, exception, match", INVALID_DATA) def test_process_invalid_tags(source, exception, match): # Test the process_tags method of the JinjaX extension env = jinja2.Environment() jinjax = JinjaX(env) with pytest.raises(exception, match=f".*{match}.*"): jinjax.process_tags(source) def test_process_nested_Same_tag(): # Test the process_tags method for the same tag nested env = jinja2.Environment() jinjax = JinjaX(env) source = """ WTF abc
Text
""" expected = """ {% call(_slot="") catalog.irender("Card", __prefix=__prefix, **{"class":"card"}) -%} WTF {% call(_slot="") catalog.irender("Card", __prefix=__prefix, **{"class":"card-header"}) -%}abc{%- endcall %} {% call(_slot="") catalog.irender("Card", __prefix=__prefix, **{"class":"card-body"}) -%}
{% call(_slot="") catalog.irender("Card", __prefix=__prefix, **{}) -%}Text{%- endcall %}
{%- endcall %} {%- endcall %} """ result = jinjax.process_tags(source) print(result) assert result.strip() == expected.strip()