def convertToPatchMethod(self): converted = PatchMethod(self.name) converted.total_add = self.total_add converted.total_del = self.total_del for nextAssert in self.assertionList: if(nextAssert[1]==ADD): converted.assert_add += 1 if(self.name==MOCK): converted.total_add += 1 elif(nextAssert[1]==REMOVE): converted.assert_del += 1 if(self.name==MOCK): converted.total_del += 1 return converted
def convertToPatchMethod(self): converted = PatchMethod(self.name) converted.total_add = self.total_add converted.total_del = self.total_del converted.exceptionDictonary = self.excepDict.copy() # for nextAssert in self.assertionList: # # if(nextAssert[1]==ADD): # converted.assert_add += 1 # if(self.name==MOCK): # converted.total_add += 1 # elif(nextAssert[1]==REMOVE): # converted.assert_del += 1 # if(self.name==MOCK): # converted.total_del += 1 return converted
def convertToPatchMethod(self): converted = PatchMethod(self.name) converted.total_add = self.total_add converted.total_del = self.total_del converted.exceptionDictonary=self.excepDict.copy() # for nextAssert in self.assertionList: # # if(nextAssert[1]==ADD): # converted.assert_add += 1 # if(self.name==MOCK): # converted.total_add += 1 # elif(nextAssert[1]==REMOVE): # converted.assert_del += 1 # if(self.name==MOCK): # converted.total_del += 1 return converted
def addMethod(self, methodName): method = PatchMethod(methodName) self.methods.append(method)
def parseText(self): ''' Preprocess the log to swap the - } catch (*Exception) { and + } catch (#Exception) { ''' #----------------------------------Initialization----------------------------------# #New keyword list for both single and block keywords. This is a list of triples. keyWordList = [] keyWordList = self.readKeywords(keyWordList) singleKeyWordList = filter(lambda w : w[2] == SINGLE, keyWordList) blockKeyWordList = filter(lambda w: w[2] == BLOCK, keyWordList) self.initialized = True lineNum = 0 # which line we are on, relative to the start of the chunk lineType = OTHER phase = LOOKFORNAME #TODO: Replace the phases with scopeTracker? commentFlag = False #Are we inside a comment? commentType = OTHER #Is the original line of the comment ADD, REMOVE, or OTHER functionName = "" shortFunctionName = "" funcStart = 0 funcEnd = 0 classContext = [] #If we are parsing inside a class, what is the closest class name? ftotal_add=0 ftotal_del=0 etotal_add=0 #TODO: From an earlier version, to be removed. etotal_del=0 keywordDictionary = OrderedDict() outsideFuncKeywordDictionary = OrderedDict() # A grouping for all keywords found outside function contexts catchLineNumber=[] tryList=[] #Initialize keywords (This is repeated three times -> make into a subfunction) for keyword in singleKeyWordList: if(keyword[1] != EXCLUDED): keywordDictionary[self.outputKeyword(keyword)+ " Adds"]=0 keywordDictionary[self.outputKeyword(keyword)+ " Dels"]=0 #Currently only support single line keyword tracking outside of functions outsideFuncKeywordDictionary[self.outputKeyword(keyword) + " Adds"]=0 outsideFuncKeywordDictionary[self.outputKeyword(keyword) + " Dels"]=0 for keyword in blockKeyWordList: #Hack to make run with the 'tryDependentCatch' keyword if(not isinstance(keyword, list) or len(keyword) != KEYLISTSIZE): continue elif(keyword[1] != EXCLUDED): keywordDictionary[self.outputKeyword(keyword) + " Adds"]=0 keywordDictionary[self.outputKeyword(keyword) + " Dels"]=0 #----------------------------------Initialization----------------------------------# #Start of iteration over the lines. for line in self.text.split("\n"): startFlag=0 lineNum += 1 #Remove whitespace on ends. fullLine = line.strip() if(Util.DEBUG==1): try: print("The real line: " + line) except: print("The real line: " + unicode(line, 'utf-8', errors='ignore')) (lineType, line)= self.markLine(line) #Remove all strings from the line. (Get rid of weird cases of brackets #or comment values being excluded from the line. line = self.removeStrings(line) line = line.replace("\\", "") #Remove backslashes. line = line.replace("^M", "") #Remove all comments from the line fChange = UNMARKED (line, lineType, commentFlag, commentType, functionName, fChange) = self.removeComments(line, commentFlag, lineType, commentType, functionName, phase) #Update the all the counts if this is a comment line if(fChange != UNMARKED): (keywordDictionary, ftotal_add, ftotal_del) = self.modifyCountForComment(fChange, lineType, keywordDictionary, blockKeyWordList, ftotal_add, ftotal_del) continue else: #Otherwise, update just the function and total counts (ftotal_add, ftotal_del) = self.updateCounts(lineType, ftotal_add, ftotal_del, phase, startFlag) #Extract the name of the function if(phase == LOOKFORNAME): if(Util.DEBUG == 1): try: print("Current Name Search: " + functionName) except: print("Current Name Search: " + unicode(functionName, 'utf-8', errors='ignore')) #What if we've hit a function defintion? #TODO: Make language independent if(self.langSwitch.checkForFunctionReset(functionName)): functionName = "" #Clear the name #Namespace problem comes in here. we add extra stuff in conjunction with functionName += ... above #How can we replace if and the { stuff below with scopeTracker methods? No, check for scope change if(self.sT.isScopeIncrease(line, lineType)): if(Util.DEBUG == 1): print("Scope increase while searching for function.") if(self.sT.scopeIncreaseCount(line, lineType) > 1): if(Util.DEBUG == 1): print("Parsing of multiscope increases like: ") print(line) print("is not yet supported.") continue functionName = self.sT.appendFunctionEnding(line, functionName) shortFunctionName = self.getFunctionPattern(functionName) if(Util.DEBUG): print("Pattern: " + shortFunctionName) if(shortFunctionName != ""): isFunction = True elif((classContext != [] and self.isConstructorOrDestructorWithList(functionName, classContext))): #classContext becomes nonempty only for OO languages isFunction = True #Replace with general function. shortFunctionName = self.langSwitch.shortenConstructorOrDestructor(functionName) else: isFunction = False #Update to function scope or other here. if(isFunction): #Skip things are aren't functions if(Util.DEBUG == 1): try: print("Function: " + shortFunctionName) except: print("Function: " + unicode(shortFunctionName, 'utf-8', errors='ignore')) self.sT.increaseScope(shortFunctionName, lineType, scopeTracker.FUNC) funcStart = lineNum phase = LOOKFOREND #Count this line as an addition or deletion #this means either a { will be counted or part #of the function name. if(lineType == REMOVE): ftotal_del = 1 startFlag=1 elif(lineType == ADD): ftotal_add = 1 startFlag=1 else: #There was a non-function scope increase. self.sT.increaseScope(line, lineType, scopeTracker.GENERIC) if(self.langSwitch.isObjectOrientedLanguage()): className = self.getClassPattern(functionName) #Would C++ constructors outside class A start with A::? if(className != ""): if(Util.DEBUG == 1): try: print("Class:" + className) except: print("Class:" + unicode(className, 'utf-8', errors='ignore')) classContext.append(self.extractClassName(className)) #Push onto the class list functionName = "" #Reset name and find next else: #No scope change to cut off, so add the whole line instead if(Util.DEBUG): print("Extending the function name") functionName += line.replace("\n", "") + " " #add the line with no new lines #Check for single line keywords if(lineType != OTHER): if(phase == LOOKFOREND): keywordDictionary = self.parseLineForKeywords(line, lineType, singleKeyWordList, keywordDictionary) elif(phase == LOOKFORNAME): outsideFuncKeywordDictionary = self.parseLineForKeywords(line, lineType, singleKeyWordList, outsideFuncKeywordDictionary) else: assert(0) #Handle cases where we have a single line function. if(phase == LOOKFOREND and self.sT.isScopeDecrease(line, lineType)): shortFunctionName = self.sT.getFuncContext(lineType) #Get the functional context self.sT.decreaseScope(line, lineType) if(self.sT.getFuncContext(lineType) == "" and phase == LOOKFOREND): funcEnd = lineNum #Add this function to our list and reset the trackers. #We use shortFunctionName, which is the string that matched our expected #function pattern regex funcToAdd = PatchMethod(self.langSwitch.parseFunctionName(shortFunctionName), funcStart, funcEnd, ftotal_add, ftotal_del,keywordDictionary,etotal_add,etotal_del,catchLineNumber) #Add assertions from current function self.functions.append(funcToAdd) #Reset asserts to current function functionName = "" shortFunctionName = "" funcStart = 0 funcEnd = 0 ftotal_add = 0 ftotal_del = 0 etotal_add = 0 etotal_del = 0 phase = LOOKFORNAME lineType=OTHER for keyword in singleKeyWordList: if(keyword[1] != EXCLUDED): keywordDictionary[self.outputKeyword(keyword) + " Adds"]=0 keywordDictionary[self.outputKeyword(keyword) + " Dels"]=0 for keyword in blockKeyWordList: #Hack to make run with the 'tryDependedCatch' keyword if(not isinstance(keyword, list) or len(keyword) != KEYLISTSIZE): continue elif(keyword[1] != EXCLUDED): keywordDictionary[self.outputKeyword(keyword) + " Adds"]=0 keywordDictionary[self.outputKeyword(keyword) + " Dels"]=0 if(Util.DEBUG == 1): print(classContext) elif(phase == LOOKFOREND): #determine the end of the function #Handle changes in scope scopeChanges = self.sT.scopeOrder(line, lineType) if(len(scopeChanges) > 2): if(Util.DEBUG == 1): print("Parsing of multiscope changes like: ") print(line) print("is not yet supported.") continue else: for nextScopeChange in scopeChanges: if(nextScopeChange == INCREASE): if(self.sT.isScopeIncrease(line, lineType)): if(self.sT.scopeIncreaseCount(line, lineType) > 1): if(Util.DEBUG == 1): print("Parsing of multiscope increases like: ") print(line) print("is not yet supported.") continue #if(phase2==LOOKFOREXCP): foundBlock=self.getBlockPattern(line,blockKeyWordList) if(foundBlock!=None): if(Util.DEBUG): print("Block start found: " + foundBlock) self.sT.increaseScope(foundBlock, lineType, scopeTracker.SBLOCK) else: self.sT.increaseScope(line, lineType, scopeTracker.GENERIC) else: if(self.sT.isScopeDecrease(line, lineType)): if(self.sT.getFuncContext(lineType) != ""): #Maybe? shortFunctionName = self.sT.getFuncContext(lineType) #Get the functional context if(self.sT.getBlockContext(lineType) != [] and lineType!=OTHER): if(Util.DEBUG): print("Current block context: " + str(self.sT.getBlockContext(lineType))) keywordDictionary = self.parseLineForKeywords(line, lineType, blockKeyWordList, keywordDictionary, self.sT.getBlockContext(lineType)) self.sT.decreaseScope(line, lineType) #Problem: This misses the last line of a block context, and if we move it before, it will miss the first #How do we capture a line with multiple block contexts? if(lineType != OTHER): if(phase == LOOKFOREND): keywordDictionary = self.parseLineForKeywords(line, lineType, singleKeyWordList, keywordDictionary) if(self.sT.getBlockContext(lineType) != []): if(Util.DEBUG): print("Current block context: " + str(self.sT.getBlockContext(lineType))) keywordDictionary = self.parseLineForKeywords(line, lineType, blockKeyWordList, keywordDictionary, self.sT.getBlockContext(lineType)) else: assert(0) if(self.sT.getFuncContext(lineType) == ""): funcEnd = lineNum #Add this function to our list and reset the trackers. if(Util.DEBUG == 1): print("OLD") print(self.sT.oldVerStack) print("NEW") print(self.sT.newVerStack) print(lineType) print(shortFunctionName) print(str(funcStart) + " : " + str(funcEnd)) funcToAdd = PatchMethod(self.langSwitch.parseFunctionName(shortFunctionName), funcStart, funcEnd, ftotal_add, ftotal_del,keywordDictionary,etotal_add,etotal_del,catchLineNumber) self.functions.append(funcToAdd) #Reset asserts to current function functionName = "" shortFunctionName = "" funcStart = 0 funcEnd = 0 ftotal_add=0 ftotal_del=0 etotal_add=0 etotal_del=0 phase = LOOKFORNAME catchLineNumber=[] foundBlock="" #currentBlock=None for keyword in singleKeyWordList: if(keyword[1] != EXCLUDED): keywordDictionary[self.outputKeyword(keyword) + " Adds"]=0 keywordDictionary[self.outputKeyword(keyword) + " Dels"]=0 for keyword in blockKeyWordList: #Hack to make run with the 'tryDependedCatch' keyword if(not isinstance(keyword, list) or len(keyword) != KEYLISTSIZE): continue elif(keyword[1] != EXCLUDED): keywordDictionary[self.outputKeyword(keyword) + " Adds"]=0 keywordDictionary[self.outputKeyword(keyword) + " Dels"]=0 #Suppose we have a function where only the top is modified, # e.g. # int renamedFunction(int arg1) { # int x = 0; #Then we want to add this into the count even though there is a hanging } if(shortFunctionName != ""): #The end of the function will be said to be the cutoff of the change. funcEnd = lineNum funcToAdd = PatchMethod(self.langSwitch.parseFunctionName(shortFunctionName), funcStart, funcEnd, ftotal_add, ftotal_del,keywordDictionary,etotal_add,etotal_del,catchLineNumber) self.functions.append(funcToAdd) #Remove any unmodified functions self.functions = filter(lambda(x) : x.total_add != 0 or x.total_del != 0 , self.functions) #Clear out the scope. self.sT.clearScope() #Create a mock function for any asserts that do not fall into another function's list if nonZeroCount(outsideFuncKeywordDictionary): mockFunction = PatchMethod(MOCK, 0, 0, 0, 0, outsideFuncKeywordDictionary, 0, 0) self.functions.append(mockFunction) if(Util.DEBUG): print("Chunk End.") return self
import sys