From ef804e91590f79a31a521dcfaed9c58e37f6fcdd Mon Sep 17 00:00:00 2001 From: "giura.paul@singulink.com" Date: Mon, 4 Mar 2024 12:51:42 +0200 Subject: [PATCH] Add tests for ImmutableSortedMoneySet --- .../ImmutableSortedMoneySetTests/AddTests.cs | 99 +++++++++++++++++++ .../RemoveTests.cs | 66 +++++++++++++ .../SetAmountTests.cs | 90 +++++++++++++++++ .../SetValueTests.cs | 43 ++++++++ 4 files changed, 298 insertions(+) create mode 100644 Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/AddTests.cs create mode 100644 Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/RemoveTests.cs create mode 100644 Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/SetAmountTests.cs create mode 100644 Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/SetValueTests.cs diff --git a/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/AddTests.cs b/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/AddTests.cs new file mode 100644 index 0000000..c1aa5a2 --- /dev/null +++ b/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/AddTests.cs @@ -0,0 +1,99 @@ +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Shouldly; + +namespace Singulink.Globalization.Tests.ImmutableSortedMoneySetTests; + +[TestClass] +public class AddTests +{ + 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 Currency DisallowedCurrency = new("Blah blah blah", "BBB", "$$", 2); + private static readonly ImmutableSortedMoneySet Set = [Usd100, Cad50]; + + // public void Add(Money value) tests + + [TestMethod] + public void AddMoney_CurrencyExists_UpdatesValue() + { + var resultSet = Set.Add(Usd100); + + resultSet.Count.ShouldBe(2); + resultSet.ShouldBe(ImmutableSortedMoneySet.Create(Money.Create(200m, "USD"), Cad50)); + } + + [TestMethod] + public void AddMoney_NewCurrency_AddsValue() + { + var resultSet = Set.Add(Eur25); + + resultSet.Count.ShouldBe(3); + resultSet.ShouldBe(ImmutableSortedMoneySet.Create(Cad50, Eur25, Usd100)); + } + + [TestMethod] + public void AddMoney_DefaultValue_NoChange() + { + var resultSet = Set.Add(default); + + resultSet.ShouldBeSameAs(Set); + } + + [TestMethod] + public void AddMoney_CurrencyDisallowed_ThrowsException() + { + var value = new Money(100, DisallowedCurrency); + Should.Throw(() => Set.Add(value)); + } + + // public void Add(decimal amount, string currencyCode) tests + + [TestMethod] + public void AddByCurrencyCode_CurrencyExists_UpdatesValue() + { + var resultSet = Set.Add(100, "USD"); + resultSet.Count.ShouldBe(2); + resultSet.ShouldBe([Cad50, new(200m, "USD")]); + } + + [TestMethod] + public void AddByCurrencyCode_NewCurrency_AddsValue() + { + var resultSet = Set.Add(25m, "EUR"); + resultSet.Count.ShouldBe(3); + resultSet.ShouldBe([Cad50, Eur25, Usd100]); + } + + [TestMethod] + public void AddByCurrencyCode_CurrencyDisallowed_ThrowsArgumentException() + { + Should.Throw(() => Set.Add(100m, DisallowedCurrency.CurrencyCode)); + } + + // public void Add(decimal amount, Currency currency) tests + + [TestMethod] + public void AddByCurrency_CurrencyExists_UpdatesValue() + { + var resultSet = Set.Add(100m, Currency.Get("USD")); + resultSet.Count.ShouldBe(2); + resultSet.ShouldBe([Cad50, new(200m, "USD")]); + } + + [TestMethod] + public void AddByCurrency_NewCurrency_AddsValue() + { + var resultSet = Set.Add(25m, Currency.Get("EUR")); + resultSet.Count.ShouldBe(3); + resultSet.ShouldBe([Cad50, Eur25, Usd100]); + } + + [TestMethod] + public void AddByCurrency_CurrencyDisallowed_ThrowsArgumentException() + { + Set.Count.ShouldBe(2); + Should.Throw(() => Set.Add(100m, DisallowedCurrency)); + } +} \ No newline at end of file diff --git a/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/RemoveTests.cs b/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/RemoveTests.cs new file mode 100644 index 0000000..12730a5 --- /dev/null +++ b/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/RemoveTests.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Shouldly; + +namespace Singulink.Globalization.Tests.ImmutableSortedMoneySetTests +{ + [TestClass] + public class RemoveTests + { + 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 ImmutableSortedMoneySet Set = [Usd100, Cad50, Eur25]; + + // public bool Remove(string currencyCode) tests + + [TestMethod] + public void RemoveCurrencyCode_CurrencyFound_RemovesValue() + { + var resultSet = Set.Remove("USD"); + resultSet.Count.ShouldBe(2); + resultSet.ShouldBe([Cad50, Eur25]); + } + + [TestMethod] + public void RemoveCurrencyCode_CurrencyNotFound_NoChange() + { + var resultSet = Set.Remove("JPY"); + resultSet.ShouldBeSameAs(Set); + } + + [TestMethod] + public void RemoveCurrencyCode_DisallowedCurrency_ThrowsArgumentException() + { + Should.Throw(() => Set.Remove("XXX")); + } + + // public bool Remove(Currency currency) tests + + [TestMethod] + public void RemoveCurrency_CurrencyFound_RemovesValue() + { + var resultSet = Set.Remove(Usd100.Currency); + resultSet.Count.ShouldBe(2); + resultSet.ShouldBe([Cad50, Eur25]); + } + + [TestMethod] + public void RemoveCurrency_CurrencyNotFound_NoChange() + { + var gbpCurrency = Currency.Get("GBP"); + var resultSet = Set.Remove(gbpCurrency); + resultSet.ShouldBeSameAs(Set); + } + + [TestMethod] + public void RemoveCurrency_DisallowedCurrency_ThrowsArgumentException() + { + var disallowedCurrency = new Currency("XXX", "Disallowed currency", "X", 2); + Should.Throw(() => Set.Remove(disallowedCurrency)); + } + } +} \ No newline at end of file diff --git a/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/SetAmountTests.cs b/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/SetAmountTests.cs new file mode 100644 index 0000000..c83c75c --- /dev/null +++ b/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/SetAmountTests.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Shouldly; + +namespace Singulink.Globalization.Tests.ImmutableSortedMoneySetTests; + +[TestClass] +public class SetAmountTests +{ + private static readonly Currency Usd = Currency.Get("USD"); + private static readonly Currency Aud = Currency.Get("AUD"); + private static readonly Currency DisallowedCurrency = new Currency("Blah blah blah", "BBB", "$$", 2); + + private static readonly Money Usd0 = new(0m, "USD"); + 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 Set = [Usd100, Cad50, Eur25]; + + // public void SetAmount(decimal amount, string currencyCode) tests + + [TestMethod] + public void SetByCurrencyCode_CurrencyExists_UpdatesValue() + { + var resultSet = Set.SetAmount(200m, "USD"); + resultSet.Count.ShouldBe(3); + resultSet.ShouldBe([Money.Create(200m, "USD"), Cad50, Eur25]); + } + + [TestMethod] + public void SetByCurrencyCode_NewCurrency_AddsValue() + { + var resultSet = Set.SetAmount(75m, "AUD"); + resultSet.Count.ShouldBe(4); + resultSet.ShouldBe([Cad50, Eur25, Usd100, Aud75]); + } + + [TestMethod] + public void SetByCurrencyCode_ZeroAmount_ZeroesValue() + { + var resultSet = Set.SetAmount(0, "USD"); + resultSet.Count.ShouldBe(3); + resultSet.ShouldBe([Usd0, Cad50, Eur25]); + } + + [TestMethod] + public void SetByCurrencyCode_CurrencyDisallowed_ThrowsArgumentException() + { + Should.Throw(() => Set.SetAmount(123m, DisallowedCurrency.CurrencyCode)); + } + + // public void SetAmount(decimal amount, Currency currency) tests + + [TestMethod] + + public void SetByCurrency_CurrencyExists_UpdatesValue() + { + var resultSet = Set.SetAmount(200m, Usd); + resultSet.Count.ShouldBe(3); + resultSet.ShouldBe([new(200m, "USD"), Cad50, Eur25]); + } + + [TestMethod] + public void SetByCurrency_CurrencyDoesNotExist_AddsValue() + { + var resultSet = Set.SetAmount(75m, Aud); + resultSet.Count.ShouldBe(4); + resultSet.ShouldBe([Cad50, Eur25, Usd100, Aud75]); + } + + [TestMethod] + public void SetByCurrency_ZeroAmount_ZeroesValue() + { + var resultSet = Set.SetAmount(0, Usd); + resultSet.Count.ShouldBe(3); + resultSet.ShouldBe([Usd0, Cad50, Eur25]); + } + + [TestMethod] + public void SetByCurrency_CurrencyDisallowed_ThrowsArgumentException() + { + Should.Throw(() => Set.SetAmount(123m, DisallowedCurrency)); + } +} \ No newline at end of file diff --git a/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/SetValueTests.cs b/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/SetValueTests.cs new file mode 100644 index 0000000..1a360fb --- /dev/null +++ b/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/SetValueTests.cs @@ -0,0 +1,43 @@ +using Shouldly; + +namespace Singulink.Globalization.Tests.ImmutableSortedMoneySetTests; + +[TestClass] +public class SetValueTests +{ + 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 Set = [Usd100, Cad50, Eur25]; + + [TestMethod] + public void SetValue_CurrencyDoesNotExist_AddsValue() + { + var resultSet = Set.SetValue(Aud75); + resultSet.Count.ShouldBe(4); + resultSet.ShouldBe([Cad50, Eur25, Usd100, Aud75]); + } + + [TestMethod] + public void SetValue_CurrencyExists_UpdatesValue() + { + var resultSet = Set.SetValue(new(200m, "USD")); + resultSet.Count.ShouldBe(3); + resultSet.ShouldBe([new(200m, "USD"), Cad50, Eur25]); + } + + [TestMethod] + public void SetValue_DefaultValue_NoChange() + { + var resultSet = Set.SetValue(default); + resultSet.ShouldBeSameAs(Set); + } + + [TestMethod] + public void SetValue_CurrencyIsNotAccepted_ThrowsArgumentException() + { + var value = new Money(100, new Currency("Blah blah blah", "BBB", "$$", 2)); + Should.Throw(() => Set.SetValue(value)); + } +} \ No newline at end of file