From 60906d5ac7231ae4a30cced4d041a507e8285b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gonza=CC=81lez=20Garci=CC=81a?= Date: Wed, 3 Jul 2024 20:31:12 +0200 Subject: [PATCH] add ability to overwrite a currency with a combination of CultureInfo and RegionInfo --- src/NMoneys/CurrencyConfiguration.cs | 39 +++++++++++++++++ .../CurrencyTester.Configuration.cs | 43 ++++++++++++++++--- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/src/NMoneys/CurrencyConfiguration.cs b/src/NMoneys/CurrencyConfiguration.cs index ca60374..3639d44 100644 --- a/src/NMoneys/CurrencyConfiguration.cs +++ b/src/NMoneys/CurrencyConfiguration.cs @@ -1,3 +1,5 @@ +using System.Globalization; + namespace NMoneys; /// @@ -52,4 +54,41 @@ public record CurrencyConfiguration() /// Override . /// public ValueTuple Reference { get; init; } = (null, null); + + /// + /// Creates a configuration override with the data of the provided + /// and its corresponding . + /// + /// Instance with the overriding information ( "currency" properties). + /// Whether the corresponding override is considered obsolete (false by default). + /// An instance with the overriding data. + public static CurrencyConfiguration From(CultureInfo culture, bool isObsolete = false) => + From(culture, new RegionInfo(culture.Name), isObsolete); + + /// + /// Creates a configuration override with the data of the provided + /// and . + /// + /// Instance with the overriding information ( "currency" properties). + /// Instance with the overriding information (currency English and native names). + /// Whether the corresponding override is considered obsolete (false by default). + /// An instance with the overriding data. + public static CurrencyConfiguration From(CultureInfo culture, RegionInfo region, bool isObsolete = false) + { + NumberFormatInfo nf = culture.NumberFormat; + CurrencyConfiguration configuration = new() + { + NativeName = region.CurrencyNativeName, + EnglishName = region.CurrencyEnglishName, + Symbol = nf.CurrencySymbol, + SignificantDecimalDigits = (byte)nf.CurrencyDecimalDigits, + DecimalSeparator = nf.CurrencyDecimalSeparator, + GroupSeparator = nf.CurrencyGroupSeparator, + GroupSizes = nf.CurrencyGroupSizes.Select(s => (byte)s).ToArray(), + PositivePattern = (byte)nf.CurrencyPositivePattern, + NegativePattern = (byte)nf.CurrencyNegativePattern, + IsObsolete = isObsolete + }; + return configuration; + } } diff --git a/tests/NMoneys.Tests/CurrencyTester.Configuration.cs b/tests/NMoneys.Tests/CurrencyTester.Configuration.cs index 68ed654..425e11f 100644 --- a/tests/NMoneys.Tests/CurrencyTester.Configuration.cs +++ b/tests/NMoneys.Tests/CurrencyTester.Configuration.cs @@ -1,3 +1,5 @@ +using System.Globalization; + namespace NMoneys.Tests; public partial class CurrencyTester @@ -6,20 +8,47 @@ public partial class CurrencyTester public void Configure_Single_OverridesInformationSubset() { string englishName = "override"; - Currency.Configure(CurrencyIsoCode.XXX, new CurrencyConfiguration{ EnglishName = englishName}); + Currency.Configure(CurrencyIsoCode.XXX, new CurrencyConfiguration { EnglishName = englishName }); Currency overriden = Currency.Get(CurrencyIsoCode.XXX); Assert.That(overriden.EnglishName, Is.EqualTo(englishName).And.Not.EqualTo(overriden.NativeName)); } + [Test, Explicit] + public void Configure_SingleCulture_OverridesInformationSubset() + { + CultureInfo paraguaySpanish = CultureInfo.GetCultureInfo("es-PY"); + + Currency.Configure(CurrencyIsoCode.PYG, CurrencyConfiguration.From(paraguaySpanish)); + Currency overriden = Currency.Get(CurrencyIsoCode.PYG); + Assert.That(overriden.Symbol, Is.EqualTo(paraguaySpanish.NumberFormat.CurrencySymbol).And + .Not.EqualTo(@"₲")); + } + + [Test, Explicit] + public void Configure_CultureAndRegion_OverridesInformationSubset() + { + CultureInfo somali = CultureInfo.GetCultureInfo("so-SO"); + RegionInfo djibuti = new("so-DJ"); + + Currency.Configure(CurrencyIsoCode.SOS, CurrencyConfiguration.From(somali, djibuti)); + Currency overriden = Currency.Get(CurrencyIsoCode.SOS); + // according to .NET, there are no decimals in shillings, but according to ISO there actually are + // (although inflation turns them useless and are not in the denominations) + Assert.That(overriden.SignificantDecimalDigits, Is.EqualTo(0).And + .Not.EqualTo(2)); + // even though somali is spoken in Djibuti, they do not have "shillings", but "francs" + Assert.That(overriden.EnglishName, Does.Contain("Franc").And + .Not.Contains("Shilling")); + } + [Test, Explicit] public void Configure_Multiple_OverridesInformationSubset() { string englishName = "override"; - Currency.Configure(new [] - { - (CurrencyIsoCode.XXX, new CurrencyConfiguration{ EnglishName = englishName}), - (CurrencyIsoCode.XTS, new CurrencyConfiguration{ EnglishName = englishName}) - }); + Currency.Configure([ + (CurrencyIsoCode.XXX, new CurrencyConfiguration { EnglishName = englishName }), + (CurrencyIsoCode.XTS, new CurrencyConfiguration { EnglishName = englishName }) + ]); Currency xxx = Currency.Get(CurrencyIsoCode.XXX); Assert.That(xxx.EnglishName, Is.EqualTo(englishName).And.Not.EqualTo(xxx.NativeName)); Currency xts = Currency.Get(CurrencyIsoCode.XTS); @@ -31,7 +60,7 @@ public void Configure_AfterInitialization_Exception() { Currency xxx = Currency.Get(CurrencyIsoCode.XXX); // initializes the currency - Assert.That(()=>Currency.Configure(CurrencyIsoCode.XXX, new CurrencyConfiguration()), + Assert.That(() => Currency.Configure(CurrencyIsoCode.XXX, new CurrencyConfiguration()), Throws.InstanceOf()); } }