Skip to content

Commit 47b4f1b

Browse files
Merge pull request #115 from TransactionProcessing/task/#73_moneyvalueobject
Added money value objects
2 parents f00d682 + f46505c commit 47b4f1b

5 files changed

Lines changed: 246 additions & 2 deletions

File tree

Shared.EventStore.Tests/Shared.EventStore.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5-
5+
<DebugType>None</DebugType>
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
88

Shared.Tests/MoneyTests.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Shared.Tests
8+
{
9+
using System.Diagnostics.PerformanceData;
10+
using Shouldly;
11+
using ValueObjects;
12+
using Xunit;
13+
14+
public class MoneyTests
15+
{
16+
[Theory]
17+
[InlineData(0)]
18+
[InlineData(1.00)]
19+
[InlineData(1.59)]
20+
[InlineData(100.00)]
21+
[InlineData(-1.59)]
22+
public void Money_CanBeCreated_IsCreated(Decimal moneyValue)
23+
{
24+
Money money = Money.Create(moneyValue);
25+
26+
money.ShouldNotBeNull();
27+
money.Value.ShouldBe(moneyValue);
28+
}
29+
30+
[Theory]
31+
[InlineData(1,100,101)]
32+
[InlineData(0, 100, 100)]
33+
[InlineData(-1, 100, 99)]
34+
[InlineData(1.01, 100.01, 101.02)]
35+
[InlineData(0.01, 100.01, 100.02)]
36+
[InlineData(-1.01, 100.01, 99)]
37+
public void Money_AddOperator_ValueAdded(Decimal initialValue, Decimal valueToAdd, Decimal expectedResult)
38+
{
39+
Money money1 = Money.Create(initialValue);
40+
Money money2 = Money.Create(valueToAdd);
41+
42+
var result = money1 + money2;
43+
result.Value.ShouldBe(expectedResult);
44+
}
45+
46+
[Theory]
47+
[InlineData(1, 100, -99)]
48+
[InlineData(0, 100, -100)]
49+
[InlineData(-1, 100, -101)]
50+
[InlineData(1.01, 100.01, -99)]
51+
[InlineData(0.01, 100.01, -100)]
52+
[InlineData(-1.01, 100.01, -101.02)]
53+
public void Money_SubtractOperator_ValueAdded(Decimal initialValue, Decimal valueToSubtract, Decimal expectedResult)
54+
{
55+
Money money1 = Money.Create(initialValue);
56+
Money money2 = Money.Create(valueToSubtract);
57+
58+
var result = money1 - money2;
59+
result.Value.ShouldBe(expectedResult);
60+
}
61+
}
62+
63+
public class PositiveMoneyTests
64+
{
65+
[Theory]
66+
[InlineData(1.00)]
67+
[InlineData(1.59)]
68+
[InlineData(100.00)]
69+
public void PositiveMoney_CanBeCreated_IsCreated(Decimal moneyValue)
70+
{
71+
PositiveMoney money = PositiveMoney.Create(Money.Create(moneyValue));
72+
73+
money.ShouldNotBeNull();
74+
money.Value.ShouldBe(moneyValue);
75+
}
76+
77+
[Theory]
78+
[InlineData(0)]
79+
[InlineData(-1)]
80+
public void PositiveMoney_NonPositiveAmountRejected_ErrorThrown(Decimal moneyValue)
81+
{
82+
Should.Throw<ArgumentOutOfRangeException>(() =>
83+
{
84+
PositiveMoney.Create(Money.Create(moneyValue));
85+
});
86+
}
87+
}
88+
}

Shared.Tests/Shared.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5-
5+
<DebugType>None</DebugType>
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
88

