Remove Get/SetRelativePath

This commit is contained in:
Ryan Nowak 2018-01-05 18:05:17 -08:00
parent d602f9d770
commit b93bab9c04
51 changed files with 181 additions and 259 deletions

View File

@ -26,13 +26,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
Options.DefaultImports = GetDefaultImports();
}
/// <inheritsdoc />
public override RazorCodeDocument CreateCodeDocument(RazorProjectItem projectItem)
{
var codeDocument = base.CreateCodeDocument(projectItem);
codeDocument.SetRelativePath(projectItem.FilePath);
return codeDocument;
return base.CreateCodeDocument(projectItem);
}
// Internal for testing.

View File

@ -20,12 +20,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
ClassDeclarationIntermediateNode @class,
MethodDeclarationIntermediateNode method)
{
var filePath = codeDocument.GetRelativePath() ?? codeDocument.Source.FilePath;
base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);
@namespace.Content = "AspNetCore";
var filePath = codeDocument.Source.RelativePath ?? codeDocument.Source.FilePath;
@class.ClassName = CSharpIdentifier.GetClassNameFromPath(filePath);
@class.BaseType = "global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>";
@class.Modifiers.Clear();

View File

@ -1,34 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Razor.Language;
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
{
internal static class RazorCodeDocumentExtensions
{
private const string RelativePathKey = "relative-path";
public static string GetRelativePath(this RazorCodeDocument document)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
return document.Items[RelativePathKey] as string;
}
public static void SetRelativePath(this RazorCodeDocument document, string relativePath)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
document.Items[RelativePathKey] = relativePath;
}
}
}

View File

@ -29,8 +29,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
}
var generatedTypeName = $"{@namespace.Content}.{@class.ClassName}";
var path = codeDocument.GetRelativePath();
var escapedPath = EscapeAsVerbatimLiteral(path);
// The MVC attributes require a relative path to be specified so that we can make a view engine path.
// We can't use a rooted path because we don't know what the project root is.
//
// If we can't sanitize the path, we'll just set it to null and let is blow up at runtime - we don't
// want to create noise if this code has to run in some unanticipated scenario.
var escapedPath = MakeVerbatimStringLiteral(ConvertToViewEnginePath(codeDocument.Source.RelativePath));
string attribute;
if (documentNode.DocumentKind == MvcViewDocumentClassifierPass.MvcViewDocumentKind)
@ -40,7 +45,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
else if (documentNode.DocumentKind == RazorPageDocumentClassifierPass.RazorPageDocumentKind &&
PageDirective.TryGetPageDirective(documentNode, out var pageDirective))
{
var escapedRoutePrefix = EscapeAsVerbatimLiteral(pageDirective.RouteTemplate);
var escapedRoutePrefix = MakeVerbatimStringLiteral(pageDirective.RouteTemplate);
attribute = $"[assembly:{RazorPageAttribute}({escapedPath}, typeof({generatedTypeName}), {escapedRoutePrefix})]";
}
else
@ -61,7 +66,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
documentNode.Children.Insert(index, pageAttribute);
}
private static string EscapeAsVerbatimLiteral(string value)
private static string MakeVerbatimStringLiteral(string value)
{
if (value == null)
{
@ -71,5 +76,22 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
value = value.Replace("\"", "\"\"");
return $"@\"{value}\"";
}
private static string ConvertToViewEnginePath(string relativePath)
{
if (string.IsNullOrEmpty(relativePath))
{
return null;
}
// Checking for both / and \ because a \ will become a /.
if (!relativePath.StartsWith("/") && !relativePath.StartsWith("\\"))
{
relativePath = "/" + relativePath;
}
relativePath = relativePath.Replace('\\', '/');
return relativePath;
}
}
}

View File

