示例#1
0
      public static Project CreateAndAddUnitTestProject(Solution2 sol, string csProjectName, string csProjectPath, UnitTestCodeType codeType)
      {
         //System.Windows.Forms.MessageBox.Show("You must have at least 1 project in your current solution with a project name that ends with UnitTest");
         Project proj = null;

         try
         {
            String csItemTemplatePath;

            switch (codeType)
            {
               case UnitTestCodeType.CSharp:
                  csItemTemplatePath = sol.GetProjectTemplate(UTGHelper.CommonStrings.DEFAULT_PROJECT_TEMPLATE, UTGHelper.CommonStrings.DEFAULT_LANGUAGE);
                  break;
               case UnitTestCodeType.VB:
                  csItemTemplatePath = sol.GetProjectTemplate(UTGHelper.CommonStrings.DEFAULT_PROJECT_TEMPLATE, UTGHelper.CommonStrings.DEFAULT_VB_LANGUAGE);
                  break;
               default:
                  throw new Exception(string.Format(UTGHelper.CommonErrors.ERR_LANUAGE_TYPE_NOT_SUPPORTED, codeType.ToString()));
            }

            proj = sol.AddFromTemplate(csItemTemplatePath, csProjectPath, csProjectName, false);
         }
         catch (Exception ex)
         {
            Logger.LogException(ex);

            throw;
         }

         return proj;
      }
        public UserAlertActionRequired(string message, Type codeElementType, CodeElement ce, ref DTE2 applicationObject, UnitTestCodeType codeType, object testFramework)
        {
            InitializeComponent();

            ivType = codeElementType;

            FormCodeElement = ce;

            FromCodeType = codeType;

            FormApplicationObject = applicationObject;

            TestFramework = testFramework;

            Dictionary <Type, ApplicableAcction> .KeyCollection.Enumerator it =
                lstApplicableTypes.Keys.GetEnumerator();

            while (it.MoveNext())
            {
                Type itemType = it.Current;

                if (itemType == codeElementType)
                {
                    ApplicableAcction tvApplicableAcction;

                    lstApplicableTypes.TryGetValue(itemType, out tvApplicableAcction);

                    if (tvApplicableAcction == ApplicableAcction.ignore)
                    {
                        ApplicableAcctionState = ApplicableAcction.ignore;
                    }
                    else if (tvApplicableAcction == ApplicableAcction.mutate)
                    {
                        ApplicableAcctionState = ApplicableAcction.mutate;
                    }
                    else if (tvApplicableAcction == ApplicableAcction.overwrite)
                    {
                        ApplicableAcctionState = ApplicableAcction.overwrite;
                    }

                    this.Close();
                    return;
                }
            }

            this.txtContent.Text += message;
        }
      public UserAlertActionRequired(string message, Type codeElementType, CodeElement ce, ref DTE2 applicationObject, UnitTestCodeType codeType, object testFramework)
      {
         InitializeComponent();

         ivType = codeElementType;

         FormCodeElement = ce;

         FromCodeType = codeType;

         FormApplicationObject = applicationObject;

         TestFramework = testFramework;

         Dictionary<Type, ApplicableAcction>.KeyCollection.Enumerator it =
            lstApplicableTypes.Keys.GetEnumerator();

         while (it.MoveNext())
         {
            Type itemType = it.Current;

            if (itemType == codeElementType)
            {
               ApplicableAcction tvApplicableAcction;

               lstApplicableTypes.TryGetValue(itemType, out tvApplicableAcction);

               if (tvApplicableAcction == ApplicableAcction.ignore)
               {
                  ApplicableAcctionState = ApplicableAcction.ignore;
               }
               else if (tvApplicableAcction == ApplicableAcction.mutate)
               {
                  ApplicableAcctionState = ApplicableAcction.mutate;
               }
               else if (tvApplicableAcction == ApplicableAcction.overwrite)
               {
                  ApplicableAcctionState = ApplicableAcction.overwrite;
               }

               this.Close();
               return;
            }
         }

         this.txtContent.Text += message;
      }
      internal static void HandelCodeClassSelection(AbstractTestFramework testFramework, CodeProperty property, DTE2 applicationObject, UnitTestCodeType codeType)
      {
         Project parentProject = ProjectExaminar.GetCodeClassParentProject((CodeClass)property.Parent);

         Project unitTestProject = ProjectExaminar.GetUnitTestProject(parentProject.Name, applicationObject, testFramework.TestProjectPostFix);

         //We might or might not use this factory, we don't know at this point
         AbstractTestClassFactory testClassFactory = new NUnitTestClassFactory();

         foreach (ProjectItem projectItem in unitTestProject.ProjectItems)
         {
            System.Collections.Generic.List<CodeClass> lstCodeClasses =
                 ProjectItemExaminor.GetCodeClasses(projectItem.FileCodeModel);

            foreach (CodeClass codeClass in lstCodeClasses)
            {
               if (codeClass.Name.Equals(((CodeClass)property.Parent).Name + testFramework.TestClassFixturePostFix))
               {
                  //if found 
                  AbstractTestClass nunitClass = null;

                  if (codeType == UnitTestCodeType.CSharp)
                  {
                     nunitClass = testClassFactory.CreateCSharpTestClass(codeClass.Namespace.FullName, codeClass.Name, codeClass.IsAbstract);
                  }
                  else if (codeType == UnitTestCodeType.VB)
                  {
                     nunitClass = testClassFactory.CreateVBTestClass(codeClass.Namespace.FullName, codeClass.Name, codeClass.IsAbstract);
                  }

                  nunitClass.GenerateTest(codeClass, property);

                  //we sure expect just one class, the first class we find matching
                  return;
               }
            }
         }

         //if we have reached this point then it means that we have not been able to locate a parent class in our
         //unit test project, then we simply create a class unit test

         OnCodeClassSelection(testFramework, (CodeClass)property.Parent, (CodeElement)property, applicationObject, unitTestProject, codeType);
      }
      internal static void HandelCodeClassSelection(AbstractTestFramework testFramework, CodeFunction function, DTE2 applicationObject, UnitTestCodeType codeType)
      {
         Project parentProject = ProjectExaminar.GetCodeClassParentProject((CodeClass)function.Parent);

         Project unitTestProject = ProjectExaminar.GetUnitTestProject(parentProject.Name, applicationObject, testFramework.TestProjectPostFix);

         //We might or might not use this factory, we don't know at this point
         AbstractTestClassFactory testClassFactory = new NUnitTestClassFactory();

         foreach (ProjectItem projectItem in unitTestProject.ProjectItems)
         {
            System.Collections.Generic.List<CodeClass> lstCodeClasses =
                 ProjectItemExaminor.GetCodeClasses(projectItem.FileCodeModel);

            foreach (CodeClass codeClass in lstCodeClasses)
            {
               CodeClass parentClass = (CodeClass)function.Parent;

               string className = codeClass.Name;
               string testClassName = ((CodeClass)function.Parent).Name + testFramework.TestClassFixturePostFix;
               
               //find the parent for function passed in
               //if (codeClass.Name.Equals(((CodeClass)function.Parent).Name + CommonStrings.DEFAULT_STRING_SEPERATOR + UTGHelper.CommonStrings.DEFAULT_UNIT_TEST_CLASS_POSTFIX))
               if (className.Equals(testClassName) == true)
               {
                  //if found 
                  AbstractTestClass nunitClass = null;

                  if (codeType == UnitTestCodeType.CSharp)
                  {
                     nunitClass = testClassFactory.CreateCSharpTestClass(codeClass.Namespace.FullName, codeClass.Name, codeClass.IsAbstract);
                  }
                  else if (codeType == UnitTestCodeType.VB)
                  {
                     nunitClass = testClassFactory.CreateVBTestClass(codeClass.Namespace.FullName, codeClass.Name, codeClass.IsAbstract);
                  }

                  nunitClass.GenerateTest(codeClass, function);

                  //we sure expect just one class, the first class we find matching
                  return;
               }
            }
         }

         //if we have reached this point then it means that we have not been able to locate a parent class in our
         //unit test project, then we simply create a class unit test

         OnCodeClassSelection(testFramework, (CodeClass)function.Parent, (CodeElement)function, applicationObject, unitTestProject, codeType);
      }
      //OK
      internal static void OnCodeClassSelection(AbstractTestFramework testFramework, CodeClass codeClass, DTE2 applicationObject, UnitTestCodeType codeType)
      {
         //We can only test public methods
         if (codeClass.Access != vsCMAccess.vsCMAccessPublic)
            return;

         Project parentProject = ProjectExaminar.GetCodeClassParentProject(codeClass);

         Project unitTestProject = ProjectExaminar.GetUnitTestProject(parentProject.Name, applicationObject, testFramework.TestProjectPostFix);

         if (unitTestProject != null && !CanGenerateHandleCodeClass(testFramework.TestClassFixturePostFix, codeClass, unitTestProject)) {
            UTGHelper.ErrorHandler.ShowMessage(CommonUserMessages.MSG_TEST_ALREADY_EXISTS);
            OnGenerationFaliure(applicationObject);
            return;
         }

         string path = ProjectManager.GetUnitTestProjectPath(parentProject.Name, applicationObject, testFramework.TestProjectPostFix);

         path = UTGHelper.IO.GetFilePath(path);

         System.IO.DirectoryInfo newDirectoryInfo;

         string fullPath = System.IO.Path.Combine(path, parentProject.Name + testFramework.TestProjectPostFix);

         try{
            //Another way of doing this would be to try and find this project in current open solution saving us the exception
            newDirectoryInfo =
               System.IO.Directory.CreateDirectory(fullPath);
         }
         catch (Exception ex){
            Logger.LogException(ex);
         }
         finally{
            //Either case we have one by now so lets get its directory information
            newDirectoryInfo = new System.IO.DirectoryInfo(fullPath);
         }

         //If for whatever reason we are still failing then simply quit this 
         if (newDirectoryInfo == null)
         {
            OnGenerationFaliure(applicationObject);

            throw new Exception(string.Format("Unable to create new Directory {0}", System.IO.Path.Combine(path, parentProject.Name + testFramework.TestProjectPostFix)));
         }

         if (unitTestProject == null)
         {
            try{
               unitTestProject = ProjectManager.CreateAndAddUnitTestProject((Solution2)applicationObject.Solution, parentProject.Name + testFramework.TestProjectPostFix, newDirectoryInfo.FullName, codeType/*, license*/);
            }
            catch (Exception ex){
               Logger.LogException(ex);

               OnGenerationFaliure(applicationObject);

               throw;
            }
         }

         //Rethink this bit
         //Since above operation fails in either case then try getting the new Unit Test Project if created and added ok..
         if (unitTestProject == null) {
            unitTestProject = ProjectExaminar.GetUnitTestProject(parentProject.Name, applicationObject, testFramework.TestProjectPostFix);
         }

         if (unitTestProject == null) {
            OnGenerationFaliure(applicationObject);
            throw new Exception("Can not create new UnitTest Project");
         }
 
         AbstractTestClassFactory testClassFactory = new NUnitTestClassFactory();

         AbstractTestClass nunitClass = null;

         if (codeType == UnitTestCodeType.CSharp) {
            nunitClass = testClassFactory.CreateCSharpTestClass(codeClass.Namespace.FullName, codeClass.Name, codeClass.IsAbstract);
         }
         else if (codeType == UnitTestCodeType.VB) {
            nunitClass = testClassFactory.CreateVBTestClass(codeClass.Namespace.FullName, codeClass.Name, codeClass.IsAbstract);
         }

         if (!nunitClass.write(newDirectoryInfo.FullName, testFramework.TestClassFixturePostFix)) {
            OnGenerationFaliure(applicationObject);
            return;
         }
        
         //add new class to project
         ProjectItem projectItem = ProjectManager.AddClassToProject(applicationObject, unitTestProject, null, nunitClass.FullName);

         List<CodeClass> lstCodeClasses = ProjectItemExaminor.GetCodeClasses(projectItem.FileCodeModel);

         if (lstCodeClasses == null || lstCodeClasses.Count == 0) {
            OnGenerationFaliure(applicationObject);
            return;
         }

         CodeClass unitTestCodeClass = lstCodeClasses[0];

         //passed this point the actual object will take care of it self.
         nunitClass.GenerateTest(unitTestCodeClass, codeClass);

         //To consider: this should be handled by an object that inherits from project type
         copyReferences(testFramework, parentProject, unitTestProject);

         applicationObject.ItemOperations.OpenFile(nunitClass.FullName, EnvDTE.Constants.vsViewKindAny);
      }
      private static void OnCodeClassSelection(AbstractTestFramework testFramework, CodeProperty ce, DTE2 applicationObject, UnitTestCodeType codeType)
      {
         Project parentProject = ProjectExaminar.GetCodeClassParentProject((CodeClass)ce.Parent);

         Project unitTestProject = ProjectExaminar.GetUnitTestProject(parentProject.Name, applicationObject, testFramework.TestProjectPostFix);

         if (unitTestProject == null) {

            OnCodeClassSelection(testFramework, (CodeClass)ce.Parent, (CodeElement)ce, applicationObject, codeType);
            return;
         }

         //In cases where we can not simply generate automatically (definition already exists) we will
         //ask the user for input, wait on input, then async back to the function actual useful handler
         //HandelCodeClassSelection
         if (!CanGenerateHandleCodeProperty(testFramework.TestClassFixturePostFix, (CodeClass)ce.Parent, ce, unitTestProject))
         {
            UTGHelper.UserAlertActionRequired tvUserAlertActionRequired = new UserAlertActionRequired(
               "Property definition already exits! What would you like to do?",
               typeof(CodeProperty),
               (CodeElement)ce,
               ref applicationObject,
               codeType,
               testFramework);

            if (!tvUserAlertActionRequired.IsDisposed)
            {
               tvUserAlertActionRequired.FormClosed += new System.Windows.Forms.FormClosedEventHandler(tvUserAlertActionRequired_FormClosed);

               tvUserAlertActionRequired.Show();
            }
            else
            {
               HandelCodeClassSelection(testFramework, ce, applicationObject, codeType);
            }

            return;
         }

         //otherwise simply call HandelCodeClassSelection
         HandelCodeClassSelection(testFramework, ce, applicationObject, codeType);

      }
      public static void OnCodeElementSelection(AbstractTestFramework testFramework, CodeElement ce, DTE2 applicationObject, UnitTestCodeType codeType)
      {
         OnGenerationStart(applicationObject);

         if (ce is CodeNamespace)
         {
            ErrorHandler.ShowMessage(string.Format("You have selected the namesapce {0}! Currently there are no features which support namespaces.", ((CodeNamespace)ce).FullName));
         }
         //else if (codeClass != null)
         else if (ce is CodeClass)
         {
            if ( ((CodeClass)ce).Access != vsCMAccess.vsCMAccessPublic)
               return;

            OnCodeClassSelection(testFramework, (CodeClass)ce, applicationObject, codeType);
         }
         //else if (codeFunction != null)
         else if (ce is CodeFunction)
         {
            if ( ((CodeFunction)ce).Access != vsCMAccess.vsCMAccessPublic)
               return;

            OnCodeClassSelection(testFramework, (CodeFunction)ce, applicationObject, codeType);
         }
         else if (ce is CodeEnum)
         {
            if ( ((CodeEnum)ce).Access != vsCMAccess.vsCMAccessPublic)
               return;

            ErrorHandler.ShowMessage(string.Format("You have selected the Enum {0}!", ((CodeEnum)ce).FullName));
         }
         else if (ce is CodeStruct)
         {
            ErrorHandler.ShowMessage(string.Format("You have selected the Structure {0}!", ((CodeStruct)ce).FullName));
         }
         else if (ce is CodeProperty)
         {
            if ( ((CodeProperty)ce).Access != vsCMAccess.vsCMAccessPublic)
               return;

            OnCodeClassSelection(testFramework, (CodeProperty)ce, applicationObject, codeType);
         }
         else
         {
            ErrorHandler.ShowMessage(UTGHelper.CommonErrors.ERR_ILLEGAL_TEXT_SELECTION);
         }
      }
      public static void OnCodeElementSelection(AbstractTestFramework testFramework, Project project, DTE2 applicationObject, UnitTestCodeType codeType)
      {
         try
         {
            foreach (ProjectItem pItem in project.ProjectItems)
            {
               try
               {
                 
                  foreach (CodeElement tvCodeElementOutter in pItem.FileCodeModel.CodeElements)
                  {
                     if (tvCodeElementOutter is CodeNamespace)
                     {
                        try
                        {
                           foreach (CodeElement tvCodeElement in ((CodeNamespace)tvCodeElementOutter).Members)
                           {
                              if (tvCodeElement != null &&
                                 tvCodeElement is CodeClass
                                 )
                              {
                                 try
                                 {
                                    if( ((CodeClass)tvCodeElement).Access == vsCMAccess.vsCMAccessPublic)
                                       OnCodeClassSelection(testFramework, (CodeClass)tvCodeElement, applicationObject, codeType/*, license*/);
                                 }
                                 catch (Exception ex)
                                 {
                                    Logger.LogException(ex);

                                    //ignore
                                    UTGHelper.ErrorHandler.HandleException(ex);
                                    return;
                                 }
                              }
                           }
                        }
                        catch (Exception ex)
                        {
                           //ignore this 
                           Logger.LogException(ex);

                        }
                     }
                     else if (tvCodeElementOutter is CodeClass)
                     {
                        try
                        {
                           if (((CodeClass)tvCodeElementOutter).Access == vsCMAccess.vsCMAccessPublic)
                              OnCodeClassSelection(testFramework, (CodeClass)tvCodeElementOutter, applicationObject, codeType/*, license*/);
                        }
                        catch (Exception ex)
                        {
                           Logger.LogException(ex);

                           //ignore
                           UTGHelper.ErrorHandler.HandleException(ex);
                           return;
                        }
                     }

                  }//if (tvCodeElementOutter is CodeNamespace)
               }
               catch (Exception ex)
               {
                  //ignore this on;
                  Logger.LogException(ex);
               }

            }//foreach (ProjectItem pItem in project.ProjectItems)

         }//first try
         catch (Exception ex)
         {
            //ignore this on;throw;
            Logger.LogException(ex);
         }
      }
