示例#1
0
        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)
                              );
        }
示例#2
0
        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);
        }
示例#3
0
        public void ParseTest()
        {
            string tf = "test.input";

            var pt = ParsedTemplate.FromTextInternal(
                ParseSample1.NormalizeNewlines(),
                new DummyHost {
                TemplateFile = tf
            }
                );

            Assert.Empty(pt.Errors);
            var content = new List <TemplateSegment> (pt.Content);
            var dirs    = new List <Directive> (pt.Directives);

            Assert.Single(dirs);
            Assert.Equal(6, content.Count);

            Assert.Equal("template", dirs[0].Name);
            Assert.Single(dirs[0].Attributes);
            Assert.Equal("C#v3.5", dirs[0].Attributes["language"]);
            Assert.Equal(new Location(tf, 1, 1), dirs[0].TagStartLocation);
            Assert.Equal(new Location(tf, 1, 34), dirs[0].EndLocation);

            Assert.Equal("Line One\nLine Two\n", content[0].Text);
            Assert.Equal("\nvar foo = 5;\n", content[1].Text);
            Assert.Equal("Line Three ", content[2].Text);
            Assert.Equal(" bar ", content[3].Text);
            Assert.Equal("\nLine Four\n", content[4].Text);
            Assert.Equal(" \nvar s = \"baz \\\\#>\";\n", content[5].Text);

            Assert.Equal(SegmentType.Content, content[0].Type);
            Assert.Equal(SegmentType.Block, content[1].Type);
            Assert.Equal(SegmentType.Content, content[2].Type);
            Assert.Equal(SegmentType.Expression, content[3].Type);
            Assert.Equal(SegmentType.Content, content[4].Type);
            Assert.Equal(SegmentType.Helper, content[5].Type);

            Assert.Equal(new Location(tf, 4, 1), content[1].TagStartLocation);
            Assert.Equal(new Location(tf, 7, 12), content[3].TagStartLocation);
            Assert.Equal(new Location(tf, 9, 1), content[5].TagStartLocation);

            Assert.Equal(new Location(tf, 2, 1), content[0].StartLocation);
            Assert.Equal(new Location(tf, 4, 3), content[1].StartLocation);
            Assert.Equal(new Location(tf, 7, 1), content[2].StartLocation);
            Assert.Equal(new Location(tf, 7, 15), content[3].StartLocation);
            Assert.Equal(new Location(tf, 7, 22), content[4].StartLocation);
            Assert.Equal(new Location(tf, 9, 4), content[5].StartLocation);

            Assert.Equal(new Location(tf, 6, 3), content[1].EndLocation);
            Assert.Equal(new Location(tf, 7, 22), content[3].EndLocation);
            Assert.Equal(new Location(tf, 11, 3), content[5].EndLocation);
        }
示例#4
0
        static string GenerateCode(ITextTemplatingEngineHost host, string content, string name, string generatorNewline)
        {
            var pt = ParsedTemplate.FromTextInternal(content, host);

            if (pt.Errors.HasErrors)
            {
                host.LogErrors(pt.Errors);
                return(null);
            }

            TemplateSettings settings = TemplatingEngine.GetSettings(host, pt);

            if (name != null)
            {
                settings.Namespace = name;
            }
            if (pt.Errors.HasErrors)
            {
                host.LogErrors(pt.Errors);
                return(null);
            }

            var ccu = TemplatingEngine.GenerateCompileUnit(host, content, pt, settings);

            if (pt.Errors.HasErrors)
            {
                host.LogErrors(pt.Errors);
                return(null);
            }

            var opts = new CodeGeneratorOptions();

            using var writer = new StringWriter()
                  {
                      NewLine = generatorNewline
                  };
            settings.Provider.GenerateCodeFromCompileUnit(ccu, writer, opts);
            return(writer.ToString());
        }