Skip to content

Commit

Permalink
Merge pull request #25 from IowaComputerGurus/feature/int-format-support
Browse files Browse the repository at this point in the history
Added support for format of int. #16
  • Loading branch information
mitchelsellers authored Nov 14, 2022
2 parents 4369546 + 4ec9b21 commit 5989495
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object, object>(),
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>(tagHelperContent);
});
}
//Assert
Assert.Equal(tagName, output.TagName);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

Loading

0 comments on commit 5989495

Please sign in to comment.