def testForStripNested(self) -> None: template = Template( "{% for a in sequence -%} {% for b in sequence -%} {{a}}{{b}} {%- end %} {%- end %}" ) result = template.render({"sequence": ["a", "b"]}) self.assertEqual("aaabbabb", result) template = Template( "{% for a in sequence -%} {% for b in sequence %} {{a}}{{b}} {%- end %} {%- end %}" ) result = template.render({"sequence": ["a", "b"]}) self.assertEqual(" aa ab ba bb", result)
def testForStrip(self) -> None: template = Template( "{% for char in sequence -%} {{char}} \t {%- end %}") result = template.render({"sequence": ["a", "b", "c", "d"]}) self.assertEqual("abcd", result) template = Template( "{% for char in sequence %} {{char}} \t {%- end %}") result = template.render({"sequence": ["a", "b", "c", "d"]}) self.assertEqual(" a b c d", result) template = Template( "{% for char in sequence -%} {{char}}\t{% end %}") result = template.render({"sequence": ["a", "b", "c", "d"]}) self.assertEqual("a\tb\tc\td\t", result)
def testAutoSrip(self) -> None: template = Template(""" {% if value %} hello {% end %}""") result = template.render({"value": True}) self.assertEqual("\n hello\n", result)
def testSimpleCondition(self) -> None: template = Template("{% if value %}hello{% end %}") result = template.render({"value": True}) self.assertEqual("hello", result) result = template.render({"value": False}) self.assertEqual("", result)
def testInCondition(self) -> None: template = Template("{% if value in [10, 23] %}hello{% end %}") result = template.render({"value": 1}) self.assertEqual("", result) result = template.render({"value": 23}) self.assertEqual("hello", result)
def testEqualStringCondition(self) -> None: template = Template("{% if value == \"yes\" %}hello{% end %}") result = template.render({"value": "no"}) self.assertEqual("", result) result = template.render({"value": "yes"}) self.assertEqual("hello", result)
def testEqualNumberCondition(self) -> None: template = Template("{% if value == 2 %}hello{% end %}") result = template.render({"value": 12}) self.assertEqual("", result) result = template.render({"value": 2}) self.assertEqual("hello", result)
def testNestedCondition(self) -> None: template = Template("a{% if value1 %}b{% if value2 %}c{% end %}{% end %}d") result = template.render({"value1": True, "value2": True}) self.assertEqual("abcd", result) result = template.render({"value1": False, "value2": True}) self.assertEqual("ad", result)
def testSripElseIfCondition(self) -> None: template = Template("{% if value1 -%} a {%- elif value2 -%} b {%- else -%} c {%- end %}") result = template.render({"value1": True, "value2": False}) self.assertEqual("a", result) result = template.render({"value1": False, "value2": True}) self.assertEqual("b", result) result = template.render({"value1": False, "value2": False}) self.assertEqual("c", result)
def testPipeSubsitution(self) -> None: template = Template("{{str | lowerCase | capitalize }}") result = template.render({ "str": "ThIs IS SOMEhting MesSy", "lowerCase": lambda x: x.lower(), "capitalize": lambda x: x.capitalize() }) self.assertEqual("This is somehting messy", result)
def testForControlNested(self) -> None: template = Template( "{% for group in message %}{% for letter in group %}{{letter}}{%end%} {%end%}" ) result = template.render({ "message": [["H", "e", "l", "l", "o"], ["W", "o", "r", "l", "d"]] }) self.assertEqual("Hello World ", result)
def testExplicitCallableArgsSubsitution(self) -> None: template = Template("Hello {{a(b1, b2)}}") result = template.render({ "a": lambda x, y: x + y, "b1": "[", "b2": "]" }) self.assertEqual("Hello []", result) template = Template("Hello {{a(3, 7)}}") result = template.render({"a": lambda x, y: x + y}) self.assertEqual("Hello 10.0", result) template = Template("Hello {{a(\"agent\", \" 007\")}}") result = template.render({"a": lambda x, y: x + y}) self.assertEqual("Hello agent 007", result) template = Template("{{a(true, false)}}") result = template.render({"a": lambda x, y: str(x) + str(y)}) self.assertEqual("TrueFalse", result)
def testElseNestedCondition(self) -> None: template = Template("{% if value1 %}0{% if value2 %}1{% else %}2{% end %}{% else %}3{% end %}") result = template.render({"value1": True, "value2": True}) self.assertEqual("01", result) result = template.render({"value1": False, "value2": True}) self.assertEqual("3", result) result = template.render({"value1": True, "value2": False}) self.assertEqual("02", result)
def testElIfCondition(self) -> None: template = Template("{% if value1 %}hello{% elif value2 %}goodbye{% else %}what?!{% end %}") result = template.render({"value1": True}) self.assertEqual("hello", result) result = template.render({"value1": False, "value2": True}) self.assertEqual("goodbye", result) result = template.render({"value1": False, "value2": False}) self.assertEqual("what?!", result)
def testElseNestedComplexCondition(self) -> None: template = Template( "{% if value %}hello{% if not value %}never{% end %}{% else %}{% if not value %}good{% else %}never{% end %}bye{% end %}" ) result = template.render({"value": True}) self.assertEqual("hello", result) result = template.render({"value": False}) self.assertEqual("goodbye", result)
def testDeepSubsitution(self) -> None: template = Template("Hello {{a.b.c.d.world}}") result = template.render( {"a": { "b": { "c": { "d": { "world": "World" } } } }}) self.assertEqual("Hello World", result)
def testMacroWithSubstitutions(self) -> None: template = Template( "{% macro hello(data) %}Vector<{{data.type}}, {{data.size}}>{% end %}{{ hello(small) }}\n{{ hello(large) }}" ) result = template.render({ "small": { "type": "int", "size": 12 }, "large": { "type": "float", "size": 34 } }) self.assertEqual("Vector<int, 12>\nVector<float, 34>", result)
def testSimpleComment(self) -> None: template = Template("a {# hello #} b") result = template.render({}) self.assertEqual("a b", result)
def testAutoStripComment(self) -> None: template = Template("{# Comment #} \n here ") result = template.render({}) self.assertEqual(" here ", result)
def testEmptyComment(self) -> None: template = Template("em{# #}pty") result = template.render({}) self.assertEqual("empty", result)
def testIndent(self) -> None: template = Template(" {{ a }}", indent=True) result = template.render({"a": "0\n1\n2"}) self.assertEqual(" 0\n 1\n 2", result)
def testStripSpecialChars(self) -> None: template = Template(" { 12 } }} \n a ") result = template.render({"a": "0"}) self.assertEqual(" { 12 } }} \n a ", result)
def testStripVarious(self) -> None: template = Template("{{a -}} | {{- a}} | {{a}} | {{- a -}} |") result = template.render({"a": "0"}) self.assertEqual("0|0 | 0 |0|", result)
def testStripRight(self) -> None: template = Template("{{world -}} ld") result = template.render({"world": "Wor"}) self.assertEqual("World", result)
def testStripLeft(self) -> None: template = Template("Hel {{-world}}") result = template.render({"world": "lo"}) self.assertEqual("Hello", result)
def testNoStrip(self) -> None: template = Template(" Hello {{world}} I am happy ") result = template.render({"world": "World"}) self.assertEqual(" Hello World I am happy ", result)
def testSimpleSubsitution(self) -> None: template = Template("Hello {{world}}") result = template.render({"world": "World"}) self.assertEqual("Hello World", result)
def testCallableNestedSubsitution(self) -> None: template = Template("Hello {{a.b}}") result = template.render({"a": lambda: {"b": lambda: "Me!"}}) self.assertEqual("Hello Me!", result)
def testSripCondition(self) -> None: template = Template("{% if value -%} hello {%- end %}") result = template.render({"value": True}) self.assertEqual("hello", result)
def testExplicitCallableSubsitution(self) -> None: template = Template("Hello {{a()}}") result = template.render({"a": lambda: 42}) self.assertEqual("Hello 42", result)