-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMoneyTests.cs
executable file
·241 lines (203 loc) · 10.1 KB
/
MoneyTests.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using FluentAssertions;
using ITLIBRIUM.BddToolkit;
using MyCompany.ECommerce.TechnicalStuff;
using Xunit;
namespace MyCompany.ECommerce.Sales.Commons;
public class MoneyTests
{
[Fact]
public void CanAddSameCurrencies() => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(1.01m, Currency.PLN))
.And(c => c.SecondValueWas(2.02m, Currency.PLN))
.When(c => c.FirstValueIsAddedToSecondValue())
.Then(c => c.ResultIs(3.03m, Currency.PLN))
.Test();
[Fact]
public void CanNotAddDifferentCurrencies() => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(1, Currency.PLN))
.And(c => c.SecondValueWas(2, Currency.EUR))
.When(c => c.FirstValueIsAddedToSecondValue())
.Then((c, r) => c.ErrorIsReturned(r))
.Test();
[Fact]
public void CanSubtractSameCurrencies() => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(1.01m, Currency.PLN))
.And(c => c.SecondValueWas(3.03m, Currency.PLN))
.When(c => c.FirstValueIsSubtractedFromSecondValue())
.Then(c => c.ResultIs(2.02m, Currency.PLN))
.Test();
[Fact]
public void CanNotSubtractDifferentCurrencies() => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(1, Currency.PLN))
.And(c => c.SecondValueWas(2, Currency.EUR))
.When(c => c.FirstValueIsSubtractedFromSecondValue())
.Then((c, r) => c.ErrorIsReturned(r))
.Test();
[Fact]
public void CanMultiplyByInteger() => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(1.01m, Currency.PLN))
.And(c => c.MultiplierWas(2))
.When(c => c.FirstValueIsMultipliedByInt())
.Then(c => c.ResultIs(2.02m, Currency.PLN))
.Test();
[Fact]
public void CanMultiplyByDecimal() => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(1.10m, Currency.PLN))
.And(c => c.MultiplierWas(2.5m))
.When(c => c.FirstValueIsMultipliedByDecimal())
.Then(c => c.ResultIs(2.75m, Currency.PLN))
.Test();
[Fact]
public void CanMultiplyByPercentage() => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(1.10m, Currency.PLN))
.And(c => c.PercentageMultiplierWas(250), "Multiplier is 250%")
.When(c => c.FirstValueIsMultipliedByPercentage())
.Then(c => c.ResultIs(2.75m, Currency.PLN))
.Test();
[Fact]
public void CanDividedByInteger() => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(2.02m, Currency.PLN))
.And(c => c.DividerWas(2))
.When(c => c.FirstValueIsDividedByInt())
.Then(c => c.ResultIs(1.01m, Currency.PLN))
.Test();
[Fact]
public void CanDividedByDecimal() => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(2.75m, Currency.PLN))
.And(c => c.DividerWas(2.5m))
.When(c => c.FirstValueIsDividedByDecimal())
.Then(c => c.ResultIs(1.10m, Currency.PLN))
.Test();
[Fact]
public void CanDividedByMoney() => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(2.75m, Currency.PLN))
.And(c => c.SecondValueWas(2.50m, Currency.PLN))
.When(c => c.FirstValueIsDividedBySecondValue())
.Then(c => c.ResultIs(110), "Result is 110%")
.Test();
[Fact]
public void CanCalculateMaxValueForSameCurrencies() => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(2.75m, Currency.PLN))
.And(c => c.SecondValueWas(2.50m, Currency.PLN))
.When(c => c.MaxValueIsCalculated())
.Then(c => c.ResultIs(2.75m, Currency.PLN))
.Test();
[Fact]
public void CanNotCalculateMaxValueForDifferentCurrencies() => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(2.75m, Currency.PLN))
.And(c => c.SecondValueWas(2.50m, Currency.EUR))
.When(c => c.MaxValueIsCalculated())
.Then((c, r) => c.ErrorIsReturned(r))
.Test();
[Theory]
[InlineData(1.01, 1.01, Comparison.Equal, true)]
[InlineData(1.01, 1.02, Comparison.Equal, false)]
[InlineData(1.01, 1.02, Comparison.NotEqual, true)]
[InlineData(1.01, 1.01, Comparison.NotEqual, false)]
[InlineData(1.02, 1.01, Comparison.Greater, true)]
[InlineData(1.01, 1.02, Comparison.Greater, false)]
[InlineData(1.02, 1.02, Comparison.GreaterOrEqual, true)]
[InlineData(1.01, 1.02, Comparison.GreaterOrEqual, false)]
[InlineData(1.01, 1.02, Comparison.Less, true)]
[InlineData(1.02, 1.01, Comparison.Less, false)]
[InlineData(1.02, 1.02, Comparison.LessOrEqual, true)]
[InlineData(1.02, 1.01, Comparison.LessOrEqual, false)]
public void CanCompareSameCurrencies(decimal firstValue, decimal secondValue, Comparison comparison,
bool result) => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(firstValue, Currency.PLN))
.And(c => c.SecondValueWas(secondValue, Currency.PLN))
.When(c => c.FirstValueIsComparedToSecondValue(comparison))
.Then(c => c.ResultIs(result))
.Test();
[Theory]
[InlineData(Comparison.Equal, false)]
[InlineData(Comparison.NotEqual, true)]
public void ValuesWithDifferentCurrenciesAreNotEqual(Comparison comparison, bool result) => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(1.01m, Currency.PLN))
.And(c => c.SecondValueWas(1.01m, Currency.EUR))
.When(c => c.FirstValueIsComparedToSecondValue(comparison))
.Then(c => c.ResultIs(result))
.Test();
[Theory]
[InlineData(Comparison.Greater)]
[InlineData(Comparison.GreaterOrEqual)]
[InlineData(Comparison.Less)]
[InlineData(Comparison.LessOrEqual)]
public void CanNotCompareDifferentCurrencies(Comparison comparison) => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(1.01m, Currency.PLN))
.And(c => c.SecondValueWas(1.02m, Currency.EUR))
.When(c => c.FirstValueIsComparedToSecondValue(comparison))
.Then((c, r) => c.ErrorIsReturned(r))
.Test();
[Theory]
[InlineData(1.01, Currency.PLN, "1.01 PLN")]
[InlineData(-1.01, Currency.EUR, "-1.01 EUR")]
[InlineData(1, Currency.PLN, "1.00 PLN")]
public void ToStringProducesHumanReadableValue(decimal value, Currency currency, string result) => Bdd.Scenario<Context>()
.Given(c => c.FirstValueWas(value, currency))
.When(c => c.StringValueIsCreatedForFirstValue())
.Then(c => c.ResultIs(result))
.Test();
public enum Comparison
{
Equal,
NotEqual,
Greater,
GreaterOrEqual,
Less,
LessOrEqual,
}
private class Context
{
private Money _firstValue;
private Money _secondValue;
private int _intMultiplier;
private decimal _decimalMultiplier;
private Percentage _percentageMultiplier;
private int _intDivider;
private decimal _decimalDivider;
private Money _moneyResult;
private Percentage _percentageResult;
private bool _boolResult;
private string _stringResult;
public void FirstValueWas(decimal value, Currency currency) => _firstValue = Money.Of(value, currency);
public void SecondValueWas(decimal value, Currency currency) => _secondValue = Money.Of(value, currency);
public void MultiplierWas(int multiplier) => _intMultiplier = multiplier;
public void MultiplierWas(decimal multiplier) => _decimalMultiplier = multiplier;
public void PercentageMultiplierWas(int multiplier) => _percentageMultiplier = Percentage.Of(multiplier);
public void DividerWas(int divider) => _intDivider = divider;
public void DividerWas(decimal divider) => _decimalDivider = divider;
public void FirstValueIsAddedToSecondValue() => _moneyResult = _firstValue + _secondValue;
public void FirstValueIsSubtractedFromSecondValue() => _moneyResult = _secondValue - _firstValue;
public void FirstValueIsMultipliedByInt() => _moneyResult = _firstValue * _intMultiplier;
public void FirstValueIsMultipliedByDecimal() => _moneyResult = _firstValue * _decimalMultiplier;
public void FirstValueIsMultipliedByPercentage() => _moneyResult = _firstValue * _percentageMultiplier;
public void FirstValueIsDividedByInt() => _moneyResult = _firstValue / _intDivider;
public void FirstValueIsDividedByDecimal() => _moneyResult = _firstValue / _decimalDivider;
public void FirstValueIsDividedBySecondValue() => _percentageResult = _firstValue / _secondValue;
public void MaxValueIsCalculated() => _moneyResult = Money.Max(_firstValue, _secondValue);
public void StringValueIsCreatedForFirstValue() => _stringResult = _firstValue.ToString();
public void FirstValueIsComparedToSecondValue(Comparison comparison) =>
_boolResult = comparison switch
{
Comparison.Equal => (_firstValue == _secondValue),
Comparison.NotEqual => (_firstValue != _secondValue),
Comparison.Greater => (_firstValue > _secondValue),
Comparison.GreaterOrEqual => (_firstValue >= _secondValue),
Comparison.Less => (_firstValue < _secondValue),
Comparison.LessOrEqual => (_firstValue <= _secondValue),
_ => throw new ArgumentOutOfRangeException(nameof(comparison))
};
public void ResultIs(decimal value, Currency currency) =>
_moneyResult.Should().Be(Money.Of(value, currency));
public void ResultIs(int value) => _percentageResult.Should().Be(Percentage.Of(value));
public void ResultIs(bool result) => _boolResult.Should().Be(result);
public void ResultIs(string result) => _stringResult.Should().Be(result);
// ReSharper disable once MemberCanBeMadeStatic.Local
public void ErrorIsReturned(Result result)
{
result.IsSuccessful.Should().BeFalse();
result.Exception.Should().BeOfType<DomainError>();
}
}
}