Add tests to validate attribute value rendering.

- Exposed internals from Mvc.Razor.Host to Mvc.Razor.Test so it can use the MvcRazorHost override that takes the IFileSystem.
- Added end-to-end code generation tests for TagHelpers with ModelExpression properties.

#1241
This commit is contained in:
N. Taylor Mullen 2014-10-08 14:02:35 -07:00 committed by NTaylorMullen
parent 2005c3cd85
commit 75a77e2d01
7 changed files with 226 additions and 0 deletions

View File

@ -0,0 +1,13 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
namespace Microsoft.AspNet.Mvc.Razor
{
public class InputTestTagHelper : TagHelper
{
public ModelExpression For { get; set; }
}
}

View File

@ -0,0 +1,10 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNet.Mvc.Rendering
{
// This is here to mimic the real ModelExpression type defined in Microsoft.AspNet.Mvc.Core.
public class ModelExpression
{
}
}

View File

@ -12,11 +12,44 @@ namespace Microsoft.AspNet.Mvc.Razor
{
public class MvcRazorHostTest
{
[Fact]
public void MvcRazorHost_GeneratesTagHelperModelExpressionCode_DesignTime()
{
// Arrange
var host = new MvcRazorHost(new TestFileSystem())
{
DesignTimeMode = true
};
var expectedLineMappings = new List<LineMapping>
{
BuildLineMapping(documentAbsoluteIndex: 7,
documentLineIndex: 0,
documentCharacterIndex: 7,
generatedAbsoluteIndex: 444,
generatedLineIndex: 12,
generatedCharacterIndex: 7,
contentLength: 8),
BuildLineMapping(documentAbsoluteIndex: 33,
documentLineIndex: 2,
documentCharacterIndex: 14,
generatedAbsoluteIndex: 823,
generatedLineIndex: 25,
generatedCharacterIndex: 14,
contentLength: 85)
};
// Act and Assert
RunDesignTimeTest(host,
testName: "ModelExpressionTagHelper",
expectedLineMappings: expectedLineMappings);
}
[Theory]
[InlineData("Basic")]
[InlineData("Inject")]
[InlineData("InjectWithModel")]
[InlineData("Model")]
[InlineData("ModelExpressionTagHelper")]
public void MvcRazorHost_ParsesAndGeneratesCodeForBasicScenarios(string scenarioName)
{
// Arrange

View File

@ -0,0 +1,50 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Reflection;
using Microsoft.AspNet.Razor.Generator;
using Microsoft.AspNet.Razor.Generator.Compiler.CSharp;
using Microsoft.AspNet.Razor.TagHelpers;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Mvc.Razor
{
public class MvcTagHelperAttributeValueCodeRendererTest
{
[Theory]
[InlineData("SomeType", "SomeType", "SomeMethod(__model => __model.MyValue)")]
[InlineData("SomeType", "SomeType2", "MyValue")]
public void RenderAttributeValue_RendersModelExpressionsCorrectly(string modelExpressionType,
string propertyType,
string expectedValue)
{
// Arrange
var renderer = new MvcTagHelperAttributeValueCodeRenderer(
new GeneratedTagHelperAttributeContext
{
ModelExpressionTypeName = modelExpressionType,
CreateModelExpressionMethodName = "SomeMethod"
});
var propertyInfo = new Mock<PropertyInfo>();
propertyInfo.Setup(mock => mock.PropertyType.FullName).Returns(propertyType);
var attributeDescriptor = new TagHelperAttributeDescriptor("MyAttribute", propertyInfo.Object);
var writer = new CSharpCodeWriter();
var generatorContext = new CodeGeneratorContext(host: null,
className: string.Empty,
rootNamespace: string.Empty,
sourceFile: string.Empty,
shouldGenerateLinePragmas: true);
var context = new CodeBuilderContext(generatorContext);
// Act
renderer.RenderAttributeValue(attributeDescriptor, writer, context,
(codeWriter) => {
codeWriter.Write("MyValue");
});
// Assert
Assert.Equal(expectedValue, writer.GenerateCode());
}
}
}

View File

@ -0,0 +1,5 @@
@model DateTime
@addtaghelper "Microsoft.AspNet.Mvc.Razor.InputTestTagHelper, Microsoft.AspNet.Mvc.Razor.Host.Test"
<inputTest for="Now" />

View File

@ -0,0 +1,59 @@
namespace Asp
{
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using System.Threading.Tasks;
public class ASPV_TestFiles_Input_ModelExpressionTagHelper_cshtml : Microsoft.AspNet.Mvc.Razor.RazorPage<
#line 1 "TestFiles/Input/ModelExpressionTagHelper.cshtml"
DateTime
#line default
#line hidden
>
{
private static object @__o;
private void @__RazorDesignTimeHelpers__()
{
#pragma warning disable 219
string __tagHelperDirectiveSyntaxHelper = null;
__tagHelperDirectiveSyntaxHelper =
#line 3 "TestFiles/Input/ModelExpressionTagHelper.cshtml"
"Microsoft.AspNet.Mvc.Razor.InputTestTagHelper, Microsoft.AspNet.Mvc.Razor.Host.Test"
#line default
#line hidden
;
#pragma warning restore 219
}
#line hidden
private System.IO.TextWriter __tagHelperStringValueBuffer = null;
private Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperExecutionContext __tagHelperExecutionContext = null;
private Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperRunner __tagHelperRunner = new Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperRunner();
private Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperScopeManager __tagHelperScopeManager = new Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperScopeManager();
private Microsoft.AspNet.Mvc.Razor.InputTestTagHelper __Microsoft_AspNet_Mvc_Razor_InputTestTagHelper = null;
#line hidden
public ASPV_TestFiles_Input_ModelExpressionTagHelper_cshtml()
{
}
#line hidden
[Microsoft.AspNet.Mvc.ActivateAttribute]
public Microsoft.AspNet.Mvc.Rendering.IHtmlHelper<DateTime> Html { get; private set; }
[Microsoft.AspNet.Mvc.ActivateAttribute]
public Microsoft.AspNet.Mvc.IViewComponentHelper Component { get; private set; }
[Microsoft.AspNet.Mvc.ActivateAttribute]
public Microsoft.AspNet.Mvc.IUrlHelper Url { get; private set; }
#line hidden
#pragma warning disable 1998
public override async Task ExecuteAsync()
{
}
#pragma warning restore 1998
}
}

View File

@ -0,0 +1,56 @@
#pragma checksum "TestFiles/Input/ModelExpressionTagHelper.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "11088b573392b1db9fe4cac4fff3a9ce68a72c40"
namespace Asp
{
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using System.Threading.Tasks;
public class ASPV_TestFiles_Input_ModelExpressionTagHelper_cshtml : Microsoft.AspNet.Mvc.Razor.RazorPage<
#line 1 "TestFiles/Input/ModelExpressionTagHelper.cshtml"
DateTime
#line default
#line hidden
>
{
#line hidden
private System.IO.TextWriter __tagHelperStringValueBuffer = null;
private Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperExecutionContext __tagHelperExecutionContext = null;
private Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperRunner __tagHelperRunner = new Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperRunner();
private Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperScopeManager __tagHelperScopeManager = new Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperScopeManager();
private Microsoft.AspNet.Mvc.Razor.InputTestTagHelper __Microsoft_AspNet_Mvc_Razor_InputTestTagHelper = null;
#line hidden
public ASPV_TestFiles_Input_ModelExpressionTagHelper_cshtml()
{
}
#line hidden
[Microsoft.AspNet.Mvc.ActivateAttribute]
public Microsoft.AspNet.Mvc.Rendering.IHtmlHelper<DateTime> Html { get; private set; }
[Microsoft.AspNet.Mvc.ActivateAttribute]
public Microsoft.AspNet.Mvc.IViewComponentHelper Component { get; private set; }
[Microsoft.AspNet.Mvc.ActivateAttribute]
public Microsoft.AspNet.Mvc.IUrlHelper Url { get; private set; }
#line hidden
#pragma warning disable 1998
public override async Task ExecuteAsync()
{
WriteLiteral("\r\n");
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("inputTest");
__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper = CreateTagHelper<Microsoft.AspNet.Mvc.Razor.InputTestTagHelper>();
__tagHelperExecutionContext.Add(__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper);
__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For = CreateModelExpression(__model => __model.Now);
__tagHelperExecutionContext.AddTagHelperAttribute("For", __Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For);
__tagHelperExecutionContext.Output = __tagHelperRunner.RunAsync(__tagHelperExecutionContext).Result;
WriteLiteral(__tagHelperExecutionContext.Output.GenerateStartTag());
WriteLiteral(__tagHelperExecutionContext.Output.GenerateEndTag());
__tagHelperExecutionContext = __tagHelperScopeManager.End();
}
#pragma warning restore 1998
}
}