-
Notifications
You must be signed in to change notification settings - Fork 45
/
MoneyFormattableSpec.cs
413 lines (366 loc) · 17 KB
/
MoneyFormattableSpec.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
using System;
using System.Globalization;
using System.Threading;
using FluentAssertions;
using Xunit;
using NodaMoney.Tests.Helpers;
namespace NodaMoney.Tests.MoneyFormattableSpec
{
public class GivenIWantMoneyAsString
{
private Money _yen = new Money(765.4321m, Currency.FromCode("JPY"));
private Money _euro = new Money(765.4321m, Currency.FromCode("EUR"));
private Money _dollar = new Money(765.4321m, Currency.FromCode("USD"));
private Money _dinar = new Money(765.4321m, Currency.FromCode("BHD"));
[Fact]
[UseCulture("en-US")]
public void WhenToStringAndCurrentCultureUS_ThenDecimalsFollowsCurrencyAndAmountFollowsCurrentCultureUS()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString().Should().Be("¥765");
_euro.ToString().Should().Be("€765.43");
_dollar.ToString().Should().Be("$765.43");
_dinar.ToString().Should().Be("BD765.432");
}
[Fact]
[UseCulture("nl-NL")]
public void WhenToStringAndCurrentCultureNL_ThenDecimalsFollowsCurrencyAndAmountFollowsCurrentCultureNL()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("nl-NL");
_yen.ToString().Should().Be("¥ 765");
_euro.ToString().Should().Be("€ 765,43");
_dollar.ToString().Should().Be("$ 765,43");
_dinar.ToString().Should().Be("BD 765,432");
}
[Fact]
[UseCulture("fr-FR")]
public void WhenToStringAndCurrentCultureFR_ThenDecimalsFollowsCurrencyAndAmountFollowsCurrentCultureFR()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("fr-FR");
_yen.ToString().Should().Be("765 ¥");
_euro.ToString().Should().Be("765,43 €");
_dollar.ToString().Should().Be("765,43 $");
_dinar.ToString().Should().Be("765,432 BD");
}
[Fact]
public void WhenNullFormat_ThenThisShouldNotThrow()
{
Action action = () => _yen.ToString((string)null);
action.Should().NotThrow<ArgumentNullException>();
}
[Fact]
[UseCulture("en-US")]
public void WhenGivenCultureInfo_ThenDecimalsFollowsCurrencyAndAmountFollowsGivenCultureInfo()
{
var ci = new CultureInfo("nl-NL");
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString(ci).Should().Be("¥ 765");
_euro.ToString(ci).Should().Be("€ 765,43");
_dollar.ToString(ci).Should().Be("$ 765,43");
_dinar.ToString(ci).Should().Be("BD 765,432");
}
[Fact]
[UseCulture("en-US")]
public void WhenGivenNumberFormat_ThenDecimalsFollowsCurrencyAndAmountFollowsGivenNumberFormat()
{
var nfi = new CultureInfo("nl-NL").NumberFormat;
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString(nfi).Should().Be("¥ 765");
_euro.ToString(nfi).Should().Be("€ 765,43");
_dollar.ToString(nfi).Should().Be("$ 765,43");
_dinar.ToString(nfi).Should().Be("BD 765,432");
}
[Fact]
public void WhenNullFormatNumberFormatIsUsed_ThenThisShouldNotThrow()
{
Action action = () => _yen.ToString((NumberFormatInfo)null);
action.Should().NotThrow<ArgumentNullException>();
}
[Fact]
public void WhenUsingToStringWithOneStringArgument_ThenExpectsTheSameAsWithTwoArguments()
{
string oneParameter() => _yen.ToString((string)null);
string twoParameter() => _yen.ToString(null, null);
oneParameter().Should().Be(twoParameter());
}
[Fact]
public void WhenUsingToStringWithOneProviderArgument_ThenExpectsTheSameAsWithTwoArguments()
{
string oneParameter() => _yen.ToString((IFormatProvider)null);
string twoParameter() => _yen.ToString(null, null);
oneParameter().Should().Be(twoParameter());
}
[Fact]
public void WhenUsingToStringWithGFormat_ThenReturnsTheSameAsTheDefaultFormat()
{
// according to https://docs.microsoft.com/en-us/dotnet/api/system.iformattable.tostring?view=netframework-4.7.2#notes-to-implementers
// the result of using "G" should return the same result as using <null>
_yen.ToString("G", null).Should().Be(_yen.ToString(null, null));
}
[Fact]
public void WhenUsingToStringWithGFormatWithCulture_ThenReturnsTheSameAsTheDefaultFormatWithThatCulture()
{
_yen.ToString("G", CultureInfo.InvariantCulture).Should().Be(_yen.ToString(null, CultureInfo.InvariantCulture));
_yen.ToString("G", CultureInfo.GetCultureInfo("nl-NL")).Should().Be(_yen.ToString(null, CultureInfo.GetCultureInfo("nl-NL")));
_yen.ToString("G", CultureInfo.GetCultureInfo("fr-FR")).Should().Be(_yen.ToString(null, CultureInfo.GetCultureInfo("fr-FR")));
}
[Fact]
public void WhenNumberOfDecimalsIsNotApplicable_ThenToStringShouldNotFail()
{
var xdr = new Money(765.4321m, Currency.FromCode("XDR"));
Action action = () => xdr.ToString();
action.Should().NotThrow<Exception>();
}
}
public class GivenIWantMoneyAsStringWithCurrencySymbol
{
private Money _yen = new Money(765.4321m, Currency.FromCode("JPY"));
private Money _euro = new Money(765.4321m, Currency.FromCode("EUR"));
private Money _dollar = new Money(765.4321m, Currency.FromCode("USD"));
private Money _dinar = new Money(765.4321m, Currency.FromCode("BHD"));
[Fact]
[UseCulture("en-US")]
public void WhenCurrentCultureUS_ThenDecimalsFollowsCurrencyAndAmountFollowsCurrentCultureNL()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("C").Should().Be("¥765");
_euro.ToString("C").Should().Be("€765.43");
_dollar.ToString("C").Should().Be("$765.43");
_dinar.ToString("C").Should().Be("BD765.432");
}
[Fact]
[UseCulture("nl-NL")]
public void WhenCurrentCultureNL_ThenDecimalsFollowsCurrencyAndAmountFollowsCurrentCultureNL()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("nl-NL");
_yen.ToString("C").Should().Be("¥ 765");
_euro.ToString("C").Should().Be("€ 765,43");
_dollar.ToString("C").Should().Be("$ 765,43");
_dinar.ToString("C").Should().Be("BD 765,432");
}
[Fact]
[UseCulture("fr-FR")]
public void WhenCurrentCultureFR_ThenDecimalsFollowsCurrencyAndAmountFollowsCurrentCultureFR()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("fr-FR");
_yen.ToString("C").Should().Be("765 ¥");
_euro.ToString("C").Should().Be("765,43 €");
_dollar.ToString("C").Should().Be("765,43 $");
_dinar.ToString("C").Should().Be("765,432 BD");
}
[Fact]
[UseCulture("en-US")]
public void WhenZeroDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("C0").Should().Be("¥765");
_euro.ToString("C0").Should().Be("€765");
_dollar.ToString("C0").Should().Be("$765");
_dinar.ToString("C0").Should().Be("BD765");
}
[Fact]
[UseCulture("en-US")]
public void WhenOneDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("C1").Should().Be("¥765.0");
_euro.ToString("C1").Should().Be("€765.4");
_dollar.ToString("C1").Should().Be("$765.4");
_dinar.ToString("C1").Should().Be("BD765.4");
}
[Fact]
[UseCulture("en-US")]
public void WhenTwoDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("C2").Should().Be("¥765.00");
_euro.ToString("C2").Should().Be("€765.43");
_dollar.ToString("C2").Should().Be("$765.43");
_dinar.ToString("C2").Should().Be("BD765.43");
}
[Fact]
[UseCulture("en-US")]
public void WhenThreeDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("C3").Should().Be("¥765.000");
_euro.ToString("C3").Should().Be("€765.430");
_dollar.ToString("C3").Should().Be("$765.430");
_dinar.ToString("C3").Should().Be("BD765.432");
}
[Fact]
[UseCulture("en-US")]
public void WhenFourDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("C4").Should().Be("¥765.0000");
_euro.ToString("C4").Should().Be("€765.4300");
_dollar.ToString("C4").Should().Be("$765.4300");
_dinar.ToString("C4").Should().Be("BD765.4320");
}
}
public class GivenIWantMoneyAsStringWithCurrencyCode
{
private Money _yen = new Money(765.4321m, Currency.FromCode("JPY"));
private Money _euro = new Money(765.4321m, Currency.FromCode("EUR"));
private Money _dollar = new Money(765.4321m, Currency.FromCode("USD"));
private Money _dinar = new Money(765.4321m, Currency.FromCode("BHD"));
[Fact]
[UseCulture("en-US")]
public void WhenCurrentCultureUS_ThenDecimalsFollowsCurrencyAndAmountFollowsCurrentCultureNL()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("I").Should().Be("JPY 765");
_euro.ToString("I").Should().Be("EUR 765.43");
_dollar.ToString("I").Should().Be("USD 765.43");
_dinar.ToString("I").Should().Be("BHD 765.432");
}
[Fact]
[UseCulture("nl-NL")]
public void WhenCurrentCultureNL_ThenDecimalsFollowsCurrencyAndAmountFollowsCurrentCultureNL()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("nl-NL");
_yen.ToString("I").Should().Be("JPY 765");
_euro.ToString("I").Should().Be("EUR 765,43");
_dollar.ToString("I").Should().Be("USD 765,43");
_dinar.ToString("I").Should().Be("BHD 765,432");
}
[Fact]
[UseCulture("fr-FR")]
public void WhenCurrentCultureFR_ThenDecimalsFollowsCurrencyAndAmountFollowsCurrentCultureFR()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("fr-FR");
_yen.ToString("I").Should().Be("765 JPY");
_euro.ToString("I").Should().Be("765,43 EUR");
_dollar.ToString("I").Should().Be("765,43 USD");
_dinar.ToString("I").Should().Be("765,432 BHD");
}
[Fact]
[UseCulture("en-US")]
public void WhenZeroDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("I0").Should().Be("JPY 765");
_euro.ToString("I0").Should().Be("EUR 765");
_dollar.ToString("I0").Should().Be("USD 765");
_dinar.ToString("I0").Should().Be("BHD 765");
}
[Fact]
[UseCulture("en-US")]
public void WhenOneDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("I1").Should().Be("JPY 765.0");
_euro.ToString("I1").Should().Be("EUR 765.4");
_dollar.ToString("I1").Should().Be("USD 765.4");
_dinar.ToString("I1").Should().Be("BHD 765.4");
}
[Fact]
[UseCulture("en-US")]
public void WhenTwoDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("I2").Should().Be("JPY 765.00");
_euro.ToString("I2").Should().Be("EUR 765.43");
_dollar.ToString("I2").Should().Be("USD 765.43");
_dinar.ToString("I2").Should().Be("BHD 765.43");
}
[Fact]
[UseCulture("en-US")]
public void WhenThreeDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("I3").Should().Be("JPY 765.000");
_euro.ToString("I3").Should().Be("EUR 765.430");
_dollar.ToString("I3").Should().Be("USD 765.430");
_dinar.ToString("I3").Should().Be("BHD 765.432");
}
[Fact]
[UseCulture("en-US")]
public void WhenFourDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("I4").Should().Be("JPY 765.0000");
_euro.ToString("I4").Should().Be("EUR 765.4300");
_dollar.ToString("I4").Should().Be("USD 765.4300");
_dinar.ToString("I4").Should().Be("BHD 765.4320");
}
}
public class GivenIWantMoneyAsStringWithEnglishCurrencyName
{
private Money _yen = new Money(765.4321m, Currency.FromCode("JPY"));
private Money _euro = new Money(765.4321m, Currency.FromCode("EUR"));
private Money _dollar = new Money(765.4321m, Currency.FromCode("USD"));
private Money _dinar = new Money(765.4321m, Currency.FromCode("BHD"));
[Fact]
[UseCulture("pt-BR")]
public void WhenCurrentCulturePTBR_ThenDecimalsFollowsCurrencyAndAmountFollowsCurrentCulturePtBRAndCurrencyNameIsInEnglish()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("pt-BR");
_yen.ToString("F").Should().Be("765 Japanese yen");
_euro.ToString("F").Should().Be("765,43 Euro");
_dollar.ToString("F").Should().Be("765,43 United States dollar");
_dinar.ToString("F").Should().Be("765,432 Bahraini dinar");
}
[Fact]
[UseCulture("en-US")]
public void WhenCurrentCultureEnUS_ThenDecimalsFollowsCurrencyAndAmountFollowsCurrentCultureEnUSAndCurrencyNameIsInEnglish()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("F").Should().Be("765 Japanese yen");
_euro.ToString("F").Should().Be("765.43 Euro");
_dollar.ToString("F").Should().Be("765.43 United States dollar");
_dinar.ToString("F").Should().Be("765.432 Bahraini dinar");
}
[Fact]
[UseCulture("en-US")]
public void WhenZeroDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("F0").Should().Be("765 Japanese yen");
_euro.ToString("F0").Should().Be("765 Euro");
_dollar.ToString("F0").Should().Be("765 United States dollar");
_dinar.ToString("F0").Should().Be("765 Bahraini dinar");
}
[Fact]
[UseCulture("en-US")]
public void WhenOneDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("F1").Should().Be("765.0 Japanese yen");
_euro.ToString("F1").Should().Be("765.4 Euro");
_dollar.ToString("F1").Should().Be("765.4 United States dollar");
_dinar.ToString("F1").Should().Be("765.4 Bahraini dinar");
}
[Fact]
[UseCulture("en-US")]
public void WhenTwoDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("F2").Should().Be("765.00 Japanese yen");
_euro.ToString("F2").Should().Be("765.43 Euro");
_dollar.ToString("F2").Should().Be("765.43 United States dollar");
_dinar.ToString("F2").Should().Be("765.43 Bahraini dinar");
}
[Fact]
[UseCulture("en-US")]
public void WhenThreeDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("F3").Should().Be("765.000 Japanese yen");
_euro.ToString("F3").Should().Be("765.430 Euro");
_dollar.ToString("F3").Should().Be("765.430 United States dollar");
_dinar.ToString("F3").Should().Be("765.432 Bahraini dinar");
}
[Fact]
[UseCulture("en-US")]
public void WhenFourDecimals_ThenThisShouldSucceed()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("F4").Should().Be("765.0000 Japanese yen");
_euro.ToString("F4").Should().Be("765.4300 Euro");
_dollar.ToString("F4").Should().Be("765.4300 United States dollar");
_dinar.ToString("F4").Should().Be("765.4320 Bahraini dinar");
}
}
}