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

Commit b69b7bf

Browse files
author
NTaylorMullen
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 5d92c94 commit b69b7bf

File tree

8 files changed

+260
-0
lines changed

8 files changed

+260
-0
lines changed

src/Microsoft.AspNet.Mvc.Razor.Host/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
using System.Runtime.CompilerServices;
55

66
[assembly: InternalsVisibleTo("Microsoft.AspNet.Mvc.Razor.Host.Test")]
7+
[assembly: InternalsVisibleTo("Microsoft.AspNet.Mvc.Razor.Test")]
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.Globalization;
5+
using System.IO;
6+
using Microsoft.AspNet.Mvc.Test.Common;
7+
using Microsoft.AspNet.Razor;
8+
using Xunit;
9+
10+
namespace Microsoft.AspNet.Mvc.Razor
11+
{
12+
public class RazorTagHelperCodeGenerationTest
13+
{
14+
[Fact]
15+
public void MvcRazorHost_GeneratesTagHelperModelExpressionCode_Runtime()
16+
{
17+
// Arrange, Act and Assert
18+
RunTest("ModelExpressionTagHelper", designTimeMode: false);
19+
}
20+
21+
[Fact]
22+
public void MvcRazorHost_GeneratesTagHelperModelExpressionCode_DesignTime()
23+
{
24+
// Arrange, Act and Assert
25+
RunTest("ModelExpressionTagHelper", designTimeMode: true);
26+
}
27+
28+
private static void RunTest(string testName, bool designTimeMode)
29+
{
30+
var mode = designTimeMode ? "DesignTime" : "Runtime";
31+
var inputFileName = string.Format(CultureInfo.InvariantCulture,
32+
"TestFiles/Input/{0}.cshtml",
33+
testName);
34+
var resourceLocation = string.Format(CultureInfo.InvariantCulture,
35+
"TestFiles/Output/{0}/{1}.cs",
36+
mode,
37+
testName);
38+
var expectedCode = ReadResource(resourceLocation);
39+
var host = new MvcRazorHost(new TestFileSystem())
40+
{
41+
DesignTimeMode = designTimeMode
42+
};
43+
44+
// Act
45+
GeneratorResults results;
46+
using (var stream = GetResourceStream(inputFileName))
47+
{
48+
results = host.GenerateCode(inputFileName, stream);
49+
}
50+
51+
// Assert
52+
Assert.True(results.Success);
53+
Assert.Equal(expectedCode, results.GeneratedCode);
54+
Assert.Empty(results.ParserErrors);
55+
}
56+
57+
private static string ReadResource(string resourceName)
58+
{
59+
using (var stream = GetResourceStream(resourceName))
60+
{
61+
using (var streamReader = new StreamReader(stream))
62+
{
63+
return streamReader.ReadToEnd();
64+
}
65+
}
66+
}
67+
68+
private static Stream GetResourceStream(string resourceName)
69+
{
70+
var assembly = typeof(RazorTagHelperCodeGenerationTest).Assembly;
71+
return assembly.GetManifestResourceStream(resourceName);
72+
}
73+
}
74+
}
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.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.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}" "46c9fff705d3cc6018bdeb4c8a23e5d1e9a61c37"
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+
}

test/Microsoft.AspNet.Mvc.Razor.Test/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"resources": "TestFiles\\**",
23
"dependencies": {
34
"Microsoft.AspNet.FileSystems": "1.0.0-*",
45
"Microsoft.AspNet.Http": "1.0.0-*",

0 commit comments

Comments
 (0)