test_pygen.py :  » Template-Engines » Mako » Mako-0.3.2 » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Template Engines » Mako 
Mako » Mako 0.3.2 » test » test_pygen.py
import unittest

from mako.pygen import PythonPrinter,adjust_whitespace
from StringIO import StringIO

class GeneratePythonTest(unittest.TestCase):
    def test_generate_normal(self):
        stream = StringIO()
        printer = PythonPrinter(stream)
        printer.writeline("import lala")
        printer.writeline("for x in foo:")
        printer.writeline("print x")
        printer.writeline(None)
        printer.writeline("print y")
        assert stream.getvalue() == \
"""import lala
for x in foo:
    print x
print y
"""
    def test_generate_adjusted(self):
        block = """
        x = 5 +6
        if x > 7:
            for y in range(1,5):
                print "<td>%s</td>" % y
"""
        stream = StringIO()
        printer = PythonPrinter(stream)
        printer.write_indented_block(block)
        printer.close()
        #print stream.getvalue()
        assert stream.getvalue() == \
"""
x = 5 +6
if x > 7:
    for y in range(1,5):
        print "<td>%s</td>" % y

"""
    def test_generate_combo(self):
        block = \
"""
                x = 5 +6
                if x > 7:
                    for y in range(1,5):
                        print "<td>%s</td>" % y
                    print "hi"
                print "there"
                foo(lala)
        """
        stream = StringIO()
        printer = PythonPrinter(stream)
        printer.writeline("import lala")
        printer.writeline("for x in foo:")
        printer.writeline("print x")
        printer.write_indented_block(block)
        printer.writeline(None)
        printer.writeline("print y")
        printer.close()
        #print "->" + stream.getvalue().replace(' ', '#') + "<-"
        assert stream.getvalue() == \
"""import lala
for x in foo:
    print x

    x = 5 +6
    if x > 7:
        for y in range(1,5):
            print "<td>%s</td>" % y
        print "hi"
    print "there"
    foo(lala)
        
print y
"""
    def test_multi_line(self):
        block = \
"""
    if test:
        print ''' this is a block of stuff.
this is more stuff in the block.
and more block.
'''
        do_more_stuff(g)
"""
        stream = StringIO()
        printer = PythonPrinter(stream)
        printer.write_indented_block(block)
        printer.close()
        #print stream.getvalue()
        assert stream.getvalue() == \
"""
if test:
    print ''' this is a block of stuff.
this is more stuff in the block.
and more block.
'''
    do_more_stuff(g)

"""
    
    def test_false_unindentor(self):
        stream = StringIO()
        printer = PythonPrinter(stream)
        for line in [
            "try:",
            "elsemyvar = 12",
            "if True:",
            "print 'hi'",
            None,
            "finally:",
            "dosomething",
            None
        ]:
            printer.writeline(line)
        
        assert stream.getvalue() == \
"""try:
    elsemyvar = 12
    if True:
        print 'hi'
finally:
    dosomething
"""    , stream.getvalue()
        
        
    def test_backslash_line(self):
        block = \
"""
            # comment
    if test:
        if (lala + hoho) + \\
(foobar + blat) == 5:
            print "hi"
    print "more indent"
"""
        stream = StringIO()
        printer = PythonPrinter(stream)
        printer.write_indented_block(block)
        printer.close()
        assert stream.getvalue() == \
"""
            # comment
if test:
    if (lala + hoho) + \\
(foobar + blat) == 5:
        print "hi"
print "more indent"

"""

class WhitespaceTest(unittest.TestCase):
    def test_basic(self):
        text = """
        for x in range(0,15):
            print x
        print "hi"
        """
        assert adjust_whitespace(text) == \
"""
for x in range(0,15):
    print x
print "hi"
"""

    def test_blank_lines(self):
        text = """
    print "hi"  # a comment
    
    # more comments
    
    print g
"""
        assert adjust_whitespace(text) == \
"""
print "hi"  # a comment

# more comments

print g
"""        
    
    def test_open_quotes_with_pound(self):
        text = '''
        print """  this is text
          # and this is text
        # and this is too """
'''
        assert adjust_whitespace(text) == \
'''
print """  this is text
          # and this is text
        # and this is too """
'''

    def test_quote_with_comments(self):
        text= """
            print 'hi'
            # this is a comment
            # another comment
            x = 7 # someone's '''comment
            print '''
        there
        '''
            # someone else's comment
"""

        assert adjust_whitespace(text) == \
"""
print 'hi'
# this is a comment
# another comment
x = 7 # someone's '''comment
print '''
        there
        '''
# someone else's comment
"""        


    def test_quotes_with_pound(self):
        text = '''
        if True:
            """#"""
        elif False:
            "bar"
'''
        assert adjust_whitespace(text) == \
'''
if True:
    """#"""
elif False:
    "bar"
'''

    def test_quotes(self):
        text = """
        print ''' aslkjfnas kjdfn
askdjfnaskfd fkasnf dknf sadkfjn asdkfjna sdakjn
asdkfjnads kfajns '''
        if x:
            print y
"""
        assert adjust_whitespace(text) == \
"""
print ''' aslkjfnas kjdfn
askdjfnaskfd fkasnf dknf sadkfjn asdkfjna sdakjn
asdkfjnads kfajns '''
if x:
    print y
"""
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.