예제 #1
0
        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]));
            });
        }
예제 #2
0
        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));
            });
        }
예제 #3
0
        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));
     });
 }
예제 #5
0
 public async Task TemplatePropertyIsAvailableForAllProjectItemsOfTargetProject()
 {
     await UIThreadDispatcher.InvokeAsync(delegate
     {
         ProjectItem projectItem = this.CreateTestProjectItem(TextFileItemTemplate);
         Assert.IsNotNull(projectItem.Properties.Item(ProjectItemProperty.CustomToolTemplate));
     });
 }
예제 #6
0
 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 });
     });
 }
예제 #7
0
 public async Task UpdateOutputFilesThrowsTransformationExceptionWhenSpecifiedProjectIsNotInTheSolution()
 {
     await UIThreadDispatcher.InvokeAsync(delegate
     {
         this.output.Project = "Invalid.proj";
         this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output });
     });
 }
예제 #8
0
 public async Task UpdateOutputFilesThrowsTransformationExceptionWhenSpecifiedItemTypeIsNotAvailable()
 {
     await UIThreadDispatcher.InvokeAsync(delegate
     {
         this.output.ItemType = "InvalidItemType";
         this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output });
     });
 }
예제 #9
0
 public async Task UpdateOutputFilesThrowsTransformationExceptionWhenRelativeDirectoryIsOutsideOfProject()
 {
     await UIThreadDispatcher.InvokeAsync(delegate
     {
         this.output.Directory = @"..\..";
         this.provider.UpdateOutputFiles(this.input.FileNames[1], new[] { this.output });
     });
 }
예제 #10
0
 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);
     });
 }
예제 #11
0
 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);
     });
 }
예제 #12
0
 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));
     });
 }
예제 #13
0
 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);
     });
 }
예제 #14
0
 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));
     });
 }
예제 #15
0
 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));
     });
 }
예제 #16
0
 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);
     });
 }
예제 #17
0
 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]));
     });
 }
예제 #18
0
 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);
     });
 }
예제 #19
0
 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);
     });
 }
예제 #20
0
        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]));
            });
        }
예제 #21
0
 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]));
     });
 }
예제 #22
0
 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));
     });
 }
예제 #23
0
 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 + "\"");
     });
 }
예제 #24
0
 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);
     });
 }
예제 #25
0
 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));
     });
 }
예제 #26
0
 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('.') + "\"");
     });
 }
예제 #27
0
 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);
     });
 }
예제 #28
0
        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);
            });
        }
예제 #29
0
 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]));
     });
 }
예제 #30
0
        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));
            });
        }