示例#10
0
        internal static void HandelCodeClassSelection(AbstractTestFramework testFramework, CodeProperty property, DTE2 applicationObject, UnitTestCodeType codeType)
        {
            Project parentProject = ProjectExaminar.GetCodeClassParentProject((CodeClass)property.Parent);

            Project unitTestProject = ProjectExaminar.GetUnitTestProject(parentProject.Name, applicationObject, testFramework.TestProjectPostFix);

            //We might or might not use this factory, we don't know at this point
            AbstractTestClassFactory testClassFactory = new NUnitTestClassFactory();

            foreach (ProjectItem projectItem in unitTestProject.ProjectItems)
            {
                System.Collections.Generic.List <CodeClass> lstCodeClasses =
                    ProjectItemExaminor.GetCodeClasses(projectItem.FileCodeModel);

                foreach (CodeClass codeClass in lstCodeClasses)
                {
                    if (codeClass.Name.Equals(((CodeClass)property.Parent).Name + testFramework.TestClassFixturePostFix))
                    {
                        //if found
                        AbstractTestClass nunitClass = null;

                        if (codeType == UnitTestCodeType.CSharp)
                        {
                            nunitClass = testClassFactory.CreateCSharpTestClass(codeClass.Namespace.FullName, codeClass.Name, codeClass.IsAbstract);
                        }
                        else if (codeType == UnitTestCodeType.VB)
                        {
                            nunitClass = testClassFactory.CreateVBTestClass(codeClass.Namespace.FullName, codeClass.Name, codeClass.IsAbstract);
                        }

                        nunitClass.GenerateTest(codeClass, property);

                        //we sure expect just one class, the first class we find matching
                        return;
                    }
                }
            }

            //if we have reached this point then it means that we have not been able to locate a parent class in our
            //unit test project, then we simply create a class unit test

            OnCodeClassSelection(testFramework, (CodeClass)property.Parent, (CodeElement)property, applicationObject, unitTestProject, codeType);
        }
