Commonizing Razor Host tests

This commit is contained in:
Pranav K 2014-09-29 16:07:11 -07:00
parent 3d5f4a2bfd
commit 038b8c7f19
12 changed files with 398 additions and 174 deletions

View File

@ -146,82 +146,6 @@ MyType1
Assert.Equal(expected, code);
}
[Fact]
public void InjectVisitor_GeneratesCorrectLineMappings()
{
// Arrange
var host = new MvcRazorHost(new TestFileSystem())
{
DesignTimeMode = true
};
host.NamespaceImports.Clear();
var engine = new RazorTemplateEngine(host);
var source = ReadResource("TestFiles/Input/Inject.cshtml");
var expectedCode = ReadResource("TestFiles/Output/Inject.cs");
var expectedLineMappings = new List<LineMapping>
{
BuildLineMapping(1, 0, 1, 30, 3, 0, 17),
BuildLineMapping(28, 1, 8, 598, 26, 8, 20)
};
// Act
GeneratorResults results;
using (var buffer = new StringTextBuffer(source))
{
results = engine.GenerateCode(buffer);
}
// Assert
Assert.True(results.Success);
Assert.Equal(expectedCode, results.GeneratedCode);
Assert.Empty(results.ParserErrors);
Assert.Equal(expectedLineMappings, results.DesignTimeLineMappings);
}
[Fact]
public void InjectVisitorWithModel_GeneratesCorrectLineMappings()
{
// Arrange
var host = new MvcRazorHost(new TestFileSystem())
{
DesignTimeMode = true
};
host.NamespaceImports.Clear();
var engine = new RazorTemplateEngine(host);
var source = ReadResource("TestFiles/Input/InjectWithModel.cshtml");
var expectedCode = ReadResource("TestFiles/Output/InjectWithModel.cs");
var expectedLineMappings = new List<LineMapping>
{
BuildLineMapping(7, 0, 7, 151, 6, 7, 7),
BuildLineMapping(24, 1, 8, 587, 26, 8, 20),
BuildLineMapping(54, 2, 8, 757, 34, 8, 23)
};
// Act
GeneratorResults results;
using (var buffer = new StringTextBuffer(source))
{
results = engine.GenerateCode(buffer);
}
// Assert
Assert.True(results.Success);
Assert.Equal(expectedCode, results.GeneratedCode);
Assert.Empty(results.ParserErrors);
Assert.Equal(expectedLineMappings, results.DesignTimeLineMappings);
}
private string ReadResource(string resourceName)
{
var assembly = typeof(InjectChunkVisitorTest).Assembly;
using (var stream = assembly.GetManifestResourceStream(resourceName))
using (var streamReader = new StreamReader(stream))
{
return streamReader.ReadToEnd();
}
}
private static CodeGeneratorContext CreateContext()
{
return CodeGeneratorContext.Create(new MvcRazorHost(new TestFileSystem()),
@ -230,25 +154,5 @@ MyType1
string.Empty,
shouldGenerateLinePragmas: true);
}
private static LineMapping BuildLineMapping(int documentAbsoluteIndex,
int documentLineIndex,
int documentCharacterIndex,
int generatedAbsoluteIndex,
int generatedLineIndex,
int generatedCharacterIndex,
int contentLength)
{
var documentLocation = new SourceLocation(documentAbsoluteIndex,
documentLineIndex,
documentCharacterIndex);
var generatedLocation = new SourceLocation(generatedAbsoluteIndex,
generatedLineIndex,
generatedCharacterIndex);
return new LineMapping(
documentLocation: new MappingLocation(documentLocation, contentLength),
generatedLocation: new MappingLocation(generatedLocation, contentLength));
}
}
}

View File

