diff --git a/src/Nox.Types.EntityFramework/CurrencyCode3Converter.cs b/src/Nox.Types.EntityFramework/CurrencyCode3Converter.cs new file mode 100644 index 0000000..e544a5a --- /dev/null +++ b/src/Nox.Types.EntityFramework/CurrencyCode3Converter.cs @@ -0,0 +1,15 @@ +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace Nox.Types.EntityFramework; + +/// +/// Class for three-letters currency code (ISO 4217). +/// +public class CurrencyCode3Converter : ValueConverter +{ + /// + /// Initializes a new instance of the class. + /// + public CurrencyCode3Converter() : base(currencyCode3 => currencyCode3.Value, currencyCode3Value => CurrencyCode3.From(currencyCode3Value)) { } +} + diff --git a/src/Nox.Types/Types/CurrencyCode3/CurrencyCode3.cs b/src/Nox.Types/Types/CurrencyCode3/CurrencyCode3.cs new file mode 100644 index 0000000..58bad97 --- /dev/null +++ b/src/Nox.Types/Types/CurrencyCode3/CurrencyCode3.cs @@ -0,0 +1,28 @@ +using System; +using System.Linq; + +namespace Nox.Types; + +/// +/// Class for three-letters currency code (ISO 4217). +/// +public sealed class CurrencyCode3 : ValueObject +{ + private readonly string[] CurrencyCodes = Enum.GetNames(typeof(CurrencyCode)); + + /// + /// Validates the object. + /// + /// A validation result indicating whether the object is valid or not. + internal override ValidationResult Validate() + { + var result = base.Validate(); + + if (!CurrencyCodes.Contains(Value)) + { + result.Errors.Add(new ValidationFailure(nameof(Value), $"Could not create a Nox CurrencyCode3 type with unsupported value '{Value}'.")); + } + + return result; + } +} diff --git a/tests/Nox.Types.Tests/CurrencyCode3Tests.cs b/tests/Nox.Types.Tests/CurrencyCode3Tests.cs new file mode 100644 index 0000000..f42d38c --- /dev/null +++ b/tests/Nox.Types.Tests/CurrencyCode3Tests.cs @@ -0,0 +1,53 @@ +using Nox.Types.Tests.Types; + +namespace Nox.Types.Tests; +public class CurrencyCode3Tests +{ + [Theory] + [ClassData(typeof(CurrencyCodeData))] + public void CurrencyCode3_Constructor_ReturnsSameValue_AllCurrencies(string currencyCode3String) + { + var currencyCode3 = CurrencyCode3.From(currencyCode3String); + + Assert.Equal(currencyCode3String, currencyCode3.Value); + } + + [Fact] + public void CurrencyCode3_Constructor_WithUnsupportedCurrencyCode3_ThrowsValidationException() + { + var exception = Assert.Throws(() => _ = + CurrencyCode3.From("ABC") + ); + + Assert.Equal("Could not create a Nox CurrencyCode3 type with unsupported value 'ABC'.", exception.Errors.First().ErrorMessage); + } + + [Fact] + public void CurrencyCode3_Equality_Tests() + { + var currencyCode3_1 = CurrencyCode3.From("USD"); + + var currencyCode3_2 = CurrencyCode3.From("USD"); + + Assert.Equal(currencyCode3_1, currencyCode3_2); + } + + [Fact] + public void CurrencyCode3_NotEqual_Tests() + { + var currencyCode3_1 = CurrencyCode3.From("RWF"); + + var currencyCode3_2 = CurrencyCode3.From("SHP"); + + Assert.NotEqual(currencyCode3_1, currencyCode3_2); + } + + [Fact] + public void CurrencyCode3_ToString_ReturnsString() + { + var currencyCode3 = CurrencyCode3.From("USD"); + + Assert.Equal("USD", currencyCode3.ToString()); + } +} + diff --git a/tests/Nox.Types.Tests/CurrencyCodeData.cs b/tests/Nox.Types.Tests/CurrencyCodeData.cs new file mode 100644 index 0000000..35f5640 --- /dev/null +++ b/tests/Nox.Types.Tests/CurrencyCodeData.cs @@ -0,0 +1,142 @@ +using System.Collections; + +namespace Nox.Types.Tests.Types; +public class CurrencyCodeData : IEnumerable +{ + public IEnumerator GetEnumerator() + { + yield return new object[] { "AED" }; + yield return new object[] { "AFN" }; + yield return new object[] { "ALL" }; + yield return new object[] { "AMD" }; + yield return new object[] { "ANG" }; + yield return new object[] { "AOA" }; + yield return new object[] { "ARS" }; + yield return new object[] { "AUD" }; + yield return new object[] { "AWG" }; + yield return new object[] { "AZN" }; + yield return new object[] { "BAM" }; + yield return new object[] { "BBD" }; + yield return new object[] { "BDT" }; + yield return new object[] { "BGN" }; + yield return new object[] { "BHD" }; + yield return new object[] { "BIF" }; + yield return new object[] { "BMD" }; + yield return new object[] { "BND" }; + yield return new object[] { "BOB" }; + yield return new object[] { "BRL" }; + yield return new object[] { "BSD" }; + yield return new object[] { "BTC" }; + yield return new object[] { "BTN" }; + yield return new object[] { "BWP" }; + yield return new object[] { "BYR" }; + yield return new object[] { "BYN" }; + yield return new object[] { "BZD" }; + yield return new object[] { "CAD" }; + yield return new object[] { "CDF" }; + yield return new object[] { "CHF" }; + yield return new object[] { "CLP" }; + yield return new object[] { "CNY" }; + yield return new object[] { "COP" }; + yield return new object[] { "CRC" }; + yield return new object[] { "CUC" }; + yield return new object[] { "CUP" }; + yield return new object[] { "CVE" }; + yield return new object[] { "CZK" }; + yield return new object[] { "DJF" }; + yield return new object[] { "DKK" }; + yield return new object[] { "DOP" }; + yield return new object[] { "DZD" }; + yield return new object[] { "EGP" }; + yield return new object[] { "ERN" }; + yield return new object[] { "ETB" }; + yield return new object[] { "EUR" }; + yield return new object[] { "FJD" }; + yield return new object[] { "FKP" }; + yield return new object[] { "GBP" }; + yield return new object[] { "GEL" }; + yield return new object[] { "GHS" }; + yield return new object[] { "GIP" }; + yield return new object[] { "GMD" }; + yield return new object[] { "GNF" }; + yield return new object[] { "GTQ" }; + yield return new object[] { "GYD" }; + yield return new object[] { "HKD" }; + yield return new object[] { "HNL" }; + yield return new object[] { "HRK" }; + yield return new object[] { "HTG" }; + yield return new object[] { "HUF" }; + yield return new object[] { "IDR" }; + yield return new object[] { "ILS" }; + yield return new object[] { "INR" }; + yield return new object[] { "IQD" }; + yield return new object[] { "IRR" }; + yield return new object[] { "ISK" }; + yield return new object[] { "JMD" }; + yield return new object[] { "JOD" }; + yield return new object[] { "JPY" }; + yield return new object[] { "KES" }; + yield return new object[] { "KGS" }; + yield return new object[] { "KHR" }; + yield return new object[] { "KMF" }; + yield return new object[] { "KPW" }; + yield return new object[] { "KRW" }; + yield return new object[] { "KWD" }; + yield return new object[] { "KYD" }; + yield return new object[] { "KZT" }; + yield return new object[] { "LAK" }; + yield return new object[] { "LBP" }; + yield return new object[] { "LKR" }; + yield return new object[] { "LRD" }; + yield return new object[] { "LSL" }; + yield return new object[] { "LYD" }; + yield return new object[] { "MAD" }; + yield return new object[] { "MDL" }; + yield return new object[] { "MGA" }; + yield return new object[] { "MKD" }; + yield return new object[] { "MMK" }; + yield return new object[] { "MNT" }; + yield return new object[] { "MOP" }; + yield return new object[] { "MRO" }; + yield return new object[] { "MTL" }; + yield return new object[] { "MUR" }; + yield return new object[] { "MVR" }; + yield return new object[] { "MWK" }; + yield return new object[] { "MXN" }; + yield return new object[] { "MYR" }; + yield return new object[] { "MZN" }; + yield return new object[] { "NAD" }; + yield return new object[] { "NGN" }; + yield return new object[] { "NIO" }; + yield return new object[] { "NOK" }; + yield return new object[] { "NPR" }; + yield return new object[] { "NZD" }; + yield return new object[] { "OMR" }; + yield return new object[] { "PAB" }; + yield return new object[] { "PEN" }; + yield return new object[] { "PGK" }; + yield return new object[] { "PHP" }; + yield return new object[] { "PKR" }; + yield return new object[] { "PLN" }; + yield return new object[] { "PYG" }; + yield return new object[] { "QAR" }; + yield return new object[] { "RON" }; + yield return new object[] { "RSD" }; + yield return new object[] { "RUB" }; + yield return new object[] { "RWF" }; + yield return new object[] { "SAR" }; + yield return new object[] { "SBD" }; + yield return new object[] { "SCR" }; + yield return new object[] { "SDD" }; + yield return new object[] { "SDG" }; + yield return new object[] { "SEK" }; + yield return new object[] { "SGD" }; + yield return new object[] { "SHP" }; + yield return new object[] { "SLL" }; + yield return new object[] { "SOS" }; + yield return new object[] { "SRD" }; + yield return new object[] { "STD" }; + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); +}