Skip to content

Commit cc2187b

Browse files
authored
Merge pull request #183 from commercetools/Fix-ComparingMoneyToNull
Handling Money Comparison
2 parents 33f2e37 + 1015f68 commit cc2187b

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

commercetools.Sdk/Tests/commercetools.Sdk.Domain.Tests/MoneyTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using NodaMoney;
23
using Xunit;
34

@@ -66,5 +67,18 @@ public void ConvertToMoney()
6667
Assert.Equal(123457, m.CentAmount);
6768
Assert.Equal("EUR", m.CurrencyCode);
6869
}
70+
71+
[Fact]
72+
public void MoneyComparison()
73+
{
74+
var m1 = new Money() { CurrencyCode = "EUR", CentAmount = 1234, FractionDigits = 2 };
75+
Money m2 = null;
76+
77+
Assert.False(m1 == m2);
78+
Assert.True(m1 != m2);
79+
Assert.False(m1 > m2);
80+
Assert.False(m1 < m2);
81+
Assert.False(m1.Equals(m2));
82+
}
6983
}
7084
}

commercetools.Sdk/commercetools.Sdk.Domain/Common/BaseMoney.cs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,93 @@ public int CompareTo(object obj)
3232

3333
public static bool operator <(BaseMoney moneyLeft, BaseMoney moneyRight)
3434
{
35+
if (ReferenceEquals(moneyLeft, moneyRight))
36+
{
37+
return false;
38+
}
39+
if (ReferenceEquals(moneyLeft,null) || ReferenceEquals(moneyRight,null))
40+
{
41+
return false;
42+
}
3543
return moneyLeft.CompareTo(moneyRight) < 0;
3644
}
3745

3846
public static bool operator >(BaseMoney moneyLeft, BaseMoney moneyRight)
3947
{
48+
if (ReferenceEquals(moneyLeft, moneyRight))
49+
{
50+
return false;
51+
}
52+
if (ReferenceEquals(moneyLeft,null) || ReferenceEquals(moneyRight,null))
53+
{
54+
return false;
55+
}
4056
return moneyLeft.CompareTo(moneyRight) > 0;
4157
}
4258

4359
public static bool operator <=(BaseMoney moneyLeft, BaseMoney moneyRight)
4460
{
61+
if (ReferenceEquals(moneyLeft, moneyRight))
62+
{
63+
return false;
64+
}
65+
if (ReferenceEquals(moneyLeft,null) || ReferenceEquals(moneyRight,null))
66+
{
67+
return false;
68+
}
4569
return moneyLeft.CompareTo(moneyRight) <= 0;
4670
}
4771

4872
public static bool operator >=(BaseMoney moneyLeft, BaseMoney moneyRight)
4973
{
74+
if (ReferenceEquals(moneyLeft, moneyRight))
75+
{
76+
return false;
77+
}
78+
if (ReferenceEquals(moneyLeft,null) || ReferenceEquals(moneyRight,null))
79+
{
80+
return false;
81+
}
5082
return moneyLeft.CompareTo(moneyRight) >= 0;
5183
}
5284

5385
public static bool operator ==(BaseMoney moneyLeft, BaseMoney moneyRight)
5486
{
87+
if (ReferenceEquals(moneyLeft, moneyRight))
88+
{
89+
return true;
90+
}
91+
if (ReferenceEquals(moneyLeft,null) || ReferenceEquals(moneyRight,null))
92+
{
93+
return false;
94+
}
5595
return moneyLeft.CompareTo(moneyRight) == 0;
5696
}
5797

5898
public static bool operator !=(BaseMoney moneyLeft, BaseMoney moneyRight)
5999
{
100+
if (ReferenceEquals(moneyLeft, moneyRight))
101+
{
102+
return false;
103+
}
104+
if (ReferenceEquals(moneyLeft,null) || ReferenceEquals(moneyRight,null))
105+
{
106+
return true;
107+
}
60108
return moneyLeft.CompareTo(moneyRight) != 0;
61109
}
62110

63111
public override bool Equals(object obj)
64112
{
65-
if (!(obj is BaseMoney money))
113+
if (ReferenceEquals(this, obj))
66114
{
67-
throw new ArgumentException();
115+
return true;
68116
}
69-
if (this.CurrencyCode != money.CurrencyCode)
117+
if (ReferenceEquals(obj,null) || ReferenceEquals(this,null))
118+
{
119+
return false;
120+
}
121+
if (!(obj is BaseMoney money))
70122
{
71123
throw new ArgumentException();
72124
}

0 commit comments

Comments
 (0)