Skip to content

Commit

Permalink
Add Subtract impl + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GiuraEmanuel96 committed Mar 4, 2024
1 parent 4445b16 commit 821ed8d
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Source/Singulink.Globalization.Currency/SortedMoneySet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,28 @@ public void SetAmount(decimal amount, Currency currency)
_amountLookup[currency] = amount;
}

/// <summary>
/// Subtracts the specified value from this set. Zero amounts are not trimmed from the set.
/// </summary>
/// <remarks>
/// Default values that are not associated with any currency are ignored.
/// </remarks>
public void Subtract(Money value) => Add(-value);

/// <summary>
/// Subtracts the specified currency and amount from this set.
/// </summary>
public void Subtract(decimal amount, string currencyCode) => Add(-amount, currencyCode);

/// <summary>
/// Adds the specified currency and amount to this set.
/// </summary>
public void Subtract(decimal amount, Currency currency)
{
EnsureCurrencyAllowed(currency, nameof(currency));
AddInternal(amount, currency);
}

/// <summary>
/// Copies the values in this set to a new immutable set that uses the same registry as this set.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Shouldly;

namespace Singulink.Globalization.Tests.SortedMoneySetTests;

[TestClass]
public class SubtractTests
{
private static readonly Money Usd100 = new(100m, "USD");
private static readonly Money Cad50 = new(50m, "CAD");
private static readonly Money Eur25 = new(25m, "EUR");
private static readonly Money Aud75 = new(75m, "AUD");
private static readonly ImmutableSortedMoneySet ImmutableSet = [Usd100, Cad50, Eur25];

private readonly SortedMoneySet _set = ImmutableSet.ToSet();

// public void Subtract(Money value) tests

[TestMethod]
public void Money_CurrencyExists_SubtractsPositiveAmountAndReturnsPositiveAmount()
{
_set.Subtract(Usd100);
_set.Count.ShouldBe(3);
_set.ShouldBe([new(0m, "USD"), Cad50, Eur25]);
}

[TestMethod]
public void MoneyCurrencyExists_SubtractsZeroAmountAndReturnsPositiveAmount()
{
_set.Subtract(new(-0m, "EUR"));
_set.Count.ShouldBe(3);
_set.ShouldBe([Usd100, Cad50, Eur25]);
}

[TestMethod]
public void MoneyCurrencyNotInSet_AddsValueToSet()
{
_set.Subtract(-Aud75);
_set.ShouldBe([Usd100, Cad50, Eur25, Aud75]);
}

[TestMethod]
public void MoneyCurrencyDisallowed_ThrowsArgumentException()
{
var value = new Money(100, new Currency("Blah blah blah", "BBB", "$$", 2));

Should.Throw<ArgumentException>(() => _set.Subtract(value))
.Message.ShouldBe($"The currency '{value.Currency}' is not present in the set's currency registry. (Parameter 'value')");

_set.Count.ShouldBe(3);
_set.ShouldBe([Usd100, Cad50, Eur25]);
}

// public void Subtract(decimal amount, string currencyCode) tests

[TestMethod]
public void CurrencyCodeExists_SubtractsPositiveAmountAndReturnsPositiveAmount()
{
_set.Subtract(100m, "USD");
_set.Count.ShouldBe(3);
_set.ShouldBe([new(0m, "USD"), Cad50, Eur25]);
}

[TestMethod]
public void CurrencyCodeExists_SubtractsZeroAmountAndReturnsPositiveAmount()
{
_set.Subtract(-0m, "EUR");
_set.Count.ShouldBe(3);
_set.ShouldBe([Usd100, Cad50, Eur25]);
}

[TestMethod]
public void CurrencyCodeNotInSet_AddsValueToSet()
{
_set.Subtract(-75m, "AUD");
_set.ShouldBe([Usd100, Cad50, Eur25, Aud75]);
}

[TestMethod]
public void CurrencyCodeDisallowed_ThrowsArgumentException()
{
Should.Throw<ArgumentException>(() => _set.Subtract(100m, "Blah blah blah"))
.Message.ShouldBe("Currency code 'Blah blah blah' not found in the 'System' registry. (Parameter 'currencyCode')");

_set.Count.ShouldBe(3);
_set.ShouldBe([Usd100, Cad50, Eur25]);
}

// public void Subtract(decimal amount, Currency currency) tests

[TestMethod]
public void CurrencyExists_SubtractsPositiveAmountAndReturnsPositiveAmount()
{
_set.Subtract(-100m, Currency.Get("USD"));
_set.Count.ShouldBe(3);
_set.ShouldBe([new(0m, "USD"), Cad50, Eur25]);
}

[TestMethod]
public void CurrencyExists_SubtractsZeroAmountAndReturnsPositiveAmount()
{
_set.Subtract(-0m, Currency.Get("EUR"));
_set.Count.ShouldBe(3);
_set.ShouldBe([Usd100, Cad50, Eur25]);
}

[TestMethod]
public void CurrencyNotInSet_AddsValueToSet()
{
_set.Subtract(75m, Currency.Get("AUD"));
_set.ShouldBe([Usd100, Cad50, Eur25, Aud75]);
}

[TestMethod]
public void CurrencyDisallowed_ThrowsArgumentException()
{
var disallowedCurrency = new Currency("XXX", "Non-existent currency", "X", 2);

Should.Throw<ArgumentException>(() => _set.Subtract(100m, disallowedCurrency))
.Message.ShouldBe($"The currency '{disallowedCurrency}' is not present in the set's currency registry. (Parameter 'currency')");

_set.Count.ShouldBe(3);
_set.ShouldBe([Usd100, Cad50, Eur25]);
}
}

0 comments on commit 821ed8d

Please sign in to comment.