Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 4c9bce4

Browse files
author
N. Taylor Mullen
committed
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
1 parent 73101f1 commit 4c9bce4

File tree

6 files changed

+227
-0
lines changed

6 files changed

+227
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.AspNet.Mvc.Rendering;
6+
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
7+
8+
namespace Microsoft.AspNet.Mvc.Razor
9+
{
10+
public class InputTestTagHelper : TagHelper
11+
{
12+
public ModelExpression For { get; set; }
13+
}
14+
}
15+
16+
namespace Microsoft.AspNet.Mvc.Rendering
17+
{
18+
// This is here to mimic the ModelExpression type defined in Microsoft.AspNet.Mvc.Core.
19+
// Normally it's understood by the MvcRazorHost via a stringified version of it (will change once we have a DTH)
20+
// so we need to provide some polyfill so we can test its functionality in Microsoft.AspNet.Mvc.Razor.Host.Test.
21+
public class ModelExpression
22+
{
23+
}
24+
}

test/Microsoft.AspNet.Mvc.Razor.Host.Test/MvcRazorHostTest.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,44 @@ namespace Microsoft.AspNet.Mvc.Razor
1212
{
1313
public class MvcRazorHostTest
1414
{
15+
[Fact]
16+
public void MvcRazorHost_GeneratesTagHelperModelExpressionCode_DesignTime()
17+
{
18+
// Arrange
19+
var host = new MvcRazorHost(new TestFileSystem())
20+
{
21+
DesignTimeMode = true
22+
};
23+
var expectedLineMappings = new List<LineMapping>
24+
{
25+
BuildLineMapping(documentAbsoluteIndex: 7,
26+
documentLineIndex: 0,
27+
documentCharacterIndex: 7,
28+
generatedAbsoluteIndex: 444,
29+
generatedLineIndex: 12,
30+
generatedCharacterIndex: 7,
31+
contentLength: 8),
32+
BuildLineMapping(documentAbsoluteIndex: 33,
33+
documentLineIndex: 2,
34+
documentCharacterIndex: 14,
35+
generatedAbsoluteIndex: 823,
36+
generatedLineIndex: 25,
37+
generatedCharacterIndex: 14,
38+
contentLength: 85)
39+
};
40+
41+
// Act and Assert
42+
RunDesignTimeTest(host,
43+
testName: "ModelExpressionTagHelper",
44+
expectedLineMappings: expectedLineMappings);
45+
}
46+
1547
[Theory]
1648
[InlineData("Basic")]
1749
[InlineData("Inject")]
1850
[InlineData("InjectWithModel")]
1951
[InlineData("Model")]
52+
[InlineData("ModelExpressionTagHelper")]
2053
public void MvcRazorHost_ParsesAndGeneratesCodeForBasicScenarios(string scenarioName)
2154
{
2255
// Arrange
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Reflection;
5+
using Microsoft.AspNet.Razor.Generator;
6+
using Microsoft.AspNet.Razor.Generator.Compiler.CSharp;
7+
using Microsoft.AspNet.Razor.TagHelpers;
8+
using Moq;
9+
using Xunit;
10+
11+
namespace Microsoft.AspNet.Mvc.Razor
12+
{
13+
public class MvcTagHelperAttributeValueCodeRendererTest
14+
{
15+
[Theory]
16+
[InlineData("SomeType", "SomeType", "SomeMethod(__model => __model.MyValue)")]
17+
[InlineData("SomeType", "SomeType2", "MyValue")]
18+
public void RenderAttributeValue_RendersModelExpressionsCorrectly(string modelExpressionType,
19+
string propertyType,
20+
string expectedValue)
21+
{
22+
// Arrange
23+
var renderer = new MvcTagHelperAttributeValueCodeRenderer(
24+
new GeneratedTagHelperAttributeContext
25+
{
26+
ModelExpressionTypeName = modelExpressionType,
27+
CreateModelExpressionMethodName = "SomeMethod"
28+
});
29+
var propertyInfo = new Mock<PropertyInfo>();
30+
propertyInfo.Setup(mock => mock.PropertyType.FullName).Returns(propertyType);
31+
var attributeDescriptor = new TagHelperAttributeDescriptor("MyAttribute", propertyInfo.Object);
32+
var writer = new CSharpCodeWriter();
33+
var generatorContext = new CodeGeneratorContext(host: null,
34+
className: string.Empty,
35+
rootNamespace: string.Empty,
36+
sourceFile: string.Empty,
37+
shouldGenerateLinePragmas: true);
38+
var context = new CodeBuilderContext(generatorContext);
39+
40+
// Act
41+
renderer.RenderAttributeValue(attributeDescriptor, writer, context,
42+
(codeWriter) => {
43+
codeWriter.Write("MyValue");
44+
});
45+
46+
// Assert
47+
Assert.Equal(expectedValue, writer.GenerateCode());
48+
}
49+
}
50+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@model DateTime
2+
3+
@addtaghelper "Microsoft.AspNet.Mvc.Razor.InputTestTagHelper, Microsoft.AspNet.Mvc.Razor.Host.Test"
4+
5+
<inputTest for="Now" />
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace Asp
2+
{
3+
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
4+
using System;
5+
using System.Linq;
6+
using System.Collections.Generic;
7+
using Microsoft.AspNet.Mvc;
8+
using Microsoft.AspNet.Mvc.Rendering;
9+
using System.Threading.Tasks;
10+
11+
public class ASPV_TestFiles_Input_ModelExpressionTagHelper_cshtml : Microsoft.AspNet.Mvc.Razor.RazorPage<
12+
#line 1 "TestFiles/Input/ModelExpressionTagHelper.cshtml"
13+
DateTime
14+
15+
#line default
16+
#line hidden
17+
>
18+
{
19+
private static object @__o;
20+
private void @__RazorDesignTimeHelpers__()
21+
{
22+
#pragma warning disable 219
23+
string __tagHelperDirectiveSyntaxHelper = null;
24+
__tagHelperDirectiveSyntaxHelper =
25+
#line 3 "TestFiles/Input/ModelExpressionTagHelper.cshtml"
26+
"Microsoft.AspNet.Mvc.Razor.InputTestTagHelper, Microsoft.AspNet.Mvc.Razor.Host.Test"
27+
28+
#line default
29+
#line hidden
30+
;
31+
#pragma warning restore 219
32+
}
33+
#line hidden
34+
private System.IO.TextWriter __tagHelperStringValueBuffer = null;
35+
private Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperExecutionContext __tagHelperExecutionContext = null;
36+
private Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperRunner __tagHelperRunner = new Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperRunner();
37+
private Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperScopeManager __tagHelperScopeManager = new Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperScopeManager();
38+
private Microsoft.AspNet.Mvc.Razor.InputTestTagHelper __Microsoft_AspNet_Mvc_Razor_InputTestTagHelper = null;
39+
#line hidden
40+
public ASPV_TestFiles_Input_ModelExpressionTagHelper_cshtml()
41+
{
42+
}
43+
#line hidden
44+
[Microsoft.AspNet.Mvc.ActivateAttribute]
45+
public Microsoft.AspNet.Mvc.Rendering.IHtmlHelper<DateTime> Html { get; private set; }
46+
[Microsoft.AspNet.Mvc.ActivateAttribute]
47+
public Microsoft.AspNet.Mvc.IViewComponentHelper Component { get; private set; }
48+
[Microsoft.AspNet.Mvc.ActivateAttribute]
49+
public Microsoft.AspNet.Mvc.IUrlHelper Url { get; private set; }
50+
51+
#line hidden
52+
53+
#pragma warning disable 1998
54+
public override async Task ExecuteAsync()
55+
{
56+
}
57+
#pragma warning restore 1998
58+
}
59+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma checksum "TestFiles/Input/ModelExpressionTagHelper.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "11088b573392b1db9fe4cac4fff3a9ce68a72c40"
2+
namespace Asp
3+
{
4+
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
5+
using System;
6+
using System.Linq;
7+
using System.Collections.Generic;
8+
using Microsoft.AspNet.Mvc;
9+
using Microsoft.AspNet.Mvc.Rendering;
10+
using System.Threading.Tasks;
11+
12+
public class ASPV_TestFiles_Input_ModelExpressionTagHelper_cshtml : Microsoft.AspNet.Mvc.Razor.RazorPage<
13+
#line 1 "TestFiles/Input/ModelExpressionTagHelper.cshtml"
14+
DateTime
15+
16+
#line default
17+
#line hidden
18+
>
19+
{
20+
#line hidden
21+
private System.IO.TextWriter __tagHelperStringValueBuffer = null;
22+
private Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperExecutionContext __tagHelperExecutionContext = null;
23+
private Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperRunner __tagHelperRunner = new Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperRunner();
24+
private Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperScopeManager __tagHelperScopeManager = new Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperScopeManager();
25+
private Microsoft.AspNet.Mvc.Razor.InputTestTagHelper __Microsoft_AspNet_Mvc_Razor_InputTestTagHelper = null;
26+
#line hidden
27+
public ASPV_TestFiles_Input_ModelExpressionTagHelper_cshtml()
28+
{
29+
}
30+
#line hidden
31+
[Microsoft.AspNet.Mvc.ActivateAttribute]
32+
public Microsoft.AspNet.Mvc.Rendering.IHtmlHelper<DateTime> Html { get; private set; }
33+
[Microsoft.AspNet.Mvc.ActivateAttribute]
34+
public Microsoft.AspNet.Mvc.IViewComponentHelper Component { get; private set; }
35+
[Microsoft.AspNet.Mvc.ActivateAttribute]
36+
public Microsoft.AspNet.Mvc.IUrlHelper Url { get; private set; }
37+
38+
#line hidden
39+
40+
#pragma warning disable 1998
41+
public override async Task ExecuteAsync()
42+
{
43+
WriteLiteral("\r\n");
44+
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("inputTest");
45+
__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper = CreateTagHelper<Microsoft.AspNet.Mvc.Razor.InputTestTagHelper>();
46+
__tagHelperExecutionContext.Add(__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper);
47+
__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For = CreateModelExpression(__model => __model.Now);
48+
__tagHelperExecutionContext.AddTagHelperAttribute("For", __Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For);
49+
__tagHelperExecutionContext.Output = __tagHelperRunner.RunAsync(__tagHelperExecutionContext).Result;
50+
WriteLiteral(__tagHelperExecutionContext.Output.GenerateStartTag());
51+
WriteLiteral(__tagHelperExecutionContext.Output.GenerateEndTag());
52+
__tagHelperExecutionContext = __tagHelperScopeManager.End();
53+
}
54+
#pragma warning restore 1998
55+
}
56+
}

0 commit comments

Comments
 (0)