Test TagHelperRunner, TagHelperOutput and TagHelperExecutionContext.
#154
This commit is contained in:
parent
4378f9613e
commit
b1a3c8c1d4
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
|
namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
|
||||||
{
|
{
|
||||||
public class Valid_PlainTagHelper : ITagHelper
|
public class Valid_PlainTagHelper : TagHelper
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -11,19 +11,19 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SingleAttributeTagHelper : ITagHelper
|
public class SingleAttributeTagHelper : TagHelper
|
||||||
{
|
{
|
||||||
public int IntAttribute { get; set; }
|
public int IntAttribute { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MissingAccessorTagHelper : ITagHelper
|
public class MissingAccessorTagHelper : TagHelper
|
||||||
{
|
{
|
||||||
public string ValidAttribute { get; set; }
|
public string ValidAttribute { get; set; }
|
||||||
public string InvalidNoGetAttribute { set { } }
|
public string InvalidNoGetAttribute { set { } }
|
||||||
public string InvalidNoSetAttribute { get { return string.Empty; } }
|
public string InvalidNoSetAttribute { get { return string.Empty; } }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PrivateAccessorTagHelper : ITagHelper
|
public class PrivateAccessorTagHelper : TagHelper
|
||||||
{
|
{
|
||||||
public string ValidAttribute { get; set; }
|
public string ValidAttribute { get; set; }
|
||||||
public string InvalidPrivateSetAttribute { get; private set; }
|
public string InvalidPrivateSetAttribute { get; private set; }
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,212 @@
|
||||||
|
// 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 Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
|
||||||
|
{
|
||||||
|
public class TagHelperOutputTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void TagName_CannotSetToNullInCtor()
|
||||||
|
{
|
||||||
|
// Arrange & Act
|
||||||
|
var tagHelperOutput = new TagHelperOutput(null);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Empty(tagHelperOutput.TagName);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void TagName_CannotSetToNull()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var tagHelperOutput = new TagHelperOutput("p");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
tagHelperOutput.TagName = null;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Empty(tagHelperOutput.TagName);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Content_CannotSetToNull()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var tagHelperOutput = new TagHelperOutput("p");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
tagHelperOutput.Content = null;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Empty(tagHelperOutput.Content);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GenerateStartTag_ReturnsFullStartTag()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var tagHelperOutput = new TagHelperOutput("p", attributes:
|
||||||
|
new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "class", "btn" },
|
||||||
|
{ "something", " spaced " }
|
||||||
|
});
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var output = tagHelperOutput.GenerateStartTag();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("<p class=\"btn\" something=\" spaced \">", output);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GenerateStartTag_ReturnsNoAttributeStartTag()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var tagHelperOutput = new TagHelperOutput("p");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var output = tagHelperOutput.GenerateStartTag();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("<p>", output);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GenerateStartTag_ReturnsSelfClosingStartTag_Attributes()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var tagHelperOutput = new TagHelperOutput("p",
|
||||||
|
attributes: new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "class", "btn" },
|
||||||
|
{ "something", " spaced " }
|
||||||
|
});
|
||||||
|
|
||||||
|
tagHelperOutput.SelfClosing = true;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var output = tagHelperOutput.GenerateStartTag();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("<p class=\"btn\" something=\" spaced \" />", output);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GenerateStartTag_ReturnsSelfClosingStartTag_NoAttributes()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var tagHelperOutput = new TagHelperOutput("p");
|
||||||
|
|
||||||
|
tagHelperOutput.SelfClosing = true;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var output = tagHelperOutput.GenerateStartTag();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("<p />", output);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GenerateStartTag_ReturnsNothingIfWhitespaceTagName()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var tagHelperOutput = new TagHelperOutput(" ",
|
||||||
|
attributes: new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "class", "btn" },
|
||||||
|
{ "something", " spaced " }
|
||||||
|
});
|
||||||
|
|
||||||
|
tagHelperOutput.SelfClosing = true;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var output = tagHelperOutput.GenerateStartTag();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Empty(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GenerateEndTag_ReturnsNothingIfWhitespaceTagName()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var tagHelperOutput = new TagHelperOutput(" "); ;
|
||||||
|
|
||||||
|
tagHelperOutput.Content = "Hello World";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var output = tagHelperOutput.GenerateEndTag();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Empty(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GenerateContent_ReturnsContent()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var tagHelperOutput = new TagHelperOutput("p");
|
||||||
|
|
||||||
|
tagHelperOutput.Content = "Hello World";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var output = tagHelperOutput.GenerateContent();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("Hello World", output);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GenerateContent_ReturnsNothingIfSelfClosing()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var tagHelperOutput = new TagHelperOutput("p")
|
||||||
|
{
|
||||||
|
SelfClosing = true
|
||||||
|
};
|
||||||
|
|
||||||
|
tagHelperOutput.Content = "Hello World";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var output = tagHelperOutput.GenerateContent();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Empty(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GenerateEndTag_ReturnsEndTag()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var tagHelperOutput = new TagHelperOutput("p");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var output = tagHelperOutput.GenerateEndTag();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("</p>", output);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GenerateEndTag_ReturnsNothingIfSelfClosing()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var tagHelperOutput = new TagHelperOutput("p")
|
||||||
|
{
|
||||||
|
SelfClosing = true
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var output = tagHelperOutput.GenerateEndTag();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Empty(output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,109 @@
|
||||||
|
// 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.IO;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
|
||||||
|
{
|
||||||
|
public class TagHelperRunnerTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task RunAsync_ProcessesAllTagHelpers()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var runner = new TagHelperRunner();
|
||||||
|
var executionContext = new TagHelpersExecutionContext("p");
|
||||||
|
var executableTagHelper1 = new ExecutableTagHelper();
|
||||||
|
var executableTagHelper2 = new ExecutableTagHelper();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
executionContext.Add(executableTagHelper1);
|
||||||
|
executionContext.Add(executableTagHelper2);
|
||||||
|
await runner.RunAsync(executionContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.True(executableTagHelper1.Processed);
|
||||||
|
Assert.True(executableTagHelper2.Processed);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RunAsync_AllowsModificationOfTagHelperOutput()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var runner = new TagHelperRunner();
|
||||||
|
var executionContext = new TagHelpersExecutionContext("p");
|
||||||
|
var executableTagHelper = new ExecutableTagHelper();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
executionContext.Add(executableTagHelper);
|
||||||
|
executionContext.AddHtmlAttribute("class", "btn");
|
||||||
|
var output = await runner.RunAsync(executionContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("foo", output.TagName);
|
||||||
|
Assert.Equal("somethingelse", output.Attributes["class"]);
|
||||||
|
Assert.Equal("world", output.Attributes["hello"]);
|
||||||
|
Assert.Equal(true, output.SelfClosing);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RunAsync_AllowsDataRetrievalFromTagHelperContext()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var runner = new TagHelperRunner();
|
||||||
|
var executionContext = new TagHelpersExecutionContext("p");
|
||||||
|
var tagHelper = new TagHelperContextTouchingTagHelper();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
executionContext.Add(tagHelper);
|
||||||
|
executionContext.AddTagHelperAttribute("foo", true);
|
||||||
|
var output = await runner.RunAsync(executionContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("True", output.Attributes["foo"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RunAsync_WithContentSetsOutputsContent()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var runner = new TagHelperRunner();
|
||||||
|
var executionContext = new TagHelpersExecutionContext("p");
|
||||||
|
var tagHelper = new ExecutableTagHelper();
|
||||||
|
var contentWriter = new StringWriter(new StringBuilder("Hello World"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
executionContext.Add(tagHelper);
|
||||||
|
var output = await runner.RunAsync(executionContext, contentWriter);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(output.Content, "Hello World");
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ExecutableTagHelper : TagHelper
|
||||||
|
{
|
||||||
|
public bool Processed { get; set; }
|
||||||
|
|
||||||
|
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
Processed = true;
|
||||||
|
|
||||||
|
output.TagName = "foo";
|
||||||
|
output.Attributes["class"] = "somethingelse";
|
||||||
|
output.Attributes["hello"] = "world";
|
||||||
|
output.SelfClosing = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TagHelperContextTouchingTagHelper : TagHelper
|
||||||
|
{
|
||||||
|
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
output.Attributes["foo"] = context.AllAttributes["foo"].ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,7 @@ using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
|
namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
|
||||||
|
|
@ -122,18 +123,34 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
|
||||||
|
|
||||||
public class Invalid_NestedPublicTagHelper : ITagHelper
|
public class Invalid_NestedPublicTagHelper : ITagHelper
|
||||||
{
|
{
|
||||||
|
public Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
return Task.FromResult(result: true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class Invalid_NestedInternalTagHelper : ITagHelper
|
internal class Invalid_NestedInternalTagHelper : ITagHelper
|
||||||
{
|
{
|
||||||
|
public Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
return Task.FromResult(result: true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Invalid_PrivateTagHelper : ITagHelper
|
private class Invalid_PrivateTagHelper : ITagHelper
|
||||||
{
|
{
|
||||||
|
public Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
return Task.FromResult(result: true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected class Invalid_ProtectedTagHelper : ITagHelper
|
protected class Invalid_ProtectedTagHelper : ITagHelper
|
||||||
{
|
{
|
||||||
|
public Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
return Task.FromResult(result: true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -141,13 +158,25 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
|
||||||
// In this case they do not fulfill other TagHelper requirements.
|
// In this case they do not fulfill other TagHelper requirements.
|
||||||
public abstract class Invalid_AbstractTagHelper : ITagHelper
|
public abstract class Invalid_AbstractTagHelper : ITagHelper
|
||||||
{
|
{
|
||||||
|
public Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
return Task.FromResult(result: true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Invalid_GenericTagHelper<T> : ITagHelper
|
public class Invalid_GenericTagHelper<T> : ITagHelper
|
||||||
{
|
{
|
||||||
|
public Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
return Task.FromResult(result: true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class Invalid_InternalTagHelper : ITagHelper
|
internal class Invalid_InternalTagHelper : ITagHelper
|
||||||
{
|
{
|
||||||
|
public Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
return Task.FromResult(result: true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,91 @@
|
||||||
|
// 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.Linq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
|
||||||
|
{
|
||||||
|
public class TagHelpersExecutionContextTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void AddHtmlAttribute_MaintainsHTMLAttributes()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var executionContext = new TagHelpersExecutionContext("p");
|
||||||
|
var expectedAttributes = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "class", "btn" },
|
||||||
|
{ "foo", "bar" }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
executionContext.AddHtmlAttribute("class", "btn");
|
||||||
|
executionContext.AddHtmlAttribute("foo", "bar");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(expectedAttributes, executionContext.HTMLAttributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void TagHelpersExecutionContext_MaintainsAllAttributes()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var executionContext = new TagHelpersExecutionContext("p");
|
||||||
|
var expectedAttributes = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
{ "class", "btn" },
|
||||||
|
{ "something", true },
|
||||||
|
{ "foo", "bar" }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
executionContext.AddHtmlAttribute("class", "btn");
|
||||||
|
executionContext.AddTagHelperAttribute("something", true);
|
||||||
|
executionContext.AddHtmlAttribute("foo", "bar");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(expectedAttributes, executionContext.AllAttributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Add_MaintainsTagHelpers()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var executionContext = new TagHelpersExecutionContext("p");
|
||||||
|
var tagHelper = new PTagHelper();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
executionContext.Add(tagHelper);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var singleTagHelper = Assert.Single(executionContext.TagHelpers);
|
||||||
|
Assert.Same(tagHelper, singleTagHelper);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Add_MaintainsMultipleTagHelpers()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var executionContext = new TagHelpersExecutionContext("p");
|
||||||
|
var tagHelper1 = new PTagHelper();
|
||||||
|
var tagHelper2 = new PTagHelper();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
executionContext.Add(tagHelper1);
|
||||||
|
executionContext.Add(tagHelper2);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var tagHelpers = executionContext.TagHelpers.ToArray();
|
||||||
|
Assert.Equal(2, tagHelpers.Length);
|
||||||
|
Assert.Same(tagHelper1, tagHelpers[0]);
|
||||||
|
Assert.Same(tagHelper2, tagHelpers[1]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private class PTagHelper : TagHelper
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue