Skip to content

Commit

Permalink
Refactor test structure and improve readability
Browse files Browse the repository at this point in the history
Reorganize and rename test files, methods, and classes for clarity and alignment with BDD principles. Introduce improved test naming conventions, focusing on behavior-driven style. Apply code formatting and cleanup to enhance readability, and update README for clearer test structure guidelines.
  • Loading branch information
RemyDuijkeren committed Jan 16, 2025
1 parent d3a00a0 commit 0505b1d
Show file tree
Hide file tree
Showing 17 changed files with 516 additions and 323 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ jobs:
- name: 🛠️ Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
dotnet-version: |
6.0.x
8.0.x
9.0.x # All target .NET SDKs for testing
cache: true
cache-dependency-path: '**/packages.lock.json'
env:
Expand All @@ -41,7 +44,10 @@ jobs:
run: dotnet build -c Release --no-restore

- name: 🚦 Test
run: dotnet test -c Release -l trx --results-directory ./artifacts/ --no-build
run: |
dotnet test -f net6.0 -c Release -l trx --results-directory ./artifacts/ --no-build
dotnet test -f net8.0 -c Release -l trx --results-directory ./artifacts/ --no-build
dotnet test -f net9.0 -c Release -l trx --results-directory ./artifacts/ --no-build
- name: 📋 Test reporter
uses: dorny/test-reporter@v1
Expand Down
27 changes: 16 additions & 11 deletions src/NodaMoney/Money.BinaryOperators.cs
Original file line number Diff line number Diff line change
@@ -1,69 +1,74 @@
namespace NodaMoney;
using System.Numerics;

namespace NodaMoney;