@ -2,14 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNet.Razor;
using Microsoft.AspNet.Razor.Generator;
using Microsoft.AspNet.Razor.Generator.Compiler;
using Microsoft.AspNet.Razor.Generator.Compiler.CSharp;
using Microsoft.AspNet.Razor.Parser.SyntaxTree;
using Microsoft.AspNet.Razor.Text;
using Xunit;
namespace Microsoft.AspNet.Mvc.Razor
@ -102,48 +98,6 @@ Environment.NewLine +
Assert.Equal(expected, code);
}
[Fact]
public void ModelVisitor_GeneratesCorrectLineMappings()
{
// Arrange
var host = new MvcRazorHost(new TestFileSystem())
{
DesignTimeMode = true
};
host.NamespaceImports.Clear();
var engine = new RazorTemplateEngine(host);
var source = ReadResource("TestFiles/Input/Model.cshtml");
var expectedCode = ReadResource("TestFiles/Output/Model.cs");
var expectedLineMappings = new List<LineMapping>
{
BuildLineMapping(7, 0, 7, 151, 6, 7, 30),
};
// Act
GeneratorResults results;
using (var buffer = new StringTextBuffer(source))
{
results = engine.GenerateCode(buffer);
}
// Assert
Assert.True(results.Success);
Assert.Equal(expectedCode, results.GeneratedCode);
Assert.Empty(results.ParserErrors);
Assert.Equal(expectedLineMappings, results.DesignTimeLineMappings);
}
private string ReadResource(string resourceName)
{
var assembly = typeof(ModelChunkVisitorTest).Assembly;
using (var stream = assembly.GetManifestResourceStream(resourceName))
using (var streamReader = new StreamReader(stream))
{
return streamReader.ReadToEnd();
}
}
private static CodeGeneratorContext CreateContext()
{
return CodeGeneratorContext.Create(new MvcRazorHost(new TestFileSystem()),
@ -152,25 +106,5 @@ Environment.NewLine +
string.Empty,
shouldGenerateLinePragmas: true);
}
private static LineMapping BuildLineMapping(int documentAbsoluteIndex,
int documentLineIndex,
int documentCharacterIndex,
int generatedAbsoluteIndex,
int generatedLineIndex,
int generatedCharacterIndex,
int contentLength)
{
var documentLocation = new SourceLocation(documentAbsoluteIndex,
documentLineIndex,
documentCharacterIndex);
var generatedLocation = new SourceLocation(generatedAbsoluteIndex,
generatedLineIndex,
generatedCharacterIndex);
return new LineMapping(
documentLocation: new MappingLocation(documentLocation, contentLength),
generatedLocation: new MappingLocation(generatedLocation, contentLength));
}
}
}

View File

@ -0,0 +1,163 @@
// 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.Collections.Generic;
using System.IO;
using Microsoft.AspNet.Razor;
using Microsoft.AspNet.Razor.Generator.Compiler;
using Microsoft.AspNet.Razor.Text;
using Xunit;
namespace Microsoft.AspNet.Mvc.Razor
{
public class MvcRazorHostTest
{
[Theory]
[InlineData("Basic")]
[InlineData("Inject")]
[InlineData("InjectWithModel")]
[InlineData("Model")]
public void MvcRazorHost_ParsesAndGeneratesCodeForBasicScenarios(string scenarioName)
{
// Arrange
var host = new MvcRazorHost(new TestFileSystem());
// Act and Assert
RunRuntimeTest(host, scenarioName);
}
[Fact]
public void InjectVisitor_GeneratesCorrectLineMappings()
{
// Arrange
var host = new MvcRazorHost(new TestFileSystem())
{
DesignTimeMode = true
};
host.NamespaceImports.Clear();
var expectedLineMappings = new List<LineMapping>
{
BuildLineMapping(1, 0, 1, 59, 3, 0, 17),
BuildLineMapping(28, 1, 8, 688, 26, 8, 20)
};
// Act and Assert
RunDesignTimeTest(host, "Inject", expectedLineMappings);
}
[Fact]
public void InjectVisitorWithModel_GeneratesCorrectLineMappings()
{
// Arrange
var host = new MvcRazorHost(new TestFileSystem())
{
DesignTimeMode = true
};
host.NamespaceImports.Clear();
var expectedLineMappings = new[]
{
BuildLineMapping(7, 0, 7, 214, 6, 7, 7),
BuildLineMapping(24, 1, 8, 713, 26, 8, 20),
BuildLineMapping(54, 2, 8, 921, 34, 8, 23)
};
// Act and Assert
RunDesignTimeTest(host, "InjectWithModel", expectedLineMappings);
}
[Fact]
public void ModelVisitor_GeneratesCorrectLineMappings()
{
// Arrange
var host = new MvcRazorHost(new TestFileSystem())
{
DesignTimeMode = true
};
host.NamespaceImports.Clear();
var expectedLineMappings = new[]
{
BuildLineMapping(7, 0, 7, 194, 6, 7, 30),
};
// Act and Assert
RunDesignTimeTest(host, "Model", expectedLineMappings);
}
private static void RunRuntimeTest(MvcRazorHost host,
string testName)
{
var inputFile = "TestFiles/Input/" + testName + ".cshtml";
var expectedCode = ReadResource("TestFiles/Output/Runtime/" + testName + ".cs");
// Act
GeneratorResults results;
using (var stream = GetResourceStream(inputFile))
{
results = host.GenerateCode(inputFile, stream);
}
// Assert
Assert.True(results.Success);
Assert.Equal(expectedCode, results.GeneratedCode);
Assert.Empty(results.ParserErrors);
}
private static void RunDesignTimeTest(MvcRazorHost host,
string testName,
IEnumerable<LineMapping> expectedLineMappings)
{
var inputFile = "TestFiles/Input/" + testName + ".cshtml";
var expectedCode = ReadResource("TestFiles/Output/DesignTime/" + testName + ".cs");
// Act
GeneratorResults results;
using (var stream = GetResourceStream(inputFile))
{
results = host.GenerateCode(inputFile, stream);
}
// Assert
Assert.True(results.Success);
Assert.Equal(expectedCode, results.GeneratedCode);
Assert.Empty(results.ParserErrors);
Assert.Equal(expectedLineMappings, results.DesignTimeLineMappings);
}
private static string ReadResource(string resourceName)
{
using (var stream = GetResourceStream(resourceName))
{
using (var streamReader = new StreamReader(stream))
{
return streamReader.ReadToEnd();
}
}
}
private static Stream GetResourceStream(string resourceName)
{
var assembly = typeof(MvcRazorHostTest).Assembly;
return assembly.GetManifestResourceStream(resourceName);
}
private static LineMapping BuildLineMapping(int documentAbsoluteIndex,
int documentLineIndex,
int documentCharacterIndex,
int generatedAbsoluteIndex,
int generatedLineIndex,
int generatedCharacterIndex,
int contentLength)
{
var documentLocation = new SourceLocation(documentAbsoluteIndex,
documentLineIndex,
documentCharacterIndex);
var generatedLocation = new SourceLocation(generatedAbsoluteIndex,
generatedLineIndex,
generatedCharacterIndex);
return new LineMapping(
documentLocation: new MappingLocation(documentLocation, contentLength),
generatedLocation: new MappingLocation(generatedLocation, contentLength));
}
}
}

