private string GenerateUnitTestContents(TestGenerationContext context)
        {
            TestFramework testFramework = context.TestFramework;
            MockFramework mockFramework = context.MockFramework;

            string fileTemplate   = StaticBoilerplateSettings.GetTemplate(testFramework, mockFramework, TemplateType.File);
            string filledTemplate = StringUtilities.ReplaceTokens(
                fileTemplate,
                (tokenName, propertyIndex, builder) =>
            {
                if (WriteGlobalToken(tokenName, builder, context))
                {
                    return;
                }

                if (WriteContentToken(tokenName, propertyIndex, builder, context, fileTemplate))
                {
                    return;
                }

                WriteTokenPassthrough(tokenName, builder);
            });

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

            return(formattedNode.ToFullString());
        }
Beispiel #2
0
        private string GenerateUnitTestContents(TestGenerationContext context)
        {
            TestFramework testFramework = context.TestFramework;
            MockFramework mockFramework = context.MockFramework;

            string fileTemplate   = StaticBoilerplateSettings.GetTemplate(testFramework, mockFramework, TemplateType.File);
            string filledTemplate = StringUtilities.ReplaceTokens(
                fileTemplate,
                (tokenName, propertyIndex, builder) =>
            {
                switch (tokenName)
                {
                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, propertyIndex));
                    break;

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

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

                case "ClassNameShortLower":
                    // Legacy, new syntax is ClassNameShort.CamelCase
                    builder.Append(GetShortClassNameLower(context.ClassName));
                    break;

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

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

            return(formattedNode.ToFullString());
        }
        /// <summary>
        /// Gets the working copy of the template on the options dialog.
        /// </summary>
        /// <param name="testFramework">The test framework the template applies to.</param>
        /// <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(TestFramework testFramework, MockFramework mockFramework, TemplateType templateType)
        {
            string template;

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

            return(StaticBoilerplateSettings.GetTemplate(testFramework, mockFramework, templateType));
        }
Beispiel #4
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.TestFramework, context.MockFramework, TemplateType.MockObjectReference);
                        mockReferenceStatement = ReplaceInterfaceTokens(template, constructorType);
                    }

                    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.TestFramework, context.MockFramework, TemplateType.MockObjectReference);
                    string mockReferenceStatement = ReplaceInterfaceTokens(template, property);

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

                builder.Append(@"}");
            }
        }
Beispiel #5
0
        private static void WriteMockFieldInitializations(StringBuilder builder, TestGenerationContext context)
        {
            string template = StaticBoilerplateSettings.GetTemplate(context.TestFramework, context.MockFramework, TemplateType.MockFieldInitialization);

            WriteFieldLines(builder, context, template);
        }