public async Task TransformationContextInitializesParameterValuesFromMetadata() { await UIThreadDispatcher.InvokeAsync(delegate { Project project = this.CreateTestProject(); string templateFile = Path.Combine(Path.GetDirectoryName(project.FullName), Path.ChangeExtension(Path.GetRandomFileName(), "tt")); ProjectItem template = CreateTestProjectItemFromFile(project.ProjectItems, templateFile); const string ParameterName = "StringParameter"; const string ParameterValue = "42"; template.SetItemAttribute(ParameterName, ParameterValue); string templateContent = "<#@ template language=\"" + this.TargetProject.DefaultTextTemplateLanguage + "\" #>" + "<#@ output extension=\"txt\" #>" + "<#@ include file=\"T4Toolbox.tt\" #>" + "<#@ parameter type=\"System.String\" name=\"" + ParameterName + "\" #>" + "<#= " + ParameterName + " #>"; File.WriteAllText(template.FileNames[1], templateContent); ((VSProjectItem)template.Object).RunCustomTool(); ProjectItem output = template.ProjectItems.Cast <ProjectItem>().Single(); Assert.AreEqual(ParameterValue, File.ReadAllText(output.FileNames[1])); }); }
public async Task UpdateOutputFilesMovesPreviouslyCreatedItemToTargetProject() { await UIThreadDispatcher.Invoke <Task>(async delegate { // Generate output file in the source project Project sourceProject = this.input.ContainingProject; this.output.Project = sourceProject.FullName; this.output.Directory = this.folder.FileNames[1]; this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output }); await this.SimulateCustomTool(); // Create a new project in the same directory string projectTemplate = Solution.GetProjectTemplate(this.TargetProject.Template, this.TargetProject.Language); string projectName = Path.GetRandomFileName(); string projectFolder = Path.GetDirectoryName(sourceProject.FullName); Solution.AddFromTemplate(projectTemplate, projectFolder, projectName); Project targetProject = Solution.Projects.Cast <Project>().Single(project => project.Name == projectName); // Re-generate output in the target project this.output.Project = targetProject.FullName; this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output }); await this.SimulateCustomTool(); // Verify that the output file was moved from the source project to the target project Assert.IsFalse(this.folder.ProjectItems.Cast <ProjectItem>().Any(item => item.Name == this.output.File)); ProjectItem targetFolder = targetProject.ProjectItems.Cast <ProjectItem>().Single(item => item.Name == this.folder.Name); Assert.IsTrue(targetFolder.ProjectItems.Cast <ProjectItem>().Any(item => item.Name == this.output.File)); }); }
private async Task VerifyFullTemplate(string templateName) { await UIThreadDispatcher.InvokeAsync(delegate { ProjectItem projectItem = this.CreateTestProjectItem(templateName); // Verify that project item created from full template has CustomTool/Generator set to enable T4 transformation const string TextTemplatingFileGenerator = "TextTemplatingFileGenerator"; Assert.AreEqual(TextTemplatingFileGenerator, projectItem.GetItemAttribute(ItemMetadata.Generator)); Assert.AreEqual(TextTemplatingFileGenerator, projectItem.Properties.Item(ProjectItemProperty.CustomTool).Value); // Verify that output file was automatically generated ProjectItem outputItem = projectItem.ProjectItems.Cast <ProjectItem>().Single(); // Verify that output file has extension default for the target language string outputFileName = outputItem.FileNames[1]; Assert.AreEqual(this.TargetProject.CodeFileExtension, Path.GetExtension(outputFileName)); // Verify that output file does not contain the T4 "ErrorGeneratingOutput" string generatedOutput = File.ReadAllText(outputFileName); Assert.AreEqual(string.Empty, generatedOutput.Trim()); // Verify that no errors were reported in the Error List winodw for the new project item or its output Assert.IsFalse(IntegrationTest.ErrorItems.Any(error => error.FileName == projectItem.FileNames[1])); Assert.IsFalse(IntegrationTest.ErrorItems.Any(error => error.FileName == outputItem.FileNames[1])); }); }
public void TestInitialize() { UIThreadDispatcher.Invoke(delegate { this.project = this.CreateTestProject(); this.provider = (ITransformationContextProvider)ServiceProvider.GetService(typeof(ITransformationContextProvider)); }); }
public async Task TemplatePropertyIsAvailableForAllProjectItemsOfTargetProject() { await UIThreadDispatcher.InvokeAsync(delegate { ProjectItem projectItem = this.CreateTestProjectItem(TextFileItemTemplate); Assert.IsNotNull(projectItem.Properties.Item(ProjectItemProperty.CustomToolTemplate)); }); }
public async Task UpdateOutputFilesThrowsTransformationExceptionWhenAbsoluteDirectoryIsOutsideOfProject() { await UIThreadDispatcher.InvokeAsync(delegate { this.output.Directory = Directory.GetParent(this.project.FullName).Parent.FullName; this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output }); }); }
public async Task UpdateOutputFilesThrowsTransformationExceptionWhenSpecifiedProjectIsNotInTheSolution() { await UIThreadDispatcher.InvokeAsync(delegate { this.output.Project = "Invalid.proj"; this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output }); }); }
public async Task UpdateOutputFilesThrowsTransformationExceptionWhenSpecifiedItemTypeIsNotAvailable() { await UIThreadDispatcher.InvokeAsync(delegate { this.output.ItemType = "InvalidItemType"; this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output }); }); }
public async Task UpdateOutputFilesThrowsTransformationExceptionWhenRelativeDirectoryIsOutsideOfProject() { await UIThreadDispatcher.InvokeAsync(delegate { this.output.Directory = @"..\.."; this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output }); }); }
public async Task PackageRegistersTransformationContextProcessor() { await UIThreadDispatcher.InvokeAsync(delegate { var templatingHost = (ITextTemplatingEngineHost)ServiceProvider.GetService(typeof(STextTemplating)); Type processorType = templatingHost.ResolveDirectiveProcessor(TransformationContextProcessor.Name); Assert.AreEqual(typeof(TransformationContextProcessor), processorType); }); }
public async Task TemplatePropertyChangesCustomToolToTemplatedFileGeneratorWhenItIsNotSpecified() { await UIThreadDispatcher.InvokeAsync(delegate { ProjectItem inputItem = this.CreateTestProjectItem(TextFileItemTemplate); ProjectItem templateItem = this.CreateTestProjectItem(TextFileItemTemplate); inputItem.Properties.Item(ProjectItemProperty.CustomToolTemplate).Value = templateItem.FileNames[1]; Assert.AreEqual(TemplatedFileGenerator.Name, inputItem.Properties.Item(ProjectItemProperty.CustomTool).Value); }); }
public async Task TemplatePropertySetsValueOfTemplateMetadata() { await UIThreadDispatcher.InvokeAsync(delegate { ProjectItem inputItem = this.CreateTestProjectItem(TextFileItemTemplate); ProjectItem templateItem = this.CreateTestProjectItem(TextFileItemTemplate); inputItem.Properties.Item(ProjectItemProperty.CustomToolTemplate).Value = templateItem.FileNames[1]; Assert.AreEqual(templateItem.FileNames[1], inputItem.GetItemAttribute(ItemMetadata.Template)); }); }
public async Task TemplatePropertyGetsValueOfTemplateMetadata() { await UIThreadDispatcher.InvokeAsync(delegate { const string TemplateFileName = "Test.tt"; ProjectItem projectItem = this.CreateTestProjectItem(TextFileItemTemplate); projectItem.SetItemAttribute(ItemMetadata.Template, TemplateFileName); Assert.AreEqual(TemplateFileName, projectItem.Properties.Item(ProjectItemProperty.CustomToolTemplate).Value); }); }
public async Task UpdateOutputFilesDoesNotStoreNameOfDefaultOutputTwice() { await UIThreadDispatcher.Invoke <Task>(async delegate { this.output.File = string.Empty; this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output }); await this.SimulateCustomTool(); Assert.AreEqual(string.Empty, this.input.GetItemAttribute(ItemMetadata.LastOutputs)); }); }
public async Task UpdateOutputFilesCreatesNewProjectItemInProjectSpecifiedAsPathRelativeToInputFile() { await UIThreadDispatcher.Invoke <Task>(async delegate { this.output.Project = FileMethods.GetRelativePath(this.input.FileNames[1], this.project.FullName); this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output }); await this.SimulateCustomTool(); Assert.IsTrue(this.project.ProjectItems.Cast <ProjectItem>().Any(item => item.Name == this.output.File)); }); }
public async Task UpdateOutputFilesChangesDefaultOutputItemToSpecifiedEncoding() { await UIThreadDispatcher.InvokeAsync(delegate { this.output.File = string.Empty; this.output.Encoding = Encoding.UTF32; this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output }); Assert.AreEqual(this.output.Encoding, this.templatingCallback.OutputEncoding); }); }
private async Task VerifyPartialTemplate(string templateName) { await UIThreadDispatcher.InvokeAsync(delegate { ProjectItem projectItem = this.CreateTestProjectItem(templateName); Assert.AreEqual(string.Empty, projectItem.GetItemAttribute(ItemMetadata.Generator)); Assert.AreEqual(0, projectItem.ProjectItems.Count); Assert.IsFalse(IntegrationTest.ErrorItems.Any(error => error.FileName == projectItem.FileNames[1])); }); }
public async Task GeneratorTriggersTransformationOfNewlyGeneratedScriptFile() { await UIThreadDispatcher.InvokeAsync(delegate { ProjectItem inputItem = this.CreateTestProjectItem(TextFileItemTemplate); inputItem.Properties.Item(ProjectItemProperty.CustomTool).Value = ScriptFileGenerator.Name; ProjectItem scriptITem = inputItem.ProjectItems.Cast <ProjectItem>().Single(); Assert.AreEqual(1, scriptITem.ProjectItems.Count); }); }
public async Task UpdateOutputFilesConfiguresDefaultOutputCreatedAfterTransformation() { await UIThreadDispatcher.Invoke <Task>(async delegate { this.output.File = string.Empty; this.output.ItemType = ItemType.Content; this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output }); ProjectItem outputItem = await this.SimulateCustomTool(); Assert.AreEqual(this.output.ItemType, outputItem.Properties.Item(ProjectItemProperty.ItemType).Value); }); }
public async Task UpdateOutputFilesCreatesNewProjectItemWithSpecifiedFile() { await UIThreadDispatcher.Invoke <Task>(async delegate { this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output }); await this.SimulateCustomTool(); ProjectItem outputItem = this.input.ProjectItems.Cast <ProjectItem>().Single(item => item.Name == this.output.File); Assert.AreEqual(this.output.Content.ToString(), File.ReadAllText(outputItem.FileNames[1])); }); }
public async Task GeneratorPreservesEncodingOfExistingScriptFile() { await UIThreadDispatcher.InvokeAsync(delegate { ProjectItem inputItem = this.CreateTestProjectItem(TextFileItemTemplate); File.WriteAllText(Path.ChangeExtension(inputItem.FileNames[1], ".tt"), TestText, Encoding.UTF32); inputItem.Properties.Item(ProjectItemProperty.CustomTool).Value = ScriptFileGenerator.Name; ProjectItem scriptItem = inputItem.ProjectItems.Cast <ProjectItem>().Single(); Assert.AreEqual(Encoding.UTF32, EncodingHelper.GetEncoding(scriptItem.FileNames[1])); }); }
public async Task TemplatePropertyDoesNotConvertRelativePathsToFull() { await UIThreadDispatcher.InvokeAsync(delegate { ProjectItem inputItem = this.CreateTestProjectItem(TextFileItemTemplate); ProjectItem templateItem = this.CreateTestProjectItem(TextFileItemTemplate); string templateFileName = Path.GetFileName(templateItem.FileNames[1]); inputItem.Properties.Item(ProjectItemProperty.CustomToolTemplate).Value = templateFileName; Assert.AreEqual(templateFileName, inputItem.GetItemAttribute(ItemMetadata.Template)); }); }
public async Task GeneratedScriptFileSpecifiesDefaultTextTemplateLanguageForProjectType() { await UIThreadDispatcher.InvokeAsync(delegate { ProjectItem inputItem = this.CreateTestProjectItem(TextFileItemTemplate); inputItem.Properties.Item(ProjectItemProperty.CustomTool).Value = ScriptFileGenerator.Name; ProjectItem scriptItem = inputItem.ProjectItems.Cast <ProjectItem>().Single(); string scriptContent = File.ReadAllText(scriptItem.FileNames[1]); StringAssert.StartsWith(scriptContent, "<#@ template language=\"" + this.TargetProject.DefaultTextTemplateLanguage + "\""); }); }
public async Task PackageLoads() { await UIThreadDispatcher.InvokeAsync(delegate { var shell = (IVsShell)ServiceProvider.GetService(typeof(SVsShell)); IVsPackage package; var packageGuid = new Guid(T4ToolboxPackage.Id); Assert.AreEqual(VSConstants.S_OK, shell.LoadPackage(ref packageGuid, out package)); Assert.IsNotNull(package); }); }
public async Task GeneratorProducesEmptyScriptFile() { await UIThreadDispatcher.InvokeAsync(delegate { ProjectItem inputItem = this.CreateTestProjectItem(TextFileItemTemplate); inputItem.Properties.Item(ProjectItemProperty.CustomTool).Value = ScriptFileGenerator.Name; ProjectItem scriptItem = inputItem.ProjectItems.Cast <ProjectItem>().Single(); Assert.AreEqual(".tt", Path.GetExtension(scriptItem.FileNames[1])); Assert.AreEqual("TextTemplatingFileGenerator", scriptItem.GetItemAttribute(ItemMetadata.Generator)); }); }
public async Task GeneratedScriptFileSpecifiesDefaultProjectFileExtension() { await UIThreadDispatcher.InvokeAsync(delegate { ProjectItem inputItem = this.CreateTestProjectItem(TextFileItemTemplate); inputItem.Properties.Item(ProjectItemProperty.CustomTool).Value = ScriptFileGenerator.Name; ProjectItem scriptItem = inputItem.ProjectItems.Cast <ProjectItem>().Single(); string scriptContent = File.ReadAllText(scriptItem.FileNames[1]); StringAssert.Contains(scriptContent, "<#@ output extension=\"" + this.TargetProject.CodeFileExtension.Trim('.') + "\""); }); }
public async Task ParametersDefinedInInputFileAreRecognized() { await UIThreadDispatcher.InvokeAsync(delegate { ProjectItem projectItem = this.CreateTestProjectItem("Text File"); projectItem.Properties.Item(ProjectItemProperty.CustomTool).Value = "TextTemplatingFileGenerator"; File.WriteAllText(projectItem.FileNames[1], "<#@ parameter name=\"TestParameter\" type=\"System.String\" #>"); var target = (ICustomTypeDescriptor)projectItem.Properties.Item(ProjectItemProperty.CustomToolParameters).Value; Assert.AreEqual("TestParameter", target.GetProperties().Cast <PropertyDescriptor>().Single().Name); }); }
private async Task VerifyPartialTemplate(string templateName) { await UIThreadDispatcher.InvokeAsync(delegate { ProjectItem projectItem = this.CreateTestProjectItem(templateName); // Assert that project item created from partial template doesn't have CustomTool/Generator set to prevent T4 transformation Assert.AreEqual(string.Empty, projectItem.GetItemAttribute(ItemMetadata.Generator)); Assert.AreEqual(string.Empty, projectItem.Properties.Item(ProjectItemProperty.CustomTool).Value); Assert.AreEqual(0, projectItem.ProjectItems.Count); }); }
public async Task UpdateOutputFilesDoesNotGenerateWarningWhenDefaultOutputFileIsEmpty() // Because there may be nothing developer can do about it { await UIThreadDispatcher.Invoke <Task>(async delegate { this.output.File = string.Empty; this.output.Content.Clear(); this.output.Content.Append(WhiteSpaceText); this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output }); await this.SimulateCustomTool(); Assert.IsFalse(ErrorItems.Any(item => item.FileName == this.input.FileNames[1])); }); }
public async Task UpdateOutputFilesCreatesNewProjectItemInDirectorySpecifiedAsPathRelativeToInputFile() { await UIThreadDispatcher.Invoke <Task>(async delegate { this.output.Directory = Path.GetRandomFileName(); this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output }); await this.SimulateCustomTool(); ProjectItem outputFolder = this.folder.ProjectItems.Cast <ProjectItem>().Single(item => item.Name == this.output.Directory); Assert.IsTrue(outputFolder.ProjectItems.Cast <ProjectItem>().Any(item => item.Name == this.output.File)); }); }