Files
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
packages
src
test
Microsoft.TestCommon
Microsoft.Web.Helpers.Test
Microsoft.Web.Http.Data.Test
Microsoft.Web.Mvc.Test
Microsoft.Web.WebPages.OAuth.Test
SPA.Test
System.Json.Test.Integration
System.Json.Test.Unit
System.Net.Http.Formatting.Test.Integration
System.Net.Http.Formatting.Test.Unit
System.Web.Helpers.Test
System.Web.Http.Integration.Test
System.Web.Http.SelfHost.Test
System.Web.Http.Test
System.Web.Http.WebHost.Test
System.Web.Mvc.Test
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
Properties
PreApplicationStartCodeTest.cs
RazorBuildProviderTest.cs
System.Web.WebPages.Razor.Test.csproj
WebCodeRazorEngineHostTest.cs
WebPageRazorEngineHostTest.cs
WebRazorHostFactoryTest.cs
app.config
packages.config
System.Web.WebPages.Test
WebMatrix.Data.Test
WebMatrix.WebData.Test
Settings.StyleCop
tools
.gitattributes
.gitignore
License.txt
README.md
Runtime.msbuild
Runtime.sln
Runtime.xunit
Settings.StyleCop
build.cmd
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
ikdasm
ikvm
linker
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
ikvm-native
libgc
llvm
m4
man
mcs
mk
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
linux-packaging-mono/external/aspnetwebstack/test/System.Web.WebPages.Razor.Test/WebPageRazorEngineHostTest.cs

103 lines
3.6 KiB
C#
Raw Normal View History

// Copyright (c) Microsoft Corporation. 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 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());
}
}
}