test_fastparser.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_fastparser.py
#!/usr/bin/env python
import unittest
from fastparser import *
from bike.parsing.load import *
from bike.parsing.fastparserast import *
from bike.testutils import *

class TestFastParser(BRMTestCase):
    def test_doesntGetClassDeclsInMLStrings(self):
        src = trimLines('''
        """
        class foo bah
        """
        ''')
        root = fastparser(src)
        assert root.getChildNodes() == []

    def test_evaluatesMLStringWithQuoteInIt(self):
        src = trimLines('''
        """some ml comment inclosing a " """
        def foo:
            pass 
        " hello "
        ''')
        root = fastparser(src)
        assert root.getChildNodes() != []

    def test_handlesClassDefsWithTwoSpacesInDecl(self):
        src = trimLines('''
        class  foo: pass
        ''')
        root = fastparser(src)
        assert root.getChildNodes() != []
        
    def test_handlesFnDefsWithTwoSpacesInDecl(self):
        src = trimLines('''
        def  foo: pass
        ''')
        root = fastparser(src)
        assert root.getChildNodes() != []


MLStringWithQuoteInIt = """
\"\"\"some ml comment inclosing a \" \"\"\"
def foo:
    pass
\" hello \"
"""

def load(path):
    files = getFilesForName(path)
    for fname in files:
        src = file(fname).read()
        fastparser(src)
        #print fname
        #myroot = parseFile(fname)



def fastparsetreeToString(root):
    class stringholder: pass
    s = stringholder()
    s.mystr = ""
    s.tabstr = ""
    def t2s(node):
        if isinstance(node, Class):
            s.mystr+=s.tabstr+"class "+node.name+"\n"
            s.tabstr+="\t"
            for n in node.getChildNodes():
                t2s(n)
            s.tabstr = s.tabstr[:-1]

        elif isinstance(node, Function):
            s.mystr+=s.tabstr+"function "+node.name+"\n"
            s.tabstr+="\t"
            for n in node.getChildNodes():
                t2s(n)
            s.tabstr = s.tabstr[:-1]


    for n in root.getChildNodes():
        t2s(n)
    return s.mystr


def compilerParseTreeToString(root):
    try:
        class TreeVisitor:
            def __init__(self):
                self.mystr = ""
                self.tabstr = ""

            def visitClass(self, node):
                self.mystr+=self.tabstr+"class "+node.name+"\n"
                self.tabstr+="\t"
                for child in node.getChildNodes():
                    self.visit(child)
                self.tabstr = self.tabstr[:-1]

            def visitFunction(self, node):
                self.mystr+=self.tabstr+"function "+node.name+"\n"
                self.tabstr+="\t"
                for child in node.getChildNodes():
                    self.visit(child)
                self.tabstr = self.tabstr[:-1]

        return compiler.walk(root, TreeVisitor()).mystr

    except:
        log.exception("ex")
        import sys
        sys.exit(0)


def compareCompilerWithFastparserOverPath(path):
    from bike.parsing.load import getFilesForName
    files = getFilesForName(path)
    for fname in files:
        if fname.endswith("bdist_wininst.py"): continue
        log.info(fname)
        src = file(fname).read()
        try:
            compiler_root = compiler.parse(src)
        except SyntaxError:
            continue
        fastparse_root = fastparser(src)
        str1 = fastparsetreeToString(fastparse_root)
        str2 = compilerParseTreeToString(compiler_root)
        assert str1 == str2, "\n"+"-"*70+"\n"+str1+"-"*70+"\n"+str2


def timeParseOfPythonLibrary(path):
    import time
    t1 = time.time()
    files = getFilesForName(path)
    import sys
    for fname in files:
        if fname.endswith("bdist_wininst.py"): continue
        src = file(fname).read()
        fastparser(src)
    print "\n", time.time()-t1


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_SoakTestFastparser(BRMTestCase):

        def test_A_timeParseOfPythonLibrary(self):
            timeParseOfPythonLibrary("/usr/local/lib/python2.2")

        def test_parsesPythonLibraryCorrectly(self):
            print ""
            compareCompilerWithFastparserOverPath("/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.