Skip to content

Commit

Permalink
* 2024-08-21 @VRDate
Browse files Browse the repository at this point in the history
1. Get latest Currencies from [www.six-group.com](https://www.six-group.com/dam/download/financial-information/data-center/iso-currrency/lists/list-one.xml)
2. Use \XML\list-one.xml with \XML\Currencies.xslt to generate \XML\Currencies.xml grouping duplicates
3. Use \XML\Currencies.xml with \XML\Currency.xslt to generate \Money\Currency.cs code
4. Move `USD` as first `Currency` to default for `$`
5. Move `JPY` as first `Currency` to default for `¥`
6. Refactored Currency.cs merged CurrencyInfo into Currency added isFund property
7. Updated Money.cs & Currency.cs parsing logic with regex and Updated Unit Tests
  • Loading branch information
VRDate committed Aug 21, 2024
1 parent 478a1a6 commit 57e2027
Show file tree
Hide file tree
Showing 18 changed files with 5,308 additions and 842 deletions.
15 changes: 15 additions & 0 deletions Money.Tests/CurrenciesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using Xunit;

namespace System.Tests
{
public class CurrenciesTest
{

[Fact]
public void Transform()
{
Currencies.Transform(Currencies.SourceUrl,Currencies.InputPath,Currencies.XsltPath,Currencies.OutputPath);
}
}
}
77 changes: 76 additions & 1 deletion Money.Tests/CurrencyTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Collections.Generic;
using System.Globalization;
using Xunit;

namespace System.Tests
Expand Down Expand Up @@ -44,5 +45,79 @@ public void CurrencyHasValueEquality()
Assert.True(currency1.Equals(currency2));
Assert.True(currency1.Equals(boxedCurrency2));
}

[Fact]
public void CurrencyTryParseByIsoCurrencySymbolsIsCorrect()
{
foreach (var expectedCurrency in Currency.CurrencyByIsoCurrencySymbol.Values)
{
AssertTryParse($"{expectedCurrency.IsoCurrencySymbol}", expectedCurrency);
}
}

[Fact]
public void CurrencyTryParseBySomeCurrencySymbolIsCorrect()
{
AssertTryParse(Currency.USD.CurrencySymbol, Currency.USD);

AssertTryParse(Currency.EUR.CurrencySymbol, Currency.EUR);

AssertTryParse(Currency.JPY.CurrencySymbol, Currency.JPY);

AssertTryParse(Currency.GBP.CurrencySymbol, Currency.GBP);

AssertTryParse(Currency.CHF.CurrencySymbol, Currency.CHF);

AssertTryParse(Currency.ZAR.CurrencySymbol, Currency.ZAR);

AssertTryParse(Currency.KWD.CurrencySymbol, Currency.KWD);

AssertTryParse(Currency.ILS.CurrencySymbol, Currency.ILS);
}

[Fact]
public void CurrencyTryParseByCurrencySymbolIsCorrect()
{
int withoutCurrencySymbol = 0;
List<Exception> exceptions = new List<Exception>();
foreach (var expectedCurrency in Currency.CurrencyByIsoCurrencySymbol.Values)
{
try
{
if (expectedCurrency.CurrencySymbol == null)
{
withoutCurrencySymbol++;
continue;
}

AssertTryParse($"{expectedCurrency.CurrencySymbol}", expectedCurrency);
}
catch (Exception e)
{
exceptions.Add(e);
}
}

var exceptionsCount = exceptions.Count;
var currencyCount = Currency.CurrencyByIsoCurrencySymbol.Values.Count;
var present = 100 - (float)exceptionsCount / currencyCount * 100;
Console.WriteLine(
$"{exceptionsCount}/{currencyCount} {present}% parsed {withoutCurrencySymbol} without CurrencySymbols {exceptionsCount} CurrencySymbols are shared with other IsoCurrencySymbols");
Assert.True(currencyCount > exceptionsCount);
Assert.Equal(25, exceptionsCount);
foreach (var exception in exceptions)
{
Console.WriteLine(exception);
}
}

