forked from haoduotnt/aspnetwebstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebPageRazorEngineHostTest.cs
101 lines (88 loc) · 3.77 KB
/
WebPageRazorEngineHostTest.cs
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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
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 Microsoft.TestCommon;
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()
{
string expectedPropertyCode =
Environment.NewLine
+ "protected Foo.Bar ApplicationInstance {" + Environment.NewLine
+ " get {" + Environment.NewLine
+ " return ((Foo.Bar)(Context.ApplicationInstance));" + Environment.NewLine
+ " }" + Environment.NewLine
+ "}" + Environment.NewLine;
// 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());
}
}
}