def changeAnnotationState_(self, doAnnotate):
        '''
        Change the class annotation state.
        '''
        LANG = ProgramExecution.Languages
        currentProject = getCurrentProject()
        mySourceCodeParser = SourceCodeParser(currentProject.getLanguage(), currentProject.getMainFile(), currentProject.getMainFunction())
        auxFileName = self.fileToAnnotate_ + ".aux"
        shutil.copy(self.fileToAnnotate_, auxFileName)
        try:
            if currentProject.getLanguage() == LANG.C_PLUS_PLUS:
                if doAnnotate:
                    mySourceCodeParser.annotateCppClasses(auxFileName, self.fileToAnnotate_, self.headerToInclude_, [self.className_])
                else:
                    mySourceCodeParser.unannotateCppClasses(auxFileName, self.fileToAnnotate_, [self.className_])
            else:
                assert currentProject.getLanguage() == LANG.PYTHON
                classNameStr = self.headerToInclude_ + '.' + self.className_
                if doAnnotate:
                    mySourceCodeParser.annotatePythonObject(classNameStr, False)
                else:
                    mySourceCodeParser.unAnnotatePythonObject(classNameStr, False)

        except Exception:
            QMessageBox.about(None, 'ERROR','Error al (des)anotar la clase. Traceback:\n\n' + traceback.format_exc())
            shutil.copy(auxFileName, self.fileToAnnotate_)
            os.remove(auxFileName)