private static void AssertTryParse(string @string, Currency expected)
{
bool result;
Currency actual;
result = Currency.TryParse(@string, out actual);
Assert.True(result, @string);
Assert.Equal(expected, actual);
}
}
}
8 changes: 6 additions & 2 deletions Money.Tests/Money.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,13 +10,14 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>System.Tests</RootNamespace>
<AssemblyName>Money.Tests</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>3.5</OldToolsVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -26,6 +27,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -34,6 +36,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand All @@ -53,6 +56,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CurrenciesTest.cs" />
<Compile Include="CurrencyTests.cs" />
<Compile Include="MoneyDistributorTests.cs" />
<Compile Include="MoneyTests.cs" />
Expand Down
132 changes: 110 additions & 22 deletions Money.Tests/MoneyTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using Xunit;

Expand Down Expand Up @@ -333,7 +334,7 @@ public void MoneyPrintsCorrectly()
{
var previousCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
var money = new Money(100.125M, Currency.Usd);
var money = new Money(100.125M, Currency.USD);
var formattedMoney = money.ToString();
Assert.Equal("$100.13", formattedMoney);
Thread.CurrentThread.CurrentCulture = previousCulture;
Expand All @@ -342,8 +343,8 @@ public void MoneyPrintsCorrectly()
[Fact]
public void MoneyOperationsInvolvingDifferentCurrencyAllFail()
{
var money1 = new Money(101.5M, Currency.Aud);
var money2 = new Money(98.5M, Currency.Cad);
var money1 = new Money(101.5M, Currency.AUD);
var money2 = new Money(98.5M, Currency.CAD);
Money m;
bool b;

Expand All @@ -358,37 +359,124 @@ public void MoneyOperationsInvolvingDifferentCurrencyAllFail()
}

[Fact]
public void MoneyTryParseIsCorrect()
public void MoneyTryParseByIsoCurrencySymbolsIsCorrect()
{
var symbolUsd = "$ 123.45";
var expectedUsd = new Money(123.45M, Currency.Usd);
AssertTryParse(symbolUsd, expectedUsd);
foreach (var expectedCurrency in Currency.CurrencyByIsoCurrencySymbol.Values)
{
AssertTryParse($"{expectedCurrency.IsoCurrencySymbol} 123.45", new Money(123.45M, expectedCurrency));
}
}

[Fact]
public void MoneyTryParseByIsoCurrencySymbolsWithoutSpaceIsCorrect()
{
foreach (var expectedCurrency in Currency.CurrencyByIsoCurrencySymbol.Values)
{
AssertTryParse($"{expectedCurrency.IsoCurrencySymbol}123.45", new Money(123.45M, expectedCurrency));
}
}

[Fact]
public void MoneyTryParseBySomeCurrencySymbolIsCorrect()
{
AssertTryParse($"{Currency.USD.CurrencySymbol} 123.45", new Money(123.45M, Currency.USD));

AssertTryParse($"{Currency.EUR.CurrencySymbol} 123.45", new Money(123.45M, Currency.EUR));

AssertTryParse($"{Currency.JPY.CurrencySymbol} 123.45", new Money(123.45M, Currency.JPY));

var usd = "USD 123.45";
AssertTryParse(usd, expectedUsd);
AssertTryParse($"{Currency.GBP.CurrencySymbol} 123.45", new Money(123.45M, Currency.GBP));

var gbpSymbol = "£ 123.45";
var expectedGbp = new Money(123.45M, Currency.Gbp);
AssertTryParse(gbpSymbol, expectedGbp);
AssertTryParse($"{Currency.CHF.CurrencySymbol} 123.45", new Money(123.45M, Currency.CHF));

var cad = "CAD 123.45";
var expectedCad = new Money(123.45M, Currency.Cad);
AssertTryParse(cad, expectedCad);
AssertTryParse($"{Currency.ZAR.CurrencySymbol} 123.45", new Money(123.45M, Currency.ZAR));

var ils = "ILS 123.45";
var expectedIls = new Money(123.45M, Currency.Ils);
AssertTryParse(ils, expectedIls);
AssertTryParse($"{Currency.KWD.CurrencySymbol} 123.45", new Money(123.45M, Currency.KWD));

var ilsSymbol = "\u20aa 123.45";
AssertTryParse(ilsSymbol, expectedIls);
AssertTryParse($"{Currency.ILS.CurrencySymbol} 123.45", new Money(123.45M, Currency.ILS));
}


[Fact]
public void MoneyTryParseByCurrencySymbolIsCorrect()
{
int withoutCurrencySymbol = 0;
List<Exception> exceptions = new List<Exception>();
foreach (var expectedCurrency in Currency.CurrencyByIsoCurrencySymbol.Values)
{
try
{
if (expectedCurrency.CurrencySymbol == null)
{
withoutCurrencySymbol++;
continue;
}

AssertTryParse($"{expectedCurrency.CurrencySymbol} 123.45",
new Money(123.45M, expectedCurrency));
}
catch (Exception e)
{
exceptions.Add(e);
}
}

var exceptionsCount = exceptions.Count;
var currencyCount = Currency.CurrencyByIsoCurrencySymbol.Values.Count;
var present = 100 - (float)exceptionsCount / currencyCount * 100;
Console.WriteLine(
$"{exceptionsCount}/{currencyCount} {present}% parsed {withoutCurrencySymbol} without CurrencySymbols {exceptionsCount} CurrencySymbols are shared with other IsoCurrencySymbols");
Assert.True(currencyCount > exceptionsCount);
Assert.Equal(25, exceptionsCount);
foreach (var exception in exceptions)
{
Console.WriteLine(exception);
}
}

[Fact]
public void MoneyTryParseByCurrencySymbolWithoutSpaceIsCorrect()
{
int withoutCurrencySymbol = 0;
List<Exception> exceptions = new List<Exception>();
foreach (var expectedCurrency in Currency.CurrencyByIsoCurrencySymbol.Values)
{
try
{
if (expectedCurrency.CurrencySymbol == null)
{
withoutCurrencySymbol++;
continue;
}

AssertTryParse($"{expectedCurrency.CurrencySymbol}123.45",
new Money(123.45M, expectedCurrency));
}
catch (Exception e)
{
exceptions.Add(e);
}
}

var exceptionsCount = exceptions.Count;
var currencyCount = Currency.CurrencyByIsoCurrencySymbol.Values.Count;
var present = 100 - (float)exceptionsCount / currencyCount * 100;
Console.WriteLine(
$"{exceptionsCount}/{currencyCount} {present}% parsed {withoutCurrencySymbol} without CurrencySymbols {exceptionsCount} CurrencySymbols are shared with other IsoCurrencySymbols");
Assert.True(currencyCount > exceptionsCount);
Assert.Equal(25, exceptionsCount);
foreach (var exception in exceptions)
{
Console.WriteLine(exception);
}
}

private static void AssertTryParse(string @string, Money expected)
{
bool result;
Money actual;
result = Money.TryParse(@string, out actual);
Assert.True(result);
Assert.True(result, @string);
Assert.Equal(expected, actual);
}
}
Expand Down
24 changes: 23 additions & 1 deletion Money.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
# Visual Studio Version 17
VisualStudioVersion = 17.10.35122.118
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Money", "Money\Money.csproj", "{F2416D95-7587-4634-8061-D6B01C6163DD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Money.Tests", "Money.Tests\Money.Tests.csproj", "{F11B6673-FDAF-4F81-8CD5-6E0BF3FA4350}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{AF7152EF-B340-4AC7-89FD-864324223A5C}"
ProjectSection(SolutionItems) = preProject
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "XML", "XML", "{7EF9046B-8AAD-4EC9-9C5E-FADEAB66B2D5}"
ProjectSection(SolutionItems) = preProject
XML\Currencies.xml = XML\Currencies.xml
XML\https---www.six-group.com-dam-download-financial-information-data-center-iso-currrency-lists-list-one.xml.url = XML\https---www.six-group.com-dam-download-financial-information-data-center-iso-currrency-lists-list-one.xml.url
XML\list-one.xml = XML\list-one.xml
XML\Currencies.xslt = XML\Currencies.xslt
XML\Currency.xslt = XML\Currency.xslt
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -23,4 +39,10 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{7EF9046B-8AAD-4EC9-9C5E-FADEAB66B2D5} = {AF7152EF-B340-4AC7-89FD-864324223A5C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6F989D27-D777-44E3-BB17-575F908DC674}
EndGlobalSection
EndGlobal
Loading

0 comments on commit 57e2027

Please sign in to comment.