Update tests for Razor Tag Helpers Unique ID feature:

- aspnet/razor#241
This commit is contained in:
DamianEdwards 2014-12-12 16:15:20 -08:00
parent 3e26142de1
commit 263e75c8a6
12 changed files with 109 additions and 37 deletions

View File

@ -3,8 +3,11 @@
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNet.FileSystems;
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.Text;
using Xunit;
@ -66,7 +69,7 @@ namespace Microsoft.AspNet.Mvc.Razor
public void MvcRazorHost_ParsesAndGeneratesCodeForBasicScenarios(string scenarioName)
{
// Arrange
var host = new MvcRazorHost(new TestFileSystem());
var host = new TestMvcRazorHost(new TestFileSystem());
// Act and Assert
RunRuntimeTest(host, scenarioName);
@ -205,5 +208,68 @@ namespace Microsoft.AspNet.Mvc.Razor
documentLocation: new MappingLocation(documentLocation, contentLength),
generatedLocation: new MappingLocation(generatedLocation, contentLength));
}
/// <summary>
/// Used when testing Tag Helpers, it disables the unique ID generation feature.
/// </summary>
private class TestMvcRazorHost : MvcRazorHost
{
public TestMvcRazorHost(IFileSystem fileSystem)
: base(fileSystem)
{ }
public override CodeBuilder DecorateCodeBuilder(CodeBuilder incomingBuilder, CodeBuilderContext context)
{
base.DecorateCodeBuilder(incomingBuilder, context);
return new TestCSharpCodeBuilder(context,
DefaultModel,
ActivateAttribute,
new GeneratedTagHelperAttributeContext
{
ModelExpressionTypeName = ModelExpressionType,
CreateModelExpressionMethodName = CreateModelExpressionMethod
});
}
protected class TestCSharpCodeBuilder : MvcCSharpCodeBuilder
{
private readonly GeneratedTagHelperAttributeContext _tagHelperAttributeContext;
public TestCSharpCodeBuilder(CodeBuilderContext context,
string defaultModel,
string activateAttribute,
GeneratedTagHelperAttributeContext tagHelperAttributeContext)
: base(context, defaultModel, activateAttribute, tagHelperAttributeContext)
{
_tagHelperAttributeContext = tagHelperAttributeContext;
}
protected override CSharpCodeVisitor CreateCSharpCodeVisitor(CSharpCodeWriter writer, CodeBuilderContext context)
{
var visitor = base.CreateCSharpCodeVisitor(writer, context);
visitor.TagHelperRenderer = new NoUniqueIdsTagHelperCodeRenderer(visitor, writer, context)
{
AttributeValueCodeRenderer =
new MvcTagHelperAttributeValueCodeRenderer(_tagHelperAttributeContext)
};
return visitor;
}
private class NoUniqueIdsTagHelperCodeRenderer : CSharpTagHelperCodeRenderer
{
public NoUniqueIdsTagHelperCodeRenderer(IChunkVisitor bodyVisitor,
CSharpCodeWriter writer,
CodeBuilderContext context)
: base(bodyVisitor, writer, context)
{ }
protected override string GenerateUniqueId()
{
return "test";
}
}
}
}
}
}

View File

@ -43,7 +43,7 @@ namespace Asp
BeginContext(120, 2, true);
WriteLiteral("\r\n");
EndContext();
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("inputTest");
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("inputTest", "test");
__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);

View File

