public void WriteCombinesFileMetadata() { OutputFile[] outputFiles = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.UpdatedOutputFiles = (input, outputs) => outputFiles = outputs; var outputInfo = new OutputItem { File = TestFile }; outputInfo.Metadata["Generator"] = "TextTemplatingFileGenerator"; context.Write(outputInfo, string.Empty); outputInfo.Metadata.Clear(); outputInfo.Metadata["LastGenOutput"] = "Test.txt"; context.Write(outputInfo, string.Empty); } OutputFile outputFile = outputFiles.Single(output => output.File == TestFile); Assert.AreEqual("TextTemplatingFileGenerator", outputFile.Metadata["Generator"]); Assert.AreEqual("Test.txt", outputFile.Metadata["LastGenOutput"]); }
public void InitializeThrowsArgumentNullExceptionWhenGenerationEnvironmentIsNull() { using (var transformation = new FakeTransformation()) { TransformationContext.Initialize(transformation, null); } }
public void DefaultNamespaceCombinesRelativeInputFilePathWithRootNamespace() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) using (var template = new TestClrTemplate()) { template.Context = context; transformation.Host.TemplateFile = Path.Combine(Environment.CurrentDirectory, "SubFolder\\Template.tt"); transformation.Host.GetMetadataValue = (hierarhcy, inputFile, metadataName) => string.Empty; transformation.Host.GetPropertyValue = (hierarchy, propertyName) => { switch (propertyName) { case "RootNamespace": return "TestNamespace"; case "MSBuildProjectFullPath": return Path.Combine(Environment.CurrentDirectory, "Project.proj"); default: return string.Empty; } }; Assert.AreEqual("TestNamespace.SubFolder", template.DefaultNamespace); } }
public void DefaultNamespaceCombinesRelativeInputFilePathWithRootNamespace() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) using (var template = new TestClrTemplate()) { template.Context = context; transformation.Host.TemplateFile = Path.Combine(Environment.CurrentDirectory, "SubFolder\\Template.tt"); transformation.Host.GetMetadataValue = (hierarhcy, inputFile, metadataName) => string.Empty; transformation.Host.GetPropertyValue = (hierarchy, propertyName) => { switch (propertyName) { case "RootNamespace": return("TestNamespace"); case "MSBuildProjectFullPath": return(Path.Combine(Environment.CurrentDirectory, "Project.proj")); default: return(string.Empty); } }; Assert.AreEqual("TestNamespace.SubFolder", template.DefaultNamespace); } }
public void WriteCombinesFileReferences() { OutputFile[] outputFiles = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.UpdatedOutputFiles = (input, outputs) => outputFiles = outputs; var outputInfo = new OutputItem { File = TestFile }; outputInfo.References.Add("System"); context.Write(outputInfo, string.Empty); outputInfo.Metadata.Clear(); outputInfo.References.Add("System.Xml"); context.Write(outputInfo, string.Empty); } OutputFile outputFile = outputFiles.Single(output => output.File == TestFile); Assert.IsTrue(outputFile.References.Contains("System")); Assert.IsTrue(outputFile.References.Contains("System.Xml")); }
public void TransformationReturnsTransformationPassedToConstructor() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { Assert.AreSame(transformation, context.Transformation); } }
public void GetPropertyValueThrowsArgumentExceptionWhenNameIsEmpty() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { context.GetPropertyValue(" \t\r\n"); } }
public void GetMetadataValueThrowsArgumentNullExceptionWhenNameIsNull() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { context.GetMetadataValue(null); } }
public void InitializeThrowsArgumentExceptionWhenTransformationHostIsNull() { using (var transformation = new FakeTransformation()) { transformation.Host = null; TransformationContext.Initialize(transformation, transformation.GenerationEnvironment); } }
public void GetServiceReturnsNullWhenServiceIsNotAvailable() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { Assert.IsNull(context.GetService(typeof(ICloneable))); // Bogus service } }
public void WriteThrowsArgumentNullExceptionWhenOutputIsNull() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { context.Write(null, string.Empty); } }
public void CleanupRemovesCurrent() { using (var transformation = new FakeTransformation()) { TransformationContext.Initialize(transformation, transformation.GenerationEnvironment); TransformationContext.Cleanup(); var value = TransformationContext.Current; } }
public void CleanupRemovesCurrent() { using (var transformation = new FakeTransformation()) { TransformationContext.Initialize(transformation, transformation.GenerationEnvironment); TransformationContext.Cleanup(); var value = TransformationContext.Current; } }
public void Dispose() { if (this.transformation != null) { TransformationContext.Cleanup(); this.transformation.Dispose(); this.transformation = null; } }
public void Dispose() { if (this.transformation != null) { TransformationContext.Cleanup(); this.transformation.Dispose(); this.transformation = null; } }
public void ContextCanBeSet() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { this.generator.Context = context; Assert.AreSame(context, this.generator.Context); } }
public void ContextCanBeSet() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { this.generator.Context = context; Assert.AreSame(context, this.generator.Context); } }
public void HostReturnsHostOfTransformation() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { Assert.IsNotNull(context.Host); Assert.AreSame(transformation.Host, context.Host); } }
public void GetPropertyValueReturnsValueSuppliedByProvider() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { const string ExpectedValue = "TestValue"; transformation.Host.GetPropertyValue = (hierarchy, propertyName) => ExpectedValue; Assert.AreEqual(ExpectedValue, context.GetPropertyValue("TestProperty")); } }
public void GetServiceDelegatesToHost() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { var expected = ((IServiceProvider)transformation.Host).GetService(typeof(ITransformationContextProvider)); var actual = context.GetService(typeof(ITransformationContextProvider)); Assert.AreSame(expected, actual); } }
public void WriteThrowsInvalidOperationExceptionWhenDirectoryIsSpecifiedWithoutFile() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { context.Write(new OutputItem { Directory = "SubFolder" }, string.Empty); } }
public void WriteRespectsTransformationIndentationForSingleLineText() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.PushIndent("\t"); context.Write(new OutputItem(), TestText); Assert.AreEqual("\t" + TestText, transformation.TransformText()); } }
public void WriteThrowsInvalidOperationExceptionWhenProjectIsSpecifiedWithoutFile() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { context.Write(new OutputItem { Project = "Test.proj" }, string.Empty); } }
public void WriteAppendsToTransformationWhenOutputFileIsNotSpecified() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Write("TransformationOutput"); context.Write(new OutputItem(), "TemplateOutput"); Assert.AreEqual("TransformationOutput" + "TemplateOutput", transformation.TransformText()); } }
public void WriteAppendsToPreviousTemplateOutput() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { context.Write(new OutputItem(), "TemplateOutput"); transformation.Write("TransformationOutput"); Assert.AreEqual("TemplateOutput" + "TransformationOutput", transformation.TransformText()); } }
public void DisposeRaisesDisposedEvent() { bool disposed = false; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { context.Disposed += delegate { disposed = true; }; } Assert.IsTrue(disposed); }
public void TransformTextGeneratesFileHeader() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { this.template.Context = context; transformation.Host.TemplateFile = Path.GetRandomFileName(); string output = this.template.TransformText(); StringAssert.Contains(output, "<autogenerated>"); StringAssert.Contains(output, transformation.Host.TemplateFile); } }
public void DisposeInvokesOutputFileManagerWhenThereAreNoAdditionalOutputsSoThatPreviouslyGeneratedFilesCanBeDeleted() { bool outputFileManagerInvoked = false; using (var transformation = new FakeTransformation()) using (new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.UpdatedOutputFiles = (input, outputs) => outputFileManagerInvoked = true; } Assert.IsTrue(outputFileManagerInvoked); }
public void WriteRespectsTransformationIndentationForMultilineText() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.PushIndent("\t"); string text = TestText + Environment.NewLine + TestText; context.Write(new OutputItem(), text); string expectedOutput = "\t" + TestText + Environment.NewLine + "\t" + TestText; Assert.AreEqual(expectedOutput, transformation.TransformText()); } }
public void TransformTextGeneratesFileHeader() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { this.template.Context = context; transformation.Host.TemplateFile = Path.GetRandomFileName(); string output = this.template.TransformText(); StringAssert.Contains(output, "<autogenerated>"); StringAssert.Contains(output, transformation.Host.TemplateFile); } }
public void DisposeLogsTransformationExceptionsThrownByTransformationEndedEventHandlers() { CompilerErrorCollection actualErrors = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.LoggedErrors = errors => actualErrors = errors; context.Disposed += delegate { throw new TransformationException(); }; } Assert.AreEqual(1, actualErrors.Count); }
public void DisposeInvokesOutputFileManagerWhenThereAreAdditionalOutputs() { OutputFile[] outputFiles = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.UpdatedOutputFiles = (input, outputs) => outputFiles = outputs; context.Write(new OutputItem { File = TestFile }, TestText); } Assert.IsNotNull(outputFiles); }
public void DisposeReportsInputFileInErrorsWhenSessionHasInputFile() { const string InputFile = "TestInput.cs"; CompilerErrorCollection actualErrors = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Session[TransformationContext.InputFileNameKey] = InputFile; transformation.Host.LoggedErrors = errors => actualErrors = errors; context.Disposed += delegate { throw new TransformationException(); }; } Assert.AreEqual(InputFile, actualErrors.Cast <CompilerError>().Single().FileName); }
public void InitializeSetsCurrent() { using (var transformation = new FakeTransformation()) { TransformationContext.Initialize(transformation, transformation.GenerationEnvironment); try { Assert.IsNotNull(TransformationContext.Current); } finally { TransformationContext.Cleanup(); } } }
public void WriteSetsFileProject() { OutputFile[] outputFiles = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.UpdatedOutputFiles = (input, outputs) => outputFiles = outputs; context.Write(new OutputItem { File = TestFile, Project = "Test.proj" }, string.Empty); } OutputFile outputFile = outputFiles.Single(output => output.File == TestFile); Assert.AreEqual("Test.proj", outputFile.Project); }
public void CleanupDisposesCurrent() { using (var transformation = new FakeTransformation()) { bool disposed = false; TransformationContext.Initialize(transformation, transformation.GenerationEnvironment); try { TransformationContext.Current.Disposed += delegate { disposed = true; }; } finally { TransformationContext.Cleanup(); } Assert.IsTrue(disposed); } }
public void WriteSetsFileCopyToOutputDirectory() { OutputFile[] outputFiles = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.UpdatedOutputFiles = (input, outputs) => outputFiles = outputs; context.Write(new OutputItem { File = TestFile, CopyToOutputDirectory = CopyToOutputDirectory.CopyAlways }, string.Empty); } OutputFile outputFile = outputFiles.Single(output => output.File == TestFile); Assert.AreEqual(CopyToOutputDirectory.CopyAlways, outputFile.CopyToOutputDirectory); }
public void WriteRespectsTransformationIndentationForSingleLineText() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.PushIndent("\t"); context.Write(new OutputItem(), TestText); Assert.AreEqual("\t" + TestText, transformation.TransformText()); } }
public void HostReturnsHostOfTransformation() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { Assert.IsNotNull(context.Host); Assert.AreSame(transformation.Host, context.Host); } }
private static void WriteThrowsInvalidOperationException(OutputItem first, OutputItem second, params string[] keywords) { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { first.File = TestFile; context.Write(first, string.Empty); try { second.File = TestFile; context.Write(second, string.Empty); } catch (InvalidOperationException e) { Assert.AreNotEqual(typeof(TransformationException), e.GetType()); foreach (string keyword in keywords) { StringAssert.Contains(e.Message, keyword); } } } }
public void WriteThrowsInvalidOperationExceptionWhenDirectoryIsSpecifiedWithoutFile() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { context.Write(new OutputItem { Directory = "SubFolder" }, string.Empty); } }
public void WriteSetsFileProject() { OutputFile[] outputFiles = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.UpdatedOutputFiles = (input, outputs) => outputFiles = outputs; context.Write(new OutputItem { File = TestFile, Project = "Test.proj" }, string.Empty); } OutputFile outputFile = outputFiles.Single(output => output.File == TestFile); Assert.AreEqual("Test.proj", outputFile.Project); }
public void WriteCombinesFileMetadata() { OutputFile[] outputFiles = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.UpdatedOutputFiles = (input, outputs) => outputFiles = outputs; var outputInfo = new OutputItem { File = TestFile }; outputInfo.Metadata["Generator"] = "TextTemplatingFileGenerator"; context.Write(outputInfo, string.Empty); outputInfo.Metadata.Clear(); outputInfo.Metadata["LastGenOutput"] = "Test.txt"; context.Write(outputInfo, string.Empty); } OutputFile outputFile = outputFiles.Single(output => output.File == TestFile); Assert.AreEqual("TextTemplatingFileGenerator", outputFile.Metadata["Generator"]); Assert.AreEqual("Test.txt", outputFile.Metadata["LastGenOutput"]); }
public void WriteAppendsToTransformationWhenOutputFileIsNotSpecified() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Write("TransformationOutput"); context.Write(new OutputItem(), "TemplateOutput"); Assert.AreEqual("TransformationOutput" + "TemplateOutput", transformation.TransformText()); } }
public void GetServiceDelegatesToHost() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { var expected = ((IServiceProvider)transformation.Host).GetService(typeof(ITransformationContextProvider)); var actual = context.GetService(typeof(ITransformationContextProvider)); Assert.AreSame(expected, actual); } }
public void TransformationReturnsTransformationPassedToConstructor() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { Assert.AreSame(transformation, context.Transformation); } }
public void InitializeThrowsArgumentNullExceptionWhenGenerationEnvironmentIsNull() { using (var transformation = new FakeTransformation()) { TransformationContext.Initialize(transformation, null); } }
public void InitializeThrowsArgumentExceptionWhenTransformationHostIsNull() { using (var transformation = new FakeTransformation()) { transformation.Host = null; TransformationContext.Initialize(transformation, transformation.GenerationEnvironment); } }
public void InitializeSetsCurrent() { using (var transformation = new FakeTransformation()) { TransformationContext.Initialize(transformation, transformation.GenerationEnvironment); try { Assert.IsNotNull(TransformationContext.Current); } finally { TransformationContext.Cleanup(); } } }
public void WriteSetsFileCustomToolNamespace() { OutputFile[] outputFiles = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.UpdatedOutputFiles = (input, outputs) => outputFiles = outputs; context.Write(new OutputItem { File = TestFile, CustomToolNamespace = "Microsoft" }, string.Empty); } OutputFile outputFile = outputFiles.Single(output => output.File == TestFile); Assert.AreEqual("Microsoft", outputFile.CustomToolNamespace); }
public void WriteSetsFileItemTypeForDefaultOutput() { OutputFile[] outputFiles = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.UpdatedOutputFiles = (input, outputs) => outputFiles = outputs; context.Write(new OutputItem { ItemType = ItemType.Compile }, string.Empty); } OutputFile outputFile = outputFiles.Single(output => string.IsNullOrEmpty(output.File)); Assert.AreEqual(ItemType.Compile, outputFile.ItemType); }
public void WriteCombinesFileReferences() { OutputFile[] outputFiles = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.UpdatedOutputFiles = (input, outputs) => outputFiles = outputs; var outputInfo = new OutputItem { File = TestFile }; outputInfo.References.Add("System"); context.Write(outputInfo, string.Empty); outputInfo.Metadata.Clear(); outputInfo.References.Add("System.Xml"); context.Write(outputInfo, string.Empty); } OutputFile outputFile = outputFiles.Single(output => output.File == TestFile); Assert.IsTrue(outputFile.References.Contains("System")); Assert.IsTrue(outputFile.References.Contains("System.Xml")); }
public void WriteThrowsArgumentNullExceptionWhenOutputIsNull() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { context.Write(null, string.Empty); } }
public void WriteCombinesTextOfDifferentOutputsWithTheSameFileName() { OutputFile[] outputFiles = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.UpdatedOutputFiles = (input, outputs) => outputFiles = outputs; context.Write(new OutputItem { File = TestFile }, TestText); context.Write(new OutputItem { File = TestFile }, TestText); } OutputFile outputFile = outputFiles.Single(output => output.File == TestFile); Assert.AreEqual(TestText + TestText, outputFile.Content.ToString()); }
public void WriteThrowsInvalidOperationExceptionWhenProjectIsSpecifiedWithoutFile() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { context.Write(new OutputItem { Project = "Test.proj" }, string.Empty); } }
public void WriteCombinesTextWrittenToTheSameOutputMultipleTimes() { OutputFile[] outputFiles = null; using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.Host.UpdatedOutputFiles = (input, outputs) => outputFiles = outputs; var outputInfo = new OutputItem { File = TestFile }; context.Write(outputInfo, TestText); context.Write(outputInfo, TestText); } OutputFile outputFile = outputFiles.Single(output => output.File == TestFile); Assert.AreEqual(TestText + TestText, outputFile.Content.ToString()); }
public GeneratorTest() { this.transformation = new FakeTransformation(); TransformationContext.Initialize(this.transformation, this.transformation.GenerationEnvironment); }
public void WriteRespectsTransformationIndentationForMultilineText() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { transformation.PushIndent("\t"); string text = TestText + Environment.NewLine + TestText; context.Write(new OutputItem(), text); string expectedOutput = "\t" + TestText + Environment.NewLine + "\t" + TestText; Assert.AreEqual(expectedOutput, transformation.TransformText()); } }
public void WriteAppendsToPreviousTemplateOutput() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { context.Write(new OutputItem(), "TemplateOutput"); transformation.Write("TransformationOutput"); Assert.AreEqual("TemplateOutput" + "TransformationOutput", transformation.TransformText()); } }
public void GetServiceReturnsNullWhenServiceIsNotAvailable() { using (var transformation = new FakeTransformation()) using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment)) { Assert.IsNull(context.GetService(typeof(ICloneable))); // Bogus service } }