示例#11
0
        internal static void HandelCodeClassSelection(AbstractTestFramework testFramework, CodeFunction function, DTE2 applicationObject, UnitTestCodeType codeType)
        {
            Project parentProject = ProjectExaminar.GetCodeClassParentProject((CodeClass)function.Parent);

            Project unitTestProject = ProjectExaminar.GetUnitTestProject(parentProject.Name, applicationObject, testFramework.TestProjectPostFix);

            //We might or might not use this factory, we don't know at this point
            AbstractTestClassFactory testClassFactory = new NUnitTestClassFactory();

            foreach (ProjectItem projectItem in unitTestProject.ProjectItems)
            {
                System.Collections.Generic.List <CodeClass> lstCodeClasses =
                    ProjectItemExaminor.GetCodeClasses(projectItem.FileCodeModel);

                foreach (CodeClass codeClass in lstCodeClasses)
                {
                    CodeClass parentClass = (CodeClass)function.Parent;

                    string className     = codeClass.Name;
                    string testClassName = ((CodeClass)function.Parent).Name + testFramework.TestClassFixturePostFix;

                    //find the parent for function passed in
                    //if (codeClass.Name.Equals(((CodeClass)function.Parent).Name + CommonStrings.DEFAULT_STRING_SEPERATOR + UTGHelper.CommonStrings.DEFAULT_UNIT_TEST_CLASS_POSTFIX))
                    if (className.Equals(testClassName) == true)
                    {
                        //if found
                        AbstractTestClass nunitClass = null;

                        if (codeType == UnitTestCodeType.CSharp)
                        {
                            nunitClass = testClassFactory.CreateCSharpTestClass(codeClass.Namespace.FullName, codeClass.Name, codeClass.IsAbstract);
                        }
                        else if (codeType == UnitTestCodeType.VB)
                        {
                            nunitClass = testClassFactory.CreateVBTestClass(codeClass.Namespace.FullName, codeClass.Name, codeClass.IsAbstract);
                        }

                        nunitClass.GenerateTest(codeClass, function);

                        //we sure expect just one class, the first class we find matching
                        return;
                    }
                }
            }

            //if we have reached this point then it means that we have not been able to locate a parent class in our
            //unit test project, then we simply create a class unit test

            OnCodeClassSelection(testFramework, (CodeClass)function.Parent, (CodeElement)function, applicationObject, unitTestProject, codeType);
        }
