Skip to content

Commit 2abf481

Browse files
committed
Add NodaMoney test
1 parent 7f931cd commit 2abf481

6 files changed

Lines changed: 168 additions & 0 deletions

File tree

src/Example.CSharp.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloneTest", "plugins\CloneT
281281
EndProject
282282
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapperlyTest", "plugins\MapperlyTest\MapperlyTest.csproj", "{6A740E32-3541-4FB0-9D54-85D2596C3242}"
283283
EndProject
284+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NodaMoneyTest", "plugins\NodaMoneyTest\NodaMoneyTest.csproj", "{19C550DA-8802-4C34-9A44-DD094DD8DE74}"
285+
EndProject
284286
Global
285287
GlobalSection(SolutionConfigurationPlatforms) = preSolution
286288
Debug|Any CPU = Debug|Any CPU
@@ -687,6 +689,10 @@ Global
687689
{6A740E32-3541-4FB0-9D54-85D2596C3242}.Debug|Any CPU.Build.0 = Debug|Any CPU
688690
{6A740E32-3541-4FB0-9D54-85D2596C3242}.Release|Any CPU.ActiveCfg = Release|Any CPU
689691
{6A740E32-3541-4FB0-9D54-85D2596C3242}.Release|Any CPU.Build.0 = Release|Any CPU
692+
{19C550DA-8802-4C34-9A44-DD094DD8DE74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
693+
{19C550DA-8802-4C34-9A44-DD094DD8DE74}.Debug|Any CPU.Build.0 = Debug|Any CPU
694+
{19C550DA-8802-4C34-9A44-DD094DD8DE74}.Release|Any CPU.ActiveCfg = Release|Any CPU
695+
{19C550DA-8802-4C34-9A44-DD094DD8DE74}.Release|Any CPU.Build.0 = Release|Any CPU
690696
EndGlobalSection
691697
GlobalSection(SolutionProperties) = preSolution
692698
HideSolutionNode = FALSE
@@ -805,6 +811,7 @@ Global
805811
{16291F9D-48EC-496B-8610-E686CA57F747} = {630114C3-1AE7-468E-8ADD-1CDE3E9EEF26}
806812
{E8A64A5D-8052-499B-9CC6-09C2768EDDAA} = {630114C3-1AE7-468E-8ADD-1CDE3E9EEF26}
807813
{6A740E32-3541-4FB0-9D54-85D2596C3242} = {630114C3-1AE7-468E-8ADD-1CDE3E9EEF26}
814+
{19C550DA-8802-4C34-9A44-DD094DD8DE74} = {630114C3-1AE7-468E-8ADD-1CDE3E9EEF26}
808815
EndGlobalSection
809816
GlobalSection(ExtensibilityGlobals) = postSolution
810817
SolutionGuid = {CBE0CD6C-2E47-4D9F-B072-C138AA4A6D5A}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace NodaMoneyTest;
2+
3+
public static partial class MoneyTest
4+
{
5+
public static class CurrencyCode
6+
{
7+
8+
#region Constants & Statics
9+
10+
// "¥"
11+
public static readonly string CNY = "CNY";
12+
13+
// "€"
14+
public static readonly string EUR = "EUR";
15+
16+
// "HK$"
17+
public static readonly string HKD = "HKD";
18+
19+
// "¥"
20+
public static readonly string JPY = "JPY";
21+
22+
// "MOP$"
23+
public static readonly string MOP = "MOP";
24+
// "NT$"
25+
public static readonly string TWD = "TWD";
26+
27+
// "US$"
28+
public static readonly string USD = "USD";
29+
30+
#endregion
31+
}
32+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using NodaMoney;
2+
using NodaMoney.Context;
3+
using System.Globalization;
4+
5+
namespace NodaMoneyTest;
6+
7+
public static partial class MoneyTest
8+
{
9+
10+
#region Constants & Statics
11+
12+
public static void DefaultCurrency_Test()
13+
{
14+
var myDefaultContext = MoneyContext.Create(
15+
opt =>
16+
{
17+
opt.MaxScale = 4;
18+
opt.DefaultCurrency = CurrencyInfo.FromCode(CurrencyCode.CNY);
19+
opt.EnforceZeroCurrencyMatching = true;
20+
});
21+
MoneyContext.DefaultThreadContext = myDefaultContext;
22+
23+
var money = new Money(10.1234m);
24+
Console.WriteLine(money); // ¥10.1234
25+
}
26+
27+
public static void FastMoney_Test()
28+
{
29+
var eur = new FastMoney(10.1264m, CurrencyCode.EUR);
30+
var fee = new FastMoney(0.1000m, CurrencyCode.EUR);
31+
var total = eur + fee;
32+
33+
var str = total.ToString();
34+
Console.WriteLine(str);
35+
36+
str = total.ToMoney().ToString("R", CultureInfo.InvariantCulture);
37+
Console.WriteLine(str);
38+
}
39+
40+
public static void Money_Test()
41+
{
42+
var price = new Money(12.99m, CurrencyCode.USD);
43+
var tax = price * 0.21m;
44+
var total = price + tax;
45+
46+
var text = total.ToString("C", new CultureInfo("en-US"));
47+
Console.WriteLine(text);
48+
// $15.72
49+
50+
// Split without losing cents
51+
var shares = (total + 0.01m).Split(3);
52+
// [$5.24, $5.24, $5.25]
53+
}
54+
55+
public static void Parse_Test()
56+
{
57+
var money = new Money(76543.21, Currency.FromCode(CurrencyCode.EUR));
58+
59+
var str = money.ToString("R", CultureInfo.InvariantCulture);
60+
Console.WriteLine(str); // EUR 76543.21
61+
var euro = Money.Parse(str, CultureInfo.InvariantCulture);
62+
63+
str = money.ToString(CultureInfo.InvariantCulture);
64+
Console.WriteLine(str); // €76,543.21
65+
euro = Money.Parse(str, CultureInfo.InvariantCulture);
66+
}
67+
68+
#endregion
69+
70+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="NodaMoney" Version="2.6.0" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace NodaMoneyTest;
2+
3+
internal static class Program
4+
{
5+
6+
#region Constants & Statics
7+
8+
private static void Main()
9+
{
10+
//MoneyTest.Money_Test();
11+
//MoneyTest.Parse_Test();
12+
//MoneyTest.FastMoney_Test();
13+
MoneyTest.DefaultCurrency_Test();
14+
15+
SerializationTest.Serialize_Test();
16+
}
17+
18+
#endregion
19+
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using NodaMoney;
2+
using System.Text.Json;
3+
4+
namespace NodaMoneyTest;
5+
6+
internal static class SerializationTest
7+
{
8+
9+
#region Constants & Statics
10+
11+
public static void Serialize_Test()
12+
{
13+
var dto = new MyDto("John Doe", 30, new FastMoney(99.99m, "USD"));
14+
var json = JsonSerializer.Serialize(dto);
15+
16+
Console.WriteLine(json);
17+
18+
var deserializedDto = JsonSerializer.Deserialize<MyDto>(json);
19+
}
20+
21+
#endregion
22+
23+
}
24+
25+
public record MyDto(string Name, int Age, FastMoney Price);

0 commit comments

Comments
 (0)