test_parserutils.py :  » Development » Bicycle-Repair-Man » bicyclerepair-0.9 » bike » parsing » 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 » Development » Bicycle Repair Man 
Bicycle Repair Man » bicyclerepair 0.9 » bike » parsing » test_parserutils.py
#!/usr/bin/env python
import unittest
from parserutils import *
from bike.testutils import *

class TestRemoveEscapedQuotes(BRMTestCase):

    def testMaskEscapedQuotes_MasksEscapedQuotes(self):
        src = '\" \\\\\\\" \' \\\\\\\\\"  \'  \''
        self.assertEqual(maskEscapedQuotes(src),'" **** \' ****"  \'  \'')

class TestMungePythonKeywordsInStrings(BRMTestCase):
    def test_mungesKeywords(self):
        src = '\"\"\"class try while\"\"\" class2 try2 while2 \'\'\' def if for \'\'\' def2 if2 for2'
        self.assertEqual(maskPythonKeywordsInStringsAndComments(src),
                      '"""CLASS TRY WHILE""" class2 try2 while2 \'\'\' DEF IF FOR \'\'\' def2 if2 for2')


class TestSplitLines(BRMTestCase):
    def test_handlesExplicitlyContinuedLineWithComment(self):
        self.assertEqual(splitLogicalLines(explicitlyContinuedLineWithComment),
              ['\n', 'z = a + b + \\  # comment\n  c + d\n', 'pass\n'])

    def test_handlesImplicitlyContinuedLine(self):
        self.assertEqual(splitLogicalLines(implicitlyContinuedLine), 
                         ['\n', 'z = a + b + (\n  c + d)\n', 'pass\n'])

    def test_handlesNestedImplicitlyContinuedLine(self):
        self.assertEqual(splitLogicalLines(implicitlyContinuedLine2), 
                         ['\n', 'z = a + b + ( c + [d\n  + e]\n  + f)   # comment\n', 'pass\n'])


    def test_handlesMultiLineStrings(self):
        self.assertEqual(splitLogicalLines(multilineComment),
                         ['\n', "''' this is an mlc\nso is this\n'''\n", 'pass\n'])
                         

class TestMakeLineParseable(BRMTestCase):
    def test_worksWithIfStatement(self):
        src = "if foo:"
        self.assertEqual(makeLineParseable(src),("if foo: pass"))

    def test_worksWithTryStatement(self):
        src = "try :"
        self.assertEqual(makeLineParseable(src),("try : pass\nexcept: pass"))

    def test_worksOnTryStatementWithCodeInlined(self):
        src = "try : a = 1"
        self.assertEqual(makeLineParseable(src),("try : a = 1\nexcept: pass"))

    def test_worksWithExceptStatement(self):
        src = "except :"
        self.assertEqual(makeLineParseable(src),("try: pass\nexcept : pass"))

    def test_worksWithFinallyStatement(self):
        src = "finally:"
        self.assertEqual(makeLineParseable(src),("try: pass\nfinally: pass"))

    def test_worksWithIfStatement(self):
        src = "if foo:"
        self.assertEqual(makeLineParseable(src),("if foo: pass"))

    def test_worksWithElseStatement(self):
        src = "else :"
        self.assertEqual(makeLineParseable(src),("if 0: pass\nelse : pass"))

    def test_worksWithElifStatement(self):
        src = "elif foo:"
        self.assertEqual(makeLineParseable(src),("if 0: pass\nelif foo: pass"))


def runOverPath(path):
    import compiler
    from parser import ParserError
    from bike.parsing.load import getFilesForName
    files = getFilesForName(path)
    for fname in files:
        print fname
        src = file(fname).read()
        #print src
        src = maskStringsAndRemoveComments(src)

        for logicalline in splitLogicalLines(src):
            #print "logicalline=",logicalline    
            logicalline = logicalline.strip()
            logicalline = makeLineParseable(logicalline)
            try:
                compiler.parse(logicalline)
            except ParserError:
                print "ParserError on logicalline:",logicalline
            except:
                log.exception("caught exception")
                

explicitlyContinuedLineWithComment = """
z = a + b + \  # comment
  c + d
pass
"""

implicitlyContinuedLine = """
z = a + b + (
  c + d)
pass
"""


implicitlyContinuedLine2 = """
z = a + b + ( c + [d
  + e]
  + f)   # comment
pass
"""

multilineComment = """
''' this is an mlc
so is this
'''
pass
"""

if __name__ == "__main__":
    from bike import logging
    logging.init()
    log = logging.getLogger("bike")
    log.setLevel(logging.INFO)
    
    # add soak tests to end of test
    class Z_SoakTest(BRMTestCase):
        def test_linesRunThroughPythonParser(self):
            print ""
            #print splitLogicalLines(file('/usr/local/lib/python2.2/aifc.py').read())
            #runOverPath('/usr/local/lib/python2.2/test/badsyntax_nocaret.py')
            runOverPath('/usr/local/lib/python2.2/')
    unittest.main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.