@ -26,13 +26,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
Options.DefaultImports = GetDefaultImports();
}
/// <inheritsdoc />
public override RazorCodeDocument CreateCodeDocument(RazorProjectItem projectItem)
{
var codeDocument = base.CreateCodeDocument(projectItem);
codeDocument.SetRelativePath(projectItem.FilePath);
return codeDocument;
return base.CreateCodeDocument(projectItem);
}
// Internal for testing.

View File

@ -20,12 +20,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
ClassDeclarationIntermediateNode @class,
MethodDeclarationIntermediateNode method)
{
var filePath = codeDocument.GetRelativePath() ?? codeDocument.Source.FilePath;
base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);
@namespace.Content = "AspNetCore";
var filePath = codeDocument.Source.RelativePath ?? codeDocument.Source.FilePath;
@class.ClassName = CSharpIdentifier.GetClassNameFromPath(filePath);
@class.BaseType = "global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>";
@class.Modifiers.Clear();

View File

@ -1,34 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Razor.Language;
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
internal static class RazorCodeDocumentExtensions
{
private const string RelativePathKey = "relative-path";
public static string GetRelativePath(this RazorCodeDocument document)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
return document.Items[RelativePathKey] as string;
}
public static void SetRelativePath(this RazorCodeDocument document, string relativePath)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
document.Items[RelativePathKey] = relativePath;
}
}
}

View File

@ -24,13 +24,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
ClassDeclarationIntermediateNode @class,
MethodDeclarationIntermediateNode method)
{
var filePath = codeDocument.GetRelativePath() ?? codeDocument.Source.FilePath;
base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);
@namespace.Content = "AspNetCore";
@class.BaseType = "global::Microsoft.AspNetCore.Mvc.RazorPages.Page";
var filePath = codeDocument.Source.RelativePath ?? codeDocument.Source.FilePath;
@class.ClassName = CSharpIdentifier.GetClassNameFromPath(filePath);
@class.Modifiers.Clear();

View File

@ -201,7 +201,37 @@ namespace Microsoft.AspNetCore.Razor.Language
/// <returns>The <see cref="RazorSourceDocument"/>.</returns>
/// <remarks>Uses <see cref="System.Text.Encoding.UTF8" /></remarks>
public static RazorSourceDocument Create(string content, string fileName)
=> Create(content, fileName, Encoding.UTF8);
{
if (content == null)
{
throw new ArgumentNullException(nameof(content));
}
return Create(content, fileName, Encoding.UTF8);
}
/// <summary>
/// Creates a <see cref="RazorSourceDocument"/> from the specified <paramref name="content"/>.
/// </summary>
/// <param name="content">The source document content.</param>
/// <param name="properties">Properties to configure the <see cref="RazorSourceDocument"/>.</param>
/// <returns>The <see cref="RazorSourceDocument"/>.</returns>
/// <remarks>Uses <see cref="System.Text.Encoding.UTF8" /></remarks>
public static RazorSourceDocument Create(string content, RazorSourceDocumentProperties properties)
{
if (content == null)
{
throw new ArgumentNullException(nameof(content));
}
if (properties == null)
{
throw new ArgumentNullException(nameof(properties));
}
return Create(content, Encoding.UTF8, properties);
}
/// <summary>
/// Creates a <see cref="RazorSourceDocument"/> from the specified <paramref name="content"/>.

View File

