diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/InputTestTagHelper.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/InputTestTagHelper.cs new file mode 100644 index 0000000000..ec1582d40b --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/InputTestTagHelper.cs @@ -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; } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/ModelExpression.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/ModelExpression.cs new file mode 100644 index 0000000000..22b38fc8cd --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/ModelExpression.cs @@ -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 + { + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/MvcRazorHostTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/MvcRazorHostTest.cs index ca6647ccbb..a51630bb22 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/MvcRazorHostTest.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/MvcRazorHostTest.cs @@ -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 + { + 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 diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/MvcTagHelperAttributeValueCodeRendererTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/MvcTagHelperAttributeValueCodeRendererTest.cs new file mode 100644 index 0000000000..1ccea9520c --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/MvcTagHelperAttributeValueCodeRendererTest.cs @@ -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.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()); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Input/ModelExpressionTagHelper.cshtml b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Input/ModelExpressionTagHelper.cshtml new file mode 100644 index 0000000000..7114a5c211 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Input/ModelExpressionTagHelper.cshtml @@ -0,0 +1,5 @@ +@model DateTime + +@addtaghelper "Microsoft.AspNet.Mvc.Razor.InputTestTagHelper, Microsoft.AspNet.Mvc.Razor.Host.Test" + + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/ModelExpressionTagHelper.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/ModelExpressionTagHelper.cs new file mode 100644 index 0000000000..2c63db3844 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/ModelExpressionTagHelper.cs @@ -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 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 + } +} diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/ModelExpressionTagHelper.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/ModelExpressionTagHelper.cs new file mode 100644 index 0000000000..2687d004cd --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/ModelExpressionTagHelper.cs @@ -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 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(); + __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 + } +}