127 lines
4.8 KiB
C#
127 lines
4.8 KiB
C#
// 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 System.Linq;
|
|
using Microsoft.AspNetCore.Mvc.Razor.Extensions.Internal;
|
|
using Microsoft.AspNetCore.Razor.Evolution;
|
|
using Microsoft.Extensions.FileProviders;
|
|
using Moq;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
|
|
{
|
|
public class MvcRazorTemplateEngineTest
|
|
{
|
|
[Fact]
|
|
public void GetDefaultImports_IncludesDefaultImports()
|
|
{
|
|
// Arrange
|
|
var expectedImports = new[]
|
|
{
|
|
"@using System",
|
|
"@using System.Linq",
|
|
"@using System.Collections.Generic",
|
|
"@using Microsoft.AspNetCore.Mvc",
|
|
"@using Microsoft.AspNetCore.Mvc.Rendering",
|
|
"@using Microsoft.AspNetCore.Mvc.ViewFeatures",
|
|
};
|
|
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
|
|
RazorEngine.Create(),
|
|
GetRazorProject(new TestFileProvider()));
|
|
|
|
// Act
|
|
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
|
|
|
|
// Assert
|
|
var importContent = GetContent(imports)
|
|
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
|
|
.Where(line => line.StartsWith("@using"));
|
|
Assert.Equal(expectedImports, importContent);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetDefaultImports_IncludesDefaulInjects()
|
|
{
|
|
// Arrange
|
|
var expectedImports = new[]
|
|
{
|
|
"@inject global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<TModel> Html",
|
|
"@inject global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json",
|
|
"@inject global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component",
|
|
"@inject global::Microsoft.AspNetCore.Mvc.IUrlHelper Url",
|
|
"@inject global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider",
|
|
};
|
|
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
|
|
RazorEngine.Create(),
|
|
GetRazorProject(new TestFileProvider()));
|
|
|
|
// Act
|
|
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
|
|
|
|
// Assert
|
|
var importContent = GetContent(imports)
|
|
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
|
|
.Where(line => line.StartsWith("@inject"));
|
|
Assert.Equal(expectedImports, importContent);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetDefaultImports_IncludesUrlTagHelper()
|
|
{
|
|
// Arrange
|
|
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
|
|
RazorEngine.Create(),
|
|
GetRazorProject(new TestFileProvider()));
|
|
|
|
// Act
|
|
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
|
|
|
|
// Assert
|
|
var importContent = GetContent(imports)
|
|
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
|
|
.Where(line => line.StartsWith("@addTagHelper"));
|
|
var addTagHelper = Assert.Single(importContent);
|
|
Assert.Equal("@addTagHelper Microsoft.AspNetCore.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor",
|
|
addTagHelper);
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateCodeDocument_SetsRelativePathOnOutput()
|
|
{
|
|
// Arrange
|
|
var path = "/Views/Home/Index.cshtml";
|
|
var fileProvider = new TestFileProvider();
|
|
fileProvider.AddFile(path, "Hello world");
|
|
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
|
|
RazorEngine.Create(),
|
|
GetRazorProject(fileProvider));
|
|
|
|
// Act
|
|
var codeDocument = mvcRazorTemplateEngine.CreateCodeDocument(path);
|
|
|
|
// Assert
|
|
Assert.Equal(path, codeDocument.GetRelativePath());
|
|
}
|
|
|
|
private string GetContent(RazorSourceDocument imports)
|
|
{
|
|
var contentChars = new char[imports.Length];
|
|
imports.CopyTo(0, contentChars, 0, imports.Length);
|
|
return new string(contentChars);
|
|
}
|
|
|
|
private static RazorProject GetRazorProject(IFileProvider fileProvider)
|
|
{
|
|
var razorProject = new Mock<RazorProject>();
|
|
razorProject.Setup(s => s.GetItem(It.IsAny<string>()))
|
|
.Returns((string path) => {
|
|
var fileInfo = fileProvider.GetFileInfo(path);
|
|
return new DefaultRazorProjectItem(fileInfo, null, path);
|
|
});
|
|
|
|
return razorProject.Object;
|
|
}
|
|
}
|
|
}
|