@ -175,8 +175,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
Engine = RazorEngine.Create(),
};
var document = TestRazorCodeDocument.CreateEmpty();
document.SetRelativePath("/Views/Index.cshtml");
var source = TestRazorSourceDocument.Create("test", new RazorSourceDocumentProperties(filePath: null, relativePath: "/Views/Index.cshtml"));
var document = RazorCodeDocument.Create(source);
// Act
pass.Execute(document, irDocument);
@ -197,7 +198,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void Execute_EscapesViewPathWhenAddingAttributeToViews()
{
// Arrange
var expectedAttribute = "[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@\"\\test\\\"\"Index.cshtml\", typeof(SomeNamespace.SomeName))]";
var expectedAttribute = "[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@\"/test/\"\"Index.cshtml\", typeof(SomeNamespace.SomeName))]";
var irDocument = new DocumentIntermediateNode
{
DocumentKind = MvcViewDocumentClassifierPass.MvcViewDocumentKind,
@ -226,8 +227,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
Engine = RazorEngine.Create(),
};
var document = TestRazorCodeDocument.CreateEmpty();
document.SetRelativePath("\\test\\\"Index.cshtml");
var source = TestRazorSourceDocument.Create("test", new RazorSourceDocumentProperties(filePath: null, relativePath: "\\test\\\"Index.cshtml"));
var document = RazorCodeDocument.Create(source);
// Act
pass.Execute(document, irDocument);
@ -283,8 +285,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
Engine = RazorEngine.Create(),
};
var document = TestRazorCodeDocument.CreateEmpty();
document.SetRelativePath("/Views/Index.cshtml");
var source = TestRazorSourceDocument.Create("test", new RazorSourceDocumentProperties(filePath: null, relativePath: "/Views/Index.cshtml"));
var document = RazorCodeDocument.Create(source);
// Act
pass.Execute(document, irDocument);
@ -306,7 +309,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void Execute_EscapesViewPathAndRouteWhenAddingAttributeToPage()
{
// Arrange
var expectedAttribute = "[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@\"\\test\\\"\"Index.cshtml\", typeof(SomeNamespace.SomeName))]";
var expectedAttribute = "[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@\"/test/\"\"Index.cshtml\", typeof(SomeNamespace.SomeName))]";
var irDocument = new DocumentIntermediateNode
{
DocumentKind = MvcViewDocumentClassifierPass.MvcViewDocumentKind,
@ -336,8 +339,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
Engine = RazorEngine.Create(),
};
var document = TestRazorCodeDocument.CreateEmpty();
document.SetRelativePath("\\test\\\"Index.cshtml");
var source = TestRazorSourceDocument.Create("test", new RazorSourceDocumentProperties(filePath: null, relativePath: "test\\\"Index.cshtml"));
var document = RazorCodeDocument.Create(source);
// Act
pass.Execute(document, irDocument);

View File

@ -2,10 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language;
using Moq;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
@ -86,28 +84,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
Assert.Contains("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.BodyTagHelper, Microsoft.AspNetCore.Mvc.Razor", importContent);
}
[Fact]
public void CreateCodeDocument_SetsRelativePathOnOutput()
{
// Arrange
var path = "/Views/Home/Index.cshtml";
var item = new TestRazorProjectItem(path)
{
Content = "Hello world",
};
var project = new TestRazorProject(new List<RazorProjectItem>() { item, });
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
RazorEngine.Create(),
project);
// Act
var codeDocument = mvcRazorTemplateEngine.CreateCodeDocument(path);
// Assert
Assert.Equal(path, codeDocument.GetRelativePath());
}
private string GetContent(RazorSourceDocument imports)
{
var contentChars = new char[imports.Length];

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Xunit;
@ -14,7 +13,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void MvcViewDocumentClassifierPass_SetsDocumentKind()
{
// Arrange
var codeDocument = CreateDocument("some-content");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml"));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
@ -33,7 +33,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void MvcViewDocumentClassifierPass_NoOpsIfDocumentKindIsAlreadySet()
{
// Arrange
var codeDocument = CreateDocument("some-content");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml"));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
irDocument.DocumentKind = "some-value";
@ -53,7 +54,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void MvcViewDocumentClassifierPass_SetsNamespace()
{
// Arrange
var codeDocument = CreateDocument("some-content");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml"));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
@ -74,14 +76,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void MvcViewDocumentClassifierPass_SetsClass()
{
// Arrange
var codeDocument = CreateDocument("some-content");
var properties = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: "Test.cshtml");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
{
Engine = engine
};
codeDocument.SetRelativePath("Test.cshtml");
// Act
pass.Execute(codeDocument, irDocument);
@ -100,8 +103,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void MvcViewDocumentClassifierPass_UsesRelativePathToGenerateTypeName(string relativePath, string expected)
{
// Arrange
var codeDocument = CreateDocument("some-content");
codeDocument.SetRelativePath(relativePath);
var properties = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: relativePath);
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
@ -122,9 +126,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void MvcViewDocumentClassifierPass_UsesAbsolutePath_IfRelativePathIsNotSet()
{
// Arrange
var expected = "x___application_Views_Home_Index";
var path = @"x::\application\Views\Home\Index.cshtml";
var codeDocument = CreateDocument("some-content", path);
var properties = new RazorSourceDocumentProperties(filePath: @"x::\application\Views\Home\Index.cshtml", relativePath: null);
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
@ -138,16 +142,16 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
visitor.Visit(irDocument);
// Assert
Assert.Equal(expected, visitor.Class.ClassName);
Assert.Equal("x___application_Views_Home_Index", visitor.Class.ClassName);
}
[Fact]
public void MvcViewDocumentClassifierPass_SanitizesClassName()
{
// Arrange
var expected = "path_with_invalid_chars";
var codeDocument = CreateDocument("some-content");
codeDocument.SetRelativePath("path.with+invalid-chars");
var properties = new RazorSourceDocumentProperties(filePath: @"x:\Test.cshtml", relativePath: "path.with+invalid-chars");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", properties));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
@ -161,14 +165,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
visitor.Visit(irDocument);
// Assert
Assert.Equal(expected, visitor.Class.ClassName);
Assert.Equal("path_with_invalid_chars", visitor.Class.ClassName);
}
[Fact]
public void MvcViewDocumentClassifierPass_SetsUpExecuteAsyncMethod()
{
// Arrange
var codeDocument = CreateDocument("some-content");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml"));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
@ -187,14 +192,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
Assert.Equal(new[] { "public", "async", "override" }, visitor.Method.Modifiers);
}
private static RazorCodeDocument CreateDocument(string content, string filePath = null)
{
filePath = filePath ?? Path.Combine(Directory.GetCurrentDirectory(), "Test.cshtml");
var source = RazorSourceDocument.Create(content, filePath);
return RazorCodeDocument.Create(source);
}
private static RazorEngine CreateEngine() => RazorEngine.Create();
private static DocumentIntermediateNode CreateIRDocument(RazorEngine engine, RazorCodeDocument codeDocument)

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Xunit;
@ -40,7 +39,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void RazorPageDocumentClassifierPass_SetsDocumentKind()
{
// Arrange
var codeDocument = CreateDocument("@page");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", "Test.cshtml"));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new RazorPageDocumentClassifierPass
@ -59,7 +59,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void RazorPageDocumentClassifierPass_NoOpsIfDocumentKindIsAlreadySet()
{
// Arrange
var codeDocument = CreateDocument("@page");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", "Test.cshtml"));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
irDocument.DocumentKind = "some-value";
@ -79,7 +80,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void RazorPageDocumentClassifierPass_NoOpsIfPageDirectiveIsMalformed()
{
// Arrange
var codeDocument = CreateDocument("@page+1");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page+1", "Test.cshtml"));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
irDocument.DocumentKind = "some-value";
@ -99,7 +101,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void RazorPageDocumentClassifierPass_SetsNamespace()
{
// Arrange
var codeDocument = CreateDocument("@page");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", "Test.cshtml"));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new RazorPageDocumentClassifierPass
@ -120,14 +123,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void RazorPageDocumentClassifierPass_SetsClass()
{
// Arrange
var codeDocument = CreateDocument("@page");
var properties = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: "Test.cshtml");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", properties));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new RazorPageDocumentClassifierPass
{
Engine = engine
};
codeDocument.SetRelativePath("Test.cshtml");
// Act
pass.Execute(codeDocument, irDocument);
@ -146,8 +150,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void RazorPageDocumentClassifierPass_UsesRelativePathToGenerateTypeName(string relativePath, string expected)
{
// Arrange
var codeDocument = CreateDocument("@page");
codeDocument.SetRelativePath(relativePath);
var properties = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: relativePath);
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", properties));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new RazorPageDocumentClassifierPass
@ -168,9 +173,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public void RazorPageDocumentClassifierPass_UsesAbsolutePath_IfRelativePathIsNotSet()
{
// Arrange
var expected = "x___application_Views_Home_Index";
var path = @"x::\application\Views\Home\Index.cshtml";
var codeDocument = CreateDocument("@page", path);
var properties = new RazorSourceDocumentProperties(filePath: @"x::\application\Views\Home\Index.cshtml", relativePath: null);
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", properties));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new RazorPageDocumentClassifierPass
@ -184,16 +189,16 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
visitor.Visit(irDocument);
// Assert
Assert.Equal(expected, visitor.Class.ClassName);
Assert.Equal("x___application_Views_Home_Index", visitor.Class.ClassName);
}
[Fact]
public void RazorPageDocumentClassifierPass_SanitizesClassName()
{
// Arrange
var expected = "path_with_invalid_chars";
var codeDocument = CreateDocument("@page");
codeDocument.SetRelativePath("path.with+invalid-chars");
var properties = new RazorSourceDocumentProperties(filePath: @"x:\Test.cshtml", relativePath: "path.with+invalid-chars");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", properties));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new RazorPageDocumentClassifierPass
@ -207,14 +212,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
visitor.Visit(irDocument);
// Assert
Assert.Equal(expected, visitor.Class.ClassName);
Assert.Equal("path_with_invalid_chars", visitor.Class.ClassName);
}
[Fact]
public void RazorPageDocumentClassifierPass_SetsUpExecuteAsyncMethod()
{
// Arrange
var codeDocument = CreateDocument("@page");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", "Test.cshtml"));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new RazorPageDocumentClassifierPass
@ -233,14 +239,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
Assert.Equal(new[] { "public", "async", "override" }, visitor.Method.Modifiers);
}
private static RazorCodeDocument CreateDocument(string content, string filePath = null)
{
filePath = filePath ?? Path.Combine(Directory.GetCurrentDirectory(), "Test.cshtml");
var source = RazorSourceDocument.Create(content, filePath);
return RazorCodeDocument.Create(source);
}
private static RazorEngine CreateEngine()
{
return RazorEngine.Create(b =>

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Basic), @"mvc.1.0.view", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Basic")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Basic))]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Basic.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Basic))]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Basic))]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Basic.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Basic))]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives), @"mvc.1.0.razor-page", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives")]
[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives), null)]
[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives), null)]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives), null)]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives), null)]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsViewModel), @"mvc.1.0.view", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InheritsViewModel")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsViewModel))]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InheritsViewModel.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsViewModel))]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsViewModel))]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InheritsViewModel.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsViewModel))]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsWithViewImports), @"mvc.1.0.razor-page", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InheritsWithViewImports")]
[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsWithViewImports), null)]
[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InheritsWithViewImports.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsWithViewImports), null)]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsWithViewImports), null)]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InheritsWithViewImports.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsWithViewImports), null)]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithModel), @"mvc.1.0.view", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithModel")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithModel))]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithModel.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithModel))]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithModel))]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithModel.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithModel))]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithSemicolon), @"mvc.1.0.view", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithSemicolon))]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithSemicolon))]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithSemicolon))]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithSemicolon))]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Inject), @"mvc.1.0.view", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Inject")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Inject))]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Inject.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Inject))]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Inject))]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Inject.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Inject))]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InvalidNamespaceAtEOF), @"mvc.1.0.view", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InvalidNamespaceAtEOF")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InvalidNamespaceAtEOF))]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InvalidNamespaceAtEOF.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InvalidNamespaceAtEOF))]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InvalidNamespaceAtEOF))]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InvalidNamespaceAtEOF.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InvalidNamespaceAtEOF))]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective), @"mvc.1.0.razor-page", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective")]
[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective), null)]
[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective), null)]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective), null)]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective), null)]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ModelExpressionTagHelper), @"mvc.1.0.view", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ModelExpressionTagHelper")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ModelExpressionTagHelper))]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ModelExpressionTagHelper.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ModelExpressionTagHelper))]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ModelExpressionTagHelper))]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ModelExpressionTagHelper.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ModelExpressionTagHelper))]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Model), @"mvc.1.0.view", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Model")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Model))]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Model.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Model))]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Model))]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Model.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Model))]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(Test.Namespace.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_PageWithNamespace), @"mvc.1.0.razor-page", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/PageWithNamespace")]
[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(null, typeof(Test.Namespace.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_PageWithNamespace), null)]
[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/PageWithNamespace.cshtml", typeof(Test.Namespace.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_PageWithNamespace), null)]
namespace Test.Namespace
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(null, typeof(Test.Namespace.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_PageWithNamespace), null)]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/PageWithNamespace.cshtml", typeof(Test.Namespace.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_PageWithNamespace), null)]
NamespaceDeclaration - - Test.Namespace
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPagesWithoutModel), @"mvc.1.0.razor-page", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPagesWithoutModel")]
[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPagesWithoutModel), null)]
[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPagesWithoutModel.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPagesWithoutModel), null)]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPagesWithoutModel), null)]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPagesWithoutModel.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPagesWithoutModel), null)]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPages), @"mvc.1.0.razor-page", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPages")]
[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPages), null)]
[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPages.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPages), null)]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPages), null)]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPages.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPages), null)]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections), @"mvc.1.0.view", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections))]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections))]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections))]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections))]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ViewComponentTagHelper), @"mvc.1.0.view", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ViewComponentTagHelper")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ViewComponentTagHelper))]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ViewComponentTagHelper.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ViewComponentTagHelper))]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ViewComponentTagHelper))]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ViewComponentTagHelper.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ViewComponentTagHelper))]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(Test.Namespace.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ViewWithNamespace), @"mvc.1.0.view", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ViewWithNamespace")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(Test.Namespace.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ViewWithNamespace))]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ViewWithNamespace.cshtml", typeof(Test.Namespace.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ViewWithNamespace))]
namespace Test.Namespace
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(Test.Namespace.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ViewWithNamespace))]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ViewWithNamespace.cshtml", typeof(Test.Namespace.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ViewWithNamespace))]
NamespaceDeclaration - - Test.Namespace
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,7 +2,7 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest__ViewImports), @"mvc.1.0.view", @"TestFiles/IntegrationTests/CodeGenerationIntegrationTest/_ViewImports")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest__ViewImports))]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/_ViewImports.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest__ViewImports))]
namespace AspNetCore
{
#line hidden

View File

@ -1,7 +1,7 @@
Document -
RazorCompiledItemAttribute -
CSharpCode -
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest__ViewImports))]
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/_ViewImports.cshtml", typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest__ViewImports))]
NamespaceDeclaration - - AspNetCore
UsingDirective - (1:0,1 [14] ) - System
UsingDirective - (16:1,1 [34] ) - System.Collections.Generic