@ -31,7 +31,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ "asp-fragment", "hello=world" },
{ "asp-host", "contoso.com" },
{ "asp-protocol", "http" }
});
},
uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
attributes: new Dictionary<string, string>
@ -84,7 +85,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{
// Arrange
var context = new TagHelperContext(
allAttributes: new Dictionary<string, object>());
allAttributes: new Dictionary<string, object>(), uniqueId: "test");
var output = new TagHelperOutput(
"a",
attributes: new Dictionary<string, string>(),
@ -118,7 +119,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{
// Arrange
var context = new TagHelperContext(
allAttributes: new Dictionary<string, object>());
allAttributes: new Dictionary<string, object>(), uniqueId: "test");
var output = new TagHelperOutput(
"a",
attributes: new Dictionary<string, string>(),

View File

@ -32,7 +32,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ "asp-controller", "home" },
{ "method", "post" },
{ "asp-anti-forgery", true }
});
},
uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
attributes: new Dictionary<string, string>
@ -91,7 +92,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
// Arrange
var viewContext = CreateViewContext();
var context = new TagHelperContext(
allAttributes: new Dictionary<string, object>());
allAttributes: new Dictionary<string, object>(), uniqueId: "test");
var output = new TagHelperOutput(
"form",
attributes: new Dictionary<string, string>(),
@ -132,7 +133,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
// Arrange
var testViewContext = CreateViewContext();
var context = new TagHelperContext(
allAttributes: new Dictionary<string, object>());
allAttributes: new Dictionary<string, object>(), uniqueId: "test");
var expectedAttribute = new KeyValuePair<string, string>("asp-ROUTEE-NotRoute", "something");
var output = new TagHelperOutput(
"form",
@ -193,7 +194,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
// Arrange
var viewContext = CreateViewContext();
var context = new TagHelperContext(
allAttributes: new Dictionary<string, object>());
allAttributes: new Dictionary<string, object>(), uniqueId: "test");
var output = new TagHelperOutput(
"form",
attributes: new Dictionary<string, string>(),
@ -243,7 +244,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new Dictionary<string, object>()
{
{ "METhod", "POST" }
});
},
uniqueId: "test");
// Act
await formTagHelper.ProcessAsync(context, output);
@ -284,7 +286,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ "aCTiON", "my-action" },
},
content: string.Empty);
var context = new TagHelperContext(allAttributes: new Dictionary<string, object>());
var context = new TagHelperContext(allAttributes: new Dictionary<string, object>(), uniqueId: "test");
// Act
await formTagHelper.ProcessAsync(context, output);

View File