/// <summary>Represents Money, an amount defined in a specific Currency.</summary>
public partial struct Money
#if NET7_0_OR_GREATER
: IAdditionOperators<Money, Money, Money>, IAdditionOperators<Money, decimal, Money>
#endif
{
/// <summary>Adds two specified <see cref="Money"/> values.</summary>
/// <param name="left">A <see cref="Money"/> object on the left side.</param>
/// <param name="right">A <see cref="Money"/> object on the right side.</param>
/// <returns>The <see cref="Money"/> result of adding left and right.</returns>
public static Money operator +(in Money left, in Money right) => Add(left, right);
public static Money operator +(Money left, Money right) => Add(left, right);

/// <summary>Add the <see cref="Money"/> value with the given value.</summary>
/// <param name="left">A <see cref="Money"/> object on the left side.</param>
/// <param name="right">A <see cref="decimal"/> object on the right side.</param>
/// <returns>The <see cref="Money"/> result of adding left and right.</returns>
public static Money operator +(in Money left, in decimal right) => Add(left, right);
public static Money operator +(Money left, decimal right) => Add(left, right);

/// <summary>Add the <see cref="Money"/> value with the given value.</summary>
/// <param name="left">A <see cref="decimal"/> object on the left side.</param>
/// <param name="right">A <see cref="Money"/> object on the right side.</param>
/// <returns>The <see cref="Money"/> result of adding left and right.</returns>
public static Money operator +(in decimal left, in Money right) => Add(right, left);
public static Money operator +(decimal left, Money right) => Add(right, left);

/// <summary>Subtracts two specified <see cref="Money"/> values.</summary>
/// <param name="left">A <see cref="Money"/> object on the left side.</param>
/// <param name="right">A <see cref="Money"/> object on the right side.</param>
/// <returns>The <see cref="Money"/> result of subtracting right from left.</returns>
public static Money operator -(in Money left, in Money right) => Subtract(left, right);
public static Money operator -(Money left, Money right) => Subtract(left, right);

/// <summary>Subtracts <see cref="Money"/> value with the given value.</summary>
/// <param name="left">A <see cref="Money"/> object on the left side.</param>
/// <param name="right">A <see cref="decimal"/> object on the right side.</param>
/// <returns>The <see cref="Money"/> result of subtracting right from left.</returns>
public static Money operator -(in Money left, in decimal right) => Subtract(left, right);
public static Money operator -(Money left, decimal right) => Subtract(left, right);

/// <summary>Subtracts <see cref="Money"/> value with the given value.</summary>
/// <param name="left">A <see cref="decimal"/> object on the left side.</param>
/// <param name="right">A <see cref="Money"/> object on the right side.</param>
/// <returns>The <see cref="Money"/> result of subtracting right from left.</returns>
public static Money operator -(in decimal left, in Money right) => Subtract(right, left);
public static Money operator -(decimal left, Money right) => Subtract(right, left);

/// <summary>Multiplies the <see cref="Money"/> value by the given value.</summary>
/// <param name="left">A <see cref="Money"/> object on the left side.</param>
/// <param name="right">A <see cref="decimal"/> object on the right side.</param>
/// <returns>The <see cref="Money"/> result of multiplying right with left.</returns>
public static Money operator *(in Money left, in decimal right) => Multiply(left, right);
public static Money operator *(Money left, decimal right) => Multiply(left, right);

/// <summary>Multiplies the <see cref="Money"/> value by the given value.</summary>
/// <param name="left">A <see cref="decimal"/> object on the left side.</param>
/// <param name="right">A <see cref="Money"/> object on the right side.</param>
/// <returns>The <see cref="Money"/> result of multiplying left with right.</returns>
public static Money operator *(in decimal left, in Money right) => Multiply(right, left);
public static Money operator *(decimal left, Money right) => Multiply(right, left);

/// <summary>Divides the <see cref="Money"/> value by the given value.</summary>
/// <param name="left">A <see cref="Money"/> object on the left side.</param>
/// <param name="right">A <see cref="decimal"/> object on the right side.</param>
/// <returns>The <see cref="Money"/> result of dividing left with right.</returns>
/// <remarks>This division can lose money! Use <see cref="Extensions.MoneyExtensions.SafeDivide(Money, int)"/> to do a safe division.</remarks>
public static Money operator /(in Money left, in decimal right) => Divide(left, right);
public static Money operator /(Money left, decimal right) => Divide(left, right);

/// <summary>Divides the <see cref="Money"/> value by the given value.</summary>
/// <param name="left">A <see cref="Money"/> object on the left side.</param>
/// <param name="right">A <see cref="Money"/> object on the right side.</param>
/// <returns>The <see cref="decimal"/> result of dividing left with right.</returns>
/// <remarks>Division of Money by Money, means the unit is lost, so the result will be a ratio <see cref="decimal"/>.</remarks>
public static decimal operator /(in Money left, in Money right) => Divide(left, right);
public static decimal operator /(Money left, Money right) => Divide(left, right);

/// <summary>Adds two specified <see cref="Money"/> values.</summary>
/// <param name="money1">The first <see cref="Money"/> object.</param>
Expand Down
48 changes: 14 additions & 34 deletions src/NodaMoney/Money.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Money(decimal amount, Currency currency)
/// <remarks>The amount will be rounded to the number of decimal digits of the specified currency
/// (<see cref="NodaMoney.CurrencyInfo.DecimalDigits"/>).</remarks>
public Money(decimal amount, string code, MidpointRounding rounding)
: this(amount, CurrencyInfo.FromCode(code), rounding)
: this(amount, CurrencyInfo.FromCode(code), rounding)
{
}

Expand All @@ -80,8 +80,8 @@ public Money(decimal amount, string code, MidpointRounding rounding)
public Money(decimal amount, Currency currency, MidpointRounding rounding)
: this()
{
this.Currency = currency;
Amount = Round(amount, currency, rounding);
this.Currency = currency;
Amount = Round(amount, currency, rounding);
}

// int, uint ([CLSCompliant(false)]) // auto-casting to decimal so not needed
Expand Down Expand Up @@ -113,7 +113,7 @@ public Money(double amount)
/// kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that
/// result from consistently rounding a midpoint value in a single direction.</para></remarks>
public Money(double amount, string code)
: this((decimal)amount, CurrencyInfo.FromCode(code))
: this((decimal)amount, CurrencyInfo.FromCode(code))
{
}

Expand Down Expand Up @@ -164,7 +164,7 @@ public Money(long amount)
/// initialize a Money object using an integer literal, without the suffix, as follows:
/// <code>Money money = new Money(10, "EUR");</code></remarks>
public Money(long amount, string code)
: this((decimal)amount, CurrencyInfo.FromCode(code))
: this((decimal)amount, CurrencyInfo.FromCode(code))
{
}

Expand Down Expand Up @@ -200,7 +200,7 @@ public Money(ulong amount)
/// <code>Money money = new Money(10, "EUR");</code></remarks>
[CLSCompliant(false)]
public Money(ulong amount, string code)
: this((decimal)amount, CurrencyInfo.FromCode(code))
: this((decimal)amount, CurrencyInfo.FromCode(code))
{
}

Expand Down Expand Up @@ -251,12 +251,12 @@ public Money(ulong amount, Currency currency)
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = (hash * 23) + Amount.GetHashCode();
return (hash * 23) + Currency.GetHashCode();
}
unchecked
{
int hash = 17;
hash = (hash * 23) + Amount.GetHashCode();
return (hash * 23) + Currency.GetHashCode();
}
}

