def processAnnotateMainTestingFunction_(self, doAdd, language, functionName): dumpFileName = "call_graph.json" assert language in (ProgramExecution.Languages.PYTHON, ProgramExecution.Languages.C_PLUS_PLUS) sourceExt = ".py" if language == ProgramExecution.Languages.PYTHON else ".cpp" mainFileName = os.path.join(TEST_FILES_FOLDER, functionName + sourceExt) expectedFileName = os.path.join(TEST_FILES_FOLDER, functionName + "_expected" + sourceExt) if not doAdd: aux = mainFileName mainFileName = expectedFileName expectedFileName = aux mainFileBackup = mainFileName + ".mybkp" try: shutil.copy(mainFileName, mainFileBackup) #Verify second (un)annotation yields the same file for i in range(2): mySourceCodeParser = SourceCodeParser(language, mainFileName) if doAdd: mySourceCodeParser.annotateMainFile(dumpFileName) else: mySourceCodeParser.unAnnotateMainFile() self.compareFileContents_(mainFileName, expectedFileName) finally: if os.path.exists(mainFileBackup): shutil.copy(mainFileBackup, mainFileName) os.remove(mainFileBackup)
def accept(self): ''' User accept. If data is valid, store the project with the help of the ProjectManager. ''' ok = self.validateProject_() if not ok: return theProjectsManager = project.projectsManagerInstance() name = str(self.projectNameTextEdit.toPlainText()) projectsFolder = str(self.projectsLocationEditBox.toPlainText()) folder = os.path.join(projectsFolder, name) language = self.getSelectedLanguage_() solutionFile = str(self.solutionEditBox.toPlainText()) cppProject = str(self.cppProjectEditBox.toPlainText()) mainFile = str(self.mainFileEditBox.toPlainText()) mainFunction = str(self.mainFunctionComboBox.currentText()) compilingCommand = str(self.compilingCommandEditBox.toPlainText()) exeCommand = str(self.exeCommandTextEdit.toPlainText()) aProject = project.Project(name, folder, language, solutionFile, mainFile, mainFunction, exeCommand, [], cppProject, compilingCommand) #Annotate main file before adding it to the tree if self.isEditMode_(): #If main function changed, unannotate previous main function previousMainFunction = self.projectToEdit_.getMainFunction() if mainFunction != previousMainFunction: mySourceCodeParser = SourceCodeParser(language, mainFile, previousMainFunction) mySourceCodeParser.unAnnotateMainFile() mySourceCodeParser = SourceCodeParser(language, mainFile, mainFunction) mainBackupFileName = mainFile + ".bkp" shutil.copyfile(mainFile, mainBackupFileName) dumpFileName = os.path.join(aProject.getExecutionsFolder(), name + ".json") mySourceCodeParser.annotateMainFile(dumpFileName) if language == ProgramExecution.Languages.C_PLUS_PLUS: self.addBraCustomInfoToCppProject_(cppProject) theProjectsManager.storeProject(aProject) theProjectsManager.setCurrentProjectName(aProject.getName()) QDialog.accept(self)
def testPythonGetAllClassesAndFunctionsAnnotations(self): expectedFunctions = [ 'myPrint', 'add', 'subtract', 'innerFunction', 'outerFunction', 'noParamsFunction', 'mySubstring', 'processList', 'processDict' ] expectedClasses = [ 'MyClass', 'ClassWithDummyParameters', 'NonAnnotatedClass', 'ClassWithConstructor', 'ClassWithStaticAndClassMethods' ] def verifyStates(classes, functions, expectedAnnotClasses, expectedAnnotFunctions): def verifyContainer(container, expectedContainer, expectedAnnotations): i = 0 for pythonObj, annotState in container: self.assertEqual(pythonObj, expectedContainer[i]) status = AnnotationState.ANNOTATED if pythonObj in expectedAnnotations else AnnotationState.NOT_ANNOTATED self.assertEqual(annotState, status) i += 1 verifyContainer(classes, expectedClasses, expectedAnnotClasses) verifyContainer(functions, expectedFunctions, expectedAnnotFunctions) #Check without annotations mainFileName = 'mainFileForTests.py' myFunctionsFileName = os.path.join(TEST_FILES_FOLDER, "MyFunctions.py") mySourceCodeParser = SourceCodeParser( ProgramExecution.Languages.PYTHON, mainFileName) classes, functions = mySourceCodeParser.getAllClassesAndFunctionsAnnotations( myFunctionsFileName, "MyFunctions") #No annotations expected, just the function and classes names verifyStates(classes, functions, expectedAnnotClasses=[], expectedAnnotFunctions=[]) #Now, annotate a couple of functions and classes mainFileBackup = mainFileName + ".mybkp" try: shutil.copy(mainFileName, mainFileBackup) #Annotate main file mySourceCodeParser.annotateMainFile("dumpFileName.json") #Annotate a couple of classes mySourceCodeParser.annotatePythonObject("MyFunctions.MyClass", False) mySourceCodeParser.annotatePythonObject( "MyFunctions.ClassWithConstructor", False) expectedAnnotClasses = ['MyClass', 'ClassWithConstructor'] #Annotate a couple of functions mySourceCodeParser.annotatePythonObject("MyFunctions", True, 'subtract') mySourceCodeParser.annotatePythonObject("MyFunctions", True, 'outerFunction') expectedAnnotFunctions = ['subtract', 'outerFunction'] #Get states, and verify annotations classes, functions = mySourceCodeParser.getAllClassesAndFunctionsAnnotations( myFunctionsFileName, "MyFunctions") verifyStates(classes, functions, expectedAnnotClasses, expectedAnnotFunctions) finally: if os.path.exists(mainFileBackup): shutil.copy(mainFileBackup, mainFileName) os.remove(mainFileBackup)