예제 #1
0
        public void RendersSimpleLayoutWithReferencedTitle()
        {
            const string TemplateText =
                @"@model Simple.Web.Razor.Tests.TestModel
@{
    Layout = ""~/Views/Layouts/SimpleLayoutWithTitle.cshtml"";
    ViewBag.Title = @Handler.Title;
}<p>@Model.Text</p>";

            const string Expected = @"<!DOCTYPE html><html><head><title>Foo</title></head><body><p>Test Text</p></body></html>";

            var type = RazorTypeBuilderHelpers.CreateTypeFromText(TemplateText);

            var output = new MockHandler {
                Model = new TestModel {
                    Text = "Test Text"
                }, Handler = new HandlerStub {
                    Title = "Foo"
                }
            };
            var writer = new StringWriter();

            RazorHtmlMediaTypeHandler.RenderView(output, writer, type);

            Assert.Equal(Expected, HtmlComparison.Cleanse(writer.ToString()));
        }
예제 #2
0
        public void RenderWithSectioDuplicated()
        {
            const string TemplateText =
                @"@model Simple.Web.Razor.Tests.TestModel
@{
    Layout = ""~/Views/Layouts/SimpleLayoutWithSectionDuplicated.cshtml"";
}
<span>@Model.Text</span>
@section Two {<p>I am in section two.</p>}";

            const string Expected = @"<!DOCTYPE html><html><head><title>Sections Layout Page</title></head><body><h1>Section Two - Once</h1><p>I am in section two.</p><p><span>Test Text</span></p><h1>Section Two - Twice</h1><p>I am in section two.</p></body></html>";

            var type = RazorTypeBuilderHelpers.CreateTypeFromText(TemplateText);

            var output = new MockHandler {
                Model = new TestModel {
                    Text = "Test Text"
                }, Handler = null
            };
            var writer = new StringWriter();

            RazorHtmlMediaTypeHandler.RenderView(output, writer, type);

            Assert.Equal(Expected, HtmlComparison.Cleanse(writer.ToString()));
        }
예제 #3
0
        public void Renders()
        {
            const string templateText =
                @"@model Simple.Web.Razor.Tests.TestModel
<!DOCTYPE html><html><head><title>@Handler.Title</title></head><body>@Model.Text</body></html>";

            const string expected = @"<!DOCTYPE html><html><head><title>Foo</title></head><body>Test Text</body></html>";

            Type type;

            using (var reader = new StringReader(templateText))
            {
                type = new RazorTypeBuilder().CreateType(reader);
            }

            var output = new MockHandler {
                Model = new TestModel {
                    Text = "Test Text"
                }, Handler = new HandlerStub {
                    Title = "Foo"
                }
            };

            var writer = new StringWriter();

            RazorHtmlMediaTypeHandler.RenderView(output, writer, type);
            Assert.Equal(expected, writer.ToString().Trim());
        }
예제 #4
0
        public void RenderWithOptionalSection()
        {
            const string TemplateText =
                @"@model Simple.Web.Razor.Tests.TestModel
@{
    Layout = ""~/Views/Layouts/SimpleLayoutWithSections.cshtml"";
}
<span>@Model.Text</span>
@section Two {<p>I am in section two.</p>}";

            const string Expected = @"<!DOCTYPE html><html><head><title>Sections Layout Page</title></head><body><h1>Section One</h1><p><span>Test Text</span></p><h1>Section Two</h1><p>I am in section two.</p></body></html>";

            Type type;

            using (var reader = new StringReader(TemplateText))
            {
                type = new RazorTypeBuilder().CreateType(reader);
            }

            var output = new MockHandler {
                Model = new TestModel {
                    Text = "Test Text"
                }, Handler = null
            };
            var writer = new StringWriter();

            RazorHtmlMediaTypeHandler.RenderView(output, writer, type);

            Assert.Equal(Expected, HtmlComparison.Cleanse(writer.ToString()));
        }
예제 #5
0
        public void RenderWithoutNonOptionalErrors()
        {
            const string TemplateText =
                @"@model Simple.Web.Razor.Tests.TestModel
@{
    Layout = ""~/Views/Layouts/SimpleLayoutWithSections.cshtml"";
}
<span>@Model.Text</span>
@section One {<p>I am in section one.</p>}";

            Type type;

            using (var reader = new StringReader(TemplateText))
            {
                type = new RazorTypeBuilder().CreateType(reader);
            }

            var output = new MockHandler {
                Model = new TestModel {
                    Text = "Test Text"
                }, Handler = null
            };
            var writer = new StringWriter();

            Assert.Throws <ArgumentException>(() => RazorHtmlMediaTypeHandler.RenderView(output, writer, type));
        }
예제 #6
0
        public void RendersSimpleLayoutWithTitle()
        {
            const string TemplateText =
                @"@model Simple.Web.Razor.Tests.TestModel
@{
    Layout = ""~/Views/Layouts/SimpleLayoutWithTitle.cshtml"";
    ViewBag.Title = ""Custom Layout Title"";
}<p>@Model.Text</p>";

            const string Expected = @"<!DOCTYPE html><html><head><title>Custom Layout Title</title></head><body><p>Test Text</p></body></html>";

            Type type;

            using (var reader = new StringReader(TemplateText))
            {
                type = new RazorTypeBuilder().CreateType(reader);
            }

            var output = new MockHandler {
                Model = new TestModel {
                    Text = "Test Text"
                }, Handler = null
            };
            var writer = new StringWriter();

            RazorHtmlMediaTypeHandler.RenderView(output, writer, type);

            Assert.Equal(Expected, HtmlComparison.Cleanse(writer.ToString()));
        }