From 7c601233e2f6f8b766262b6bc279c3603abf025e Mon Sep 17 00:00:00 2001 From: Mike Marynowski Date: Mon, 4 Mar 2024 00:18:52 -0500 Subject: [PATCH] Epic cleanup --- .editorconfig | 12 +- .../Money.ToString.cs | 10 +- .../RoundToCurrencyDigitsTests.cs | 38 +++--- .../TryGetAmountTests.cs | 18 +-- .../MoneyTests/CompareToTests.cs | 11 +- .../MoneyTests/OperatorsTests.cs | 120 +++++++++--------- .../MoneyTests/RoundToCurrencyDigitsTests.cs | 34 ++--- .../MoneyTests/ToStringTests.cs | 10 +- .../SortedMoneySetTests/AddRangeTests.cs | 33 ++--- .../SortedMoneySetTests/AddTests.cs | 64 +++++----- .../SortedMoneySetTests/RemoveAllTests.cs | 72 ++++++----- .../SortedMoneySetTests/RemoveTests.cs | 37 +++--- .../SortedMoneySetTests/SetAmountTests.cs | 70 +++++----- .../SortedMoneySetTests/SetValueTests.cs | 21 +-- .../SortedMoneySetTests/TryGetAmountTests.cs | 11 +- .../SortedMoneySetTests/TryGetValueTests.cs | 49 ++++--- 16 files changed, 315 insertions(+), 295 deletions(-) diff --git a/.editorconfig b/.editorconfig index 6363997..630687f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -184,9 +184,9 @@ dotnet_naming_rule.types_should_be_pascalcase.severity = warning dotnet_naming_rule.types_should_be_pascalcase.symbols = types dotnet_naming_rule.types_should_be_pascalcase.style = pascalcase -dotnet_naming_rule.private_static_field_should_be_s_prefixed_camelcase.severity = suggestion +dotnet_naming_rule.private_static_field_should_be_s_prefixed_camelcase.severity = none dotnet_naming_rule.private_static_field_should_be_s_prefixed_camelcase.symbols = private_static_field -dotnet_naming_rule.private_static_field_should_be_s_prefixed_camelcase.style = s_prefixed_camelcase +dotnet_naming_rule.private_static_field_should_be_s_prefixed_camelcase.style = prefixed_camelcase dotnet_naming_rule.private_field_should_be_prefixed_camelcase.severity = warning dotnet_naming_rule.private_field_should_be_prefixed_camelcase.symbols = private_field @@ -236,11 +236,6 @@ dotnet_naming_style.prefixed_camelcase.required_suffix = dotnet_naming_style.prefixed_camelcase.word_separator = dotnet_naming_style.prefixed_camelcase.capitalization = camel_case -dotnet_naming_style.s_prefixed_camelcase.required_prefix = s_ -dotnet_naming_style.s_prefixed_camelcase.required_suffix = -dotnet_naming_style.s_prefixed_camelcase.word_separator = -dotnet_naming_style.s_prefixed_camelcase.capitalization = camel_case - dotnet_naming_style.begins_with_i.required_prefix = I dotnet_naming_style.begins_with_i.required_suffix = dotnet_naming_style.begins_with_i.word_separator = @@ -372,3 +367,6 @@ dotnet_diagnostic.SA1519.severity = silent # SA1214: Readonly fields should appear before non-readonly fields dotnet_diagnostic.SA1214.severity = suggestion + +# IDE0028: Simplify collection initialization +dotnet_diagnostic.IDE0028.severity = warning \ No newline at end of file diff --git a/Source/Singulink.Globalization.Currency/Money.ToString.cs b/Source/Singulink.Globalization.Currency/Money.ToString.cs index 1756e0d..ad71e79 100644 --- a/Source/Singulink.Globalization.Currency/Money.ToString.cs +++ b/Source/Singulink.Globalization.Currency/Money.ToString.cs @@ -19,8 +19,8 @@ private static readonly ImmutableArray NegativeInternationalPatterns private static readonly ImmutableArray NegativeReverseInternationalPatterns = ["(n) $", "-n $", "-n $", "n- $", "(n) $", "-n $", "n- $", "n- $", "-n $", "-n $", "n- $", "n- $", "-n $", "n- $", "(n) $", "(n) $"]; - private static readonly ConditionalWeakTable s_regionInfoLookup = []; - private static readonly ConditionalWeakTable s_absNumberFormatInfoLookup = []; + private static readonly ConditionalWeakTable _regionInfoLookup = []; + private static readonly ConditionalWeakTable _absNumberFormatInfoLookup = []; /// /// Returns a string representation of this value's currency and amount. @@ -204,7 +204,7 @@ static NumberFormatInfo GetAbsNumberFormatInfo(IFormatProvider? formatProvider) { var formatInfo = NumberFormatInfo.GetInstance(formatProvider); - return s_absNumberFormatInfoLookup.GetValue(formatInfo, static fi => new NumberFormatInfo { + return _absNumberFormatInfoLookup.GetValue(formatInfo, static fi => new NumberFormatInfo { NumberDecimalDigits = fi.CurrencyDecimalDigits, NumberDecimalSeparator = fi.CurrencyDecimalSeparator, NumberGroupSeparator = fi.CurrencyGroupSeparator, @@ -216,7 +216,7 @@ static RegionInfo GetRegion(CultureInfo culture) { RegionInfo region = null; - if ((culture.CultureTypes & CultureTypes.SpecificCultures) != 0 && !s_regionInfoLookup.TryGetValue(culture, out region)) + if ((culture.CultureTypes & CultureTypes.SpecificCultures) != 0 && !_regionInfoLookup.TryGetValue(culture, out region)) { try { @@ -224,7 +224,7 @@ static RegionInfo GetRegion(CultureInfo culture) } catch { } - s_regionInfoLookup.AddOrUpdate(culture, region); + _regionInfoLookup.AddOrUpdate(culture, region); } return region ?? RegionInfo.CurrentRegion; diff --git a/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/RoundToCurrencyDigitsTests.cs b/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/RoundToCurrencyDigitsTests.cs index 2902c8f..03e6c17 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/RoundToCurrencyDigitsTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/RoundToCurrencyDigitsTests.cs @@ -5,38 +5,42 @@ namespace Singulink.Globalization.Tests.ImmutableSortedMoneySetTests; [TestClass] public class RoundToCurrencyDigitsTests -{ - private static readonly ImmutableSortedMoneySet _roundDownResult = [new Money(10, "USD")]; - private static readonly ImmutableSortedMoneySet _roundDownValue = [new Money(10.004m, "USD")]; - private static readonly ImmutableSortedMoneySet _midpointValue = [new Money(10.005m, "USD")]; - private static readonly ImmutableSortedMoneySet _roundUpValue = [new Money(10.006m, "USD")]; - private static readonly ImmutableSortedMoneySet _roundUpResult = [new Money(10.01m, "USD")]; +{ +#pragma warning disable SA1025 // Code should not contain multiple whitespace in a row + private static readonly ImmutableSortedMoneySet RoundDownResults = [new(10.000m, "USD"), new(6.0m, "JPY")]; + private static readonly ImmutableSortedMoneySet RoundDownValues = [new(10.004m, "USD"), new(6.2m, "JPY")]; + private static readonly ImmutableSortedMoneySet MidpointValues = [new(10.005m, "USD"), new(6.5m, "JPY")]; + private static readonly ImmutableSortedMoneySet RoundUpValues = [new(10.006m, "USD"), new(6.7m, "JPY")]; + private static readonly ImmutableSortedMoneySet RoundUpResults = [new(10.010m, "USD"), new(7.0m, "JPY")]; +#pragma warning restore SA1025 [TestMethod] public void ToEven() { const MidpointRounding mode = MidpointRounding.ToEven; - _roundDownResult.RoundToCurrencyDigits(mode).ShouldBe(_roundDownResult); - _roundDownValue.RoundToCurrencyDigits(mode).ShouldBe(_roundDownResult); - _midpointValue.RoundToCurrencyDigits(mode).ShouldBe(_roundDownResult); - _roundUpValue.RoundToCurrencyDigits(mode).ShouldBe(_roundUpResult); - _roundUpResult.RoundToCurrencyDigits(mode).ShouldBe(_roundUpResult); + + RoundDownResults.RoundToCurrencyDigits(mode).ShouldBe(RoundDownResults); + RoundDownValues.RoundToCurrencyDigits(mode).ShouldBe(RoundDownResults); + MidpointValues.RoundToCurrencyDigits(mode).ShouldBe(RoundDownResults); + RoundUpValues.RoundToCurrencyDigits(mode).ShouldBe(RoundUpResults); + RoundUpResults.RoundToCurrencyDigits(mode).ShouldBe(RoundUpResults); } [TestMethod] public void AwayFromZero() { const MidpointRounding mode = MidpointRounding.AwayFromZero; - _roundDownResult.RoundToCurrencyDigits(mode).ShouldBe(_roundDownResult); - _roundDownValue.RoundToCurrencyDigits(mode).ShouldBe(_roundDownResult); - _midpointValue.RoundToCurrencyDigits(mode).ShouldBe(_roundUpResult); - _roundUpValue.RoundToCurrencyDigits(mode).ShouldBe(_roundUpResult); - _roundUpResult.RoundToCurrencyDigits(mode).ShouldBe(_roundUpResult); + + RoundDownResults.RoundToCurrencyDigits(mode).ShouldBe(RoundDownResults); + RoundDownValues.RoundToCurrencyDigits(mode).ShouldBe(RoundDownResults); + MidpointValues.RoundToCurrencyDigits(mode).ShouldBe(RoundUpResults); + RoundUpValues.RoundToCurrencyDigits(mode).ShouldBe(RoundUpResults); + RoundUpResults.RoundToCurrencyDigits(mode).ShouldBe(RoundUpResults); } [TestMethod] public void Default() { - _roundDownResult.RoundToCurrencyDigits().ShouldBe(_roundDownResult); + RoundDownResults.RoundToCurrencyDigits().ShouldBe(RoundDownResults); } } \ No newline at end of file diff --git a/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/TryGetAmountTests.cs b/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/TryGetAmountTests.cs index 2309467..1c3f25f 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/TryGetAmountTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/ImmutableSortedMoneySetTests/TryGetAmountTests.cs @@ -5,33 +5,33 @@ namespace Singulink.Globalization.Tests.ImmutableSortedMoneySetTests; [TestClass] public class TryGetAmountTests { - 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]; + 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]; [TestMethod] public void AmountExists_ReturnsTrueAndOutputsAmount() { - _set.TryGetAmount("USD", out decimal amount).ShouldBeTrue(); + Set.TryGetAmount("USD", out decimal amount).ShouldBeTrue(); amount.ShouldBe(100m); - _set.TryGetAmount("CAD", out amount).ShouldBeTrue(); + Set.TryGetAmount("CAD", out amount).ShouldBeTrue(); amount.ShouldBe(50m); - _set.TryGetAmount("EUR", out amount).ShouldBeTrue(); + Set.TryGetAmount("EUR", out amount).ShouldBeTrue(); amount.ShouldBe(25m); } [TestMethod] public void AmountDoesNotExist_ReturnsFalse() { - _set.TryGetAmount("GBP", out _).ShouldBeFalse(); + Set.TryGetAmount("GBP", out _).ShouldBeFalse(); } [TestMethod] public void CurrencyDoesNotExist_ThrowsArgumentException() { - Should.Throw(() => _set.TryGetAmount("AAA", out _)); + Should.Throw(() => Set.TryGetAmount("AAA", out _)); } } \ No newline at end of file diff --git a/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/CompareToTests.cs b/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/CompareToTests.cs index 423bca0..a9e8847 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/CompareToTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/CompareToTests.cs @@ -5,27 +5,30 @@ namespace Singulink.Globalization.Tests.MoneyTests; [TestClass] public class CompareToTests { + private static readonly Money Usd100 = new(100m, "USD"); + private static readonly Money Usd200 = new(200m, "USD"); + [TestMethod] public void LessThan_MinusOneResult() { - new Money(100m, "USD").CompareTo(new Money(200m, "USD")).ShouldBe(-1); + Usd100.CompareTo(Usd200).ShouldBe(-1); } [TestMethod] public void GreaterThan_PlusOneResult() { - new Money(200m, "USD").CompareTo(new Money(100m, "USD")).ShouldBe(1); + Usd200.CompareTo(Usd100).ShouldBe(1); } [TestMethod] public void Equal_ZeroResult() { - new Money(100m, "USD").CompareTo(new Money(100m, "USD")).ShouldBe(0); + Usd100.CompareTo(Usd100).ShouldBe(0); } [TestMethod] public void DifferentCurrency_ThrowsArgumentException() { - Should.Throw(() => new Money(100m, "USD").CompareTo(new Money(200m, "CAD"))); + Should.Throw(() => Usd100.CompareTo(new Money(200m, "CAD"))); } } \ No newline at end of file diff --git a/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/OperatorsTests.cs b/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/OperatorsTests.cs index 2391a9c..67c06ad 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/OperatorsTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/OperatorsTests.cs @@ -7,331 +7,331 @@ namespace Singulink.Globalization.Tests.MoneyTests; [TestClass] public class OperatorsTests { - private static readonly Money _usdMinus200 = new(-200m, "USD"); - private static readonly Money _usd0 = new(0m, "USD"); - private static readonly Money _usd100 = new(100m, "USD"); - private static readonly Money _usd200 = new(200m, "USD"); - private static readonly Money _usd300 = new(300m, "USD"); - private static readonly Money _cad100 = new(100m, "CAD"); - private static readonly Money _cad200 = new(200m, "CAD"); + private static readonly Money UsdMinus200 = new(-200m, "USD"); + private static readonly Money Usd0 = new(0m, "USD"); + private static readonly Money Usd100 = new(100m, "USD"); + private static readonly Money Usd200 = new(200m, "USD"); + private static readonly Money Usd300 = new(300m, "USD"); + private static readonly Money Cad100 = new(100m, "CAD"); + private static readonly Money Cad200 = new(200m, "CAD"); [TestMethod] public void Equal_EqualValues_ReturnsTrue() { - (_usd100 == _usd100).ShouldBeTrue(); + (Usd100 == Usd100).ShouldBeTrue(); } [TestMethod] public void Equal_DifferentValues_ReturnsFalse() { - (_usd100 == _usd200).ShouldBeFalse(); + (Usd100 == Usd200).ShouldBeFalse(); } [TestMethod] public void Inequality_DifferentValues_ReturnsTrue() { - (_usd100 != _usd200).ShouldBeTrue(); + (Usd100 != Usd200).ShouldBeTrue(); } [TestMethod] public void Inequality_DifferentValues_ReturnsFalse() { - (_usd100 != _usd100).ShouldBeFalse(); + (Usd100 != Usd100).ShouldBeFalse(); } [TestMethod] public void LessThan_LessThan_ReturnsTrue() { - (_usd100 < _usd200).ShouldBeTrue(); + (Usd100 < Usd200).ShouldBeTrue(); } [TestMethod] public void LessThan_GreaterThan_ReturnsFalse() { - (_usd200 < _usd100).ShouldBeFalse(); + (Usd200 < Usd100).ShouldBeFalse(); } [TestMethod] public void LessThan_EqualValues_ReturnsFalse() { - (_usd100 < _usd100).ShouldBeFalse(); + (Usd100 < Usd100).ShouldBeFalse(); } [TestMethod] public void LessThan_DifferentCurrency_ThrowsArgumentException() { - Should.Throw(() => _usd200 < _cad100); + Should.Throw(() => Usd200 < Cad100); } [TestMethod] public void GreaterThan_GreaterThan_ReturnsTrue() { - (_usd200 > _usd100).ShouldBeTrue(); + (Usd200 > Usd100).ShouldBeTrue(); } [TestMethod] public void GreaterThan_LessThan_ReturnsFalse() { - (_usd100 > _usd200).ShouldBeFalse(); + (Usd100 > Usd200).ShouldBeFalse(); } [TestMethod] public void GreaterThan_EqualValues_ReturnsFalse() { - (_usd100 > _usd100).ShouldBeFalse(); + (Usd100 > Usd100).ShouldBeFalse(); } [TestMethod] public void GreaterThan_DifferentCurrency_ThrowsArgumentException() { - Should.Throw(() => _usd200 > _cad100); + Should.Throw(() => Usd200 > Cad100); } [TestMethod] public void LessThanOrEqual_LessThan_ReturnsTrue() { - (_usd100 <= _usd200).ShouldBeTrue(); + (Usd100 <= Usd200).ShouldBeTrue(); } [TestMethod] public void LessThanOrEqual_Equal_ReturnsTrue() { - (_usd100 <= _usd100).ShouldBeTrue(); + (Usd100 <= Usd100).ShouldBeTrue(); } [TestMethod] public void LessThanOrEqual_GreaterThan_ReturnsFalse() { - (_usd200 <= _usd100).ShouldBeFalse(); + (Usd200 <= Usd100).ShouldBeFalse(); } [TestMethod] public void LessThanOrEqual_DifferentCurrency_ThrowsArgumentException() { - Should.Throw(() => _usd100 <= _cad200); + Should.Throw(() => Usd100 <= Cad200); } [TestMethod] public void GreaterThanOrEqual_GreaterThan_ReturnsTrue() { - (_usd200 >= _usd100).ShouldBeTrue(); + (Usd200 >= Usd100).ShouldBeTrue(); } [TestMethod] public void GreaterThanOrEqual_Equal_ReturnsTrue() { - (_usd100 >= _usd100).ShouldBeTrue(); + (Usd100 >= Usd100).ShouldBeTrue(); } [TestMethod] public void GreaterThanOrEqual_LessThan_ReturnsFalse() { - (_usd100 >= _usd200).ShouldBeFalse(); + (Usd100 >= Usd200).ShouldBeFalse(); } [TestMethod] public void GreaterThanOrEqual_DifferentCurrency_ThrowsArgumentException() { - Should.Throw(() => _usd100 >= _cad200); + Should.Throw(() => Usd100 >= Cad200); } [TestMethod] public void Addition_SameCurrency_ReturnsCorrectResult() { - (_usd100 + _usd200).ShouldBe(_usd300); + (Usd100 + Usd200).ShouldBe(Usd300); } [TestMethod] public void Addition_DifferentCurrency_ThrowsArgumentException() { - Should.Throw(() => _usd100 + _cad200); + Should.Throw(() => Usd100 + Cad200); } [TestMethod] public void Addition_MoneyAndDecimal_ReturnsCorrectResult() { - (_usd100 + 200m).ShouldBe(_usd300); + (Usd100 + 200m).ShouldBe(Usd300); } [TestMethod] public void Addition_MoneyAndNegativeDecimal_ReturnsCorrectResult() { - (_usd200 + -100m).ShouldBe(_usd100); + (Usd200 + -100m).ShouldBe(Usd100); } [TestMethod] public void Addition_DecimalAndMoney_ReturnsCorrectResult() { - (100m + _usd200).ShouldBe(_usd300); + (100m + Usd200).ShouldBe(Usd300); } [TestMethod] public void Addition_NegativeDecimalAndMoney_ReturnsCorrectResult() { - (-100m + _usd200).ShouldBe(_usd100); + (-100m + Usd200).ShouldBe(Usd100); } [TestMethod] public void Subtraction_SameCurrency_ReturnsCorrectResult() { - (_usd200 - _usd100).ShouldBe(_usd100); + (Usd200 - Usd100).ShouldBe(Usd100); } [TestMethod] public void Subtraction_DifferentCurrency_ThrowsArgumentException() { - Should.Throw(() => _usd200 - _cad100); + Should.Throw(() => Usd200 - Cad100); } [TestMethod] public void Subtraction_MoneyAndDecimal_ReturnsCorrectResult() { - (_usd200 - 100m).ShouldBe(_usd100); + (Usd200 - 100m).ShouldBe(Usd100); } [TestMethod] public void Subtraction_MoneyAndNegativeDecimal_ReturnsCorrectResult() { - (_usd200 - -100m).ShouldBe(_usd300); + (Usd200 - -100m).ShouldBe(Usd300); } [TestMethod] public void Subtraction_DecimalAndMoney_ReturnsCorrectResult() { - (200m - _usd100).ShouldBe(_usd100); + (200m - Usd100).ShouldBe(Usd100); } [TestMethod] public void Subtraction_NegativeDecimalAndMoney_ReturnsCorrectResult() { - (-100m - _usd200).ShouldBe(-_usd300); + (-100m - Usd200).ShouldBe(-Usd300); } [TestMethod] public void Subtraction_DecimalMinusMoney_ReturnsCorrectResult() { - (200m - _usd100).ShouldBe(_usd100); + (200m - Usd100).ShouldBe(Usd100); } [TestMethod] public void Subtraction_NegativeDecimalMinusMoney_ReturnsCorrectResult() { - (-100m - _usd200).ShouldBe(-_usd300); + (-100m - Usd200).ShouldBe(-Usd300); } [TestMethod] public void Multiplication_MoneyAndDecimal_ReturnsCorrectResult() { - (_usd100 * 2m).ShouldBe(_usd200); + (Usd100 * 2m).ShouldBe(Usd200); } [TestMethod] public void Multiplication_MoneyAndNegativeDecimal_ReturnsCorrectResult() { - (_usd100 * -2m).ShouldBe(-_usd200); + (Usd100 * -2m).ShouldBe(-Usd200); } [TestMethod] public void Multiplication_MoneyAndZero_ReturnsZero() { - (_usd100 * 0m).ShouldBe(_usd0); + (Usd100 * 0m).ShouldBe(Usd0); } [TestMethod] public void Multiplication_DecimalAndMoney_ReturnsCorrectResult() { - (2m * _usd100).ShouldBe(_usd200); + (2m * Usd100).ShouldBe(Usd200); } [TestMethod] public void Multiplication_NegativeDecimalAndMoney_ReturnsCorrectResult() { - (-2m * _usd100).ShouldBe(-_usd200); + (-2m * Usd100).ShouldBe(-Usd200); } [TestMethod] public void Multiplication_ZeroAndMoney_ReturnsZero() { - (0m * _usd100).ShouldBe(_usd0); + (0m * Usd100).ShouldBe(Usd0); } [TestMethod] public void Division_MoneyAndDecimal_ReturnsCorrectResult() { - (_usd200 / 2m).ShouldBe(_usd100); + (Usd200 / 2m).ShouldBe(Usd100); } [TestMethod] public void Division_MoneyAndNegativeDecimal_ReturnsCorrectResult() { - (_usd300 / -1.5m).ShouldBe(_usdMinus200); + (Usd300 / -1.5m).ShouldBe(UsdMinus200); } [TestMethod] public void Division_MoneyAndOne_ReturnsSameMoney() { - (_usd100 / 1m).ShouldBe(_usd100); + (Usd100 / 1m).ShouldBe(Usd100); } [TestMethod] public void Division_MoneyAndZero_ThrowsDivideByZeroException() { - Should.Throw(() => _usd100 / 0m); + Should.Throw(() => Usd100 / 0m); } [TestMethod] public void Division_DecimalAndMoney_ReturnsCorrectResult() { - (20000m / _usd100).ShouldBe(_usd200); + (20000m / Usd100).ShouldBe(Usd200); } [TestMethod] public void Division_NegativeDecimalAndMoney_ReturnsCorrectResult() { - (-20000m / _usd100).ShouldBe(_usdMinus200); + (-20000m / Usd100).ShouldBe(UsdMinus200); } [TestMethod] public void Division_DecimalAndZeroMoney_ThrowsDivideByZeroException() { - Should.Throw(() => 200m / _usd0); + Should.Throw(() => 200m / Usd0); } [TestMethod] public void Increment_ReturnsCorrectResult() { - var value = _usd100; + var value = Usd100; (++value).ShouldBe(new Money(101m, "USD")); } [TestMethod] public void Decrement_ReturnsCorrectResult() { - var value = _usd100; + var value = Usd100; (--value).ShouldBe(new Money(99m, "USD")); } [TestMethod] public void UnaryPlus_PositiveValue_ReturnsSameValue() { - (+_usd100).ShouldBe(_usd100); + (+Usd100).ShouldBe(Usd100); } [TestMethod] public void UnaryPlus_NegativeValue_ReturnsSameValue() { - (+_usdMinus200).ShouldBe(_usdMinus200); + (+UsdMinus200).ShouldBe(UsdMinus200); } [TestMethod] public void UnaryMinus_PositiveValue_ReturnsNegativeValue() { - (-_usd200).ShouldBe(_usdMinus200); + (-Usd200).ShouldBe(UsdMinus200); } [TestMethod] public void UnaryMinus_NegativeValue_ReturnsPositiveValue() { - (-_usdMinus200).ShouldBe(_usd200); + (-UsdMinus200).ShouldBe(Usd200); } } \ No newline at end of file diff --git a/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/RoundToCurrencyDigitsTests.cs b/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/RoundToCurrencyDigitsTests.cs index bb43571..3f5e3af 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/RoundToCurrencyDigitsTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/RoundToCurrencyDigitsTests.cs @@ -11,21 +11,22 @@ namespace Singulink.Globalization.Tests.MoneyTests [TestClass] public class RoundToCurrencyDigitsTests { - private static readonly Money _roundDownResult = new Money(10, "USD"); - private static readonly Money _roundDownValue = new Money(10.004m, "USD"); - private static readonly Money _midpointValue = new Money(10.005m, "USD"); - private static readonly Money _roundUpValue = new Money(10.006m, "USD"); - private static readonly Money _roundUpResult = new Money(10.01m, "USD"); + private static readonly Money RoundDownResult = new(10, "USD"); + private static readonly Money RoundDownValue = new(10.004m, "USD"); + private static readonly Money MidpointValue = new(10.005m, "USD"); + private static readonly Money RoundUpValue = new(10.006m, "USD"); + private static readonly Money RoundUpResult = new(10.01m, "USD"); [TestMethod] public void ToEven() { const MidpointRounding mode = MidpointRounding.ToEven; - _roundDownResult.RoundToCurrencyDigits(mode).ShouldBe(_roundDownResult); - _roundDownValue.RoundToCurrencyDigits(mode).ShouldBe(_roundDownResult); - _midpointValue.RoundToCurrencyDigits(mode).ShouldBe(_roundDownResult); - _roundUpValue.RoundToCurrencyDigits(mode).ShouldBe(_roundUpResult); - _roundUpResult.RoundToCurrencyDigits(mode).ShouldBe(_roundUpResult); + + RoundDownResult.RoundToCurrencyDigits(mode).ShouldBe(RoundDownResult); + RoundDownValue.RoundToCurrencyDigits(mode).ShouldBe(RoundDownResult); + MidpointValue.RoundToCurrencyDigits(mode).ShouldBe(RoundDownResult); + RoundUpValue.RoundToCurrencyDigits(mode).ShouldBe(RoundUpResult); + RoundUpResult.RoundToCurrencyDigits(mode).ShouldBe(RoundUpResult); Money.Default.RoundToCurrencyDigits(mode).ShouldBe(Money.Default); } @@ -33,18 +34,19 @@ public void ToEven() public void AwayFromZero() { const MidpointRounding mode = MidpointRounding.AwayFromZero; - _roundDownResult.RoundToCurrencyDigits(mode).ShouldBe(_roundDownResult); - _roundDownValue.RoundToCurrencyDigits(mode).ShouldBe(_roundDownResult); - _midpointValue.RoundToCurrencyDigits(mode).ShouldBe(_roundUpResult); - _roundUpValue.RoundToCurrencyDigits(mode).ShouldBe(_roundUpResult); - _roundUpResult.RoundToCurrencyDigits(mode).ShouldBe(_roundUpResult); + + RoundDownResult.RoundToCurrencyDigits(mode).ShouldBe(RoundDownResult); + RoundDownValue.RoundToCurrencyDigits(mode).ShouldBe(RoundDownResult); + MidpointValue.RoundToCurrencyDigits(mode).ShouldBe(RoundUpResult); + RoundUpValue.RoundToCurrencyDigits(mode).ShouldBe(RoundUpResult); + RoundUpResult.RoundToCurrencyDigits(mode).ShouldBe(RoundUpResult); Money.Default.RoundToCurrencyDigits(mode).ShouldBe(Money.Default); } [TestMethod] public void Default() { - _midpointValue.RoundToCurrencyDigits().ShouldBe(_roundDownResult); + MidpointValue.RoundToCurrencyDigits().ShouldBe(RoundDownResult); } } } \ No newline at end of file diff --git a/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/ToStringTests.cs b/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/ToStringTests.cs index 4087271..97e0fda 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/ToStringTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/MoneyTests/ToStringTests.cs @@ -7,30 +7,30 @@ namespace Singulink.Globalization.Tests.MoneyTests; public class ToStringTests { private const char Nbsp = '\u00A0'; - private static readonly CultureInfo _enUS = CultureInfo.GetCultureInfo("en-US"); + private static readonly CultureInfo EnUS = CultureInfo.GetCultureInfo("en-US"); [TestMethod] public void ToString_DefaultFormat() { - CultureInfo.CurrentCulture = _enUS; + CultureInfo.CurrentCulture = EnUS; new Money(100m, "USD").ToString(null, null).ShouldBe($"USD{Nbsp}100.00"); } [TestMethod] public void ToString_CultureDependentInternational_NumberWithGroupSeparators_DecimalsWithBankerRounding() { - new Money(100m, "USD").ToString("CN$", _enUS).ShouldBe($"USD{Nbsp}100.00"); + new Money(100m, "USD").ToString("CN$", EnUS).ShouldBe($"USD{Nbsp}100.00"); } [TestMethod] public void ToString_InternationalCulture_DigitsWithNoSeparators_NoDecimals() { - new Money(1000.001m, "USD").ToString("ID*", _enUS).ShouldBe($"USD{Nbsp}1000.001"); + new Money(1000.001m, "USD").ToString("ID*", EnUS).ShouldBe($"USD{Nbsp}1000.001"); } [TestMethod] public void ToString_Local_NumberWithGroupSeparators_AwayFromZero() { - new Money(1000.005m, "CAD").ToString("LDF", _enUS).ShouldBe($"CAD{Nbsp}1000.01"); + new Money(1000.005m, "CAD").ToString("LDF", EnUS).ShouldBe($"CAD{Nbsp}1000.01"); } } \ No newline at end of file diff --git a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/AddRangeTests.cs b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/AddRangeTests.cs index 22163bb..fe56caa 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/AddRangeTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/AddRangeTests.cs @@ -5,22 +5,23 @@ namespace Singulink.Globalization.Tests.SortedMoneySetTests; [TestClass] public class AddRangeTests { - 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 _immutableSet = [_usd100, _cad50, _eur25]; - private readonly SortedMoneySet _set = _immutableSet.ToSet(); + 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 ImmutableSet = [Usd100, Cad50, Eur25]; + + private readonly SortedMoneySet _set = ImmutableSet.ToSet(); [TestMethod] - public void AllCurrenciesExistInTheSet_IsSuccessful() + public void AllCurrenciesExist_UpdatesValues() { - _set.AddRange([_usd100, _cad50, _eur25]); + _set.AddRange([Usd100, Cad50, Eur25]); _set.Count.ShouldBe(3); _set.ShouldBe([new(200m, "USD"), new(100m, "CAD"), new(50m, "EUR")]); } [TestMethod] - public void SomeCurrenciesDoNotExistInTheSet_IsSuccessful() + public void SomeNewCurrencies_UpdatesExistingAndAddsNewValues() { _set.AddRange([new(100m, "USD"), new(50m, "JPY"), new(25m, "CHF")]); _set.Count.ShouldBe(5); @@ -28,7 +29,7 @@ public void SomeCurrenciesDoNotExistInTheSet_IsSuccessful() } [TestMethod] - public void NoCurrencyExistsInTheSet_IsSuccessful() + public void AllNewCurrencies_AddsValues() { _set.AddRange([new(100m, "GBP"), new(50m, "JPY"), new(25m, "CHF")]); _set.Count.ShouldBe(6); @@ -38,16 +39,16 @@ public void NoCurrencyExistsInTheSet_IsSuccessful() [TestMethod] public void EmptyCollection_NoChange() { - _set.AddRange(new List()); + _set.AddRange([]); _set.Count.ShouldBe(3); - _set.ShouldBe(_immutableSet); + _set.ShouldBe(ImmutableSet); } [TestMethod] - public void NonExistentCurrency_ThrowsArgumentException() + public void DisallowedCurrency_ThrowsArgumentException() { var disallowedCurrency = new Currency("Disallowed Currency", "XXX", "X", 2); - List values = [new(100m, "USD"), new Money(100m, disallowedCurrency), new Money(100m, disallowedCurrency), new(50m, "CAD"), new(25m, "EUR")]; + IEnumerable values = [new(100m, "USD"), new(100m, disallowedCurrency), new(100m, disallowedCurrency), new(50m, "CAD"), new(25m, "EUR")]; Should.Throw(() => _set.AddRange(values)) .Message.ShouldBe($"The following currencies are not present in the set's currency registry: {disallowedCurrency} (Parameter 'values')"); @@ -56,12 +57,12 @@ public void NonExistentCurrency_ThrowsArgumentException() } [TestMethod] - public void NonExistentCurrencies_ThrowsArgumentException() + public void DisallowedCurrencies_ThrowsArgumentException() { var disallowedCurrencyX = new Currency("Disallowed Currency", "XXX", "X", 2); var disallowedCurrencyY = new Currency("Disallowed Currency2", "YYY", "Y", 2); - List values = [new(100m, "USD"), new Money(100m, disallowedCurrencyX), new Money(100m, disallowedCurrencyX), new(50m, "CAD"), - new Money(100m, disallowedCurrencyY), new Money(100m, disallowedCurrencyY), new(25m, "EUR")]; + IEnumerable values = [new(100m, "USD"), new(100m, disallowedCurrencyX), new(100m, disallowedCurrencyX), new(50m, "CAD"), + new(100m, disallowedCurrencyY), new(100m, disallowedCurrencyY), new(25m, "EUR")]; Should.Throw(() => _set.AddRange(values)) .Message.ShouldBe($"The following currencies are not present in the set's currency registry: {disallowedCurrencyX}, {disallowedCurrencyY} (Parameter 'values')"); diff --git a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/AddTests.cs b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/AddTests.cs index a7bc10a..2a94802 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/AddTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/AddTests.cs @@ -5,28 +5,30 @@ namespace Singulink.Globalization.Tests.SortedMoneySetTests; [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 _bbbCurrency = new("Blah blah blah", "BBB", "$$", 2); - private static readonly ImmutableSortedMoneySet _immutableSet = [_usd100, _cad50]; - private readonly SortedMoneySet _set = _immutableSet.ToSet(); + 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.ToSet(); // public void Add(Money value) tests + [TestMethod] - public void AddMoney_CurrencyExistsInTheSet_IsSuccesful() + public void AddMoney_CurrencyExists_UpdatesValue() { - _set.Add(_usd100); + _set.Add(Usd100); _set.Count.ShouldBe(2); - _set.ShouldBe([_cad50, new Money(200m, "USD")]); + _set.ShouldBe([Cad50, Money.Create(200m, "USD")]); } [TestMethod] - public void AddMoney_CurrencyDoesNotExistInSet_IsSuccessful() + public void AddMoney_NewCurrency_AddsValue() { - _set.Add(_eur25); + _set.Add(Eur25); _set.Count.ShouldBe(3); - _set.ShouldBe([_cad50, _eur25, _usd100]); + _set.ShouldBe([Cad50, Eur25, Usd100]); } [TestMethod] @@ -34,62 +36,62 @@ public void AddMoney_DefaultValue_NoChange() { _set.Add(default); _set.Count.ShouldBe(2); - _set.ShouldBe(_immutableSet); + _set.ShouldBe(ImmutableSet); } [TestMethod] - public void AddMoney_CurrencyIsNotAcceptedInTheSet_ThrowsArgumentException() + public void AddMoney_CurrencyDisallowed_ThrowsArgumentException() { - var value = new Money(100, _bbbCurrency); + var value = new Money(100, DisallowedCurrency); Should.Throw(() => _set.Add(value)); } // public void Add(decimal amount, string currencyCode) tests + [TestMethod] - public void AddAmountCurrencyCode_CurrencyExistsInTheSet_IsSuccesful() + public void AddByCurrencyCode_CurrencyExists_UpdatesValue() { _set.Add(100, "USD"); _set.Count.ShouldBe(2); - _set.ShouldBe([_cad50, new Money(200m, "USD")]); + _set.ShouldBe([Cad50, new(200m, "USD")]); } [TestMethod] - public void AddAmountCurrencyCode_CurrencyDoesNotExistInSet_IsSuccessful() + public void AddByCurrencyCode_NewCurrency_AddsValue() { _set.Add(25m, "EUR"); _set.Count.ShouldBe(3); - _set.ShouldBe([_cad50, _eur25, _usd100]); + _set.ShouldBe([Cad50, Eur25, Usd100]); } [TestMethod] - public void AddAmountCurrencyCode_CurrencyIsNotAcceptedInTheSet_ThrowsArgumentException() + public void AddByCurrencyCode_CurrencyDisallowed_ThrowsArgumentException() { - Should.Throw(() => _set.Add(100m, "BBB")); + Should.Throw(() => _set.Add(100m, DisallowedCurrency.CurrencyCode)); } // public void Add(decimal amount, Currency currency) tests + [TestMethod] - public void AddAmountCurrency_CurrencyExistsInTheSet_IsSuccesful() + public void AddByCurrency_CurrencyExists_UpdatesValue() { - var myCurrency = Currency.Get("USD"); - _set.Add(100m, myCurrency); + _set.Add(100m, Currency.Get("USD")); _set.Count.ShouldBe(2); - _set.ShouldBe([_cad50, new Money(200m, "USD")]); + _set.ShouldBe([Cad50, new(200m, "USD")]); } [TestMethod] - public void AddAmountCurrency_CurrencyDoesNotExistInSet_IsSuccessful() + public void AddByCurrency_NewCurrency_AddsValue() { - var myCurrency = Currency.Get("EUR"); - _set.Add(25m, myCurrency); + _set.Add(25m, Currency.Get("EUR")); _set.Count.ShouldBe(3); - _set.ShouldBe([_cad50, _eur25, _usd100]); + _set.ShouldBe([Cad50, Eur25, Usd100]); } [TestMethod] - public void AddAmountCurrency_CurrencyIsNotAcceptedInTheSet_ThrowsArgumentException() + public void AddByCurrency_CurrencyDisallowed_ThrowsArgumentException() { _set.Count.ShouldBe(2); - Should.Throw(() => _set.Add(100m, _bbbCurrency)); + Should.Throw(() => _set.Add(100m, DisallowedCurrency)); } } \ No newline at end of file diff --git a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/RemoveAllTests.cs b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/RemoveAllTests.cs index 6876a1e..767d479 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/RemoveAllTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/RemoveAllTests.cs @@ -5,92 +5,100 @@ namespace Singulink.Globalization.Tests.SortedMoneySetTests; [TestClass] public class RemoveAllTests { - 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 _immutableSet = [_usd100, _cad50, _eur25]; - private readonly SortedMoneySet _set = _immutableSet.ToSet(); + private static readonly Currency Usd = Currency.Get("USD"); + private static readonly Currency Cad = Currency.Get("CAD"); + private static readonly Currency Eur = Currency.Get("EUR"); + + 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 ImmutableSet = [Usd100, Cad50, Eur25]; + + private readonly SortedMoneySet _set = ImmutableSet.ToSet(); // public int RemoveAll(IEnumerable currencies) tests [TestMethod] - public void AllExistingCurrenciesFromSet_IsSuccessful() + public void RemoveCurrencies_AllMatchingCurrencies_RemovesAllValues() { - List currencyList = [_usd100.Currency, _cad50.Currency, _eur25.Currency]; - int removedValuesCount = _set.RemoveAll(currencyList); + int removedValuesCount = _set.RemoveAll([Usd, Cad, Eur]); + removedValuesCount.ShouldBe(3); _set.Count.ShouldBe(0); } [TestMethod] - public void SomeExistingCurrenciesFromSet_IsSuccessful() + public void RemoveCurrencies_SomeMatchingCurrencies_RemovesMatchingCurrencyValues() { - List currencyList = [_usd100.Currency, _cad50.Currency]; - int removedValuesCount = _set.RemoveAll(currencyList); + int removedValuesCount = _set.RemoveAll([Usd, Cad]); + removedValuesCount.ShouldBe(2); _set.Count.ShouldBe(1); - _set.ShouldBe([_eur25]); + _set.ShouldBe([Eur25]); } [TestMethod] - public void NoExistingCurrenciesFromSet_IsSuccessful() + public void RemoveCurrencies_NoMatchingCurrencies_NoChange() { - List currencyList = [Currency.Get("JPY"), Currency.Get("GBP")]; - int removedValuesCount = _set.RemoveAll(currencyList); + int removedValuesCount = _set.RemoveAll([Currency.Get("JPY"), Currency.Get("GBP")]); + removedValuesCount.ShouldBe(0); _set.Count.ShouldBe(3); - _set.ShouldBe(_immutableSet); + _set.ShouldBe(ImmutableSet); } [TestMethod] - public void EmptyCollection_NoChange() + public void RemoveCurrencies_EmptyCollection_NoChange() { - int removedValuesCount = _set.RemoveAll(new List()); + int removedValuesCount = _set.RemoveAll([]); + removedValuesCount.ShouldBe(0); _set.Count.ShouldBe(3); - _set.ShouldBe(_immutableSet); + _set.ShouldBe(ImmutableSet); } [TestMethod] - public void NonExistentCurrency_ThrowsArgumentException() + public void RemoveCurrencies_CurrencyDisallowed_ThrowsArgumentException() { var disallowedCurrency = new Currency("XXX", "Non-existent currency", "X", 2); - List currencyList = [_usd100.Currency, disallowedCurrency, disallowedCurrency, _cad50.Currency]; - Should.Throw(() => _set.RemoveAll(currencyList)) + Should.Throw(() => _set.RemoveAll([Usd, disallowedCurrency, disallowedCurrency, Cad])) .Message.ShouldBe($"The following currencies are not present in the set's currency registry: {disallowedCurrency} (Parameter 'currencies')"); + _set.Count.ShouldBe(1); - _set.ShouldBe([_eur25]); + _set.ShouldBe([Eur25]); } [TestMethod] - public void NonExistentCurrencies_ThrowsArgumentException() + public void RemoveCurrencies_CurrenciesDisallowed_ThrowsArgumentException() { var disallowedCurrencyX = new Currency("XXX", "Non-existent currency", "X", 2); var disallowedCurrencyY = new Currency("YYY", "Non-existent currency", "Y", 2); - List currencyList = [_usd100.Currency, disallowedCurrencyX, disallowedCurrencyX, disallowedCurrencyY, disallowedCurrencyY, _cad50.Currency]; - Should.Throw(() => _set.RemoveAll(currencyList)) - .Message.ShouldBe($"The following currencies are not present in the set's currency registry: {disallowedCurrencyX}, {disallowedCurrencyY} (Parameter 'currencies')"); + Should.Throw(() => _set.RemoveAll([Usd, disallowedCurrencyX, disallowedCurrencyY, disallowedCurrencyX, Cad])) + .Message.ShouldBe("The following currencies are not present in the set's currency registry: " + + $"{disallowedCurrencyX}, {disallowedCurrencyY} (Parameter 'currencies')"); + _set.Count.ShouldBe(1); - _set.ShouldBe([_eur25]); + _set.ShouldBe([Eur25]); } // public SortedMoneySet RemoveAll(Func predicate) tests [TestMethod] - public void RemoveAllByPredicate_IsSuccessful() + public void RemoveCurrenciesByPredicate_SomeMatches_RemovesMatching() { _set.RemoveAll(m => m.Amount > 30); _set.Count.ShouldBe(1); - _set.ShouldBe([_eur25]); + _set.ShouldBe([Eur25]); } [TestMethod] - public void RemoveAllByPredicate_NoMatch_NoChange() + public void RemoveCurrenciesByPredicate_NoMatches_NoChange() { _set.RemoveAll(m => m.Amount > 100); _set.Count.ShouldBe(3); - _set.ShouldBe(_immutableSet); + _set.ShouldBe(ImmutableSet); } } \ No newline at end of file diff --git a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/RemoveTests.cs b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/RemoveTests.cs index ceb658a..25bed95 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/RemoveTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/RemoveTests.cs @@ -5,32 +5,33 @@ namespace Singulink.Globalization.Tests.SortedMoneySetTests; [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 _immutableSet = [_usd100, _cad50, _eur25]; - private readonly SortedMoneySet _set = _immutableSet.ToSet(); + 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 ImmutableSet = [Usd100, Cad50, Eur25]; + + private readonly SortedMoneySet _set = ImmutableSet.ToSet(); // public bool Remove(string currencyCode) tests [TestMethod] - public void RemoveCurrencyCode_CurrencyExistsInTheSet_ReturnsTrue() + public void RemoveCurrencyCode_CurrencyFound_RemovesValueAndReturnsTrue() { _set.Remove("USD").ShouldBeTrue(); _set.Count.ShouldBe(2); - _set.ShouldBe([_cad50, _eur25]); + _set.ShouldBe([Cad50, Eur25]); } [TestMethod] - public void RemoveCurrencyCode_CurrencyDoesNotExistInSet_ReturnsFalse() + public void RemoveCurrencyCode_CurrencyNotFound_ReturnsFalseAndNoChange() { _set.Remove("JPY").ShouldBeFalse(); _set.Count.ShouldBe(3); - _set.ShouldBe(_immutableSet); + _set.ShouldBe(ImmutableSet); } [TestMethod] - public void RemoveCurrencyCode_NonExistentCurrency_ThrowsArgumentException() + public void RemoveCurrencyCode_DisallowedCurrency_ThrowsArgumentException() { Should.Throw(() => _set.Remove("XXX")); } @@ -38,26 +39,26 @@ public void RemoveCurrencyCode_NonExistentCurrency_ThrowsArgumentException() // public bool Remove(Currency currency) tests [TestMethod] - public void RemoveCurrency_CurrencyExistsInTheSet_ReturnsTrue() + public void RemoveCurrency_CurrencyFound_RemovesValueAndReturnsTrue() { - _set.Remove(_usd100.Currency).ShouldBeTrue(); + _set.Remove(Usd100.Currency).ShouldBeTrue(); _set.Count.ShouldBe(2); - _set.ShouldBe([_cad50, _eur25]); + _set.ShouldBe([Cad50, Eur25]); } [TestMethod] - public void RemoveCurrency_CurrencyDoesNotExistInSet_ReturnsFalse() + public void RemoveCurrency_CurrencyNotFound_ReturnsFalseAndNoChange() { var gbpCurrency = Currency.Get("GBP"); _set.Remove(gbpCurrency).ShouldBeFalse(); _set.Count.ShouldBe(3); - _set.ShouldBe(_immutableSet); + _set.ShouldBe(ImmutableSet); } [TestMethod] - public void RemoveCurrency_NonExistentCurrency_ThrowsArgumentException() + public void RemoveCurrency_DisallowedCurrency_ThrowsArgumentException() { - var nonExistentCurrency = new Currency("XXX", "Non-existent currency", "X", 2); - Should.Throw(() => _set.Remove(nonExistentCurrency)); + 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/SortedMoneySetTests/SetAmountTests.cs b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/SetAmountTests.cs index f9a8dc4..f9ad459 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/SetAmountTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/SetAmountTests.cs @@ -5,82 +5,82 @@ namespace Singulink.Globalization.Tests.SortedMoneySetTests; [TestClass] public class SetAmountTests { - 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(); + 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 ImmutableSet = [Usd100, Cad50, Eur25]; + + private readonly SortedMoneySet _set = ImmutableSet.ToSet(); // public void SetAmount(decimal amount, string currencyCode) tests + [TestMethod] - public void SetAmount_CurrencyExists_UpdatesValue() + public void SetByCurrencyCode_CurrencyExists_UpdatesValue() { _set.SetAmount(200m, "USD"); _set.Count.ShouldBe(3); - _set.ShouldBe([new(200m, "USD"), _cad50, _eur25]); + _set.ShouldBe([Money.Create(200m, "USD"), Cad50, Eur25]); } [TestMethod] - public void SetAmount_CurrencyDoesNotExist_AddsValue() + public void SetByCurrencyCode_NewCurrency_AddsValue() { _set.SetAmount(75m, "AUD"); _set.Count.ShouldBe(4); - _set.ShouldBe([_cad50, _eur25, _usd100, _aud75]); + _set.ShouldBe([Cad50, Eur25, Usd100, Aud75]); } [TestMethod] - public void SetAmount_DefaultValue_IsSuccessful() + public void SetByCurrencyCode_ZeroAmount_ZeroesValue() { - _set.SetAmount(default, _usd100.Currency.CurrencyCode); + _set.SetAmount(0, "USD"); _set.Count.ShouldBe(3); - _set.ShouldBe([new(0m, "USD"), _cad50, _eur25]); + _set.ShouldBe([Usd0, Cad50, Eur25]); } [TestMethod] - public void SetAmount_CurrencyIsNotAccepted_ThrowsArgumentException() + public void SetByCurrencyCode_CurrencyDisallowed_ThrowsArgumentException() { - var value = new Money(100, new Currency("Blah blah blah", "BBB", "$$", 2)); - Should.Throw(() => _set.SetAmount(value.Amount, value.Currency.CurrencyCode)); + Should.Throw(() => _set.SetAmount(123m, DisallowedCurrency.CurrencyCode)); } // public void SetAmount(decimal amount, Currency currency) tests + [TestMethod] - public void SetAmountCurrency_CurrencyExists_UpdatesValue() + public void SetByCurrency_CurrencyExists_UpdatesValue() { - _set.SetAmount(200m, _usd100.Currency); + _set.SetAmount(200m, Usd); _set.Count.ShouldBe(3); - _set.ShouldBe([new(200m, "USD"), _cad50, _eur25]); + _set.ShouldBe([new(200m, "USD"), Cad50, Eur25]); } [TestMethod] - public void SetAmountCurrency_CurrencyDoesNotExist_AddsValue() + public void SetByCurrency_CurrencyDoesNotExist_AddsValue() { - var currency = Currency.Get("AUD"); - _set.SetAmount(75m, currency); + _set.SetAmount(75m, Aud); _set.Count.ShouldBe(4); - _set.ShouldBe([_cad50, _eur25, _usd100, _aud75]); + _set.ShouldBe([Cad50, Eur25, Usd100, Aud75]); } [TestMethod] - public void SetAmountCurrency_DefaultValue_IsSuccessful() + public void SetByCurrency_ZeroAmount_ZeroesValue() { - _set.SetAmount(default, _usd100.Currency); + _set.SetAmount(0, Usd); _set.Count.ShouldBe(3); - _set.ShouldBe([new(0m, "USD"), _cad50, _eur25]); - } - - [TestMethod] - public void SetAmountCurrency_CurrencyIsNotAccepted_ThrowsArgumentException() - { - var value = new Money(100, new Currency("Blah blah blah", "BBB", "$$", 2)); - Should.Throw(() => _set.SetAmount(value.Amount, value.Currency)); + _set.ShouldBe([Usd0, Cad50, Eur25]); } [TestMethod] - public void SetAmount_CurrencyIsNull_ThrowsArgumentNullException() + public void SetByCurrency_CurrencyDisallowed_ThrowsArgumentException() { - Should.Throw(() => _set.SetAmount(100m, (Currency)null!)); + Should.Throw(() => _set.SetAmount(123m, DisallowedCurrency)); } } \ No newline at end of file diff --git a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/SetValueTests.cs b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/SetValueTests.cs index b47b436..25b94d4 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/SetValueTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/SetValueTests.cs @@ -5,19 +5,20 @@ namespace Singulink.Globalization.Tests.SortedMoneySetTests; [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 _immutableSet = [_usd100, _cad50, _eur25]; - private readonly SortedMoneySet _set = _immutableSet.ToSet(); + 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(); [TestMethod] public void SetValue_CurrencyDoesNotExist_AddsValue() { - _set.SetValue(_aud75); + _set.SetValue(Aud75); _set.Count.ShouldBe(4); - _set.ShouldBe([_cad50, _eur25, _usd100, _aud75]); + _set.ShouldBe([Cad50, Eur25, Usd100, Aud75]); } [TestMethod] @@ -25,7 +26,7 @@ public void SetValue_CurrencyExists_UpdatesValue() { _set.SetValue(new(200m, "USD")); _set.Count.ShouldBe(3); - _set.ShouldBe([new(200m, "USD"), _cad50, _eur25]); + _set.ShouldBe([new(200m, "USD"), Cad50, Eur25]); } [TestMethod] @@ -33,7 +34,7 @@ public void SetValue_DefaultValue_NoChange() { _set.SetValue(default); _set.Count.ShouldBe(3); - _set.ShouldBe(_immutableSet); + _set.ShouldBe(ImmutableSet); } [TestMethod] diff --git a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/TryGetAmountTests.cs b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/TryGetAmountTests.cs index d485634..5b75226 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/TryGetAmountTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/TryGetAmountTests.cs @@ -5,11 +5,12 @@ namespace Singulink.Globalization.Tests.SortedMoneySetTests; [TestClass] public class TryGetAmountTests { - 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 _immutableSet = [_usd100, _cad50, _eur25]; - private readonly SortedMoneySet _set = _immutableSet.ToSet(); + 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 ImmutableSet = [Usd100, Cad50, Eur25]; + + private readonly SortedMoneySet _set = ImmutableSet.ToSet(); [TestMethod] public void AmountExists_ReturnsTrueAndOutputsAmount() diff --git a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/TryGetValueTests.cs b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/TryGetValueTests.cs index bbad24b..00551f2 100644 --- a/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/TryGetValueTests.cs +++ b/Tests/Singulink.Globalization.Currency.Tests/SortedMoneySetTests/TryGetValueTests.cs @@ -5,61 +5,60 @@ namespace Singulink.Globalization.Tests.SortedMoneySetTests; [TestClass] public class TryGetValueTests { - 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 _immutableSet = [_usd100, _cad50, _eur25]; - private readonly SortedMoneySet _set = _immutableSet.ToSet(); + 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 ImmutableSet = [Usd100, Cad50, Eur25]; + + private readonly SortedMoneySet _set = ImmutableSet.ToSet(); [TestMethod] - public void GetByCurrency_ValueExists_ReturnsTrue() + public void GetByCurrency_ValueExists_ReturnsTrueAndOutputsValue() { - _set.TryGetValue(Currency.Get("USD"), out var money).ShouldBeTrue(); - money.ShouldBe(_usd100); + _set.TryGetValue(Currency.Get("USD"), out var value).ShouldBeTrue(); + value.ShouldBe(Usd100); - _set.TryGetValue(Currency.Get("CAD"), out money).ShouldBeTrue(); - money.ShouldBe(_cad50); + _set.TryGetValue(Currency.Get("CAD"), out value).ShouldBeTrue(); + value.ShouldBe(Cad50); - _set.TryGetValue(Currency.Get("EUR"), out money).ShouldBeTrue(); - money.ShouldBe(_eur25); + _set.TryGetValue(Currency.Get("EUR"), out value).ShouldBeTrue(); + value.ShouldBe(Eur25); } [TestMethod] public void GetByCurrency_ValueDoesNotExist_ReturnsFalse() { - _set.TryGetValue(Currency.Get("GBP"), out var money).ShouldBeFalse(); - money.ShouldBe(default); + _set.TryGetValue(Currency.Get("GBP"), out var value).ShouldBeFalse(); } [TestMethod] public void GetByCurrency_CurrencyDoesNotExist_ThrowsArgumentException() { - Should.Throw(() => _set.TryGetValue(Currency.Get("XXX"), out var money)); + Should.Throw(() => _set.TryGetValue(Currency.Get("XXX"), out _)); } [TestMethod] - public void GetByCurrencyCode_ValueExists_ReturnsTrue() + public void GetByCurrencyCode_ValueExists_ReturnsTrueAndOutputsValue() { - _set.TryGetValue("USD", out var money).ShouldBeTrue(); - money.ShouldBe(_usd100); + _set.TryGetValue("USD", out var value).ShouldBeTrue(); + value.ShouldBe(Usd100); - _set.TryGetValue("CAD", out money).ShouldBeTrue(); - money.ShouldBe(_cad50); + _set.TryGetValue("CAD", out value).ShouldBeTrue(); + value.ShouldBe(Cad50); - _set.TryGetValue("EUR", out money).ShouldBeTrue(); - money.ShouldBe(_eur25); + _set.TryGetValue("EUR", out value).ShouldBeTrue(); + value.ShouldBe(Eur25); } [TestMethod] public void GetByCurrencyCode_ValueDoesNotExist_ReturnsFalse() { - _set.TryGetValue("GBP", out var money).ShouldBeFalse(); - money.ShouldBe(default); + _set.TryGetValue("GBP", out var value).ShouldBeFalse(); } [TestMethod] public void GetByCurrencyCode_CurrencyDoesNotExist_ThrowsArgumentException() { - Should.Throw(() => _set.TryGetValue("XXX", out var money)); + Should.Throw(() => _set.TryGetValue("XXX", out _)); } } \ No newline at end of file