-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmountTest.cs
100 lines (84 loc) · 2.29 KB
/
AmountTest.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
using System.Linq;
using CsCheck;
using NUnit.Framework;
namespace Finance.Accounting.Test;
public class AmountTest
{
Gen<Amount> _amountGen;
[SetUp]
public void Setup()
{
_amountGen = Gen.Dictionary(Gen.String[1, 2], Gen.Decimal)
.Select(x =>
{
var amount = new Amount();
foreach (var entry in x)
{
amount.Add(entry.Key, entry.Value);
}
return amount;
});
}
[Test]
public void AlwaysHasValue()
{
Gen.String[1, 5].Sample(commodity =>
{
var amount = new Amount();
Assert.That(amount[commodity], Is.Zero);
});
}
[Test]
public void Negation()
{
_amountGen.Sample(amount =>
{
var negation = -amount;
foreach (var commodity in amount.Commodities)
{
Assert.That(negation[commodity], Is.EqualTo(-amount[commodity]));
}
});
}
[Test]
public void Addition()
{
_amountGen.Array[2].Sample(amounts =>
{
var left = amounts[0];
var right = amounts[1];
var total = left + right;
foreach (var commodity in left.Commodities.Union(right.Commodities))
{
Assert.That(total[commodity], Is.EqualTo(left[commodity] + right[commodity]));
}
});
}
[Test]
public void Subtraction()
{
_amountGen.Array[2].Sample(amounts =>
{
var left = amounts[0];
var right = amounts[1];
var total = left - right;
foreach (var commodity in left.Commodities.Union(right.Commodities))
{
Assert.That(total[commodity], Is.EqualTo(left[commodity] - right[commodity]));
}
});
}
[Test]
public void Multiplication()
{
Gen.Select(_amountGen, Gen.Decimal[-100, 100])
.Sample((amount, factor) =>
{
var total = amount * factor;
foreach (var commodity in amount.Commodities)
{
Assert.That(total[commodity], Is.EqualTo(amount[commodity] * factor));
}
});
}
}