test_common.py :  » Development » Bicycle-Repair-Man » bicyclerepair-0.9 » bike » query » 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 » query » test_common.py
#!/usr/bin/env python
import setpath
import unittest
import os
import compiler
from bike.testutils import *
from bike.parsing.load import getSourceNode
from bike.parsing.fastparser import fastparser
from bike.parsing.fastparserast import Module,Class
from common import indexToCoordinates,getScopeForLine,walkLinesContainingStrings,translateSourceCoordsIntoASTNode


class TestGetScopeForLine(BRMTestCase):

    def test_worksWithFunctionScope(self):
        src = trimLines("""
        class a:
            def foo():
                pass
        """)
        node = createAST(src)
        self.assertEqual(getScopeForLine(node,3).name,"foo")

    def test_worksWithModuleScope(self):
        src = trimLines("""
        class TheClass:
            pass
        a = TheClass()
        """)
        node = createAST(src)
        assert isinstance(getScopeForLine(node,3),Module)

    def test_worksWithInlineClass(self):
        src = trimLines("""
        class TheClass: pass""")
        node = createAST(src)
        assert isinstance(getScopeForLine(node,1),Class)


class TestIndexToCoordinates(BRMTestCase):

    def test_worksOnSingleLineString(self):
        src = trimLines('''
        foo bah
        ''')
        x,y = indexToCoordinates(src,src.index("bah"))
        self.assertEqual(x,4)
        self.assertEqual(y,0)
        x,y = indexToCoordinates(src,src.index("foo"))
        self.assertEqual(x,0)
        self.assertEqual(y,0)

    def test_worksOnMultilLineString(self):
        src = trimLines('''
        foo bah
        baz boh
        ''')
        x,y = indexToCoordinates(src,src.index("boh"))
        self.assertEqual(x,4)
        self.assertEqual(y,1)


class TestTranslateSourceCoordsIntoASTNode(BRMTestCase):

    def test_worksOnImport(self):
        src = trimLines('''
        from foo import bar,baz
        ''')
        createSourceNodeAt(src,"mymodule")
        filename = os.path.abspath("mymodule.py")
        node = translateSourceCoordsIntoASTNode(filename, 1, 17)
        assert node.name == 'bar'

    def test_worksOnMultiline(self):
        src = trimLines("""
        def foo(x,
                y,
                z):
            return x*y*z
        """)
        createSourceNodeAt(src,"mymodule")
        filename = os.path.abspath("mymodule.py")
        node = translateSourceCoordsIntoASTNode(filename, 3, 8)
        assert node.name == 'z'

        


class TestMatchFinder(BRMTestCase):

    def test_visitLambda(self):
        from common import MatchFinder
        finder = MatchFinder()
        src = '''x = lambda a, b, c=None, d=None: (a + b) and c or d'''
        ast = compiler.parse(src)
        finder.reset(src)
        compiler.walk(ast, finder)


class TestWalkLinesContainingStrings(BRMTestCase):
    def test_walksClasses(self):
        src=trimLines("""
        class TestClass(a,
                        baseclass):
            pass
        """)
        class MyWalker:            
            def visitClass(self, node):
                self.basenames = []
                for name in node.bases:
                    self.basenames.append(name.name)
        
        writeTmpTestFile(src)
        srcnode = getSourceNode(tmpfile)
        walker = MyWalker()
        walkLinesContainingStrings(srcnode.fastparseroot,walker,
                                   "baseclass")
        self.assertEqual(["a","baseclass"],walker.basenames)


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.