示例#12
0
        //OK
        internal static void OnCodeClassSelection(AbstractTestFramework testFramework, CodeClass codeClass, DTE2 applicationObject, UnitTestCodeType codeType)
        {
            //We can only test public methods
            if (codeClass.Access != vsCMAccess.vsCMAccessPublic)
            {
                return;
            }

            Project parentProject = ProjectExaminar.GetCodeClassParentProject(codeClass);

            Project unitTestProject = ProjectExaminar.GetUnitTestProject(parentProject.Name, applicationObject, testFramework.TestProjectPostFix);

            if (unitTestProject != null && !CanGenerateHandleCodeClass(testFramework.TestClassFixturePostFix, codeClass, unitTestProject))
            {
                UTGHelper.ErrorHandler.ShowMessage(CommonUserMessages.MSG_TEST_ALREADY_EXISTS);
                OnGenerationFaliure(applicationObject);
                return;
            }

            string path = ProjectManager.GetUnitTestProjectPath(parentProject.Name, applicationObject, testFramework.TestProjectPostFix);

            path = UTGHelper.IO.GetFilePath(path);

            System.IO.DirectoryInfo newDirectoryInfo;

            string fullPath = System.IO.Path.Combine(path, parentProject.Name + testFramework.TestProjectPostFix);

            try{
                //Another way of doing this would be to try and find this project in current open solution saving us the exception
                newDirectoryInfo =
                    System.IO.Directory.CreateDirectory(fullPath);
            }
            catch (Exception ex) {
                Logger.LogException(ex);
            }
            finally{
                //Either case we have one by now so lets get its directory information
                newDirectoryInfo = new System.IO.DirectoryInfo(fullPath);
            }

            //If for whatever reason we are still failing then simply quit this
            if (newDirectoryInfo == null)
            {
                OnGenerationFaliure(applicationObject);

                throw new Exception(string.Format("Unable to create new Directory {0}", System.IO.Path.Combine(path, parentProject.Name + testFramework.TestProjectPostFix)));
            }

            if (unitTestProject == null)
            {
                try{
                    unitTestProject = ProjectManager.CreateAndAddUnitTestProject((Solution2)applicationObject.Solution, parentProject.Name + testFramework.TestProjectPostFix, newDirectoryInfo.FullName, codeType /*, license*/);
                }
                catch (Exception ex) {
                    Logger.LogException(ex);

                    OnGenerationFaliure(applicationObject);

                    throw;
                }
            }

            //Rethink this bit
            //Since above operation fails in either case then try getting the new Unit Test Project if created and added ok..
            if (unitTestProject == null)
            {
                unitTestProject = ProjectExaminar.GetUnitTestProject(parentProject.Name, applicationObject, testFramework.TestProjectPostFix);
            }

            if (unitTestProject == null)
            {
                OnGenerationFaliure(applicationObject);
                throw new Exception("Can not create new UnitTest Project");
            }

            AbstractTestClassFactory testClassFactory = new NUnitTestClassFactory();

            AbstractTestClass nunitClass = null;

            if (codeType == UnitTestCodeType.CSharp)
            {
                nunitClass = testClassFactory.CreateCSharpTestClass(codeClass.Namespace.FullName, codeClass.Name, codeClass.IsAbstract);
            }
            else if (codeType == UnitTestCodeType.VB)
            {
                nunitClass = testClassFactory.CreateVBTestClass(codeClass.Namespace.FullName, codeClass.Name, codeClass.IsAbstract);
            }

            if (!nunitClass.write(newDirectoryInfo.FullName, testFramework.TestClassFixturePostFix))
            {
                OnGenerationFaliure(applicationObject);
                return;
            }

            //add new class to project
            ProjectItem projectItem = ProjectManager.AddClassToProject(applicationObject, unitTestProject, null, nunitClass.FullName);

            List <CodeClass> lstCodeClasses = ProjectItemExaminor.GetCodeClasses(projectItem.FileCodeModel);

            if (lstCodeClasses == null || lstCodeClasses.Count == 0)
            {
                OnGenerationFaliure(applicationObject);
                return;
            }

            CodeClass unitTestCodeClass = lstCodeClasses[0];

            //passed this point the actual object will take care of it self.
            nunitClass.GenerateTest(unitTestCodeClass, codeClass);

            //To consider: this should be handled by an object that inherits from project type
            copyReferences(testFramework, parentProject, unitTestProject);

            applicationObject.ItemOperations.OpenFile(nunitClass.FullName, EnvDTE.Constants.vsViewKindAny);
        }