Shared/ValueObjects/Money.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
namespace Shared.ValueObjects
2+
{
3+
using System;
4+
5+
/// <summary>
6+
///
7+
/// </summary>
8+
/// <seealso cref="System.IEquatable&lt;Shared.ValueObjects.Money&gt;" />
9+
public record Money
10+
{
11+
#region Constructors
12+
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="Money"/> class.
15+
/// </summary>
16+
/// <param name="value">The value.</param>
17+
private Money(Decimal value)
18+
{
19+
this.Value = value;
20+
}
21+
22+
#endregion
23+
24+
#region Properties
25+
26+
/// <summary>
27+
/// Gets the value.
28+
/// </summary>
29+
/// <value>
30+
/// The value.
31+
/// </value>
32+
public Decimal Value { get; }
33+
34+
#endregion
35+
36+
#region Methods
37+
38+
/// <summary>
39+
/// Creates the specified value.
40+
/// </summary>
41+
/// <param name="value">The value.</param>
42+
/// <returns></returns>
43+
public static Money Create(Decimal value)
44+
{
45+
return new Money(value);
46+
}
47+
48+
/// <summary>
49+
/// Implements the operator +.
50+
/// </summary>
51+
/// <param name="leftHand">The left hand.</param>
52+
/// <param name="rightHand">The right hand.</param>
53+
/// <returns>
54+
/// The result of the operator.
55+
/// </returns>
56+
public static Money operator +(Money leftHand,
57+
Money rightHand)
58+
{
59+
return new Money(leftHand.Value + rightHand.Value);
60+
}
61+
62+
/// <summary>
63+
/// Implements the operator -.
64+
/// </summary>
65+
/// <param name="leftHand">The left hand.</param>
66+
/// <param name="rightHand">The right hand.</param>
67+
/// <returns>
68+
/// The result of the operator.
69+
/// </returns>
70+
public static Money operator -(Money leftHand,
71+
Money rightHand)
72+
{
73+
return new Money(leftHand.Value - rightHand.Value);
74+
}
75+
76+
#endregion
77+
}
78+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
namespace Shared.ValueObjects
2+
{
3+
using System;
4+
5+
/// <summary>
6+
///
7+
/// </summary>
8+
/// <seealso cref="System.IEquatable&lt;Shared.ValueObjects.PositiveMoney&gt;" />
9+
public record PositiveMoney
10+
{
11+
#region Constructors
12+
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="PositiveMoney"/> class.
15+
/// </summary>
16+
/// <param name="moneyAmount">The money amount.</param>
17+
private PositiveMoney(Money moneyAmount)
18+
{
19+
this.GuardMonetaryAmount(moneyAmount);
20+
this.Money = moneyAmount;
21+
}
22+
23+
#endregion
24+
25+
#region Properties
26+
27+
/// <summary>
28+
/// Gets the value.
29+
/// </summary>
30+
/// <value>
31+
/// The value.
32+
/// </value>
33+
public Decimal Value
34+
{
35+
get
36+
{
37+
return this.Money.Value;
38+
}
39+
}
40+
41+
/// <summary>
42+
/// Gets the money.
43+
/// </summary>
44+
/// <value>
45+
/// The money.
46+
/// </value>
47+
private Money Money { get; }
48+
49+
#endregion
50+
51+
#region Methods
52+
53+
/// <summary>
54+
/// Creates the specified money amount.
55+
/// </summary>
56+
/// <param name="moneyAmount">The money amount.</param>
57+
/// <returns></returns>
58+
public static PositiveMoney Create(Money moneyAmount)
59+
{
60+
return new PositiveMoney(moneyAmount);
61+
}
62+
63+
/// <summary>
64+
/// Guards the monetary amount.
65+
/// </summary>
66+
/// <param name="moneyAmount">The money amount.</param>
67+
/// <exception cref="System.ArgumentOutOfRangeException">Value of {moneyAmount.Value} is not a valid value</exception>
68+
private void GuardMonetaryAmount(Money moneyAmount)
69+
{
70+
if (moneyAmount.Value <= 0)
71+
{
72+
throw new ArgumentOutOfRangeException($"Value of {moneyAmount.Value} is not a valid value");
73+
}
74+
}
75+
76+
#endregion
77+
}
78+
}

0 commit comments

Comments
 (0)