示例#1
0
 def testVariableUnicode(self):
     tmpl = "{{=i}}"
     tmpl = _unicod(tmpl)
     context = dict(i="свят")
     # ??? python2 to convert to unicode output ???
     expected_output = _unicod("свят")
     instream = StringIO(tmpl)
     outstream = StringIO()
     Render(instream, outstream, context=context)()
     self.assertEqual(outstream.getvalue(), expected_output)
示例#2
0
    def testCodeTextUnicode(self):
        tmpl = """{{for i in range(0, 2):}}
свят
{{pass}}
"""
        tmpl = _unicod(tmpl)
        expected_output = _unicod("""свят
свят
""")
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#3
0
 def testException(self):
     tmpl = "{{raise RuntimeError}}"
     tmpl = _unicod(tmpl)
     instream = StringIO(tmpl)
     outstream = StringIO()
     with self.assertRaises(RuntimeError):
         Render(instream, outstream)()
示例#4
0
 def testDelimitersChange(self):
     tmpl = "[[True]]"
     tmpl = _unicod(tmpl)
     expected_output = ""
     instream = StringIO(tmpl)
     outstream = StringIO()
     Render(instream, outstream, delimiters="[[ ]]")()
     self.assertEqual(outstream.getvalue(), expected_output)
示例#5
0
 def testDelimitersThreeChars(self):
     tmpl = "<<<True>>>"
     tmpl = _unicod(tmpl)
     expected_output = ""
     instream = StringIO(tmpl)
     outstream = StringIO()
     Render(instream, outstream, delimiters="<<< >>>")()
     self.assertEqual(outstream.getvalue(), expected_output)
示例#6
0
 def testTag(self):
     tmpl = "{{True}}"
     tmpl = _unicod(tmpl)
     expected_output = ""
     instream = StringIO(tmpl)
     outstream = StringIO()
     Render(instream, outstream)()
     self.assertEqual(outstream.getvalue(), expected_output)
示例#7
0
 def testUnindentRelaxedSyntax(self):
     tmpl = "{{  pass   }}"
     tmpl = _unicod(tmpl)
     expected_output = ""
     instream = StringIO(tmpl)
     outstream = StringIO()
     Render(instream, outstream)()
     self.assertEqual(outstream.getvalue(), expected_output)
示例#8
0
 def testVariableRelaxedSyntax(self):
     tmpl = "{{ = i }}"
     tmpl = _unicod(tmpl)
     context = dict(i=100)
     expected_output = "100"
     instream = StringIO(tmpl)
     outstream = StringIO()
     Render(instream, outstream, context=context)()
     self.assertEqual(outstream.getvalue(), expected_output)
示例#9
0
 def testVariableAndClearText(self):
     tmpl = "lorem ipsum {{=i}} text"
     tmpl = _unicod(tmpl)
     context = dict(i=100)
     expected_output = "lorem ipsum 100 text"
     instream = StringIO(tmpl)
     outstream = StringIO()
     Render(instream, outstream, context=context)()
     self.assertEqual(outstream.getvalue(), expected_output)
示例#10
0
 def testVariableDict(self):
     tmpl = "{{=dict(a=2, b=3)}}"
     tmpl = _unicod(tmpl)
     context = dict(i=1)
     expected_output = "{'a': 2, 'b': 3}"
     instream = StringIO(tmpl)
     outstream = StringIO()
     Render(instream, outstream, context=context)()
     self.assertEqual(outstream.getvalue(), expected_output)
示例#11
0
 def testVariableList(self):
     tmpl = "{{=[1, 2, 3]}}"
     tmpl = _unicod(tmpl)
     context = dict(i=1)
     expected_output = "[1, 2, 3]"
     instream = StringIO(tmpl)
     outstream = StringIO()
     Render(instream, outstream, context=context)()
     self.assertEqual(outstream.getvalue(), expected_output)
