test_fastparserast.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_fastparserast.py
#!/usr/bin/env python
import unittest
from fastparserast import *
from fastparser import fastparser
from bike.query.getTypeOf import getTypeOf
from bike.testutils import *

class TestGetModule(BRMTestCase):

    def test_getRootWorksAfterDefinedByCreateSourceNodeAt(self):
        src=trimLines("""
        class TheClass:
            pass
        a = TheClass()
        """)
        root = createSourceNodeAt(src,"mymodule")
        assert root == getRoot()

    def test_returnsNoneIfModuleDoesntExist(self):
        assert getModule(tmpfile) == None


class TestGetEndLine(BRMTestCase):
    def test_returnsEndLineWithSimpleFunction(self):
        src = trimLines("""
        class TheClass:
            def theMethod():
                pass
        def foo():
            b = TheClass()
            return b
        a = foo()
        a.theMethod()
        """)
        root = fastparser(src)
        fn = getTypeOf(root,"foo")
        self.assertEqual(fn.getEndLine(),7)

    def test_worksWithFunctionsThatHaveEmptyLinesInThem(self):
        src = fnWithEmptyLineInIt
        root = fastparser(src)
        fn = getTypeOf(root,"TheClass.theFunction")
        self.assertEqual(fn.getEndLine(),8)

class TestGetBaseClassNames(BRMTestCase):
    def test_worksForClassHierarchy(self):
        src = trimLines("""
        class root:
            def theMethod():
                pass

        class a(root):
            def theMethod():
                pass    

        class b(root):
            pass

        class TheClass(a,b):
            def theMethod():
                pass

        rootinstance = root()
        rootinstance.theMethod()
        """)
        #classes = getASTNodeFromSrc(src,"Source").fastparseroot.getChildNodes()
        classes = createAST(src).fastparseroot.getChildNodes()
        self.assertEqual(classes[3].getBaseClassNames(),['a','b'])

    def test_returnsEmptyListForClassWithNoBases(self):
        src = trimLines("""
        class root:
            pass
        """)
        #classes = getASTNodeFromSrc(src,"Source").fastparseroot.getChildNodes()
        classes = createAST(src).fastparseroot.getChildNodes()        
        self.assertEqual(classes[0].getBaseClassNames(),[])


class TestGetMaskedLines(BRMTestCase):
    def test_doit(self):
        src =trimLines("""
        class foo: #bah
            pass
        """)
        mod = createAST(src).fastparseroot
        lines = mod.getMaskedModuleLines()
        assert lines[0] == "class foo: #***\n"


class TestGetLinesNotIncludingThoseBelongingToChildScopes(BRMTestCase):
    def test_worksForModule(self):
        src =trimLines("""
        class TheClass:
            def theMethod():
                pass
        def foo():
            b = TheClass()
            return b
        a = foo()
        a.theMethod()
        """)
        mod = createAST(src).fastparseroot
        self.assertEqual(''.join(mod.getLinesNotIncludingThoseBelongingToChildScopes()),
                         trimLines("""
                         a = foo()
                         a.theMethod()
                         """))

    def test_worksForModuleWithSingleLineFunctions(self):
        src=trimLines("""
        a = blah()
        def foo(): pass
        b = 1
        """)
        mod = createAST(src).fastparseroot
        lines = mod.getLinesNotIncludingThoseBelongingToChildScopes()
        self.assertEqual(''.join(lines),
                         trimLines("""
                         a = blah()
                         b = 1
                         """))


    def test_worksForSingleLineFunction(self):
        src=trimLines("""
        a = blah()
        def foo(): pass
        b = 1
        """)
        fn = createAST(src).fastparseroot.getChildNodes()[0]
        lines = fn.getLinesNotIncludingThoseBelongingToChildScopes()
        self.assertEqual(''.join(lines),
                         trimLines("""
                         def foo(): pass
                         """))


fnWithEmptyLineInIt = """
class TheClass:
    def theFunction():
        a = foo()

        print 'a'

    # end of function
"""

if __name__ == "__main__":
    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.