Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
	def testAutoSrip(self) -> None:
		template = Template("""
	{% if value %}
  hello
			{% end %}""")
		result = template.render({"value": True})
		self.assertEqual("\n  hello\n", result)
Пример #4
0
	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)
Пример #5
0
	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)
Пример #6
0
	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)
Пример #7
0
	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)
Пример #8
0
	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)
Пример #9
0
	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)
Пример #10
0
 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)
Пример #11
0
 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)
Пример #12
0
    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)
Пример #13
0
	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)
Пример #14
0
	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)
Пример #15
0
	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)
Пример #16
0
 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)
Пример #17
0
 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)
Пример #18
0
 def testSimpleComment(self) -> None:
     template = Template("a {# hello #} b")
     result = template.render({})
     self.assertEqual("a  b", result)
Пример #19
0
 def testAutoStripComment(self) -> None:
     template = Template("{# Comment #}  \n here ")
     result = template.render({})
     self.assertEqual(" here ", result)
Пример #20
0
 def testEmptyComment(self) -> None:
     template = Template("em{# #}pty")
     result = template.render({})
     self.assertEqual("empty", result)
Пример #21
0
 def testIndent(self) -> None:
     template = Template("    {{ a }}", indent=True)
     result = template.render({"a": "0\n1\n2"})
     self.assertEqual("    0\n    1\n    2", result)
Пример #22
0
 def testStripSpecialChars(self) -> None:
     template = Template(" {  12 }  }}   \n     a ")
     result = template.render({"a": "0"})
     self.assertEqual(" {  12 }  }}   \n     a ", result)
Пример #23
0
 def testStripVarious(self) -> None:
     template = Template("{{a -}} | {{- a}} | {{a}} | {{- a -}} |")
     result = template.render({"a": "0"})
     self.assertEqual("0|0 | 0 |0|", result)
Пример #24
0
 def testStripRight(self) -> None:
     template = Template("{{world -}} ld")
     result = template.render({"world": "Wor"})
     self.assertEqual("World", result)
Пример #25
0
 def testStripLeft(self) -> None:
     template = Template("Hel {{-world}}")
     result = template.render({"world": "lo"})
     self.assertEqual("Hello", result)
Пример #26
0
 def testNoStrip(self) -> None:
     template = Template("   Hello {{world}}    I am   happy ")
     result = template.render({"world": "World"})
     self.assertEqual("   Hello World    I am   happy ", result)
Пример #27
0
 def testSimpleSubsitution(self) -> None:
     template = Template("Hello {{world}}")
     result = template.render({"world": "World"})
     self.assertEqual("Hello World", result)
Пример #28
0
 def testCallableNestedSubsitution(self) -> None:
     template = Template("Hello {{a.b}}")
     result = template.render({"a": lambda: {"b": lambda: "Me!"}})
     self.assertEqual("Hello Me!", result)
Пример #29
0
	def testSripCondition(self) -> None:
		template = Template("{% if value -%} hello {%- end %}")
		result = template.render({"value": True})
		self.assertEqual("hello", result)
Пример #30
0
 def testExplicitCallableSubsitution(self) -> None:
     template = Template("Hello {{a()}}")
     result = template.render({"a": lambda: 42})
     self.assertEqual("Hello 42", result)