示例#12
0
    def testUnindentNewLine(self):
        tmpl = """{{pass}}
"""
        tmpl = _unicod(tmpl)
        context = dict(i=100)
        expected_output = ""
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream, context=context)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#13
0
    def testIndentationRelaxedSyntax(self):
        tmpl = """{{  if True :  }}
{{True}}
{{pass}}"""
        tmpl = _unicod(tmpl)
        expected_output = ""
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#14
0
    def testVariableIndented(self):
        tmpl = """{{if True:}}
{{=i}}{{pass}}"""
        tmpl = _unicod(tmpl)
        context = dict(i=100)
        expected_output = "100"
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream, context=context)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#15
0
    def testVariableCodeText(self):
        tmpl = """{{if True:}}
lorem ipsum {{=i}} texting
{{pass}}"""
        tmpl = _unicod(tmpl)
        context = dict(i=100)
        expected_output = """lorem ipsum 100 texting
"""
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream, context=context)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#16
0
    def testClearTextWithDelimiterStart(self):
        tmpl = """
lorem ipsum dolor sim amet
text lorem {{ ipsum text
clear text clear text
texting clearly
"""
        tmpl = _unicod(tmpl)
        expected_output = tmpl
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#17
0
    def testMultilineCode(self):
        tmpl = """{{
import sys
def one():
    return 1
}}
"""
        tmpl = _unicod(tmpl)
        expected_output = ""
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#18
0
    def testVariableFunction(self):
        tmpl = """{{
def one():
     return 1
}}
{{=one()}}"""
        tmpl = _unicod(tmpl)
        context = dict(i=1)
        expected_output = "1"
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream, context=context)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#19
0
    def testClearTextWithThreeQuotes(self):
        tmpl = '''
lorem ipsum dolor sim amet
text lorem """ ipsum text
clear text clear text
texting clearly
'''
        tmpl = _unicod(tmpl)
        expected_output = tmpl
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#20
0
    def testLazy(self):
        rndr = Render()

        tmpl = "X"
        tmpl = _unicod(tmpl)
        expected_output = "X"
        instream = StringIO(tmpl)
        outstream = StringIO()
        rndr.instream = instream
        rndr.outstream = outstream
        rndr()
        self.assertEqual(outstream.getvalue(), expected_output)

        tmpl = "Y"
        tmpl = _unicod(tmpl)
        expected_output = "Y"
        instream = StringIO(tmpl)
        outstream = StringIO()
        rndr.instream = instream
        rndr.outstream = outstream
        rndr()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#21
0
    def testClearTextWithUnicode(self):
        tmpl = '''
lorem ipsum dolor sim amet
Моето име е Тодор clear txt continues
lorem Здравей, свят!
clear text clear text
texting clearly
'''
        tmpl = _unicod(tmpl)
        expected_output = tmpl
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#22
0
    def testMultilineCodeIndented(self):
        tmpl = """{{if True:}}
{{
def one():
    return 1
}}
{{pass}}
{{=one()}}"""
        tmpl = _unicod(tmpl)
        expected_output = "1"
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#23
0
    def testTryExcept(self):
        tmpl = """{{try:}}
{{True}}
{{except Exception as e:}}
X: {{=str(e)}}
{{finally:}}
Y
{{pass}}
"""
        tmpl = _unicod(tmpl)
        expected_output = """Y
"""
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#24
0
    def testIfElse(self):
        tmpl = """{{a = 1}}
{{if a == 0:}}
X
{{elif a == 1:}}
Y
{{else:}}
Z
{{pass}}
"""
        tmpl = _unicod(tmpl)
        expected_output = """Y
"""
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#25
0
    def testWhileElse(self):
        tmpl = """{{i = 0}}
{{while i <= 2:}}
{{=i}}
{{i += 1}}
{{else:}}
X
{{pass}}
"""
        tmpl = _unicod(tmpl)
        expected_output = """0
1
2
X
"""
        instream = StringIO(tmpl)
        outstream = StringIO()
        Render(instream, outstream)()
        self.assertEqual(outstream.getvalue(), expected_output)
示例#26
0
    def testFiles(self):
        tmpl = "X"
        tmpl = _unicod(tmpl)
        expected_output = "X"
        infile = os.path.join(TMP, '%s.tmpl' % (os.getpid()))
        outfile = os.path.join(TMP, '%s.out' % (os.getpid()))

        with open(infile, 'w') as f:
            f.write(tmpl)

        with open(infile, 'r') as instream:
            with open(outfile, 'w') as outstream:
                Render(instream, outstream)()

        with open(outfile, 'r') as f:
            output = f.read()

        os.remove(infile)
        os.remove(outfile)

        self.assertEqual(output, expected_output)