示例#13
0
        private static void OnCodeClassSelection(AbstractTestFramework testFramework, CodeProperty ce, DTE2 applicationObject, UnitTestCodeType codeType)
        {
            Project parentProject = ProjectExaminar.GetCodeClassParentProject((CodeClass)ce.Parent);

            Project unitTestProject = ProjectExaminar.GetUnitTestProject(parentProject.Name, applicationObject, testFramework.TestProjectPostFix);

            if (unitTestProject == null)
            {
                OnCodeClassSelection(testFramework, (CodeClass)ce.Parent, (CodeElement)ce, applicationObject, codeType);
                return;
            }

            //In cases where we can not simply generate automatically (definition already exists) we will
            //ask the user for input, wait on input, then async back to the function actual useful handler
            //HandelCodeClassSelection
            if (!CanGenerateHandleCodeProperty(testFramework.TestClassFixturePostFix, (CodeClass)ce.Parent, ce, unitTestProject))
            {
                UTGHelper.UserAlertActionRequired tvUserAlertActionRequired = new UserAlertActionRequired(
                    "Property definition already exits! What would you like to do?",
                    typeof(CodeProperty),
                    (CodeElement)ce,
                    ref applicationObject,
                    codeType,
                    testFramework);

                if (!tvUserAlertActionRequired.IsDisposed)
                {
                    tvUserAlertActionRequired.FormClosed += new System.Windows.Forms.FormClosedEventHandler(tvUserAlertActionRequired_FormClosed);

                    tvUserAlertActionRequired.Show();
                }
                else
                {
                    HandelCodeClassSelection(testFramework, ce, applicationObject, codeType);
                }

                return;
            }

            //otherwise simply call HandelCodeClassSelection
            HandelCodeClassSelection(testFramework, ce, applicationObject, codeType);
        }
