summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/test/System.Web.WebPages.Razor.Test/WebCodeRazorEngineHostTest.cs
blob: 516f78b4217d3e81568d6592f3f63b3df6db8c1f (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
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.CodeDom;
using System.Linq;
using System.Web.Razor.Generator;
using Xunit;

namespace System.Web.WebPages.Razor.Test
{
    public class WebCodeRazorEngineHostTest
    {
        [Fact]
        public void ConstructorWithMalformedVirtualPathSetsDefaultProperties()
        {
            // Act
            WebCodeRazorHost host = new WebCodeRazorHost(@"~/Foo/App_Code\Bar\Baz\Qux.cshtml");

            // Assert
            Assert.Equal("System.Web.WebPages.HelperPage", host.DefaultBaseClass);
            Assert.Equal("ASP.Bar.Baz", host.DefaultNamespace);
            Assert.Equal("Qux", host.DefaultClassName);
            Assert.False(host.DefaultDebugCompilation);
            Assert.True(host.StaticHelpers);
        }

        [Fact]
        public void ConstructorWithFileOnlyVirtualPathSetsDefaultProperties()
        {
            // Act
            WebCodeRazorHost host = new WebCodeRazorHost(@"Foo.cshtml");

            // Assert
            Assert.Equal("System.Web.WebPages.HelperPage", host.DefaultBaseClass);
            Assert.Equal("ASP", host.DefaultNamespace);
            Assert.Equal("Foo", host.DefaultClassName);
            Assert.False(host.DefaultDebugCompilation);
        }

        [Fact]
        public void ConstructorWithVirtualPathSetsDefaultProperties()
        {
            // Act
            WebCodeRazorHost host = new WebCodeRazorHost("~/Foo/App_Code/Bar/Baz/Qux.cshtml");

            // Assert
            Assert.Equal("System.Web.WebPages.HelperPage", host.DefaultBaseClass);
            Assert.Equal("ASP.Bar.Baz", host.DefaultNamespace);
            Assert.Equal("Qux", host.DefaultClassName);
            Assert.False(host.DefaultDebugCompilation);
        }

        [Fact]
        public void ConstructorWithVirtualAndPhysicalPathSetsDefaultProperties()
        {
            // Act
            WebCodeRazorHost host = new WebCodeRazorHost("~/Foo/App_Code/Bar/Baz/Qux.cshtml", @"C:\Qux.doodad");

            // Assert
            Assert.Equal("System.Web.WebPages.HelperPage", host.DefaultBaseClass);
            Assert.Equal("ASP.Bar.Baz", host.DefaultNamespace);
            Assert.Equal("Qux", host.DefaultClassName);
            Assert.False(host.DefaultDebugCompilation);
        }

        [Fact]
        public void PostProcessGeneratedCodeRemovesExecuteMethod()
        {
            // Arrange
            WebCodeRazorHost host = new WebCodeRazorHost("Foo.cshtml");
            CodeGeneratorContext context = CodeGeneratorContext.Create(
                host,
                () => new CSharpCodeWriter(),
                "TestClass",
                "TestNamespace",
                "TestFile.cshtml",
                shouldGenerateLinePragmas: true);

            // Act
            host.PostProcessGeneratedCode(context);

            // Assert
            Assert.Equal(0, context.GeneratedClass.Members.OfType<CodeMemberMethod>().Count());
        }

        [Fact]
        public void PostProcessGeneratedCodeAddsStaticApplicationInstanceProperty()
        {
            // Arrange
            WebCodeRazorHost host = new WebCodeRazorHost("Foo.cshtml");
            CodeGeneratorContext context =
                CodeGeneratorContext.Create(
                    host,
                    () => new CSharpCodeWriter(),
                    "TestClass",
                    "TestNamespace",
                    "Foo.cshtml",
                    shouldGenerateLinePragmas: true);

            // Act
            host.PostProcessGeneratedCode(context);

            // Assert
            CodeMemberProperty appInstance = context.GeneratedClass
                .Members
                .OfType<CodeMemberProperty>()
                .Where(p => p.Name.Equals("ApplicationInstance"))
                .SingleOrDefault();
            Assert.NotNull(appInstance);
            Assert.True(appInstance.Attributes.HasFlag(MemberAttributes.Static));
        }
    }
}