[Pydev-cvs] org.python.pydev/PySrc/tests test_pyserver.py,NONE,1.1 test_simpleTipper.py,NONE,1.1 tes
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2004-10-08 16:39:16
|
Update of /cvsroot/pydev/org.python.pydev/PySrc/tests In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11038/PySrc/tests Added Files: test_pyserver.py test_simpleTipper.py test_refactoring.py Log Message: --- NEW FILE: test_simpleTipper.py --- ''' @author Fabio Zadrozny ''' import os import sys #make it as if we were executing from the directory above this one (so that we can use pycompletionserver #without the need for it being in the pythonpath) sys.argv[0] = os.path.dirname(sys.argv[0]) #twice the dirname to get the previous level from this file. sys.path.insert(1, os.path.join( os.path.dirname( sys.argv[0] )) ) import unittest import simpleTipper import importsTipper class Test(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) def tearDown(self): unittest.TestCase.tearDown(self) def getDoc1(self): s = \ ''' import math class C(object): \'\'\' CDescription \'\'\' def __init__(self): print dir(self) def a(self): \'\'\' ADescription \'\'\' pass def b(self): self ''' return s def testEnv1(self): comps = simpleTipper.GenerateTip(self.getDoc1(), None, True) import math, inspect checkedMath = False checkedC = False for tup in comps: if tup[0] == 'math': checkedMath = True self.assertEquals(inspect.getdoc(math),tup[1]) elif tup[0] == 'C': checkedC = True self.assert_('CDescription' in tup[1]) self.assert_(checkedC and checkedMath) def testEnv1CToken(self): comps = simpleTipper.GenerateTip(self.getDoc1(), 'C', True) checkedA = False for tup in comps: if tup[0] == 'a': checkedA = True self.assert_('ADescription' in tup[1]) self.assert_(checkedA) def getDoc2(self): s = \ ''' class C(object): def __init__(self): self.a = 1 self.b = 2 ''' return s def testEnv2(self): ''' Now, check completion for C - should return object methods, 'a' and 'b' ''' comps = simpleTipper.GenerateTip(self.getDoc2(), 'C', True) # print comps checkedA = False for tup in comps: if tup[0] == 'a': checkedA = True self.assert_(checkedA) def testImports(self): ''' You can print the results to check... ''' importsTipper.GenerateTip('inspect.') importsTipper.GenerateTip('compiler.') importsTipper.GenerateImportsTip(['scbr']) importsTipper.GenerateImportsTip([ ] ) importsTipper.GenerateImportsTip(['os']) importsTipper.GenerateImportsTip(['os','path']) importsTipper.GenerateImportsTip(['unittest']) importsTipper.GenerateImportsTip(['compiler', 'ast']) importsTipper.GenerateImportsTip(['compiler', 'ast', 'Node']) def testEnv3(self): comps = simpleTipper.GenerateTip(self.getDoc3(), None, False) def getDoc3(self): s= \ ''' import sys class TestLocals(object): sys.path ''' return s if __name__ == '__main__': unittest.main() --- NEW FILE: test_refactoring.py --- ''' Refactoring tests. ''' import os import sys #make it as if we were executing from the directory above this one (so that we can use pycompletionserver #without the need for it being in the pythonpath) sys.argv[0] = os.path.dirname(sys.argv[0]) #twice the dirname to get the previous level from this file. sys.path.insert(1, os.path.join( os.path.dirname( sys.argv[0] )) ) import unittest import refactoring #=============================================================================== # delete #=============================================================================== def delete(filename): '''Removes filename, or does nothing if the file doesn't exist. ''' try: os.remove(filename) except OSError: pass #================================================================================ # createFile #================================================================================ def createFile(filename, contents='', flag='w'): '''Creates the given filename with the given contents. ''' f = file(filename, flag) f.write(contents) f.close() FILE = 'temporary_file.py' def getInitialFile(): s = \ ''' class C: def a(self): a = 2 b = 3 c = a+b #this should be refactored. return c c = C() ''' return s def getRenameRefactored(): s = \ ''' class G: def a(self): a = 2 b = 3 c = a+b #this should be refactored. return c c = G() ''' return s class Test(unittest.TestCase): def getRefactoredFile(self): s = \ ''' class C: def a(self): a = 2 b = 3 c = self.plusMet(a, b) #this should be refactored. return c def plusMet(self, a, b): return a+b c = C() ''' return s def setUp(self): unittest.TestCase.setUp(self) createFile(FILE, getInitialFile()) def tearDown(self): unittest.TestCase.tearDown(self) delete(FILE) def testExtractMethod(self): r = refactoring.Refactoring() s = r.extractMethod(FILE, 5+1, 12, 5+1, 12+3, 'plusMet') f = file(FILE, 'r') contents = f.read() f.close() self.assertEquals(self.getRefactoredFile(), contents) def testRename(self): r = refactoring.Refactoring() s = r.renameByCoordinates(FILE, 1+1, 6, 'G') f = file(FILE, 'r') contents = f.read() f.close() self.assertEquals(getRenameRefactored(), contents) def testRename2(self): r = refactoring.Refactoring() s = r.renameByCoordinates(FILE, 7+1, 4, 'G') f = file(FILE, 'r') contents = f.read() f.close() self.assertEquals(getRenameRefactored(), contents) def testFind(self): r = refactoring.Refactoring() s = r.findDefinition(FILE, 7+1, 4) s = s.replace('[(','').replace(')]','').split(',') self.assert_( s[0].endswith('temporary_file.py')) self.assertEquals('2', s[1]) #line self.assertEquals('6', s[2]) #col self.assertEquals('100', s[3]) #accuracy createFile(FILE, getFindFile()) s = r.findDefinition(FILE, 5+1, 2) s1 = r.findDefinition(FILE, 5+1, 3) s2 = r.findDefinition(FILE, 5+1, 4) self.assert_(s == s1 == s2) def getFindFile(): s = \ ''' class C: def aaa(self): return 0 c = C() c.aaa() ''' return s if __name__ == '__main__': unittest.main() --- NEW FILE: test_pyserver.py --- ''' @author Fabio Zadrozny ''' import sys import os #make it as if we were executing from the directory above this one (so that we can use pycompletionserver #without the need for it being in the pythonpath) sys.argv[0] = os.path.dirname(sys.argv[0]) #twice the dirname to get the previous level from this file. sys.path.insert(1, os.path.join( os.path.dirname( sys.argv[0] )) ) import unittest import pycompletionserver import socket import urllib class Test(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) def tearDown(self): unittest.TestCase.tearDown(self) def testMessage(self): t = pycompletionserver.T(0,0) l = [] l.append(('Def','description' )) l.append(('Def1','description1')) l.append(('Def2','description2')) msg = t.formatCompletionMessage(l) self.assertEquals('@@COMPLETIONS((Def,description),(Def1,description1),(Def2,description2))END@@', msg) l = [] l.append(('Def','desc,,r,,i()ption' )) l.append(('Def(1','descriptio(n1')) l.append(('De,f)2','de,s,c,ription2')) msg = t.formatCompletionMessage(l) self.assertEquals('@@COMPLETIONS((Def,desc%2C%2Cr%2C%2Ci%28%29ption),(Def%281,descriptio%28n1),(De%2Cf%292,de%2Cs%2Cc%2Cription2))END@@', msg) def createConnections(self, p1 = 50002,p2 = 50003): ''' Creates the connections needed for testing. ''' t = pycompletionserver.T(p1,p2) t.start() sToWrite = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sToWrite.connect((pycompletionserver.HOST, p1)) sToRead = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sToRead.bind((pycompletionserver.HOST, p2)) sToRead.listen(1) #socket to receive messages. connToRead, addr = sToRead.accept() return t, sToWrite, sToRead, connToRead, addr def readMsg(self): msg = '@@PROCESSING_END@@' while msg.startswith('@@PROCESSING'): msg = self.connToRead.recv(1024*4) if msg.startswith('@@PROCESSING:'): print 'Status msg:', msg return msg def testCompletionSocketsAndMessages(self): t, sToWrite, sToRead, self.connToRead, addr = self.createConnections() try: #now that we have the connections all set up, check the code completion messages. msg = urllib.quote_plus('import math\n') sToWrite.send('@@GLOBALS:%sEND@@'%msg) #only 1 global should be returned: math itself. completions = self.readMsg() msg = urllib.quote_plus('This module is always available. It provides access to the\n'\ 'mathematical functions defined by the C standard.') start = '@@COMPLETIONS((math,%s)'%msg self.assert_(completions.startswith(start), '%s DOESNT START WITH %s' % ( completions, start) ) #it returns math and builtins...just check for math. msg1 = urllib.quote_plus('math') msg2 = urllib.quote_plus('import math\n') #check token msg. sToWrite.send('@@TOKEN_GLOBALS(%s):%sEND@@' % (msg1, msg2)) completions = self.readMsg() self.assert_('@@COMPLETIONS' in completions) self.assert_('END@@' in completions) s = \ ''' class C(object): def __init__(self): print dir(self) def a(self): pass def b(self): self.c=1 pass ''' msg = urllib.quote_plus(s) sToWrite.send('@@TOKEN_GLOBALS(C):%s\nEND@@'%s) completions = self.readMsg() sToWrite.send('@@CLASS_GLOBALS(C):%s\nEND@@'%s) completions2 = self.readMsg() self.assert_(len(completions) != len(completions2)) #reload modules test # sToWrite.send('@@RELOAD_MODULES_END@@') # ok = self.readMsg() # self.assertEquals('@@MSG_OK_END@@' , ok) # this test is not executed because it breaks our current enviroment. #change dir test curr = os.getcwd( ) newDir = None if curr.find('/') != -1: newDir = curr[0:curr.rindex('/')] elif curr.find('\\') != -1: newDir = curr[0:curr.rindex('\\')] self.assert_(newDir != None) newDir = urllib.quote_plus(newDir) sToWrite.send('@@CHANGE_DIR:%sEND@@'%newDir) ok = self.readMsg() self.assertEquals('@@MSG_OK_END@@' , ok) msg1 = urllib.quote_plus('math.acos') #with point msg2 = urllib.quote_plus('import math\n') sToWrite.send('@@TOKEN_GLOBALS(%s):%sEND@@' %(msg1, msg2)) completions = self.readMsg() self.assert_('@@COMPLETIONS' in completions) self.assert_('END@@' in completions) msg1 = urllib.quote_plus('math acos') #with space msg2 = urllib.quote_plus('import math\n') sToWrite.send('@@TOKEN_GLOBALS(%s):%sEND@@' %(msg1, msg2)) completions2 = self.readMsg() self.assertEquals(completions, completions2) finally: try: self.sendKillMsg(sToWrite) while not hasattr(t, 'ended'): pass #wait until it receives the message and quits. sToRead.close() sToWrite.close() self.connToRead.close() except: pass def sendKillMsg(self, socket): socket.send(pycompletionserver.MSG_KILL_SERVER) def testRefactoringSocketsAndMessages(self): t, sToWrite, sToRead, self.connToRead, addr = self.createConnections(50002+2,50003+2) import refactoring from test_refactoring import delete, createFile, FILE, getInitialFile, getRenameRefactored createFile(FILE, getInitialFile()) sToWrite.send('@@BIKEfindDefinition %s %s %sEND@@'%(FILE, 7+1, 4)) result = self.readMsg() self.assert_('BIKE_OK:' in result) sToWrite.send('@@BIKErenameByCoordinates %s %s %s %sEND@@'%(FILE, 1+1, 6, 'G')) result = self.readMsg() self.assert_('BIKE_OK:' in result) self.sendKillMsg(sToWrite) while not hasattr(t, 'ended'): pass #wait until it receives the message and quits. sToRead.close() sToWrite.close() self.connToRead.close() if __name__ == '__main__': unittest.main() |