Exemplo n.º 1
0
 def extractLocalVariable(self, filename_path, begin_line, begin_col,
                          end_line, end_col, variablename):
     filename_path = self.normalizeFilename(filename_path)
     extractVariable.extractLocalVariable(filename_path,
                                          coords(begin_line, begin_col),
                                          coords(end_line, end_col),
                                          variablename)
Exemplo n.º 2
0
    def test_extractsFromFirstBlockOfIfElseStatement(self):
        srcBefore=trimLines("""
        def foo():
            if bah:
                print \"hello1\"
                print \"hello2\"
                
            elif foo:
                pass
        """)
        srcAfter=trimLines("""
        def foo():
            if bah:
                newFunction()
                print \"hello2\"
                
            elif foo:
                pass

        def newFunction():
            print \"hello1\"
        """)
        src = helper(srcBefore, coords(3, 0),
                         coords(4, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 3
0
    def test_extractAssignedVariableUsedInOuterForLoop(self):
        srcBefore=trimLines("""
        def foo():
            b = 0
            for a in range(1, 3):
                b = b+1
                while b != 2:
                    print a
                    b += 1
        """)
        srcAfter=trimLines("""
        def foo():
            b = 0
            for a in range(1, 3):
                b = b+1
                while b != 2:
                    b = newFunction(a, b)

        def newFunction(a, b):
            print a
            b += 1
            return b
        """)

        src = helper(srcBefore, coords(6, 0),
                         coords(8, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 4
0
    def test_extractsFromFirstBlockOfIfElseStatement(self):
        srcBefore=trimLines("""
        def foo():
            if bah:
                print \"hello1\"
                print \"hello2\"
                
            elif foo:
                pass
        """)
        srcAfter=trimLines("""
        def foo():
            if bah:
                newFunction()
                print \"hello2\"
                
            elif foo:
                pass

        def newFunction():
            print \"hello1\"
        """)
        src = helper(srcBefore, coords(3, 0),
                         coords(4, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 5
0
    def test_newMethodHasArgumentsForUsedTemporarys(self):

        srcBefore=trimLines("""
        class MyClass:
            def myMethod(self, c): 
                a = something()
                b = somethingelse()
                print a + b + c + d
                print \"hello\"
                dosomethingelse(a, b)
        """)                
        srcAfter=trimLines("""
        class MyClass:
            def myMethod(self, c): 
                a = something()
                b = somethingelse()
                self.newMethod(a, b, c)
                dosomethingelse(a, b)

            def newMethod(self, a, b, c):
                print a + b + c + d
                print \"hello\"
        """)

        src = helper(srcBefore, coords(5, 8), coords(6, 21), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 6
0
    def test_newMethodHasArgumentsForUsedTemporarys(self):

        srcBefore = trimLines("""
        class MyClass:
            def myMethod(self, c): 
                a = something()
                b = somethingelse()
                print a + b + c + d
                print \"hello\"
                dosomethingelse(a, b)
        """)
        srcAfter = trimLines("""
        class MyClass:
            def myMethod(self, c): 
                a = something()
                b = somethingelse()
                self.newMethod(a, b, c)
                dosomethingelse(a, b)

            def newMethod(self, a, b, c):
                print a + b + c + d
                print \"hello\"
        """)

        src = helper(srcBefore, coords(5, 8), coords(6, 21), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 7
0
 def extractLocalVariable(self,filename_path, begin_line, begin_col,
                          end_line, end_col, variablename):
     filename_path = self.normalizeFilename(filename_path)
     extractVariable.extractLocalVariable(filename_path,
                                          coords(begin_line, begin_col),
                                          coords(end_line, end_col),
                                          variablename)
Exemplo n.º 8
0
    def test_extractAssignedVariableUsedInOuterForLoop(self):
        srcBefore = trimLines("""
        def foo():
            b = 0
            for a in range(1, 3):
                b = b+1
                while b != 2:
                    print a
                    b += 1
        """)
        srcAfter = trimLines("""
        def foo():
            b = 0
            for a in range(1, 3):
                b = b+1
                while b != 2:
                    b = newFunction(a, b)

        def newFunction(a, b):
            print a
            b += 1
            return b
        """)

        src = helper(srcBefore, coords(6, 0), coords(8, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 9
0
 def test_getsTabwidthForFunctionAtRootScope(self):
     src = trimLines("""
     def myFn(self):
         pass
     """)
     sourcenode = writeSourceAndCreateNode(src)
     em = ExtractMethod(sourcenode, coords(2, 0), coords(2, 9), "foobah")
     self.assertEqual(em.getTabwidthOfParentFunction(), 0)
Exemplo n.º 10
0
 def extract(self, filename_path, 
             begin_line, begin_col,
             end_line, end_col, 
             name):
     filename_path = self.normalizeFilename(filename_path)
     extractMethod.extractMethod(filename_path,
                                 coords(begin_line, begin_col), 
                                 coords(end_line, end_col), name)
Exemplo n.º 11
0
 def test_getsTabwidthForFunctionAtRootScope(self):
     src=trimLines("""
     def myFn(self):
         pass
     """)
     sourcenode = writeSourceAndCreateNode(src)
     em = ExtractMethod(sourcenode, coords(2, 0),
                          coords(2, 9), "foobah")
     self.assertEqual(em.getTabwidthOfParentFunction(), 0)
Exemplo n.º 12
0
 def test_getsTabwidthForSimpleMethod(self):
     src = trimLines("""
     class MyClass:
         def myMethod(self):
             pass
     """)
     sourcenode = writeSourceAndCreateNode(src)
     em = ExtractMethod(sourcenode, coords(3, 11), coords(3, 17), "foobah")
     self.assertEqual(em.getTabwidthOfParentFunction(), 4)
Exemplo n.º 13
0
 def test_getsTabwidthForSimpleMethod(self):
     src=trimLines("""
     class MyClass:
         def myMethod(self):
             pass
     """)
     sourcenode = writeSourceAndCreateNode(src)
     em = ExtractMethod(sourcenode, coords(3, 11),
                          coords(3, 17), "foobah")
     self.assertEqual(em.getTabwidthOfParentFunction(), 4)
Exemplo n.º 14
0
 def test_getsSingleLinePassStatementWhenWholeLineIsHighlighted(self):
     src = trimLines("""
     class MyClass:
         def myMethod(self):
             pass
     """)
     sourcenode = writeSourceAndCreateNode(src)
     em = ExtractMethod(sourcenode, coords(3, 0), coords(3, 12), "foobah")
     em.getRegionToBuffer()
     self.assertEqual(len(em.extractedLines), 1)
     self.assertEqual(em.extractedLines[0], "pass\n")
Exemplo n.º 15
0
    def test_doesntBarfWhenEncountersMethodCalledOnCreatedObj(self):
        srcBefore = trimLines(r"""
        results = QueryEngine(q).foo()
        """)
        srcAfter = trimLines(r"""
        newFunction()

        def newFunction():
            results = QueryEngine(q).foo()
        """)
        src = helper(srcBefore, coords(1, 0), coords(2, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 16
0
 def test_getsSingleLinePassStatementWhenWholeLineIsHighlighted(self):
     src=trimLines("""
     class MyClass:
         def myMethod(self):
             pass
     """)
     sourcenode = writeSourceAndCreateNode(src)
     em = ExtractMethod(sourcenode, coords(3, 0),
                          coords(3, 12), "foobah")
     em.getRegionToBuffer()
     self.assertEqual(len(em.extractedLines), 1)
     self.assertEqual(em.extractedLines[0], "pass\n")
Exemplo n.º 17
0
 def test_getsHighlightedSingleLinePassStatement(self):
     src=trimLines("""
     class MyClass:
         def myMethod(self):
             pass
     """)
     sourcenode = createAST(src)
     em = ExtractMethod(sourcenode, coords(3, 8),
                          coords(3, 12), "foobah")
     em.getRegionToBuffer()
     self.assertEqual(len(em.extractedLines), 1)
     self.assertEqual(em.extractedLines[0], "pass\n")
Exemplo n.º 18
0
    def test_doesntBarfWhenEncountersMethodCalledOnCreatedObj(self):
        srcBefore=trimLines(r"""
        results = QueryEngine(q).foo()
        """)
        srcAfter=trimLines(r"""
        newFunction()

        def newFunction():
            results = QueryEngine(q).foo()
        """)
        src = helper(srcBefore, coords(1, 0),
                         coords(2, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 19
0
 def test_getsHighlightedSubstringOfLine(self):
     src = trimLines("""
     class MyClass:
         def myMethod(self):
             if a == 3:
                 pass
     """)
     region = trimLines("""
     a == 3
     """)
     sourcenode = writeSourceAndCreateNode(src)
     em = ExtractMethod(sourcenode, coords(3, 11), coords(3, 17), "foobah")
     em.getRegionToBuffer()
     self.assertEqual(em.extractedLines, region.splitlines(1))
Exemplo n.º 20
0
    def test_worksIfNoLinesBeforeExtractedCode(self):
        srcBefore = trimLines(r"""
        print a + 2
        f(b)	
        """)
        srcAfter = trimLines(r"""
        newFunction()

        def newFunction():
            print a + 2
            f(b)	
        """)
        src = helper(srcBefore, coords(1, 0), coords(2, 4), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 21
0
 def test_getsMultiLineRegionWhenJustRegionIsHighlighted(self):
     src = trimLines("""
     class MyClass:
         def myMethod(self):
             print 'hello'
             pass
     """)
     region = trimLines("""
     print 'hello'
     pass
     """)
     sourcenode = writeSourceAndCreateNode(src)
     em = ExtractMethod(sourcenode, coords(3, 8), coords(4, 12), "foobah")
     em.getRegionToBuffer()
     self.assertEqual(em.extractedLines, region.splitlines(1))
Exemplo n.º 22
0
    def test_worksIfNoLinesBeforeExtractedCode(self):
        srcBefore=trimLines(r"""
        print a + 2
        f(b)	
        """)
        srcAfter=trimLines(r"""
        newFunction()

        def newFunction():
            print a + 2
            f(b)	
        """)
        src = helper(srcBefore, coords(1, 0),
                         coords(2, 4), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 23
0
    def test_extractsPassWhenFunctionAllOnOneLine(self):
        srcBefore=trimLines("""
        class MyClass:
            def myMethod(self): pass # comment
        """)

        srcAfter=trimLines("""
        class MyClass:
            def myMethod(self): self.newMethod() # comment

            def newMethod(self):
                pass
        """)
        src = helper(srcBefore, coords(2, 24), coords(2, 28),"newMethod")
        self.assertEqual(src,srcAfter)
Exemplo n.º 24
0
    def test_extractsQueryWhenFunctionAllOnOneLine(self):
        srcBefore = trimLines("""
        class MyClass:
            def myMethod(self, a): print a # comment
        """)

        srcAfter = trimLines("""
        class MyClass:
            def myMethod(self, a): self.newMethod(a) # comment

            def newMethod(self, a):
                print a
        """)
        src = helper(srcBefore, coords(2, 27), coords(2, 34), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 25
0
    def test_extractsPassWhenFunctionAllOnOneLine(self):
        srcBefore = trimLines("""
        class MyClass:
            def myMethod(self): pass # comment
        """)

        srcAfter = trimLines("""
        class MyClass:
            def myMethod(self): self.newMethod() # comment

            def newMethod(self):
                pass
        """)
        src = helper(srcBefore, coords(2, 24), coords(2, 28), "newMethod")
        self.assertEqual(src, srcAfter)
Exemplo n.º 26
0
 def test_getsHighlightedSubstringOfLine(self):
     src=trimLines("""
     class MyClass:
         def myMethod(self):
             if a == 3:
                 pass
     """)
     region=trimLines("""
     a == 3
     """)
     sourcenode = writeSourceAndCreateNode(src)
     em = ExtractMethod(sourcenode, coords(3, 11),
                          coords(3, 17), "foobah")
     em.getRegionToBuffer()
     self.assertEqual(em.extractedLines, region.splitlines(1))
Exemplo n.º 27
0
    def test_extractsQueryWhenFunctionAllOnOneLine(self):
        srcBefore=trimLines("""
        class MyClass:
            def myMethod(self, a): print a # comment
        """)

        srcAfter=trimLines("""
        class MyClass:
            def myMethod(self, a): self.newMethod(a) # comment

            def newMethod(self, a):
                print a
        """)
        src = helper(srcBefore, coords(2, 27), coords(2, 34), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 28
0
    def test_extractsPass(self):
        srcBefore=trimLines("""
        class MyClass:
            def myMethod(self):
                pass
        """)
        srcAfter=trimLines("""
        class MyClass:
            def myMethod(self):
                self.newMethod()

            def newMethod(self):
                pass
        """)
        src = helper(srcBefore, coords(3, 8), coords(3, 12), "newMethod")
        self.assertEqual(src,srcAfter)
Exemplo n.º 29
0
    def test_extractsPass(self):
        srcBefore = trimLines("""
        class MyClass:
            def myMethod(self):
                pass
        """)
        srcAfter = trimLines("""
        class MyClass:
            def myMethod(self):
                self.newMethod()

            def newMethod(self):
                pass
        """)
        src = helper(srcBefore, coords(3, 8), coords(3, 12), "newMethod")
        self.assertEqual(src, srcAfter)
Exemplo n.º 30
0
    def test_extractsConditionalFromExpression(self):
        srcBefore = trimLines("""
        def foo():
            if 123+3:
                print aoue
        """)
        srcAfter = trimLines("""
        def foo():
            if newFunction():
                print aoue

        def newFunction():
            return 123+3
        """)
        src = helper(srcBefore, coords(2, 7), coords(2, 12), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 31
0
    def test_extractsPassWhenSelectionCoordsAreReversed(self):
        srcBefore = trimLines("""
        class MyClass:
            def myMethod(self):
                pass
        """)
        srcAfter = trimLines("""
        class MyClass:
            def myMethod(self):
                self.newMethod()

            def newMethod(self):
                pass
        """)
        src = helper(srcBefore, coords(3, 12), coords(3, 8), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 32
0
    def test_doesntHaveToBeInsideAFunction(self):
        srcBefore = trimLines(r"""
        a = 1
        print a + 2
        f(b)	
        """)
        srcAfter = trimLines(r"""
        a = 1
        newFunction(a)

        def newFunction(a):
            print a + 2
            f(b)	
        """)
        src = helper(srcBefore, coords(2, 0), coords(3, 4), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 33
0
    def test_extractsPassWhenSelectionCoordsAreReversed(self):
        srcBefore=trimLines("""
        class MyClass:
            def myMethod(self):
                pass
        """)
        srcAfter=trimLines("""
        class MyClass:
            def myMethod(self):
                self.newMethod()

            def newMethod(self):
                pass
        """)
        src = helper(srcBefore, coords(3, 12), coords(3, 8), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 34
0
 def test_getsMultiLineRegionWhenJustRegionIsHighlighted(self):
     src=trimLines("""
     class MyClass:
         def myMethod(self):
             print 'hello'
             pass
     """)
     region=trimLines("""
     print 'hello'
     pass
     """)
     sourcenode = writeSourceAndCreateNode(src)
     em = ExtractMethod(sourcenode, coords(3, 8),
                          coords(4, 12), "foobah")
     em.getRegionToBuffer()
     self.assertEqual(em.extractedLines, region.splitlines(1))
Exemplo n.º 35
0
    def test_extractsAssignToAttribute(self):
        srcBefore = trimLines("""
        def simulateLoad(path):
            item = foo()
            item.decl = line
        """)
        srcAfter = trimLines("""
        def simulateLoad(path):
            item = foo()
            newFunction(item)

        def newFunction(item):
            item.decl = line
        """)

        src = helper(srcBefore, coords(3, 0), coords(4, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 36
0
    def test_doesntHaveToBeInsideAFunction(self):
        srcBefore=trimLines(r"""
        a = 1
        print a + 2
        f(b)	
        """)
        srcAfter=trimLines(r"""
        a = 1
        newFunction(a)

        def newFunction(a):
            print a + 2
            f(b)	
        """)
        src = helper(srcBefore, coords(2, 0),
                         coords(3, 4), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 37
0
    def test_extractsForLoopUsingLoopVariable(self):
        srcBefore = trimLines("""
        def foo():
            for i in range(1, 3):
                print i
        """)
        srcAfter = trimLines("""
        def foo():
            for i in range(1, 3):
                newFunction(i)

        def newFunction(i):
            print i
        """)

        src = helper(srcBefore, coords(3, 0), coords(4, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 38
0
    def test_extractsConditionalFromExpression(self):
        srcBefore=trimLines("""
        def foo():
            if 123+3:
                print aoue
        """)
        srcAfter=trimLines("""
        def foo():
            if newFunction():
                print aoue

        def newFunction():
            return 123+3
        """)
        src = helper(srcBefore, coords(2, 7),
                         coords(2, 12), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 39
0
    def test_getsMultiLineRegionWhenRegionLinesAreHighlighted(self):
        src=trimLines("""
        class MyClass:
            def myMethod(self):
                print 'hello'
                pass

        """)
        region=trimLines("""
        print 'hello'
        pass
        """)
        sourcenode = createAST(src)
        em = ExtractMethod(sourcenode, coords(3, 0),
                             coords(5, 0), "foobah")
        em.getRegionToBuffer()
        self.assertEqual(em.extractedLines, region.splitlines(1))
Exemplo n.º 40
0
    def test_extractsAssignToAttribute(self):
        srcBefore=trimLines("""
        def simulateLoad(path):
            item = foo()
            item.decl = line
        """)
        srcAfter=trimLines("""
        def simulateLoad(path):
            item = foo()
            newFunction(item)

        def newFunction(item):
            item.decl = line
        """)

        src = helper(srcBefore, coords(3, 0),
                         coords(4, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 41
0
    def test_extractsPassFromForLoop(self):
        srcBefore = trimLines("""
        class MyClass:
            def myMethod(self): # comment
                for i in foo:
                    pass
        """)
        srcAfter = trimLines("""
        class MyClass:
            def myMethod(self): # comment
                for i in foo:
                    self.newMethod()

            def newMethod(self):
                pass
        """)
        src = helper(srcBefore, coords(4, 12), coords(4, 16), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 42
0
    def test_extractsForLoopUsingLoopVariable(self):
        srcBefore=trimLines("""
        def foo():
            for i in range(1, 3):
                print i
        """)
        srcAfter=trimLines("""
        def foo():
            for i in range(1, 3):
                newFunction(i)

        def newFunction(i):
            print i
        """)

        src = helper(srcBefore, coords(3, 0),
                         coords(4, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 43
0
    def test_extractsPassFromForLoop(self):
        srcBefore=trimLines("""
        class MyClass:
            def myMethod(self): # comment
                for i in foo:
                    pass
        """)                
        srcAfter=trimLines("""
        class MyClass:
            def myMethod(self): # comment
                for i in foo:
                    self.newMethod()

            def newMethod(self):
                pass
        """)
        src = helper(srcBefore, coords(4, 12), coords(4, 16), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 44
0
    def test_extractsExpression(self):
        srcBefore=trimLines("""
        class MyClass:
            def myMethod(self): # comment
                a = 32
                b = 2 + a * 1 + 2
        """)
        srcAfter=trimLines("""
        class MyClass:
            def myMethod(self): # comment
                a = 32
                b = 2 + self.newMethod(a) + 2

            def newMethod(self, a):
                return a * 1
        """)
        src = helper(srcBefore, coords(4, 16), coords(4, 21), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 45
0
    def test_extractsExpression(self):
        srcBefore = trimLines("""
        class MyClass:
            def myMethod(self): # comment
                a = 32
                b = 2 + a * 1 + 2
        """)
        srcAfter = trimLines("""
        class MyClass:
            def myMethod(self): # comment
                a = 32
                b = 2 + self.newMethod(a) + 2

            def newMethod(self, a):
                return a * 1
        """)
        src = helper(srcBefore, coords(4, 16), coords(4, 21), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 46
0
    def test_canExtractQueryFromNestedIfStatement(self):
        srcBefore = trimLines("""
        def theFunction():
            if foo: # comment
                if bah:
                    pass
        """)
        srcAfter = trimLines("""
        def theFunction():
            if foo: # comment
                if newFunction():
                    pass

        def newFunction():
            return bah
        """)
        src = helper(srcBefore, coords(3, 11), coords(3, 14), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 47
0
    def test_extractsExpression2(self):
        srcBefore = trimLines("""
        class MyClass:
            def myMethod(self): # comment
                g = 32
                assert output.thingy(g) == \"bah\"
        """)
        srcAfter = trimLines("""
        class MyClass:
            def myMethod(self): # comment
                g = 32
                assert self.newMethod(g) == \"bah\"

            def newMethod(self, g):
                return output.thingy(g)
        """)
        src = helper(srcBefore, coords(4, 15), coords(4, 31), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 48
0
    def test_extractsExpression2(self):
        srcBefore=trimLines("""
        class MyClass:
            def myMethod(self): # comment
                g = 32
                assert output.thingy(g) == \"bah\"
        """)
        srcAfter=trimLines("""
        class MyClass:
            def myMethod(self): # comment
                g = 32
                assert self.newMethod(g) == \"bah\"

            def newMethod(self, g):
                return output.thingy(g)
        """)
        src = helper(srcBefore, coords(4, 15), coords(4, 31), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 49
0
    def test_extractCodeAfterCommentInMiddleOfFnDoesntRaiseParseException(self):
        srcBefore=trimLines("""
        def theFunction():
            print 1
            # comment
            print 2
        """)
        srcAfter=trimLines("""
        def theFunction():
            print 1
            # comment
            newFunction()

        def newFunction():
            print 2
        """)
        src = helper(srcBefore, coords(4, 0),
                         coords(5, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 50
0
    def test_extractsAugAssign(self):
        srcBefore = trimLines("""
        def foo():
            a = 3
            a += 1
            print a
        """)
        srcAfter = trimLines("""
        def foo():
            a = 3
            a = newFunction(a)
            print a

        def newFunction(a):
            a += 1
            return a
        """)
        src = helper(srcBefore, coords(3, 0), coords(4, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 51
0
    def test_worksWhenUserSelectsABlockButDoesntSelectTheHangingDedent(self):
        srcBefore = trimLines("""
        class MyClass:
            def myMethod(self): # comment
                for i in foo:
                    pass
        """)
        srcAfter = trimLines("""
        class MyClass:
            def myMethod(self): # comment
                for i in foo:
                    self.newMethod()

            def newMethod(self):
                pass
        """)

        src = helper(srcBefore, coords(4, 8), coords(4, 16), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 52
0
    def test_extractCodeAfterCommentInMiddleOfFnDoesntRaiseParseException(
            self):
        srcBefore = trimLines("""
        def theFunction():
            print 1
            # comment
            print 2
        """)
        srcAfter = trimLines("""
        def theFunction():
            print 1
            # comment
            newFunction()

        def newFunction():
            print 2
        """)
        src = helper(srcBefore, coords(4, 0), coords(5, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 53
0
    def test_canExtractQueryFromNestedIfStatement(self):
        srcBefore=trimLines("""
        def theFunction():
            if foo: # comment
                if bah:
                    pass
        """)
        srcAfter=trimLines("""
        def theFunction():
            if foo: # comment
                if newFunction():
                    pass

        def newFunction():
            return bah
        """)
        src = helper(srcBefore, coords(3, 11),
                         coords(3, 14), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 54
0
    def test_worksWhenAssignmentsToTuples(self):
        srcBefore = trimLines("""
        class MyClass:
            def myMethod(self): 
                a, b, c = 35, 36, 37
                print a + b
        """)
        srcAfter = trimLines("""
        class MyClass:
            def myMethod(self): 
                a, b, c = 35, 36, 37
                self.newMethod(a, b)

            def newMethod(self, a, b):
                print a + b
        """)

        src = helper(srcBefore, coords(4, 8), coords(4, 19), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 55
0
    def test_extractWhileLoopVariableIncrement(self):
        srcBefore = trimLines("""
        def foo():
            a = 0
            while a != 3:
                a = a+1
        """)
        srcAfter = trimLines("""
        def foo():
            a = 0
            while a != 3:
                a = newFunction(a)

        def newFunction(a):
            a = a+1
            return a
        """)
        src = helper(srcBefore, coords(4, 0), coords(5, 0), "newFunction")
        self.assertEqual(srcAfter, src)
Exemplo n.º 56
0
    def test_worksWhenUserSelectsABlockButDoesntSelectTheHangingDedent(self):
        srcBefore=trimLines("""
        class MyClass:
            def myMethod(self): # comment
                for i in foo:
                    pass
        """)
        srcAfter=trimLines("""
        class MyClass:
            def myMethod(self): # comment
                for i in foo:
                    self.newMethod()

            def newMethod(self):
                pass
        """)

        src = helper(srcBefore, coords(4, 8), coords(4, 16), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 57
0
    def test_worksWhenAssignmentsToTuples(self):
        srcBefore=trimLines("""
        class MyClass:
            def myMethod(self): 
                a, b, c = 35, 36, 37
                print a + b
        """)
        srcAfter=trimLines("""
        class MyClass:
            def myMethod(self): 
                a, b, c = 35, 36, 37
                self.newMethod(a, b)

            def newMethod(self, a, b):
                print a + b
        """)

        src = helper(srcBefore, coords(4, 8), coords(4, 19), "newMethod")
        self.assertEqual(srcAfter, src)
Exemplo n.º 58
0
    def test_doesntMessUpTheNextFunctionOrClass(self):
        srcBefore=trimLines("""
        def myFunction():
            a = 3
            print \"hello\"+a  # extract me
            
        class MyClass:
            def myMethod(self):
                b = 12      # extract me
                c = 3       # and me
                d = 2       # and me
                print b, c
        """)
        srcAfter=trimLines("""
        def myFunction():
            a = 3
            newFunction(a)

        def newFunction(a):
            print \"hello\"+a  # extract me
            
        class MyClass:
            def myMethod(self):
                b = 12      # extract me
                c = 3       # and me
                d = 2       # and me
                print b, c
        """)

        # extract code on one line
        src = helper(srcBefore, coords(3, 4),
                         coords(3, 34), "newFunction")
        self.assertEqual(srcAfter, src)

        from bike.transformer.save import resetOutputQueue

        resetOutputQueue()
        
        # extract code on 2 lines (most common user method)
        src = helper(srcBefore, coords(3, 0),
                         coords(4, 0), "newFunction")
        self.assertEqual(srcAfter, src)