Example #1
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);
        }
Example #2
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);
        }
Example #3
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);
        }
        protected override string FormatFrameworkArgument(TestFixturOption runOption)
        {
            #region example in plain test
            //PartCover.exe
            //--target="C:\Program Files\NUnit 2.4.6\bin\nunit-console.exe"
            //--target-args="C:\Temp\MyClassLibrary\MyClassLibrary.nunit\MyClassLibrary.nunit.csproj"
            //--include=[MyClassLibrary]*
            #endregion

            //fix was found http://www.pinvoke.net/default.aspx/kernel32.GetShortPathName
            string tvArguments = string.Empty;

            tvArguments += "--target=\"" + System.IO.Path.Combine(this.CommandsDirectory, this.ConsoleName);

            string tvOutputReportFullName = ProjectExaminar.GetProjectPath(runOption.Project) + "\\PartCoverOutput.xml";

            if (System.IO.File.Exists(tvOutputReportFullName))
            {
                System.IO.File.Delete(tvOutputReportFullName);
            }

            if (!UTGHelper.IO.CreateFile(tvOutputReportFullName))
            {
                return(string.Empty);
            }

            tvArguments += "\" --output=\"";

            tvArguments += ToShortPathName(tvOutputReportFullName);

            tvArguments += "\" --target-args=\"";

            //Next line can be iffy when doing this runOption.Project.Name + ".csproj"
            tvArguments += ToShortPathName(ProjectExaminar.GetProjectPath(runOption.Project)) + "\\" + runOption.Project.Name + ".csproj";

            if (runOption.CClass == null)
            {
                tvArguments += "\" --include=[";
                tvArguments += ProjectExaminar.GetProjectNamespaceName(ProjectExaminar.GetUnitTestProjectsOriginalProject(
                                                                           runOption.Project.Name, (DTE2)runOption.Project.DTE, testFramework.TestProjectPostFix));
                tvArguments += "]";
                tvArguments += "*";
            }
            else
            {
                tvArguments += "\"";

                string tvClassName      = ProjectItemExaminor.GetUnitTestClassOriginalClassName(runOption.CClass.Name, testFramework.TestClassFixturePostFix);
                string tvClassNamespace = ProjectItemExaminor.GetUnitTestClassOriginalClassNamespace(runOption.CClass.Namespace.FullName);

                Project project = ProjectExaminar.GetUnitTestProjectsOriginalProject(runOption.Project.Name, (DTE2)runOption.Project.DTE, testFramework.TestProjectPostFix);

                if (project == null)
                {
                    throw new Exception(string.Format("Unable to locate Unit Test Project for the Project {0}.", runOption.Project.Name));
                }

                tvArguments += " --include=[";
                tvArguments += ProjectExaminar.GetProjectNamespaceName(project);
                tvArguments += "]";
                tvArguments += tvClassNamespace;
                tvArguments += ".";
                tvArguments += tvClassName;
                tvArguments += "* ";
            }

            return(tvArguments);
        }