Skip to content

Commit

Permalink
Epic cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mikernet committed Mar 4, 2024
1 parent 304e456 commit 7c60123
Show file tree
Hide file tree
Showing 16 changed files with 315 additions and 295 deletions.
12 changes: 5 additions & 7 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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
10 changes: 5 additions & 5 deletions Source/Singulink.Globalization.Currency/Money.ToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ private static readonly ImmutableArray<string> NegativeInternationalPatterns
private static readonly ImmutableArray<string> NegativeReverseInternationalPatterns
= ["(n) $", "-n $", "-n $", "n- $", "(n) $", "-n $", "n- $", "n- $", "-n $", "-n $", "n- $", "n- $", "-n $", "n- $", "(n) $", "(n) $"];

private static readonly ConditionalWeakTable<CultureInfo, RegionInfo?> s_regionInfoLookup = [];
private static readonly ConditionalWeakTable<NumberFormatInfo, NumberFormatInfo> s_absNumberFormatInfoLookup = [];
private static readonly ConditionalWeakTable<CultureInfo, RegionInfo?> _regionInfoLookup = [];
private static readonly ConditionalWeakTable<NumberFormatInfo, NumberFormatInfo> _absNumberFormatInfoLookup = [];

/// <summary>
/// Returns a string representation of this value's currency and amount.
Expand Down Expand Up @@ -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,
Expand All @@ -216,15 +216,15 @@ 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
{
region = new RegionInfo(culture.Name);
}
catch { }

s_regionInfoLookup.AddOrUpdate(culture, region);
_regionInfoLookup.AddOrUpdate(culture, region);
}

return region ?? RegionInfo.CurrentRegion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentException>(() => _set.TryGetAmount("AAA", out _));
Should.Throw<ArgumentException>(() => Set.TryGetAmount("AAA", out _));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentException>(() => new Money(100m, "USD").CompareTo(new Money(200m, "CAD")));
Should.Throw<ArgumentException>(() => Usd100.CompareTo(new Money(200m, "CAD")));
}
}
Loading

0 comments on commit 7c60123

Please sign in to comment.