View File

@ -2,10 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language;
using Moq;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
@ -84,28 +82,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
Assert.Contains("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor", importContent);
}
[Fact]
public void CreateCodeDocument_SetsRelativePathOnOutput()
{
// Arrange
var path = "/Views/Home/Index.cshtml";
var item = new TestRazorProjectItem(path)
{
Content = "Hello world",
};
var project = new TestRazorProject(new List<RazorProjectItem>() { item, });
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
RazorEngine.Create(),
project);
// Act
var codeDocument = mvcRazorTemplateEngine.CreateCodeDocument(path);
// Assert
Assert.Equal(path, codeDocument.GetRelativePath());
}
private string GetContent(RazorSourceDocument imports)
{
var contentChars = new char[imports.Length];

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Xunit;
@ -14,7 +13,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
public void MvcViewDocumentClassifierPass_SetsDocumentKind()
{
// Arrange
var codeDocument = CreateDocument("some-content");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml"));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
@ -33,7 +33,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
public void MvcViewDocumentClassifierPass_NoOpsIfDocumentKindIsAlreadySet()
{
// Arrange
var codeDocument = CreateDocument("some-content");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml"));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
irDocument.DocumentKind = "some-value";
@ -53,7 +54,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
public void MvcViewDocumentClassifierPass_SetsNamespace()
{
// Arrange
var codeDocument = CreateDocument("some-content");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml"));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
@ -74,14 +76,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
public void MvcViewDocumentClassifierPass_SetsClass()
{
// Arrange
var codeDocument = CreateDocument("some-content");
var properties = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: "Test.cshtml");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
{
Engine = engine
};
codeDocument.SetRelativePath("Test.cshtml");
// Act
pass.Execute(codeDocument, irDocument);
@ -100,8 +103,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
public void MvcViewDocumentClassifierPass_UsesRelativePathToGenerateTypeName(string relativePath, string expected)
{
// Arrange
var codeDocument = CreateDocument("some-content");
codeDocument.SetRelativePath(relativePath);
var properties = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: relativePath);
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
@ -122,9 +126,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
public void MvcViewDocumentClassifierPass_UsesAbsolutePath_IfRelativePathIsNotSet()
{
// Arrange
var expected = "x___application_Views_Home_Index";
var path = @"x::\application\Views\Home\Index.cshtml";
var codeDocument = CreateDocument("some-content", path);
var properties = new RazorSourceDocumentProperties(filePath: @"x::\application\Views\Home\Index.cshtml", relativePath: null);
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
@ -138,16 +142,16 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
visitor.Visit(irDocument);
// Assert
Assert.Equal(expected, visitor.Class.ClassName);
Assert.Equal("x___application_Views_Home_Index", visitor.Class.ClassName);
}
[Fact]
public void MvcViewDocumentClassifierPass_SanitizesClassName()
{
// Arrange
var expected = "path_with_invalid_chars";
var codeDocument = CreateDocument("some-content");
codeDocument.SetRelativePath("path.with+invalid-chars");
var properties = new RazorSourceDocumentProperties(filePath: @"x:\Test.cshtml", relativePath: "path.with+invalid-chars");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", properties));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
@ -161,14 +165,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
visitor.Visit(irDocument);
// Assert
Assert.Equal(expected, visitor.Class.ClassName);
Assert.Equal("path_with_invalid_chars", visitor.Class.ClassName);
}
[Fact]
public void MvcViewDocumentClassifierPass_SetsUpExecuteAsyncMethod()
{
// Arrange
var codeDocument = CreateDocument("some-content");
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml"));
var engine = CreateEngine();
var irDocument = CreateIRDocument(engine, codeDocument);
var pass = new MvcViewDocumentClassifierPass
@ -187,14 +192,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
Assert.Equal(new[] { "public", "async", "override" }, visitor.Method.Modifiers);
}
private static RazorCodeDocument CreateDocument(string content, string filePath = null)
{
filePath = filePath ?? Path.Combine(Directory.GetCurrentDirectory(), "Test.cshtml");
var source = RazorSourceDocument.Create(content, filePath);
return RazorCodeDocument.Create(source);
}
private static RazorEngine CreateEngine() => RazorEngine.Create();
private static DocumentIntermediateNode CreateIRDocument(RazorEngine engine, RazorCodeDocument codeDocument)