-
Notifications
You must be signed in to change notification settings - Fork 153
/
MoneySpecs.cs
160 lines (139 loc) · 4.93 KB
/
MoneySpecs.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using DddInPractice.Logic;
using DddInPractice.Logic.SharedKernel;
using FluentAssertions;
using Xunit;
namespace DddInPractice.Tests
{
public class MoneySpecs
{
[Fact]
public void Sum_of_two_moneys_produces_correct_result()
{
Money money1 = new Money(1, 2, 3, 4, 5, 6);
Money money2 = new Money(1, 2, 3, 4, 5, 6);
Money sum = money1 + money2;
sum.OneCentCount.Should().Be(2);
sum.TenCentCount.Should().Be(4);
sum.QuarterCount.Should().Be(6);
sum.OneDollarCount.Should().Be(8);
sum.FiveDollarCount.Should().Be(10);
sum.TwentyDollarCount.Should().Be(12);
}
[Fact]
public void Two_money_instances_equal_if_contain_the_same_money_amount()
{
Money money1 = new Money(1, 2, 3, 4, 5, 6);
Money money2 = new Money(1, 2, 3, 4, 5, 6);
money1.Should().Be(money2);
money1.GetHashCode().Should().Be(money2.GetHashCode());
}
[Fact]
public void Two_money_instances_do_not_equal_if_contain_different_money_amounts()
{
Money dollar = new Money(0, 0, 0, 1, 0, 0);
Money hundredCents = new Money(100, 0, 0, 0, 0, 0);
dollar.Should().NotBe(hundredCents);
dollar.GetHashCode().Should().NotBe(hundredCents.GetHashCode());
}
[Theory]
[InlineData(-1, 0, 0, 0, 0, 0)]
[InlineData(0, -2, 0, 0, 0, 0)]
[InlineData(0, 0, -3, 0, 0, 0)]
[InlineData(0, 0, 0, -4, 0, 0)]
[InlineData(0, 0, 0, 0, -5, 0)]
[InlineData(0, 0, 0, 0, 0, -6)]
public void Cannot_create_money_with_negative_value(
int oneCentCount,
int tenCentCount,
int quarterCount,
int oneDollarCount,
int fiveDollarCount,
int twentyDollarCount)
{
Action action = () => new Money(
oneCentCount,
tenCentCount,
quarterCount,
oneDollarCount,
fiveDollarCount,
twentyDollarCount);
action.ShouldThrow<InvalidOperationException>();
}
[Theory]
[InlineData(0, 0, 0, 0, 0, 0, 0)]
[InlineData(1, 0, 0, 0, 0, 0, 0.01)]
[InlineData(1, 2, 0, 0, 0, 0, 0.21)]
[InlineData(1, 2, 3, 0, 0, 0, 0.96)]
[InlineData(1, 2, 3, 4, 0, 0, 4.96)]
[InlineData(1, 2, 3, 4, 5, 0, 29.96)]
[InlineData(1, 2, 3, 4, 5, 6, 149.96)]
[InlineData(11, 0, 0, 0, 0, 0, 0.11)]
[InlineData(110, 0, 0, 0, 100, 0, 501.1)]
public void Amount_is_calculated_correctly(
int oneCentCount,
int tenCentCount,
int quarterCount,
int oneDollarCount,
int fiveDollarCount,
int twentyDollarCount,
double expectedAmount)
{
Money money = new Money(
oneCentCount,
tenCentCount,
quarterCount,
oneDollarCount,
fiveDollarCount,
twentyDollarCount);
money.Amount.ShouldBeEquivalentTo(expectedAmount);
}
[Fact]
public void Subtraction_of_two_moneys_produces_correct_result()
{
Money money1 = new Money(10, 10, 10, 10, 10, 10);
Money money2 = new Money(1, 2, 3, 4, 5, 6);
Money result = money1 - money2;
result.OneCentCount.Should().Be(9);
result.TenCentCount.Should().Be(8);
result.QuarterCount.Should().Be(7);
result.OneDollarCount.Should().Be(6);
result.FiveDollarCount.Should().Be(5);
result.TwentyDollarCount.Should().Be(4);
}
[Fact]
public void Cannot_subtract_more_than_exists()
{
Money money1 = new Money(0, 1, 0, 0, 0, 0);
Money money2 = new Money(1, 0, 0, 0, 0, 0);
Action action = () =>
{
Money money = money1 - money2;
};
action.ShouldThrow<InvalidOperationException>();
}
[Theory]
[InlineData(1, 0, 0, 0, 0, 0, "¢1")]
[InlineData(0, 0, 0, 1, 0, 0, "$1.00")]
[InlineData(1, 0, 0, 1, 0, 0, "$1.01")]
[InlineData(0, 0, 2, 1, 0, 0, "$1.50")]
public void To_string_returns_correct_string_representation(
int oneCentCount,
int tenCentCount,
int quarterCount,
int oneDollarCount,
int fiveDollarCount,
int twentyDollarCount,
string expectedString)
{
Money money = new Money(
oneCentCount,
tenCentCount,
quarterCount,
oneDollarCount,
fiveDollarCount,
twentyDollarCount);
money.ToString().Should().Be(expectedString);
}
}
}