public CreateUnitTestBoilerplateViewModel()
        {
            this.dte = (DTE2)ServiceProvider.GlobalProvider.GetService(typeof(DTE));


            this.TestProjects = new List <TestProject>();
            IList <Project> allProjects = Utilities.GetProjects(this.dte);


            string lastSelectedProject = StaticBoilerplateSettings.GetLastSelectedProject(this.dte.Solution.FileName);


            var newProjectList = new List <TestProject>();

            foreach (Project project in allProjects)
            {
                TestProject testProject = new TestProject
                {
                    Name    = project.Name,
                    Project = project
                };

                newProjectList.Add(testProject);
            }

            this.TestProjects = newProjectList.OrderBy(p => p.Name).ToList();

            // First see if we've saved an entry for the last selected test project for this solution.
            if (this.selectedProject == null && lastSelectedProject != null)
            {
                foreach (var project in this.TestProjects)
                {
                    if (string.Equals(lastSelectedProject, project.Project.FileName, StringComparison.OrdinalIgnoreCase))
                    {
                        this.selectedProject = project;
                        break;
                    }
                }
            }

            // If we don't have an entry yet, look for a project name that contains "Test"
            if (this.selectedProject == null)
            {
                foreach (var project in this.TestProjects)
                {
                    if (project.Name.ToLowerInvariant().Contains("test"))
                    {
                        this.selectedProject = project;
                        break;
                    }
                }
            }

            // Otherwise select the first project
            if (this.selectedProject == null && this.TestProjects.Count > 0)
            {
                this.selectedProject = this.TestProjects[0];
            }
        }
        private static void WriteExplicitConstructor(StringBuilder builder, TestGenerationContext context, string currentIndent)
        {
            builder.Append($"new {context.ClassName}");

            if (context.ConstructorTypes.Count > 0)
            {
                builder.AppendLine("(");

                for (int i = 0; i < context.ConstructorTypes.Count; i++)
                {
                    string         mockReferenceStatement;
                    InjectableType constructorType = context.ConstructorTypes[i];
                    if (constructorType == null)
                    {
                        mockReferenceStatement = "TODO";
                    }
                    else
                    {
                        string template = StaticBoilerplateSettings.GetTemplate(context.MockFramework, TemplateType.MockObjectReference);
                        mockReferenceStatement = template
                                                 .Replace("$InterfaceName$", constructorType.TypeName)
                                                 .Replace("$InterfaceNameBase$", constructorType.TypeBaseName);
                    }

                    builder.Append($"{currentIndent}    {mockReferenceStatement}");

                    if (i < context.ConstructorTypes.Count - 1)
                    {
                        builder.AppendLine(",");
                    }
                }

                builder.Append(")");
            }
            else if (context.Properties.Count == 0)
            {
                builder.Append("()");
            }

            if (context.Properties.Count > 0)
            {
                builder.AppendLine();
                builder.AppendLine("{");

                foreach (InjectableProperty property in context.Properties)
                {
                    string template = StaticBoilerplateSettings.GetTemplate(context.MockFramework, TemplateType.MockObjectReference);
                    string mockReferenceStatement = template
                                                    .Replace("$InterfaceName$", property.TypeName)
                                                    .Replace("$InterfaceNameBase$", property.TypeBaseName);

                    builder.AppendLine($"{property.Name} = {mockReferenceStatement},");
                }

                builder.Append(@"}");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the working copy of the template on the options dialog.
        /// </summary>
        /// <param name="mockFramework">The mock framework the template applies to.</param>
        /// <param name="templateType">The template type.</param>
        /// <returns>The working copy of the template on the options dialog.</returns>
        private string GetTemplate(MockFramework mockFramework, TemplateType templateType)
        {
            string template;

            if (this.templateHoldingDictionary.TryGetValue(GetDictionaryKey(mockFramework, templateType), out template))
            {
                return(template);
            }

            return(StaticBoilerplateSettings.GetTemplate(mockFramework, templateType));
        }
Exemplo n.º 4
0
        public void Apply()
        {
            foreach (KeyValuePair <string, string> pair in this.templateHoldingDictionary)
            {
                string[] keyParts = pair.Key.Split('_');

                string mockFrameworkString = keyParts[0];
                string templateTypeString  = keyParts[1];

                StaticBoilerplateSettings.SetTemplate(mockFrameworkString, templateTypeString, pair.Value);
            }
        }
        private static void WriteMockFieldInitializations(StringBuilder builder, TestGenerationContext context)
        {
            string template = StaticBoilerplateSettings.GetTemplate(context.MockFramework, TemplateType.MockFieldInitialization);

            WriteFieldLines(builder, context, template);
        }
        private string GenerateUnitTestContents(TestGenerationContext context)
        {
            TestFramework testFramework = context.TestFramework;
            MockFramework mockFramework = context.MockFramework;

            string pascalCaseShortClassName = null;

            foreach (string suffix in ClassSuffixes)
            {
                if (className.EndsWith(suffix))
                {
                    pascalCaseShortClassName = suffix;
                    break;
                }
            }

            if (pascalCaseShortClassName == null)
            {
                pascalCaseShortClassName = className;
            }

            string classVariableName = pascalCaseShortClassName.Substring(0, 1).ToLowerInvariant() + pascalCaseShortClassName.Substring(1);

            string fileTemplate = StaticBoilerplateSettings.GetTemplate(mockFramework, TemplateType.File);
            var    builder      = new StringBuilder();

            for (int i = 0; i < fileTemplate.Length; i++)
            {
                char c = fileTemplate[i];
                if (c == '$')
                {
                    int endIndex = -1;
                    for (int j = i + 1; j < fileTemplate.Length; j++)
                    {
                        if (fileTemplate[j] == '$')
                        {
                            endIndex = j;
                            break;
                        }
                    }

                    if (endIndex < 0)
                    {
                        // We couldn't find the end index for the replacement property name. Continue.
                        builder.Append(c);
                    }
                    else
                    {
                        // Calculate values on demand from switch statement. Some are preset values, some need a bit of calc like base name,
                        // some are dependent on the test framework (attributes), some need to pull down other templates and loop through mock fields
                        string propertyName = fileTemplate.Substring(i + 1, endIndex - i - 1);
                        switch (propertyName)
                        {
                        case "UsingStatements":
                            WriteUsings(builder, context);
                            break;

                        case "Namespace":
                            builder.Append(context.UnitTestNamespace);
                            break;

                        case "MockFieldDeclarations":
                            WriteMockFieldDeclarations(builder, context);
                            break;

                        case "MockFieldInitializations":
                            WriteMockFieldInitializations(builder, context);
                            break;

                        case "ExplicitConstructor":
                            WriteExplicitConstructor(builder, context, FindIndent(fileTemplate, i));
                            break;

                        case "ClassName":
                            builder.Append(context.ClassName);
                            break;

                        case "ClassNameShort":
                            builder.Append(GetShortClassName(context.ClassName));
                            break;

                        case "ClassNameShortLower":
                            builder.Append(GetShortClassNameLower(context.ClassName));
                            break;

                        case "TestClassAttribute":
                            builder.Append(TestFrameworkAbstraction.GetTestClassAttribute(testFramework));
                            break;

                        case "TestInitializeAttribute":
                            builder.Append(TestFrameworkAbstraction.GetTestInitializeAttribute(testFramework));
                            break;

                        case "TestCleanupAttribute":
                            builder.Append(TestFrameworkAbstraction.GetTestCleanupAttribute(testFramework));
                            break;

                        case "TestMethodAttribute":
                            builder.Append(TestFrameworkAbstraction.GetTestMethodAttribute(testFramework));
                            break;

                        default:
                            // We didn't recognize it, just pass through.
                            builder.Append($"${propertyName}$");
                            break;
                        }

                        i = endIndex;
                    }
                }
                else
                {
                    builder.Append(c);
                }
            }

            SyntaxTree tree          = CSharpSyntaxTree.ParseText(builder.ToString());
            SyntaxNode formattedNode = Formatter.Format(tree.GetRoot(), CreateUnitTestBoilerplateCommandPackage.VisualStudioWorkspace);

            return(formattedNode.ToString());
        }