@ -92,7 +92,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedContent = "original content";
var expectedTagName = "not-input";
var context = new TagHelperContext(new Dictionary<string, object>());
var context = new TagHelperContext(new Dictionary<string, object>(), uniqueId: "test");
var originalAttributes = new Dictionary<string, string>
{
{ "class", "form-control" },
@ -137,7 +137,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var originalTagName = "not-input";
var expectedContent = originalContent + "<input class=\"form-control\" /><hidden />";
var context = new TagHelperContext(allAttributes: new Dictionary<string, object>());
var context = new TagHelperContext(allAttributes: new Dictionary<string, object>(), uniqueId: "test");
var originalAttributes = new Dictionary<string, string>
{
{ "class", "form-control" },
@ -217,7 +217,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedContent = "something";
var expectedTagName = "not-input";
var context = new TagHelperContext(allAttributes: contextAttributes);
var context = new TagHelperContext(allAttributes: contextAttributes, uniqueId: "test");
var originalAttributes = new Dictionary<string, string>
{
{ "class", "form-control" },
@ -294,7 +294,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedContent = "something";
var expectedTagName = "not-input";
var context = new TagHelperContext(allAttributes: contextAttributes);
var context = new TagHelperContext(allAttributes: contextAttributes, uniqueId: "test");
var originalAttributes = new Dictionary<string, string>
{
{ "class", "form-control" },
@ -368,7 +368,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedContent = "something";
var expectedTagName = "not-input";
var context = new TagHelperContext(allAttributes: contextAttributes);
var context = new TagHelperContext(allAttributes: contextAttributes, uniqueId: "test");
var originalAttributes = new Dictionary<string, string>
{
{ "class", "form-control" },
@ -457,7 +457,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedContent = "something";
var expectedTagName = "not-input";
var context = new TagHelperContext(allAttributes: contextAttributes);
var context = new TagHelperContext(allAttributes: contextAttributes, uniqueId: "test");
var originalAttributes = new Dictionary<string, string>
{
{ "class", "form-control" },
@ -515,7 +515,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedContent = "original content";
var expectedTagName = "input";
var tagHelperContext = new TagHelperContext(new Dictionary<string, object>());
var tagHelperContext = new TagHelperContext(new Dictionary<string, object>(), uniqueId: "test");
var output = new TagHelperOutput(expectedTagName, expectedAttributes, expectedContent)
{
SelfClosing = false,
@ -563,7 +563,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedMessage = "Unable to format without a 'asp-for' expression for <input>. 'asp-format' must " +
"be null if 'asp-for' is null.";
var tagHelperContext = new TagHelperContext(contextAttributes);
var tagHelperContext = new TagHelperContext(contextAttributes, uniqueId: "test");
var output = new TagHelperOutput(expectedTagName, originalAttributes, content);
var tagHelper = new InputTagHelper
{

View File

@ -113,7 +113,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
For = modelExpression,
};
var tagHelperContext = new TagHelperContext(allAttributes: new Dictionary<string, object>());
var tagHelperContext = new TagHelperContext(allAttributes: new Dictionary<string, object>(), uniqueId: "test");
var htmlAttributes = new Dictionary<string, string>
{
{ "class", "form-control" },
@ -153,7 +153,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var modelExpression = new ModelExpression(nameof(Model.Text), metadata);
var tagHelper = new LabelTagHelper();
var tagHelperContext = new TagHelperContext(allAttributes: new Dictionary<string, object>());
var tagHelperContext = new TagHelperContext(allAttributes: new Dictionary<string, object>(), uniqueId: "test");
var output = new TagHelperOutput(expectedTagName, expectedAttributes, expectedContent);
var htmlGenerator = new TestableHtmlGenerator(metadataProvider);

View File

@ -136,7 +136,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ "selected", selected },
{ "value", value },
};
var tagHelperContext = new TagHelperContext(contextAttributes);
var tagHelperContext = new TagHelperContext(contextAttributes, uniqueId: "test");
var output = new TagHelperOutput(expectedTagName, originalAttributes, originalContent)
{
SelfClosing = false,
@ -188,7 +188,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ "selected", selected },
{ "value", value },
};
var tagHelperContext = new TagHelperContext(contextAttributes);
var tagHelperContext = new TagHelperContext(contextAttributes, uniqueId: "test");
var output = new TagHelperOutput(originalTagName, originalAttributes, originalContent)
{
SelfClosing = false,
@ -235,7 +235,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ "selected", selected },
{ "value", value },
};
var tagHelperContext = new TagHelperContext(contextAttributes);
var tagHelperContext = new TagHelperContext(contextAttributes, uniqueId: "test");
var output = new TagHelperOutput(originalTagName, originalAttributes, originalContent)
{
SelfClosing = false,

View File

@ -182,7 +182,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var metadata = metadataProvider.GetMetadataForProperty(modelAccessor, containerType, propertyName: "Text");
var modelExpression = new ModelExpression(nameAndId.Name, metadata);
var tagHelperContext = new TagHelperContext(new Dictionary<string, object>());
var tagHelperContext = new TagHelperContext(new Dictionary<string, object>(), uniqueId: "test");
var output = new TagHelperOutput(expectedTagName, originalAttributes, expectedContent)
{
SelfClosing = true,
@ -252,7 +252,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var metadata = metadataProvider.GetMetadataForProperty(modelAccessor, containerType, propertyName: "Text");
var modelExpression = new ModelExpression(nameAndId.Name, metadata);
var tagHelperContext = new TagHelperContext(new Dictionary<string, object>());
var tagHelperContext = new TagHelperContext(new Dictionary<string, object>(), uniqueId: "test");
var output = new TagHelperOutput(expectedTagName, originalAttributes, originalContent)
{
SelfClosing = true,
@ -312,7 +312,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var propertyName = "Property1";
var expectedTagName = "select";
var tagHelperContext = new TagHelperContext(contextAttributes);
var tagHelperContext = new TagHelperContext(contextAttributes, uniqueId: "test");
var output = new TagHelperOutput(expectedTagName, originalAttributes, content);
// TODO: https://github.com/aspnet/Mvc/issues/1253
@ -376,7 +376,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var propertyName = "Property1";
var tagName = "select";
var tagHelperContext = new TagHelperContext(contextAttributes);
var tagHelperContext = new TagHelperContext(contextAttributes, uniqueId: "test");
var output = new TagHelperOutput(tagName, originalAttributes, content);
var metadataProvider = new EmptyModelMetadataProvider();
@ -440,7 +440,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedContent = "original content";
var expectedTagName = "select";
var tagHelperContext = new TagHelperContext(contextAttributes);
var tagHelperContext = new TagHelperContext(contextAttributes, uniqueId: "test");
var output = new TagHelperOutput(expectedTagName, originalAttributes, expectedContent)
{
SelfClosing = true,
@ -471,7 +471,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedTagName = "select";
var expectedMessage = "Cannot determine body for <select>. 'asp-items' must be null if 'asp-for' is null.";
var tagHelperContext = new TagHelperContext(contextAttributes);
var tagHelperContext = new TagHelperContext(contextAttributes, uniqueId: "test");
var output = new TagHelperOutput(expectedTagName, originalAttributes, content);
var tagHelper = new SelectTagHelper
{
@ -502,7 +502,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedMessage = "Cannot parse 'multiple' value '" + multiple +
"' for <select>. Acceptable values are 'false', 'true' and 'multiple'.";
var tagHelperContext = new TagHelperContext(contextAttributes);
var tagHelperContext = new TagHelperContext(contextAttributes, uniqueId: "test");
var output = new TagHelperOutput(expectedTagName, originalAttributes, content);
var metadataProvider = new EmptyModelMetadataProvider();

View File

@ -25,7 +25,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
new Dictionary<string, object>(StringComparer.Ordinal)
{
{ attributeName, attributeValue }
});
},
uniqueId: "test");
var expectedAttribute = new KeyValuePair<string, string>(attributeName, attributeValue);
// Act
@ -53,7 +54,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
new Dictionary<string, object>(StringComparer.Ordinal)
{
{ attributeName, "world" }
});
}
, uniqueId: "test");
// Act
tagHelperOutput.CopyHtmlAttribute(attributeName, tagHelperContext);

View File

@ -107,7 +107,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
For = modelExpression,
};
var tagHelperContext = new TagHelperContext(new Dictionary<string, object>());
var tagHelperContext = new TagHelperContext(new Dictionary<string, object>(), uniqueId: "test");
var htmlAttributes = new Dictionary<string, string>
{
{ "class", "form-control" },
@ -157,7 +157,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var modelExpression = new ModelExpression(nameof(Model.Text), metadata);
var tagHelper = new TextAreaTagHelper();
var tagHelperContext = new TagHelperContext(new Dictionary<string, object>());
var tagHelperContext = new TagHelperContext(new Dictionary<string, object>(), uniqueId: "test");
var output = new TagHelperOutput(expectedTagName, expectedAttributes, expectedContent)
{
SelfClosing = true,

View File

@ -33,7 +33,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{
{ "id", "myvalidationmessage" },
{ "for", modelExpression },
});
},
uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
attributes: new Dictionary<string, string>

View File

@ -28,7 +28,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
ValidationSummaryValue = "All"
};
var tagHelperContext = new TagHelperContext(new Dictionary<string, object>());
var tagHelperContext = new TagHelperContext(new Dictionary<string, object>(), uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
attributes: new Dictionary<string, string>