View File

@ -0,0 +1,4 @@
<div class="@logo">
Hello world
@Html.Input("SomeKey")
</div>

View File

@ -0,0 +1,59 @@
namespace Asp
{
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_Basic_cshtml : Microsoft.AspNet.Mvc.Razor.RazorPage<dynamic>
{
#line hidden
public ASPV_TestFiles_Input_Basic_cshtml()
{
}
#line hidden
[Microsoft.AspNet.Mvc.ActivateAttribute]
public Microsoft.AspNet.Mvc.Rendering.IHtmlHelper<dynamic> 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()
{
PageExecutionContext.BeginContext(0, 4, true);
WriteLiteral("<div");
PageExecutionContext.EndContext();
WriteAttribute("class", Tuple.Create(" class=\"", 4), Tuple.Create("\"", 17),
Tuple.Create(Tuple.Create("", 12), Tuple.Create<System.Object, System.Int32>(
#line 1 "TestFiles/Input/Basic.cshtml"
logo
#line default
#line hidden
, 12), false));
PageExecutionContext.BeginContext(18, 24, true);
WriteLiteral(">\r\n Hello world\r\n ");
PageExecutionContext.EndContext();
PageExecutionContext.BeginContext(43, 21, false);
Write(
#line 3 "TestFiles/Input/Basic.cshtml"
Html.Input("SomeKey")
#line default
#line hidden
);
PageExecutionContext.EndContext();
PageExecutionContext.BeginContext(64, 8, true);
WriteLiteral("\r\n</div>");
PageExecutionContext.EndContext();
}
#pragma warning restore 1998
}
}

View File

