Ejemplo n.º 1
0
	def testAutoSrip(self) -> None:
		template = Template("""
	{% if value %}
  hello
			{% end %}""")
		result = template.render({"value": True})
		self.assertEqual("\n  hello\n", result)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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)
Ejemplo n.º 8
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)
Ejemplo n.º 9
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)
Ejemplo n.º 10
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)
Ejemplo n.º 11
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)
Ejemplo n.º 12
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)
Ejemplo n.º 13
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)
Ejemplo n.º 14
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)
Ejemplo n.º 15
0
def formatCc(bdl: Object) -> str:

    template = Template.fromPath(Path(__file__).parent / "template/file.h.btl",
                                 indent=True)
    output = template.render(bdl.tree, Transform())

    return output
Ejemplo n.º 16
0
def compositionCc(composition: Composition) -> str:

    template = Template.fromPath(Path(__file__).parent /
                                 "template/composition.cc.btl",
                                 indent=True)
    output = template.render(composition, Transform(composition))

    return output
Ejemplo n.º 17
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)
Ejemplo n.º 18
0
def formatBdl(bdl: Object) -> str:

    template = Template.fromPath(Path(__file__).parent /
                                 "template/file.bdl.btl",
                                 indent=True)
    output = template.render(
        bdl.tree, {
            "typeToStr": _typeToStr,
            "namespaceToStr": _namespaceToStr,
            "inlineComment": _inlineComment,
            "normalComment": _normalComment,
            "inheritanceToStr": _inheritanceToStr
        })

    return output
Ejemplo n.º 19
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)
Ejemplo n.º 20
0
 def testSimpleComment(self) -> None:
     template = Template("a {# hello #} b")
     result = template.render({})
     self.assertEqual("a  b", result)
Ejemplo n.º 21
0
 def testAutoStripComment(self) -> None:
     template = Template("{# Comment #}  \n here ")
     result = template.render({})
     self.assertEqual(" here ", result)
Ejemplo n.º 22
0
 def testEmptyComment(self) -> None:
     template = Template("em{# #}pty")
     result = template.render({})
     self.assertEqual("empty", result)
Ejemplo n.º 23
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)
Ejemplo n.º 24
0
 def testStripSpecialChars(self) -> None:
     template = Template(" {  12 }  }}   \n     a ")
     result = template.render({"a": "0"})
     self.assertEqual(" {  12 }  }}   \n     a ", result)
Ejemplo n.º 25
0
 def testStripVarious(self) -> None:
     template = Template("{{a -}} | {{- a}} | {{a}} | {{- a -}} |")
     result = template.render({"a": "0"})
     self.assertEqual("0|0 | 0 |0|", result)
Ejemplo n.º 26
0
 def testStripRight(self) -> None:
     template = Template("{{world -}} ld")
     result = template.render({"world": "Wor"})
     self.assertEqual("World", result)
Ejemplo n.º 27
0
 def testStripLeft(self) -> None:
     template = Template("Hel {{-world}}")
     result = template.render({"world": "lo"})
     self.assertEqual("Hello", result)
Ejemplo n.º 28
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)
Ejemplo n.º 29
0
 def testSimpleSubsitution(self) -> None:
     template = Template("Hello {{world}}")
     result = template.render({"world": "World"})
     self.assertEqual("Hello World", result)
Ejemplo n.º 30
0
 def testCallableNestedSubsitution(self) -> None:
     template = Template("Hello {{a.b}}")
     result = template.render({"a": lambda: {"b": lambda: "Me!"}})
     self.assertEqual("Hello Me!", result)