示例#2
0
    def changeAnnotationState_(self, doAnnotate):
        '''
        Change the class annotation state.
        '''
        LANG = ProgramExecution.Languages
        currentProject = getCurrentProject()
        mySourceCodeParser = SourceCodeParser(currentProject.getLanguage(),
                                              currentProject.getMainFile(),
                                              currentProject.getMainFunction())
        auxFileName = self.fileToAnnotate_ + ".aux"
        shutil.copy(self.fileToAnnotate_, auxFileName)
        try:
            if currentProject.getLanguage() == LANG.C_PLUS_PLUS:
                if doAnnotate:
                    mySourceCodeParser.annotateCppClasses(
                        auxFileName, self.fileToAnnotate_,
                        self.headerToInclude_, [self.className_])
                else:
                    mySourceCodeParser.unannotateCppClasses(
                        auxFileName, self.fileToAnnotate_, [self.className_])
            else:
                assert currentProject.getLanguage() == LANG.PYTHON
                classNameStr = self.headerToInclude_ + '.' + self.className_
                if doAnnotate:
                    mySourceCodeParser.annotatePythonObject(
                        classNameStr, False)
                else:
                    mySourceCodeParser.unAnnotatePythonObject(
                        classNameStr, False)

        except Exception:
            QMessageBox.about(
                None, 'ERROR',
                'Error al (des)anotar la clase. Traceback:\n\n' +
                traceback.format_exc())
            shutil.copy(auxFileName, self.fileToAnnotate_)
            os.remove(auxFileName)
    def processCppTestingFunction_(self,
                                   doAdd,
                                   functionName,
                                   classNames=None,
                                   functionNames=None):
        mainFileName = os.path.join(TEST_FILES_FOLDER,
                                    "testAnnotateMainCpp.cpp")
        originalFileName = os.path.join(TEST_FILES_FOLDER, functionName + ".h")
        resultFileName = os.path.join(TEST_FILES_FOLDER,
                                      functionName + "_result.h")
        expectedFileName = os.path.join(TEST_FILES_FOLDER,
                                        functionName + "_expected.h")

        if not doAdd:
            aux = originalFileName
            originalFileName = expectedFileName
            expectedFileName = aux

        mySourceCodeParser = SourceCodeParser(
            ProgramExecution.Languages.C_PLUS_PLUS, mainFileName)
        headerToInclude = functionName + ".h"

        #Verify second (un)annotation yields the same file
        try:
            originalFileBkp = originalFileName + '.bkp'
            shutil.copy(originalFileName, originalFileBkp)
            for i in range(2):
                try:
                    ok = False
                    if doAdd:
                        if classNames is None and functionNames is None:
                            mySourceCodeParser.annotateCppFile(
                                originalFileName, resultFileName,
                                headerToInclude)
                        else:
                            if classNames is not None:
                                if functionNames is not None:
                                    assert len(classNames) == 1
                                    mySourceCodeParser.annotateCppClassFunctions(
                                        originalFileName, resultFileName,
                                        headerToInclude, classNames[0],
                                        functionNames)
                                else:
                                    mySourceCodeParser.annotateCppClasses(
                                        originalFileName, resultFileName,
                                        headerToInclude, classNames)
                            if functionNames is not None and classNames is None:
                                mySourceCodeParser.annotateCppFunctions(
                                    originalFileName, resultFileName,
                                    headerToInclude, functionNames)
                    else:
                        if classNames is None and functionNames is None:
                            mySourceCodeParser.unannotateCppFile(
                                originalFileName, resultFileName)
                        else:
                            if classNames is not None:
                                mySourceCodeParser.unannotateCppClasses(
                                    originalFileName, resultFileName,
                                    classNames)
                            if functionNames is not None and classNames is None:
                                mySourceCodeParser.unannotateCppFunctions(
                                    originalFileName, resultFileName,
                                    functionNames)
                    self.compareFileContents_(resultFileName, expectedFileName)
                    ok = True
                finally:
                    if i == 0 and ok:
                        shutil.copy(resultFileName, originalFileName)
                    if os.path.exists(resultFileName):
                        os.remove(resultFileName)
        finally:
            if os.path.exists(originalFileBkp):
                shutil.copy(originalFileBkp, originalFileName)
                os.remove(originalFileBkp)
    def testCppGetAllClassesAndFunctionsAnnotations(self):
        MY_HEADER = "MyFunctions.h"
        expectedFunctions = [
            'myPrint', 'add', 'subtract', 'innerFunction', 'outerFunction',
            'noParamsFunction', 'mySubstring', 'processVector', 'processIntMap'
        ]
        expectedClasses = [
            'MyClass', 'ClassWithConstructor', 'ClassWithStaticMethods'
        ]

        def verifyStates(classes, functions, expectedAnnotClasses,
                         expectedAnnotFunctions):
            def verifyContainer(container, expectedContainer,
                                expectedAnnotations):
                i = 0
                for obj, annotState in container:
                    self.assertEqual(obj, expectedContainer[i])
                    status = AnnotationState.ANNOTATED if obj in expectedAnnotations else AnnotationState.NOT_ANNOTATED
                    self.assertEqual(annotState, status)
                    i += 1

            verifyContainer(classes, expectedClasses, expectedAnnotClasses)
            verifyContainer(functions, expectedFunctions,
                            expectedAnnotFunctions)

        #Check without annotations
        mainFileName = os.path.join(TEST_FILES_FOLDER,
                                    "testAnnotateMainCpp.cpp")
        myFunctionsFileName = os.path.join(TEST_FILES_FOLDER, MY_HEADER)

        mySourceCodeParser = SourceCodeParser(
            ProgramExecution.Languages.C_PLUS_PLUS, mainFileName)
        classes, functions = mySourceCodeParser.getAllClassesAndFunctionsAnnotations(
            myFunctionsFileName, )

        #No annotations expected, just the function and classes names
        verifyStates(classes,
                     functions,
                     expectedAnnotClasses=[],
                     expectedAnnotFunctions=[])

        #Now, annotate a couple of functions and classes
        myFunctionsFileBackup = myFunctionsFileName + ".mybkp"
        resultFileName = os.path.join(TEST_FILES_FOLDER,
                                      myFunctionsFileName + "_result.h")
        resultFileName2 = resultFileName + ".2"
        try:
            shutil.copy(myFunctionsFileName, myFunctionsFileBackup)

            #Annotate a couple of classes
            expectedAnnotClasses = ['MyClass', 'ClassWithConstructor']
            mySourceCodeParser.annotateCppClasses(myFunctionsFileName,
                                                  resultFileName, MY_HEADER,
                                                  expectedAnnotClasses)

            #Annotate a couple of functions
            expectedAnnotFunctions = ['subtract', 'outerFunction']
            mySourceCodeParser.annotateCppFunctions(resultFileName,
                                                    resultFileName2, MY_HEADER,
                                                    expectedAnnotFunctions)

            #Get states, and verify annotations
            classes, functions = mySourceCodeParser.getAllClassesAndFunctionsAnnotations(
                resultFileName2, MY_HEADER)
            verifyStates(classes, functions, expectedAnnotClasses,
                         expectedAnnotFunctions)
        finally:
            if os.path.exists(myFunctionsFileBackup):
                shutil.copy(myFunctionsFileBackup, myFunctionsFileName)
                os.remove(myFunctionsFileBackup)
            if os.path.exists(resultFileName):
                os.remove(resultFileName)
            if os.path.exists(resultFileName2):
                os.remove(resultFileName2)