From 6f60a5eccd44e8f9073bba3b95b37406ffd5ed90 Mon Sep 17 00:00:00 2001 From: Mike Marynowski Date: Thu, 7 Mar 2024 05:32:02 -0500 Subject: [PATCH] Refactor Add tests to generic MoneySetTests --- .../ImmutableSortedMoneySetTests/Add.cs | 95 --------- .../MoneySetTests/Add.cs | 200 ++++++++++++++++++ .../SortedMoneySetTests/Add.cs | 95 --------- 3 files changed, 200 insertions(+), 190 deletions(-) delete mode 100644 Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/Add.cs create mode 100644 Tests/Singulink.Globalization.Currency.Tests/MoneySetTests/Add.cs delete mode 100644 Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/Add.cs diff --git a/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/Add.cs b/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/Add.cs deleted file mode 100644 index 0e2c948..0000000 --- a/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/Add.cs +++ /dev/null @@ -1,95 +0,0 @@ -namespace Singulink.Globalization.Tests.ImmutableSortedMoneySetTests; - -[PrefixTestClass] -public class Add -{ - 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/MoneySetTests/Add.cs b/Tests/Singulink.Globalization.Currency.Tests/MoneySetTests/Add.cs new file mode 100644 index 0000000..24cccb9 --- /dev/null +++ b/Tests/Singulink.Globalization.Currency.Tests/MoneySetTests/Add.cs @@ -0,0 +1,200 @@ +namespace Singulink.Globalization.Tests.MoneySetTests; + +public static class Add +{ + [PrefixTestClass] + public class Set : Tests { } + + [PrefixTestClass] + public class SortedSet : Tests { } + + [PrefixTestClass] + public class ImmutableSet : ImmutableTests { } + + [PrefixTestClass] + public class ImmutableSortedSet : ImmutableTests { } + + [PrefixTestClass] + public class Tests where T : IMoneySet + { + 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 ImmutableSet = [Usd100, Cad50]; + + private readonly IMoneySet _set = T.Create(CurrencyRegistry.Default, ImmutableSet); + + [TestMethod] + public void AddMoney_CurrencyExists_UpdatesValue() + { + _set.Add(Usd100); + _set.Count.ShouldBe(2); + _set.ShouldBe([Cad50, Money.Create(200m, "USD")], ignoreOrder: true); + } + + [TestMethod] + public void AddMoney_NewCurrency_AddsValue() + { + _set.Add(Eur25); + _set.Count.ShouldBe(3); + _set.ShouldBe([Cad50, Eur25, Usd100], ignoreOrder: true); + } + + [TestMethod] + public void AddMoney_DefaultValue_NoChange() + { + _set.Add(default); + _set.Count.ShouldBe(2); + _set.ShouldBe(ImmutableSet, ignoreOrder: true); + } + + [TestMethod] + public void AddMoney_CurrencyDisallowed_ThrowsArgumentException() + { + var value = new Money(100, DisallowedCurrency); + Should.Throw(() => _set.Add(value)); + } + + /////////////////////////// + + [TestMethod] + public void AddByCurrencyCode_CurrencyExists_UpdatesValue() + { + _set.Add(100, "USD"); + _set.Count.ShouldBe(2); + _set.ShouldBe([Cad50, new(200m, "USD")], ignoreOrder: true); + } + + [TestMethod] + public void AddByCurrencyCode_NewCurrency_AddsValue() + { + _set.Add(25m, "EUR"); + _set.Count.ShouldBe(3); + _set.ShouldBe([Cad50, Eur25, Usd100], ignoreOrder: true); + } + + [TestMethod] + public void AddByCurrencyCode_CurrencyDisallowed_ThrowsArgumentException() + { + Should.Throw(() => _set.Add(100m, DisallowedCurrency.CurrencyCode)); + } + + /////////////////////////// + + [TestMethod] + public void AddByCurrency_CurrencyExists_UpdatesValue() + { + _set.Add(100m, Currency.Get("USD")); + _set.Count.ShouldBe(2); + _set.ShouldBe([Cad50, new(200m, "USD")], ignoreOrder: true); + } + + [TestMethod] + public void AddByCurrency_NewCurrency_AddsValue() + { + _set.Add(25m, Currency.Get("EUR")); + _set.Count.ShouldBe(3); + _set.ShouldBe([Cad50, Eur25, Usd100], ignoreOrder: true); + } + + [TestMethod] + public void AddByCurrency_CurrencyDisallowed_ThrowsArgumentException() + { + _set.Count.ShouldBe(2); + Should.Throw(() => _set.Add(100m, DisallowedCurrency)); + } + } + + [PrefixTestClass] + public class ImmutableTests where T : IImmutableMoneySet + { + 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 IImmutableMoneySet Set = T.Create(CurrencyRegistry.Default, [Usd100, Cad50]); + + [TestMethod] + public void AddMoney_CurrencyExists_UpdatesValue() + { + var resultSet = Set.Add(Usd100); + + resultSet.Count.ShouldBe(2); + resultSet.ShouldBe([Money.Create(200m, "USD"), Cad50], ignoreOrder: true); + } + + [TestMethod] + public void AddMoney_NewCurrency_AddsValue() + { + var resultSet = Set.Add(Eur25); + + resultSet.Count.ShouldBe(3); + resultSet.ShouldBe([Cad50, Eur25, Usd100], ignoreOrder: true); + } + + [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)); + } + + /////////////////////////// + + [TestMethod] + public void AddByCurrencyCode_CurrencyExists_UpdatesValue() + { + var resultSet = Set.Add(100, "USD"); + resultSet.Count.ShouldBe(2); + resultSet.ShouldBe([Cad50, new(200m, "USD")], ignoreOrder: true); + } + + [TestMethod] + public void AddByCurrencyCode_NewCurrency_AddsValue() + { + var resultSet = Set.Add(25m, "EUR"); + resultSet.Count.ShouldBe(3); + resultSet.ShouldBe([Cad50, Eur25, Usd100], ignoreOrder: true); + } + + [TestMethod] + public void AddByCurrencyCode_CurrencyDisallowed_ThrowsArgumentException() + { + Should.Throw(() => Set.Add(100m, DisallowedCurrency.CurrencyCode)); + } + + /////////////////////////// + + [TestMethod] + public void AddByCurrency_CurrencyExists_UpdatesValue() + { + var resultSet = Set.Add(100m, Currency.Get("USD")); + resultSet.Count.ShouldBe(2); + resultSet.ShouldBe([Cad50, new(200m, "USD")], ignoreOrder: true); + } + + [TestMethod] + public void AddByCurrency_NewCurrency_AddsValue() + { + var resultSet = Set.Add(25m, Currency.Get("EUR")); + resultSet.Count.ShouldBe(3); + resultSet.ShouldBe([Cad50, Eur25, Usd100], ignoreOrder: true); + } + + [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/SortedMoneySetTests/Add.cs b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/Add.cs deleted file mode 100644 index 54c3d84..0000000 --- a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/Add.cs +++ /dev/null @@ -1,95 +0,0 @@ -namespace Singulink.Globalization.Tests.SortedMoneySetTests; - -[PrefixTestClass] -public class Add -{ - 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 ImmutableSet = [Usd100, Cad50]; - - private readonly SortedMoneySet _set = ImmutableSet.ToSortedMoneySet(); - - // public void Add(Money value) tests - - [TestMethod] - public void AddMoney_CurrencyExists_UpdatesValue() - { - _set.Add(Usd100); - _set.Count.ShouldBe(2); - _set.ShouldBe([Cad50, Money.Create(200m, "USD")]); - } - - [TestMethod] - public void AddMoney_NewCurrency_AddsValue() - { - _set.Add(Eur25); - _set.Count.ShouldBe(3); - _set.ShouldBe([Cad50, Eur25, Usd100]); - } - - [TestMethod] - public void AddMoney_DefaultValue_NoChange() - { - _set.Add(default); - _set.Count.ShouldBe(2); - _set.ShouldBe(ImmutableSet); - } - - [TestMethod] - public void AddMoney_CurrencyDisallowed_ThrowsArgumentException() - { - 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() - { - _set.Add(100, "USD"); - _set.Count.ShouldBe(2); - _set.ShouldBe([Cad50, new(200m, "USD")]); - } - - [TestMethod] - public void AddByCurrencyCode_NewCurrency_AddsValue() - { - _set.Add(25m, "EUR"); - _set.Count.ShouldBe(3); - _set.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() - { - _set.Add(100m, Currency.Get("USD")); - _set.Count.ShouldBe(2); - _set.ShouldBe([Cad50, new(200m, "USD")]); - } - - [TestMethod] - public void AddByCurrency_NewCurrency_AddsValue() - { - _set.Add(25m, Currency.Get("EUR")); - _set.Count.ShouldBe(3); - _set.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