@ -1,6 +1,6 @@
namespace Asp
{
#line 1 ""
#line 1 "TestFiles/Input/Inject.cshtml"
using MyNamespace
#line default
@ -8,7 +8,7 @@ using MyNamespace
;
using System.Threading.Tasks;
public class __CompiledTemplate : Microsoft.AspNet.Mvc.Razor.RazorPage<dynamic>
public class ASPV_TestFiles_Input_Inject_cshtml : Microsoft.AspNet.Mvc.Razor.RazorPage<dynamic>
{
private static object @__o;
private void @__RazorDesignTimeHelpers__()
@ -17,13 +17,13 @@ using MyNamespace
#pragma warning restore 219
}
#line hidden
public __CompiledTemplate()
public ASPV_TestFiles_Input_Inject_cshtml()
{
}
#line hidden
[Microsoft.AspNet.Mvc.ActivateAttribute]
public
#line 2 ""
#line 2 "TestFiles/Input/Inject.cshtml"
MyApp MyPropertyName
#line default

View File

@ -2,8 +2,8 @@
{
using System.Threading.Tasks;
public class __CompiledTemplate : Microsoft.AspNet.Mvc.Razor.RazorPage<
#line 1 ""
public class ASPV_TestFiles_Input_InjectWithModel_cshtml : Microsoft.AspNet.Mvc.Razor.RazorPage<
#line 1 "TestFiles/Input/InjectWithModel.cshtml"
MyModel
#line default
@ -17,13 +17,13 @@
#pragma warning restore 219
}
#line hidden
public __CompiledTemplate()
public ASPV_TestFiles_Input_InjectWithModel_cshtml()
{
}
#line hidden
[Microsoft.AspNet.Mvc.ActivateAttribute]
public
#line 2 ""
#line 2 "TestFiles/Input/InjectWithModel.cshtml"
MyApp MyPropertyName
#line default
@ -31,7 +31,7 @@
{ get; private set; }
[Microsoft.AspNet.Mvc.ActivateAttribute]
public
#line 3 ""
#line 3 "TestFiles/Input/InjectWithModel.cshtml"
MyService<MyModel> Html
#line default

View File

@ -2,8 +2,8 @@
{
using System.Threading.Tasks;
public class __CompiledTemplate : Microsoft.AspNet.Mvc.Razor.RazorPage<
#line 1 ""
public class ASPV_TestFiles_Input_Model_cshtml : Microsoft.AspNet.Mvc.Razor.RazorPage<
#line 1 "TestFiles/Input/Model.cshtml"
System.Collections.IEnumerable
#line default
@ -17,7 +17,7 @@
#pragma warning restore 219
}
#line hidden
public __CompiledTemplate()
public ASPV_TestFiles_Input_Model_cshtml()
{
}
#line hidden

View File

@ -0,0 +1,42 @@
namespace Asp
{
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_Basic_cshtml : Microsoft.AspNet.Mvc.Razor.RazorPage<dynamic>
{
#line hidden
public ASPV_TestFiles_Input_Basic_cshtml()
{
}
#line hidden
[Microsoft.AspNet.Mvc.ActivateAttribute]
public Microsoft.AspNet.Mvc.Rendering.IHtmlHelper<dynamic> 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("<div");
WriteAttribute("class", Tuple.Create(" class=\"", 4), Tuple.Create("\"", 17),
Tuple.Create(Tuple.Create("", 12), Tuple.Create<System.Object, System.Int32>(logo, 12), false));
WriteLiteral(">\r\n Hello world\r\n ");
#line 3 "TestFiles/Input/Basic.cshtml"
Write(Html.Input("SomeKey"));
#line default
#line hidden
WriteLiteral("\r\n</div>");
}
#pragma warning restore 1998
}
}

View File

@ -0,0 +1,40 @@
namespace Asp
{
#line 1 "TestFiles/Input/Inject.cshtml"
using MyNamespace
#line default
#line hidden
;
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_Inject_cshtml : Microsoft.AspNet.Mvc.Razor.RazorPage<dynamic>
{
#line hidden
public ASPV_TestFiles_Input_Inject_cshtml()
{
}
#line hidden
[Microsoft.AspNet.Mvc.ActivateAttribute]
public MyApp MyPropertyName { get; private set; }
[Microsoft.AspNet.Mvc.ActivateAttribute]
public Microsoft.AspNet.Mvc.Rendering.IHtmlHelper<dynamic> 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,40 @@
namespace Asp
{
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_InjectWithModel_cshtml : Microsoft.AspNet.Mvc.Razor.RazorPage<
#line 1 "TestFiles/Input/InjectWithModel.cshtml"
MyModel
#line default
#line hidden
>
{
#line hidden
public ASPV_TestFiles_Input_InjectWithModel_cshtml()
{
}
#line hidden
[Microsoft.AspNet.Mvc.ActivateAttribute]
public MyApp MyPropertyName { get; private set; }
[Microsoft.AspNet.Mvc.ActivateAttribute]
public MyService<MyModel> 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,38 @@
namespace Asp
{
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_Model_cshtml : Microsoft.AspNet.Mvc.Razor.RazorPage<
#line 1 "TestFiles/Input/Model.cshtml"
System.Collections.IEnumerable
#line default
#line hidden
>
{
#line hidden
public ASPV_TestFiles_Input_Model_cshtml()
{
}
#line hidden
[Microsoft.AspNet.Mvc.ActivateAttribute]
public Microsoft.AspNet.Mvc.Rendering.IHtmlHelper<System.Collections.IEnumerable> 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
}
}