Skip to content

Commit

Permalink
Close #49
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémy van Duijkeren committed Aug 17, 2017
1 parent e8c5ca1 commit 23920cc
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
54 changes: 54 additions & 0 deletions src/NodaMoney/Money.BinaryOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ public partial struct Money
return 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 +(Money left, decimal right)
{
return 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 +(decimal left, Money right)
{
return 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>
Expand All @@ -21,6 +39,24 @@ public partial struct Money
return 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 -(Money left, decimal right)
{
return 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 -(decimal left, Money right)
{
return 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>
Expand Down Expand Up @@ -69,6 +105,15 @@ public static Money Add(Money money1, Money money2)
return new Money(decimal.Add(money1.Amount, money2.Amount), money1.Currency);
}

/// <summary>Adds two specified <see cref="Money"/> values.</summary>
/// <param name="money1">The first <see cref="Money"/> object.</param>
/// <param name="money2">The second <see cref="decimal"/> object.</param>
/// <returns>A <see cref="Money"/> object with the values of both <see cref="decimal"/> objects added.</returns>
public static Money Add(Money money1, decimal money2)
{
return new Money(decimal.Add(money1.Amount, money2), money1.Currency);
}

/// <summary>Subtracts one specified <see cref="Money"/> value from another.</summary>
/// <param name="money1">The first <see cref="Money"/> object.</param>
/// <param name="money2">The second <see cref="Money"/> object.</param>
Expand All @@ -79,6 +124,15 @@ public static Money Subtract(Money money1, Money money2)
return new Money(decimal.Subtract(money1.Amount, money2.Amount), money1.Currency);
}

/// <summary>Subtracts one specified <see cref="Money"/> value from another.</summary>
/// <param name="money1">The first <see cref="Money"/> object.</param>
/// <param name="money2">The second <see cref="decimal"/> object.</param>
/// <returns>A <see cref="Money"/> object where the second <see cref="decimal"/> object is subtracted from the first.</returns>
public static Money Subtract(Money money1, decimal money2)
{
return new Money(decimal.Subtract(money1.Amount, money2), money1.Currency);
}

/// <summary>Multiplies the specified money.</summary>
/// <param name="money">The money.</param>
/// <param name="multiplier">The multiplier.</param>
Expand Down
59 changes: 57 additions & 2 deletions tests/NodaMoney.Tests/MoneyBinaryOperatorsSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FluentAssertions.Common;

using Xunit;
using NodaMoney.Tests.Helpers;
// ReSharper disable All

namespace NodaMoney.Tests.MoneyBinaryOperatorsSpec
Expand All @@ -22,7 +23,7 @@ public class GivenIWantToAddAndSubstractMoney
};

[Theory, MemberData("TestData")]
public void WhenUsingAdditionOperator_ThenMoneyShouldBeAdd(decimal value1, decimal value2, decimal expected)
public void WhenUsingAdditionOperator_ThenMoneyShouldBeAdded(decimal value1, decimal value2, decimal expected)
{
var money1 = new Money(value1);
var money2 = new Money(value2);
Expand All @@ -35,7 +36,7 @@ public void WhenUsingAdditionOperator_ThenMoneyShouldBeAdd(decimal value1, decim
}

[Theory, MemberData("TestData")]
public void WhenUsingAdditionMethod_ThenMoneyShouldBeAdd(decimal value1, decimal value2, decimal expected)
public void WhenUsingAdditionMethod_ThenMoneyShouldBeAdded(decimal value1, decimal value2, decimal expected)
{
var money1 = new Money(value1);
var money2 = new Money(value2);
Expand Down Expand Up @@ -116,6 +117,60 @@ public void WhenUsingSubstractionMethodWithDifferentCurrency_ThenThrowException(

action.ShouldThrow<InvalidCurrencyException>().WithMessage("The requested operation expected the currency*");
}

[Theory, MemberData("TestData")]
[UseCulture("en-us")]
public void WhenUsingAddtionOperatorWithDecimal_ThenMoneyShouldBeAdded(decimal value1, decimal value2, decimal expected)
{
var money1 = new Money(value1, "EUR");

Money result1 = money1 + value2;
Money result2 = value2 + money1;

result1.Should().Be(new Money(expected, "EUR"));
result1.Should().NotBeSameAs(money1);
result2.Should().Be(new Money(expected, "EUR"));
result2.Should().NotBeSameAs(money1);
}

[Theory, MemberData("TestData")]
[UseCulture("en-us")]
public void WhenUsingAdditionMethodWithDecimal_ThenMoneyShouldBeAdded(decimal value1, decimal value2, decimal expected)
{
var money1 = new Money(value1, "EUR");

var result = Money.Add(money1, value2);

result.Should().Be(new Money(expected, "EUR"));
result.Should().NotBeSameAs(money1);
}

[Theory, MemberData("TestData")]
[UseCulture("en-us")]
public void WhenUsingSubstractionOperatorWithDecimal_ThenMoneyShouldBeAdded(decimal expected, decimal value2, decimal value1)
{
var money1 = new Money(value1, "EUR");

Money result1 = money1 - value2;
Money result2 = value2 - money1;

result1.Should().Be(new Money(expected, "EUR"));
result1.Should().NotBeSameAs(money1);
result2.Should().Be(new Money(expected, "EUR"));
result2.Should().NotBeSameAs(money1);
}

[Theory, MemberData("TestData")]
[UseCulture("en-us")]
public void WhenUsingSubstractionMethodWithDecimal_ThenMoneyShouldBeSubtracted(decimal expected, decimal value2, decimal value1)
{
var money1 = new Money(value1, "EUR");

var result = Money.Subtract(money1, value2);

result.Should().Be(new Money(expected, "EUR"));
result.Should().NotBeSameAs(money1);
}
}

public class GivenIWantToMultiplyAndDivideMoney
Expand Down

0 comments on commit 23920cc

Please sign in to comment.