示例#14
0
        public static void OnCodeElementSelection(AbstractTestFramework testFramework, CodeElement ce, DTE2 applicationObject, UnitTestCodeType codeType)
        {
            OnGenerationStart(applicationObject);

            if (ce is CodeNamespace)
            {
                ErrorHandler.ShowMessage(string.Format("You have selected the namesapce {0}! Currently there are no features which support namespaces.", ((CodeNamespace)ce).FullName));
            }
            //else if (codeClass != null)
            else if (ce is CodeClass)
            {
                if (((CodeClass)ce).Access != vsCMAccess.vsCMAccessPublic)
                {
                    return;
                }

                OnCodeClassSelection(testFramework, (CodeClass)ce, applicationObject, codeType);
            }
            //else if (codeFunction != null)
            else if (ce is CodeFunction)
            {
                if (((CodeFunction)ce).Access != vsCMAccess.vsCMAccessPublic)
                {
                    return;
                }

                OnCodeClassSelection(testFramework, (CodeFunction)ce, applicationObject, codeType);
            }
            else if (ce is CodeEnum)
            {
                if (((CodeEnum)ce).Access != vsCMAccess.vsCMAccessPublic)
                {
                    return;
                }

                ErrorHandler.ShowMessage(string.Format("You have selected the Enum {0}!", ((CodeEnum)ce).FullName));
            }
            else if (ce is CodeStruct)
            {
                ErrorHandler.ShowMessage(string.Format("You have selected the Structure {0}!", ((CodeStruct)ce).FullName));
            }
            else if (ce is CodeProperty)
            {
                if (((CodeProperty)ce).Access != vsCMAccess.vsCMAccessPublic)
                {
                    return;
                }

                OnCodeClassSelection(testFramework, (CodeProperty)ce, applicationObject, codeType);
            }
            else
            {
                ErrorHandler.ShowMessage(UTGHelper.CommonErrors.ERR_ILLEGAL_TEXT_SELECTION);
            }
        }
