Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WebPageRazorEngineHostTest.cs « System.Web.WebPages.Razor.Test « test - github.com/mono/aspnetwebstack.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 24f0b39caac097b0bf207095a873ea14bc0fe02d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Razor;
using System.Web.Razor.Generator;
using Microsoft.CSharp;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;

namespace System.Web.WebPages.Razor.Test
{
    public class WebPageRazorEngineHostTest
    {
        [Fact]
        public void ConstructorRequiresNonNullOrEmptyVirtualPath()
        {
            Assert.ThrowsArgumentNullOrEmptyString(() => new WebPageRazorHost(null), "virtualPath");
            Assert.ThrowsArgumentNullOrEmptyString(() => new WebPageRazorHost(String.Empty), "virtualPath");
            Assert.ThrowsArgumentNullOrEmptyString(() => new WebPageRazorHost(null, "foo"), "virtualPath");
            Assert.ThrowsArgumentNullOrEmptyString(() => new WebPageRazorHost(String.Empty, "foo"), "virtualPath");
        }

        [Fact]
        public void ConstructorWithVirtualPathUsesItToDetermineBaseClassClassNameAndLanguage()
        {
            // Act
            WebPageRazorHost host = new WebPageRazorHost("~/Foo/Bar.cshtml");

            // Assert
            Assert.Equal("_Page_Foo_Bar_cshtml", host.DefaultClassName);
            Assert.Equal("System.Web.WebPages.WebPage", host.DefaultBaseClass);
            Assert.IsType<CSharpRazorCodeLanguage>(host.CodeLanguage);
            Assert.False(host.StaticHelpers);
        }

        [Fact]
        public void PostProcessGeneratedCodeAddsGlobalImports()
        {
            // Arrange
            WebPageRazorHost.AddGlobalImport("Foo.Bar");
            WebPageRazorHost host = new WebPageRazorHost("Foo.cshtml");
            CodeGeneratorContext context = CodeGeneratorContext.Create(
                host,
                () => new CSharpCodeWriter(),
                "TestClass",
                "TestNs",
                "TestFile.cshtml",
                shouldGenerateLinePragmas: true);

            // Act
            host.PostProcessGeneratedCode(context);

            // Assert
            Assert.True(context.Namespace.Imports.OfType<CodeNamespaceImport>().Any(import => String.Equals("Foo.Bar", import.Namespace)));
        }

        [Fact]
        public void PostProcessGeneratedCodeAddsApplicationInstanceProperty()
        {
            const string expectedPropertyCode = @"
protected Foo.Bar ApplicationInstance {
    get {
        return ((Foo.Bar)(Context.ApplicationInstance));
    }
}
";

            // Arrange
            WebPageRazorHost host = new WebPageRazorHost("Foo.cshtml")
            {
                GlobalAsaxTypeName = "Foo.Bar"
            };
            CodeGeneratorContext context = CodeGeneratorContext.Create(
                host,
                () => new CSharpCodeWriter(),
                "TestClass",
                "TestNs",
                "TestFile.cshtml",
                shouldGenerateLinePragmas: true);

            // Act
            host.PostProcessGeneratedCode(context);

            // Assert
            CodeMemberProperty property = context.GeneratedClass.Members[0] as CodeMemberProperty;
            Assert.NotNull(property);

            CSharpCodeProvider provider = new CSharpCodeProvider();
            StringBuilder builder = new StringBuilder();
            using (StringWriter writer = new StringWriter(builder))
            {
                provider.GenerateCodeFromMember(property, writer, new CodeGeneratorOptions());
            }

            Assert.Equal(expectedPropertyCode, builder.ToString());
        }
    }
}