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()); }
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)); }
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(@"}"); } }
public void Apply() { foreach (KeyValuePair <string, string> pair in this.templateHoldingDictionary) { string[] keyParts = pair.Key.Split('_'); string testFrameworkString = keyParts[0]; string mockFrameworkString = keyParts[1]; string templateTypeString = keyParts[2]; StaticBoilerplateSettings.SetTemplate(testFrameworkString, mockFrameworkString, templateTypeString, pair.Value); } }
internal async Task CreateUnitTestAsync(IList <ProjectItemSummary> selectedFiles, bool addToProject = true) { var generationService = new TestGenerationService(); var createdTestPaths = new List <string>(); foreach (ProjectItemSummary selectedFile in selectedFiles) { string generatedTestPath = await generationService.GenerateUnitTestFileAsync( selectedFile, this.SelectedProject.Project, this.SelectedTestFramework, this.SelectedMockFramework); createdTestPaths.Add(generatedTestPath); } if (addToProject) { bool focusSet = false; foreach (string createdTestPath in createdTestPaths) { // Add the file to project ProjectItem testItem = this.SelectedProject.Project.ProjectItems.AddFromFile(createdTestPath); Window testWindow = testItem.Open(EnvDTE.Constants.vsViewKindCode); testItem.ExpandView(); testWindow.Visible = true; if (!focusSet) { testWindow.SetFocus(); focusSet = true; } } StaticBoilerplateSettings.SaveSelectedTestProject(this.dte.Solution.FileName, this.SelectedProject.Project.FileName); } this.View?.Close(); }
public CreateUnitTestBoilerplateViewModel() { this.dte = (DTE2)ServiceProvider.GlobalProvider.GetService(typeof(DTE)); this.TestProjects = new List <TestProject>(); IList <Project> allProjects = SolutionUtilities.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]; } this.TestFrameworkChoices = TestFrameworks.List; this.MockFrameworkChoices = MockFrameworks.List; // Populate selected test/mock frameworks based on selected project this.UpdateSelectedFrameworks(); }
private static void WriteMockFieldInitializations(StringBuilder builder, TestGenerationContext context) { string template = StaticBoilerplateSettings.GetTemplate(context.TestFramework, context.MockFramework, TemplateType.MockFieldInitialization); WriteFieldLines(builder, context, template); }