/// <summary>Deconstructs the current instance into its components.</summary>
Expand All @@ -277,29 +277,9 @@ private static decimal Round(in decimal amount, in Currency currency, in Midpoin
: Math.Round(amount / currencyInfo.MinimalAmount, 0, rounding) * currencyInfo.MinimalAmount;

// https://stackoverflow.com/questions/43289478/how-can-i-tell-if-a-number-is-a-power-of-10-in-kotlin-or-java
static bool IsPowerOf10(long n)
{
while (n > 9 && n % 10 == 0)
{
n /= 10;
}

return n == 1;
}

// NOT GOOD, IS ONLY FOR INT. EXTEND
static bool IsPowerOfTen(in double x)
static bool IsPowerOfTen(double x)
{
return x == 1
|| x == 10
|| x == 100
|| x == 1000
|| x == 10000
|| x == 100000
|| x == 1000000
|| x == 10000000
|| x == 100000000
|| x == 1000000000;
return x is 1 or 10 or 100 or 1000 or 10000 or 100000 or 1000000 or 10000000 or 100000000 or 1000000000;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/NodaMoney/NodaMoney.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Text.Json" Version="9.0.0" />
<PackageReference Include="System.Text.Json" Version="9.0.1" />
</ItemGroup>

</Project>
24 changes: 12 additions & 12 deletions src/NodaMoney/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
},
"System.Text.Json": {
"type": "Direct",
"requested": "[9.0.0, )",
"resolved": "9.0.0",
"contentHash": "js7+qAu/9mQvnhA4EfGMZNEzXtJCDxgkgj8ohuxq/Qxv+R56G+ljefhiJHOxTNiw54q8vmABCWUwkMulNdlZ4A==",
"requested": "[9.0.1, )",
"resolved": "9.0.1",
"contentHash": "eqWHDZqYPv1PvuvoIIx5pF74plL3iEOZOl/0kQP+Y0TEbtgNnM2W6k8h8EPYs+LTJZsXuWa92n5W5sHTWvE3VA==",
"dependencies": {
"Microsoft.Bcl.AsyncInterfaces": "9.0.0",
"Microsoft.Bcl.AsyncInterfaces": "9.0.1",
"System.Buffers": "4.5.1",
"System.IO.Pipelines": "9.0.0",
"System.IO.Pipelines": "9.0.1",
"System.Memory": "4.5.5",
"System.Runtime.CompilerServices.Unsafe": "6.0.0",
"System.Text.Encodings.Web": "9.0.0",
"System.Text.Encodings.Web": "9.0.1",
"System.Threading.Tasks.Extensions": "4.5.4"
}
},
Expand All @@ -58,8 +58,8 @@
},
"Microsoft.Bcl.AsyncInterfaces": {
"type": "Transitive",
"resolved": "9.0.0",
"contentHash": "owmu2Cr3IQ8yQiBleBHlGk8dSQ12oaF2e7TpzwJKEl4m84kkZJjEY1n33L67Y3zM5jPOjmmbdHjbfiL0RqcMRQ==",
"resolved": "9.0.1",
"contentHash": "IVkmUqf+KzbuXKrxi2tyQlg11RArYk26t2eU5cHekff+7Ao09vH8vt8idC0BJSMnpiRV2OK66zM2EwJU6Tm5Cw==",
"dependencies": {
"System.Threading.Tasks.Extensions": "4.5.4"
}
Expand All @@ -76,8 +76,8 @@
},
"System.IO.Pipelines": {
"type": "Transitive",
"resolved": "9.0.0",
"contentHash": "eA3cinogwaNB4jdjQHOP3Z3EuyiDII7MT35jgtnsA4vkn0LUrrSHsU0nzHTzFzmaFYeKV7MYyMxOocFzsBHpTw==",
"resolved": "9.0.1",
"contentHash": "uXf5o8eV/gtzDQY4lGROLFMWQvcViKcF8o4Q6KpIOjloAQXrnscQSu6gTxYJMHuNJnh7szIF9AzkaEq+zDLoEg==",
"dependencies": {
"System.Buffers": "4.5.1",
"System.Memory": "4.5.5",
Expand All @@ -101,8 +101,8 @@
},
"System.Text.Encodings.Web": {
"type": "Transitive",
"resolved": "9.0.0",
"contentHash": "e2hMgAErLbKyUUwt18qSBf9T5Y+SFAL3ZedM8fLupkVj8Rj2PZ9oxQ37XX2LF8fTO1wNIxvKpihD7Of7D/NxZw==",
"resolved": "9.0.1",
"contentHash": "XkspqduP2t1e1x2vBUAD/xZ5ZDvmywuUwsmB93MvyQLospJfqtX0GsR/kU0vUL2h4kmvf777z3txV2W4NrQ9Qg==",
"dependencies": {
"System.Buffers": "4.5.1",
"System.Memory": "4.5.5",
Expand Down
Loading

0 comments on commit 0505b1d

Please sign in to comment.