示例#15
0
        public static void OnCodeElementSelection(AbstractTestFramework testFramework, Project project, DTE2 applicationObject, UnitTestCodeType codeType)
        {
            try
            {
                foreach (ProjectItem pItem in project.ProjectItems)
                {
                    try
                    {
                        foreach (CodeElement tvCodeElementOutter in pItem.FileCodeModel.CodeElements)
                        {
                            if (tvCodeElementOutter is CodeNamespace)
                            {
                                try
                                {
                                    foreach (CodeElement tvCodeElement in ((CodeNamespace)tvCodeElementOutter).Members)
                                    {
                                        if (tvCodeElement != null &&
                                            tvCodeElement is CodeClass
                                            )
                                        {
                                            try
                                            {
                                                if (((CodeClass)tvCodeElement).Access == vsCMAccess.vsCMAccessPublic)
                                                {
                                                    OnCodeClassSelection(testFramework, (CodeClass)tvCodeElement, applicationObject, codeType /*, license*/);
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                Logger.LogException(ex);

                                                //ignore
                                                UTGHelper.ErrorHandler.HandleException(ex);
                                                return;
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    //ignore this
                                    Logger.LogException(ex);
                                }
                            }
                            else if (tvCodeElementOutter is CodeClass)
                            {
                                try
                                {
                                    if (((CodeClass)tvCodeElementOutter).Access == vsCMAccess.vsCMAccessPublic)
                                    {
                                        OnCodeClassSelection(testFramework, (CodeClass)tvCodeElementOutter, applicationObject, codeType /*, license*/);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogException(ex);

                                    //ignore
                                    UTGHelper.ErrorHandler.HandleException(ex);
                                    return;
                                }
                            }
                        }//if (tvCodeElementOutter is CodeNamespace)
                    }
                    catch (Exception ex)
                    {
                        //ignore this on;
                        Logger.LogException(ex);
                    }
                } //foreach (ProjectItem pItem in project.ProjectItems)
            }     //first try
            catch (Exception ex)
            {
                //ignore this on;throw;
                Logger.LogException(ex);
            }
        }
示例#16
0
        public static Project CreateAndAddUnitTestProject(Solution2 sol, string csProjectName, string csProjectPath, UnitTestCodeType codeType)
        {
            //System.Windows.Forms.MessageBox.Show("You must have at least 1 project in your current solution with a project name that ends with UnitTest");
            Project proj = null;

            try
            {
                String csItemTemplatePath;

                switch (codeType)
                {
                case UnitTestCodeType.CSharp:
                    csItemTemplatePath = sol.GetProjectTemplate(UTGHelper.CommonStrings.DEFAULT_PROJECT_TEMPLATE, UTGHelper.CommonStrings.DEFAULT_LANGUAGE);
                    break;

                case UnitTestCodeType.VB:
                    csItemTemplatePath = sol.GetProjectTemplate(UTGHelper.CommonStrings.DEFAULT_PROJECT_TEMPLATE, UTGHelper.CommonStrings.DEFAULT_VB_LANGUAGE);
                    break;

                default:
                    throw new Exception(string.Format(UTGHelper.CommonErrors.ERR_LANUAGE_TYPE_NOT_SUPPORTED, codeType.ToString()));
                }

                proj = sol.AddFromTemplate(csItemTemplatePath, csProjectPath, csProjectName, false);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);

                throw;
            }

            return(proj);
        }