public void GenerateStaticPropertyForParameterCanInitilialize(string value) { var engine = new TemplatingEngine(); var host = new DummyHost() { TemplateFile = "test.tt" }; host.Parameters.Add("TestParameter", value); var tt = engine.CompileTemplate(T4ParameterSample, host); foreach (TemplateError error in host.Errors) { Console.Error.WriteLine(error.Message); } Type ttType = tt.textTransformation?.GetType(); Assert.IsNotNull(ttType); var initMethod = ttType.GetMethod("Initialize"); var parameter = ttType.GetProperty("TestParameter", BindingFlags.Public | BindingFlags.Static); initMethod.Invoke(tt.textTransformation, null); Assert.AreEqual(value, parameter.GetValue(null)); }
public void IncludeOnceTest() { var host = new DummyHost(); host.Locations.Add(FooIncludeName, FooIncludeName); host.Contents.Add(FooIncludeName, FooInclude.NormalizeNewlines()); host.Locations.Add(BarIncludeName, BarIncludeName); host.Contents.Add(BarIncludeName, BarInclude.NormalizeNewlines()); var pt = ParsedTemplate.FromTextInternal(IncludeSample.NormalizeNewlines(), host); Assert.Empty(pt.Errors); var content = new List <TemplateSegment> (pt.Content); var dirs = new List <Directive> (pt.Directives); Assert.Empty(dirs); Assert.Collection(content, t => Assert.Equal("One\n", t.Text), t => Assert.Equal("Foo\n", t.Text), t => Assert.Equal("Two\n", t.Text), t => Assert.Equal("Bar\n", t.Text), t => Assert.Equal("Three\n", t.Text), t => Assert.Equal("Four\n", t.Text), t => Assert.Equal("Bar\n", t.Text), t => Assert.Equal("Five\n", t.Text) ); }
//NOTE: we set the newline property on the code generator so that the whole files has matching newlines, // in order to match the newlines in the verbatim code blocks void Generate (string input, string expectedOutput, string newline) { DummyHost host = new DummyHost (); string className = "GeneratedTextTransformation4f504ca0"; string code = GenerateCode (host, input, className, newline); Assert.AreEqual (0, host.Errors.Count); Assert.AreEqual (expectedOutput, TemplatingEngineHelper.StripHeader (code, newline)); }
//NOTE: we set the newline property on the code generator so that the whole files has matching newlines, // in order to match the newlines in the verbatim code blocks void Generate(string input, string expectedOutput, string newline) { DummyHost host = new DummyHost(); string className = "GeneratedTextTransformation4f504ca0"; string code = GenerateCode(host, input, className, newline); Assert.AreEqual(0, host.Errors.Count); Assert.AreEqual(expectedOutput, TemplatingEngineHelper.StripHeader(code, newline)); }
public void DefaultLanguage () { DummyHost host = new DummyHost (); string template = @"<#= DateTime.Now #>"; ParsedTemplate pt = ParsedTemplate.FromText (template, host); Assert.AreEqual (0, host.Errors.Count); TemplateSettings settings = TemplatingEngine.GetSettings (host, pt); Assert.AreEqual (settings.Language, "C#"); }
public void Preprocess_ControlBlockAfterIncludedTemplateWithClassFeatureBlock_ReturnsValidCSharpOutput() { string input = InputTemplate_ControlBlockAfterIncludedTemplateWithClassFeatureBlock.NormalizeNewlines(); DummyHost host = CreateDummyHostForControlBlockAfterIncludedTemplateWithClassFeatureBlockTest(); string expectedOutput = TemplatingEngineHelper.CleanCodeDom(Output_ControlBlockAfterIncludedTemplateWithClassFeatureBlock.NormalizeNewlines(), "\n"); string output = Preprocess(input, host); Assert.AreEqual(expectedOutput, output, output); }
public void Preprocess_ControlBlockAfterIncludedTemplateWithClassFeatureBlock_ReturnsValidCSharpOutput() { string input = InputTemplate_ControlBlockAfterIncludedTemplateWithClassFeatureBlock; DummyHost host = CreateDummyHostForControlBlockAfterIncludedTemplateWithClassFeatureBlockTest(); string expectedOutput = Output_ControlBlockAfterIncludedTemplateWithClassFeatureBlock; string output = Preprocess(input, host); Assert.AreEqual(expectedOutput, output, output); }
DummyHost CreateDummyHostForControlBlockAfterIncludedTemplateWithClassFeatureBlockTest() { DummyHost host = new DummyHost(); string includeTemplateFileName = @"d:\test\IncludedFile.tt"; host.Locations.Add(includeTemplateFileName, includeTemplateFileName); host.Contents.Add(includeTemplateFileName, IncludedTemplate_ControlBlockAfterIncludedTemplate); return(host); }
public void DefaultLanguage() { DummyHost host = new DummyHost(); string template = @"<#= DateTime.Now #>"; ParsedTemplate pt = ParsedTemplate.FromText(template, host); Assert.AreEqual(0, host.Errors.Count); TemplateSettings settings = TemplatingEngine.GetSettings(host, pt); Assert.AreEqual(settings.Language, "C#"); }
public void DefaultLanguage() { var host = new DummyHost(); string template = @"<#= DateTime.Now #>"; var pt = ParsedTemplate.FromTextInternal(template, host); Assert.Empty(host.Errors); TemplateSettings settings = TemplatingEngine.GetSettings(host, pt); Assert.Equal("C#", settings.Language); }
public void ControlBlockAfterIncludedTemplateWithClassFeatureBlock() { string input = InputTemplate_ControlBlockAfterIncludedTemplateWithClassFeatureBlock.NormalizeNewlines(); DummyHost host = CreateDummyHostForControlBlockAfterIncludedTemplateWithClassFeatureBlockTest(); host.HostOptions.Add("UseRelativeLinePragmas", true); string expectedOutput = TemplatingEngineHelper.CleanCodeDom(Output_ControlBlockAfterIncludedTemplateWithClassFeatureBlock.NormalizeNewlines(), "\n"); string output = Preprocess(input, host); Assert.Equal(expectedOutput, output); }
public void ParseTest() { string tf = "test.input"; var pt = new ParsedTemplate("test.input"); var tk = new Tokeniser(tf, ParseSample1.NormalizeNewlines()); var host = new DummyHost(); pt.Parse(host, tk); Assert.AreEqual(0, pt.Errors.Count); var content = new List <TemplateSegment> (pt.Content); var dirs = new List <Directive> (pt.Directives); Assert.AreEqual(1, dirs.Count); Assert.AreEqual(6, content.Count); Assert.AreEqual("template", dirs[0].Name); Assert.AreEqual(1, dirs[0].Attributes.Count); Assert.AreEqual("C#v3.5", dirs[0].Attributes["language"]); Assert.AreEqual(new Location(tf, 1, 1), dirs[0].TagStartLocation); Assert.AreEqual(new Location(tf, 1, 34), dirs[0].EndLocation); Assert.AreEqual("Line One\nLine Two\n", content[0].Text); Assert.AreEqual("\nvar foo = 5;\n", content[1].Text); Assert.AreEqual("Line Three ", content[2].Text); Assert.AreEqual(" bar ", content[3].Text); Assert.AreEqual("\nLine Four\n", content[4].Text); Assert.AreEqual(" \nvar s = \"baz \\\\#>\";\n", content[5].Text); Assert.AreEqual(SegmentType.Content, content[0].Type); Assert.AreEqual(SegmentType.Block, content[1].Type); Assert.AreEqual(SegmentType.Content, content[2].Type); Assert.AreEqual(SegmentType.Expression, content[3].Type); Assert.AreEqual(SegmentType.Content, content[4].Type); Assert.AreEqual(SegmentType.Helper, content[5].Type); Assert.AreEqual(new Location(tf, 4, 1), content[1].TagStartLocation); Assert.AreEqual(new Location(tf, 7, 12), content[3].TagStartLocation); Assert.AreEqual(new Location(tf, 9, 1), content[5].TagStartLocation); Assert.AreEqual(new Location(tf, 2, 1), content[0].StartLocation); Assert.AreEqual(new Location(tf, 4, 3), content[1].StartLocation); Assert.AreEqual(new Location(tf, 7, 1), content[2].StartLocation); Assert.AreEqual(new Location(tf, 7, 15), content[3].StartLocation); Assert.AreEqual(new Location(tf, 7, 22), content[4].StartLocation); Assert.AreEqual(new Location(tf, 9, 4), content[5].StartLocation); Assert.AreEqual(new Location(tf, 6, 3), content[1].EndLocation); Assert.AreEqual(new Location(tf, 7, 22), content[3].EndLocation); Assert.AreEqual(new Location(tf, 11, 3), content[5].EndLocation); }
string Preprocess (string input) { DummyHost host = new DummyHost (); string className = "PreprocessedTemplate"; string classNamespace = "Templating"; string language = null; string[] references = null; TemplatingEngine engine = new TemplatingEngine (); string output = engine.PreprocessTemplate (input, host, className, classNamespace, out language, out references); output = output.Replace ("\r\n", "\n"); return TemplatingEngineHelper.StripHeader (output, "\n"); }
//NOTE: we set the newline property on the code generator so that the whole files has matching newlines, // in order to match the newlines in the verbatim code blocks void Generate(string input, string expectedOutput, string newline) { var host = new DummyHost(); string nameSpaceName = "Microsoft.VisualStudio.TextTemplating4f504ca0"; string code = GenerateCode(host, input, nameSpaceName, newline); Assert.AreEqual(0, host.Errors.Count); var generated = TemplatingEngineHelper.CleanCodeDom(code, newline); expectedOutput = TemplatingEngineHelper.CleanCodeDom(expectedOutput, newline); Assert.AreEqual(expectedOutput, generated); }
public void ParseTest() { string tf = "test.input"; ParsedTemplate pt = new ParsedTemplate ("test.input"); Tokeniser tk = new Tokeniser (tf, ParseSample1); DummyHost host = new DummyHost (); pt.Parse (host, tk); Assert.AreEqual (0, pt.Errors.Count); var content = new List<TemplateSegment> (pt.Content); var dirs = new List<Directive> (pt.Directives); Assert.AreEqual (1, dirs.Count); Assert.AreEqual (6, content.Count); Assert.AreEqual ("template", dirs[0].Name); Assert.AreEqual (1, dirs[0].Attributes.Count); Assert.AreEqual ("C#v3.5", dirs[0].Attributes["language"]); Assert.AreEqual (new Location (tf, 1, 1), dirs[0].TagStartLocation); Assert.AreEqual (new Location (tf, 1, 34), dirs[0].EndLocation); Assert.AreEqual ("Line One\r\nLine Two\r\n", content[0].Text); Assert.AreEqual ("\r\nfoo\r\n", content[1].Text); Assert.AreEqual ("Line Three ", content[2].Text); Assert.AreEqual (" bar ", content[3].Text); Assert.AreEqual ("\r\nLine Four\r\n", content[4].Text); Assert.AreEqual (" \r\nbaz \\#>\r\n", content[5].Text); Assert.AreEqual (SegmentType.Content, content[0].Type); Assert.AreEqual (SegmentType.Block, content[1].Type); Assert.AreEqual (SegmentType.Content, content[2].Type); Assert.AreEqual (SegmentType.Expression, content[3].Type); Assert.AreEqual (SegmentType.Content, content[4].Type); Assert.AreEqual (SegmentType.Helper, content[5].Type); Assert.AreEqual (new Location (tf, 4, 1), content[1].TagStartLocation); Assert.AreEqual (new Location (tf, 7, 12), content[3].TagStartLocation); Assert.AreEqual (new Location (tf, 9, 1), content[5].TagStartLocation); Assert.AreEqual (new Location (tf, 2, 1), content[0].StartLocation); Assert.AreEqual (new Location (tf, 4, 3), content[1].StartLocation); Assert.AreEqual (new Location (tf, 7, 1), content[2].StartLocation); Assert.AreEqual (new Location (tf, 7, 15), content[3].StartLocation); Assert.AreEqual (new Location (tf, 7, 22), content[4].StartLocation); Assert.AreEqual (new Location (tf, 9, 4), content[5].StartLocation); Assert.AreEqual (new Location (tf, 6, 3), content[1].EndLocation); Assert.AreEqual (new Location (tf, 7, 22), content[3].EndLocation); Assert.AreEqual (new Location (tf, 11, 3), content[5].EndLocation); }
string Preprocess (string input, DummyHost host) { string className = "PreprocessedTemplate"; string classNamespace = "Templating"; string language = null; string[] references = null; TemplatingEngine engine = new TemplatingEngine (); string output = engine.PreprocessTemplate (input, host, className, classNamespace, out language, out references); ReportErrors (host.Errors); if (output != null) { return TemplatingEngineHelper.CleanCodeDom (output, "\n"); } return null; }
static string Preprocess(string input, DummyHost host) { string className = "PreprocessedTemplate"; string classNamespace = "Templating"; var engine = new TemplatingEngine(); string output = engine.PreprocessTemplate(input, host, className, classNamespace, out _, out _); ReportErrors(host.Errors); if (output != null) { return(TemplatingEngineHelper.CleanCodeDom(output, "\n")); } return(null); }
string Preprocess(string input) { DummyHost host = new DummyHost(); string className = "PreprocessedTemplate"; string classNamespace = "Templating"; string language = null; string[] references = null; TemplatingEngine engine = new TemplatingEngine(); string output = engine.PreprocessTemplate(input, host, className, classNamespace, out language, out references); output = output.Replace("\r\n", "\n"); return(TemplatingEngineHelper.StripHeader(output, "\n")); }
public void GenerateStaticPropertyForParameter() { var engine = new TemplatingEngine(); var host = new DummyHost(); var output = engine.PreprocessTemplate(T4ParameterSample, host, "ParameterTestClass", "Testing", out string language, out string[] references); foreach (TemplateError error in host.Errors) { Console.Error.WriteLine(error.Message); } Assert.IsTrue(output.Contains("public static string TestParameter")); Console.Out.WriteLine(output); }
string Preprocess(string input, DummyHost host) { string className = "PreprocessedTemplate"; string classNamespace = "Templating"; string language = null; string[] references = null; TemplatingEngine engine = new TemplatingEngine(); string output = engine.PreprocessTemplate(input, host, className, classNamespace, out language, out references); ReportErrors(host.Errors.ToCompilerErrorCollection()); if (output != null) { return(TemplatingEngineHelper.CleanCodeDom(output, "\n")); } return(null); }
static DummyHost CreateDummyHostForControlBlockAfterIncludedTemplateWithClassFeatureBlockTest() { var host = new DummyHost(); string includeTemplateRequestedFileName = @"Some\Requested\Path\IncludedFile.tt"; if (System.IO.Path.DirectorySeparatorChar == '/') { includeTemplateRequestedFileName = includeTemplateRequestedFileName.Replace('\\', '/'); } string includeTemplateResolvedFileName = @"Some\Resolved\Path\IncludedFile.tt"; host.Locations.Add(includeTemplateRequestedFileName, includeTemplateResolvedFileName); host.Contents.Add(includeTemplateResolvedFileName, IncludedTemplate_ControlBlockAfterIncludedTemplate.NormalizeNewlines()); return(host); }
static string Preprocess(string input) { var host = new DummyHost(); return(Preprocess(input, host)); }
string Preprocess (string input) { DummyHost host = new DummyHost (); return Preprocess (input, host); }
string Preprocess(string input) { DummyHost host = new DummyHost(); return(Preprocess(input, host)); }
DummyHost CreateDummyHostForControlBlockAfterIncludedTemplateWithClassFeatureBlockTest() { DummyHost host = new DummyHost (); string includeTemplateFileName = @"d:\test\IncludedFile.tt"; host.Locations.Add (includeTemplateFileName, includeTemplateFileName); host.Contents.Add (includeTemplateFileName, IncludedTemplate_ControlBlockAfterIncludedTemplate); return host; }