Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
denisvieira-dev committed Jul 4, 2023
1 parent 3273cfe commit 3abd919
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
30 changes: 27 additions & 3 deletions src/Nox.Types/Types/Percentage/Percentage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,31 @@ namespace Nox.Types;
/// </summary>
public sealed class Percentage : ValueObject<float, Percentage>
{
private readonly PercentageTypeOptions _percentageTypeOptions = new();
private PercentageTypeOptions _percentageTypeOptions = new();

/// <summary>
/// Creates a new instance of <see cref="Percentage"/> class with the specified values.
/// </summary>
/// <param name="value">The value to create the <see cref="Percentage"/> with</param>
/// <returns></returns>
/// <exception cref="TypeValidationException"></exception>
public static Percentage From(float value, PercentageTypeOptions options)
{
var newObject = new Percentage
{
Value = value,
_percentageTypeOptions = options
};

var validationResult = newObject.Validate();

if (!validationResult.IsValid)
{
throw new TypeValidationException(validationResult.Errors);
}

return newObject;
}

/// <summary>
/// Validates a <see cref="Percentage"/> object.
Expand All @@ -18,12 +42,12 @@ internal override ValidationResult Validate()
{
var result = base.Validate();

if (Value < _percentageTypeOptions.MinValue)
if (Value < _percentageTypeOptions.MinValue && !float.IsNaN(Value) && !float.IsInfinity(Value))
{
result.Errors.Add(new ValidationFailure(nameof(Value), $"Could not create a Nox Percentage type as value {Value} is less than than the minimum specified value of {_percentageTypeOptions.MinValue}"));
}

if (Value > _percentageTypeOptions.MaxValue)
if (Value > _percentageTypeOptions.MaxValue && !float.IsNaN(Value) && !float.IsInfinity(Value))
{
result.Errors.Add(new ValidationFailure(nameof(Value), $"Could not create a Nox Percentage type a value {Value} is greater than than the maximum specified value of {_percentageTypeOptions.MaxValue}"));
}
Expand Down
52 changes: 43 additions & 9 deletions tests/Nox.Types.Tests/Types/Percentage/PercentageTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Nox.Types.Tests.Types;
using FluentAssertions;
using System.Globalization;

namespace Nox.Types.Tests.Types;

public class PercentageTests
{
Expand All @@ -9,27 +12,30 @@ public void Percentage_Constructor_ReturnsSameValue()

var number = Percentage.From(testPercentage);

Assert.Equal(testPercentage, number.Value);
number.Value.Should().Be(testPercentage);
}

[Fact]
public void Percentage_Constructor_ThrowsException_WhenValueExceedsMaxAllowed()
{
var testPercentage = 3.2f;

Assert.Throws<TypeValidationException>(() => _ =
Percentage.From(testPercentage)
);
var action = () => Percentage.From(testPercentage);

action.Should().Throw<TypeValidationException>()
.And.Errors.Should().BeEquivalentTo(new[] { new ValidationFailure("Value", "Could not create a Nox Percentage type a value 3.2 is greater than than the maximum specified value of 1") });

}

[Fact]
public void Percentage_Constructor_ThrowsException_WhenValueIsLessThanMinAllowed()
{
var testPercentage = -0.3f;

Assert.Throws<TypeValidationException>(() => _ =
Percentage.From(testPercentage)
);
var action = () => Percentage.From(testPercentage);

action.Should().Throw<TypeValidationException>()
.And.Errors.Should().BeEquivalentTo(new[] { new ValidationFailure("Value", "Could not create a Nox Percentage type as value -0.3 is less than than the minimum specified value of 0") });
}

[Fact]
Expand All @@ -39,7 +45,7 @@ public void Percentage_Constructor_RoundsFloatValues_WhenConstructedWithFloatInp

var percentage = Percentage.From(testPercentage);

Assert.Equal(0.4f, percentage.Value);
percentage.Value.Should().Be(0.4f);
}

[Fact]
Expand All @@ -58,4 +64,32 @@ void Test()

TestUtility.RunInInvariantCulture(Test);
}

[Theory]
[InlineData("en-US")]
[InlineData("pt-PT")]
public void Percentage_ValueInFloat_ToString_IsCultureIndepdendent(string culture)
{
void Test()
{
var percentage = Percentage.From(0.25f);
percentage.ToString().Should().Be("25%");
}

TestUtility.RunInCulture(Test, culture);
}

[Theory]
[InlineData("en-US", "0.43%")]
[InlineData("pt-PT", "0,43%")]
public void Percentage_ValueInFloat_ToString_IsCultureDependent(string culture, string expected)
{
void Test()
{
var percentage = Percentage.From(0.43f);
percentage.ToString(new CultureInfo(culture)).Should().Be(expected);
}

TestUtility.RunInCulture(Test, culture);
}
}

0 comments on commit 3abd919

Please sign in to comment.