diff --git a/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests.csproj b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests.csproj index 3406dd1..49a25aa 100644 --- a/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests.csproj +++ b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests.csproj @@ -8,6 +8,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/BoolFontAwesomeIconTagHelperTests.cs b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/BoolFontAwesomeIconTagHelperTests.cs index f8328ac..1ca0b88 100644 --- a/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/BoolFontAwesomeIconTagHelperTests.cs +++ b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/BoolFontAwesomeIconTagHelperTests.cs @@ -4,90 +4,60 @@ using Microsoft.AspNetCore.Razor.TagHelpers; using Xunit; -namespace ICG.AspNetCore.Utilities.FontAwesomeTagHelpers.Tests +namespace ICG.AspNetCore.Utilities.FontAwesomeTagHelpers.Tests; + +public class BoolFontAwesomeIconTagHelperTests : TagHelperTestBase { - public class BoolFontAwesomeIconTagHelperTests + [Theory] + [InlineData(false, "fas fa-times text-danger")] + [InlineData(true, "fas fa-check text-success")] + public void ShouldRenderProperIconClass_WithDefaultConfiguration(bool value, string expectedClass) { - [Theory] - [InlineData(false, "fas fa-times text-danger")] - [InlineData(true, "fas fa-check text-success")] - public void ShouldRenderProperIconClass_WithDefaultConfiguration(bool value, string expectedClass) - { - //Arrange - var context = MakeTagHelperContext(); - var output = MakeTagHelperOutput(" "); - - //Act - var helper = new BoolFontAwesomeIconTagHelper{Value = value}; - helper.Process(context, output); - - //Assert - Assert.Equal("span", output.TagName); - Assert.Equal(expectedClass, output.Attributes["class"].Value); - } - - [Theory] - [InlineData(false, "fas fa-circle text-success", "fas fa-circle text-warning", "fas fa-circle text-warning")] - [InlineData(true, "fas fa-circle text-success", "fas fa-circle text-warning", "fas fa-circle text-success")] - public void ShouldRenderProperIconClass_WithCustomConfiguration(bool value, string yesClass, string noClass, - string expectedClass) - { - //Arrange - var context = MakeTagHelperContext(); - var output = MakeTagHelperOutput(" "); - - //Act - var helper = new BoolFontAwesomeIconTagHelper{ Value = value, FalseIconClass = noClass, TrueIconClass = yesClass}; - helper.Process(context, output); + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); - //Assert - Assert.Equal("span", output.TagName); - Assert.Equal(expectedClass, output.Attributes["class"].Value); - } + //Act + var helper = new BoolFontAwesomeIconTagHelper{Value = value}; + helper.Process(context, output); - [Theory] - [InlineData(false, "i")] - public void ShouldRenderProperTagName_WithCustomTagName(bool value, string tagName) - { - //Arrange - var context = MakeTagHelperContext(); - var output = MakeTagHelperOutput(" "); + //Assert + Assert.Equal("span", output.TagName); + Assert.Equal(expectedClass, output.Attributes["class"].Value); + } - //Act - var helper = new BoolFontAwesomeIconTagHelper { Value = value, Tag = tagName }; - helper.Process(context, output); + [Theory] + [InlineData(false, "fas fa-circle text-success", "fas fa-circle text-warning", "fas fa-circle text-warning")] + [InlineData(true, "fas fa-circle text-success", "fas fa-circle text-warning", "fas fa-circle text-success")] + public void ShouldRenderProperIconClass_WithCustomConfiguration(bool value, string yesClass, string noClass, + string expectedClass) + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); - //Assert - Assert.Equal(tagName, output.TagName); - } + //Act + var helper = new BoolFontAwesomeIconTagHelper{ Value = value, FalseIconClass = noClass, TrueIconClass = yesClass}; + helper.Process(context, output); - private TagHelperContext MakeTagHelperContext(TagHelperAttributeList attributes = null) - { - attributes = attributes ?? new TagHelperAttributeList(); + //Assert + Assert.Equal("span", output.TagName); + Assert.Equal(expectedClass, output.Attributes["class"].Value); + } - return new TagHelperContext( - tagName: "span", - allAttributes: attributes, - items: new Dictionary(), - uniqueId: Guid.NewGuid().ToString("N")); - } + [Theory] + [InlineData(false, "i")] + public void ShouldRenderProperTagName_WithCustomTagName(bool value, string tagName) + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); - private TagHelperOutput MakeTagHelperOutput( - string tagName, - TagHelperAttributeList attributes = null, - string childContent = null) - { - attributes = attributes ?? new TagHelperAttributeList(); + //Act + var helper = new BoolFontAwesomeIconTagHelper { Value = value, Tag = tagName }; + helper.Process(context, output); - return new TagHelperOutput( - tagName, - attributes, - getChildContentAsync: (useCachedResult, encoder) => - { - var tagHelperContent = new DefaultTagHelperContent(); - tagHelperContent.SetContent(childContent); - return Task.FromResult(tagHelperContent); - }); - } + //Assert + Assert.Equal(tagName, output.TagName); } } diff --git a/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/DateTimeFontAwesomeTagHelperTests.cs b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/DateTimeFontAwesomeTagHelperTests.cs new file mode 100644 index 0000000..926ee28 --- /dev/null +++ b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/DateTimeFontAwesomeTagHelperTests.cs @@ -0,0 +1,94 @@ +using Microsoft.AspNetCore.Razor.TagHelpers; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Xunit; + +namespace ICG.AspNetCore.Utilities.FontAwesomeTagHelpers.Tests; + +public class DateTimeFontAwesomeTagHelperTests : TagHelperTestBase +{ + [Fact] + public void ShouldRenderProperIconClass_WhenNull_WithDefaultConfiguration() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var expectedClass = "fas fa-minus"; + + //Act + var helper = new DateTimeFontAwesomeIconTagHelper { Value = null }; + helper.Process(context, output); + + //Assert + Assert.Equal("span", output.TagName); + Assert.Equal(expectedClass, output.Attributes["class"].Value); + } + + [Fact] + public void ShouldRenderProperIconClass_WhenNull_WithCustomConfiguration() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var expectedClass = "fas fa-times"; + + //Act + var helper = new DateTimeFontAwesomeIconTagHelper { Value = null, NullValueIconClass = expectedClass }; + helper.Process(context, output); + + //Assert + Assert.Equal("span", output.TagName); + Assert.Equal(expectedClass, output.Attributes["class"].Value); + } + + [Fact] + public void ShouldRenderProperTagName_WithCustomTagName() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var tagName = "i"; + + //Act + var helper = new DateTimeFontAwesomeIconTagHelper { Value = null, Tag = tagName }; + helper.Process(context, output); + + //Assert + Assert.Equal(tagName, output.TagName); + } + + [Fact] + public void SHouldRenderProperValue_WhenNotNull() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var value = new DateTime(2022, 11, 15); + + //Act + var helper = new DateTimeFontAwesomeIconTagHelper { Value = value }; + helper.Process(context, output); + + //Assert + Assert.Equal(value.ToString(), output.PostContent.GetContent()); + } + + [Fact] + public void SHouldRenderProperValue_WhenNotNull_WithFormatString() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var value = new DateTime(2022, 11, 15); + var formatString = "MMdd"; + var expectedValue = "1115"; + + //Act + var helper = new DateTimeFontAwesomeIconTagHelper { Value = value, Format = formatString }; + helper.Process(context, output); + + //Assert + Assert.Equal(expectedValue, output.PostContent.GetContent()); + } +} diff --git a/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/DecimalFontAwesomeTagHelperTests.cs b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/DecimalFontAwesomeTagHelperTests.cs new file mode 100644 index 0000000..4e48089 --- /dev/null +++ b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/DecimalFontAwesomeTagHelperTests.cs @@ -0,0 +1,79 @@ +using Microsoft.AspNetCore.Razor.TagHelpers; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Xunit; + +namespace ICG.AspNetCore.Utilities.FontAwesomeTagHelpers.Tests; + +public class DecimalFontAwesomeTagHelperTests : TagHelperTestBase +{ + [Fact] + public void ShouldRenderProperIconClass_WhenNull_WithDefaultConfiguration() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var expectedClass = "fas fa-minus"; + + //Act + var helper = new DecimalFontAwesomeIconTagHelper { Value = null }; + helper.Process(context, output); + + //Assert + Assert.Equal("span", output.TagName); + Assert.Equal(expectedClass, output.Attributes["class"].Value); + } + + [Fact] + public void ShouldRenderProperIconClass_WhenNull_WithCustomConfiguration() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var expectedClass = "fas fa-times"; + + //Act + var helper = new DecimalFontAwesomeIconTagHelper { Value = null, NullValueIconClass = expectedClass }; + helper.Process(context, output); + + //Assert + Assert.Equal("span", output.TagName); + Assert.Equal(expectedClass, output.Attributes["class"].Value); + } + + [Fact] + public void ShouldRenderProperTagName_WithCustomTagName() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var tagName = "i"; + + //Act + var helper = new DecimalFontAwesomeIconTagHelper { Value = null, Tag = tagName }; + helper.Process(context, output); + + //Assert + Assert.Equal(tagName, output.TagName); + } + + [Theory] + [InlineData(100, "", "100")] + [InlineData(1000, "", "1000")] + [InlineData(1000, "N1", "1,000.0")] + public void SHouldRenderProperValue_WhenNotNull(decimal value, string formatString, string expectedValue) + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + + //Act + var helper = new DecimalFontAwesomeIconTagHelper { Value = value, Format = formatString }; + helper.Process(context, output); + + //Assert + Assert.Equal(expectedValue, output.PostContent.GetContent()); + } +} + diff --git a/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/IntFontAwesomeTagHelperTests.cs b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/IntFontAwesomeTagHelperTests.cs new file mode 100644 index 0000000..83b69a1 --- /dev/null +++ b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/IntFontAwesomeTagHelperTests.cs @@ -0,0 +1,78 @@ +using Microsoft.AspNetCore.Razor.TagHelpers; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Xunit; + +namespace ICG.AspNetCore.Utilities.FontAwesomeTagHelpers.Tests; + +public class IntFontAwesomeTagHelperTests : TagHelperTestBase +{ + [Fact] + public void ShouldRenderProperIconClass_WhenNull_WithDefaultConfiguration() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var expectedClass = "fas fa-minus"; + + //Act + var helper = new IntFontAwesomeIconTagHelper { Value = null }; + helper.Process(context, output); + + //Assert + Assert.Equal("span", output.TagName); + Assert.Equal(expectedClass, output.Attributes["class"].Value); + } + + [Fact] + public void ShouldRenderProperIconClass_WhenNull_WithCustomConfiguration() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var expectedClass = "fas fa-times"; + + //Act + var helper = new IntFontAwesomeIconTagHelper { Value = null, NullValueIconClass = expectedClass }; + helper.Process(context, output); + + //Assert + Assert.Equal("span", output.TagName); + Assert.Equal(expectedClass, output.Attributes["class"].Value); + } + + [Fact] + public void ShouldRenderProperTagName_WithCustomTagName() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var tagName = "i"; + + //Act + var helper = new IntFontAwesomeIconTagHelper { Value = null, Tag = tagName }; + helper.Process(context, output); + + //Assert + Assert.Equal(tagName, output.TagName); + } + + [Theory] + [InlineData(100, "", "100")] + [InlineData(1000, "", "1000")] + [InlineData(1000, "N0", "1,000")] + public void SHouldRenderProperValue_WhenNotNull(int value, string formatString, string expectedValue) + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + + //Act + var helper = new IntFontAwesomeIconTagHelper { Value = value, Format = formatString }; + helper.Process(context, output); + + //Assert + Assert.Equal(expectedValue, output.PostContent.GetContent()); + } +} diff --git a/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/StringFontAwesomeTagHelperTests.cs b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/StringFontAwesomeTagHelperTests.cs new file mode 100644 index 0000000..16aaee2 --- /dev/null +++ b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/StringFontAwesomeTagHelperTests.cs @@ -0,0 +1,78 @@ +using Microsoft.AspNetCore.Razor.TagHelpers; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace ICG.AspNetCore.Utilities.FontAwesomeTagHelpers.Tests; + +public class StringFontAwesomeTagHelperTests : TagHelperTestBase +{ + [Fact] + public void ShouldRenderProperIconClass_WhenNull_WithDefaultConfiguration() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var expectedClass = "fas fa-minus"; + + //Act + var helper = new StringFontAwesomeIconTagHelper { Value = null }; + helper.Process(context, output); + + //Assert + Assert.Equal("span", output.TagName); + Assert.Equal(expectedClass, output.Attributes["class"].Value); + } + + [Fact] + public void ShouldRenderProperIconClass_WhenNull_WithCustomConfiguration() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var expectedClass = "fas fa-times"; + + //Act + var helper = new StringFontAwesomeIconTagHelper { Value = null, NullValueIconClass = expectedClass }; + helper.Process(context, output); + + //Assert + Assert.Equal("span", output.TagName); + Assert.Equal(expectedClass, output.Attributes["class"].Value); + } + + [Fact] + public void ShouldRenderProperTagName_WithCustomTagName() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var tagName = "i"; + + //Act + var helper = new StringFontAwesomeIconTagHelper { Value = null, Tag = tagName }; + helper.Process(context, output); + + //Assert + Assert.Equal(tagName, output.TagName); + } + + [Fact] + public void SHouldRenderProperValue_WhenNotNull() + { + //Arrange + var context = MakeTagHelperContext(); + var output = MakeTagHelperOutput(" "); + var value = "My Value"; + + //Act + var helper = new StringFontAwesomeIconTagHelper { Value = value}; + helper.Process(context, output); + + //Assert + Assert.Equal(value, output.PostContent.GetContent()); + } +} diff --git a/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/TagHelperTestBase.cs b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/TagHelperTestBase.cs new file mode 100644 index 0000000..e709ccc --- /dev/null +++ b/src/AspNetCore.Utilities.FontAwesomeTagHelpers.Tests/TagHelperTestBase.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.Razor.TagHelpers; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace ICG.AspNetCore.Utilities.FontAwesomeTagHelpers.Tests; + +public abstract class TagHelperTestBase +{ + public TagHelperContext MakeTagHelperContext(TagHelperAttributeList attributes = null) + { + attributes = attributes ?? new TagHelperAttributeList(); + + return new TagHelperContext( + tagName: "span", + allAttributes: attributes, + items: new Dictionary(), + uniqueId: Guid.NewGuid().ToString("N")); + } + + public TagHelperOutput MakeTagHelperOutput( + string tagName, + TagHelperAttributeList attributes = null, + string childContent = null) + { + attributes = attributes ?? new TagHelperAttributeList(); + + return new TagHelperOutput( + tagName, + attributes, + getChildContentAsync: (useCachedResult, encoder) => + { + var tagHelperContent = new DefaultTagHelperContent(); + tagHelperContent.SetContent(childContent); + return Task.FromResult(tagHelperContent); + }); + } +} diff --git a/src/AspNetCore.Utilities.FontAwesomeTagHelpers/IntFontAwesomeIconTagHelper.cs b/src/AspNetCore.Utilities.FontAwesomeTagHelpers/IntFontAwesomeIconTagHelper.cs index b1f1f4d..b66f018 100644 --- a/src/AspNetCore.Utilities.FontAwesomeTagHelpers/IntFontAwesomeIconTagHelper.cs +++ b/src/AspNetCore.Utilities.FontAwesomeTagHelpers/IntFontAwesomeIconTagHelper.cs @@ -28,6 +28,11 @@ public class IntFontAwesomeIconTagHelper : TagHelper /// public string NullValueIconClass { get; set; } = "fas fa-minus"; + /// + /// An optional format string for display + /// + public string Format { get; set; } = ""; + /// /// Renders the tag as desired /// @@ -40,7 +45,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output) { output.TagName = null; output.TagMode = TagMode.StartTagAndEndTag; - output.PostContent.SetContent(Value.Value.ToString()); + output.PostContent.SetContent(Value.Value.ToString(Format)); } else {