Fix the `ValueEquals` bug.

Resolve #12509
pull/12510/head
maliming 3 years ago
parent 70308de085
commit 35ee7dd3b1
No known key found for this signature in database
GPG Key ID: 096224957E51C89E

@ -16,21 +16,29 @@ public abstract class ValueObject
return false;
}
ValueObject other = (ValueObject)obj;
var other = (ValueObject)obj;
IEnumerator<object> thisValues = GetAtomicValues().GetEnumerator();
IEnumerator<object> otherValues = other.GetAtomicValues().GetEnumerator();
var thisValues = GetAtomicValues().GetEnumerator();
var otherValues = other.GetAtomicValues().GetEnumerator();
while (thisValues.MoveNext() && otherValues.MoveNext())
var thisMoveNext = thisValues.MoveNext();
var otherMoveNext = otherValues.MoveNext();
while (thisMoveNext && otherMoveNext)
{
if (ReferenceEquals(thisValues.Current, null) ^
ReferenceEquals(otherValues.Current, null))
if (ReferenceEquals(thisValues.Current, null) ^ ReferenceEquals(otherValues.Current, null))
{
return false;
}
if (thisValues.Current != null &&
!thisValues.Current.Equals(otherValues.Current))
if (thisValues.Current != null && !thisValues.Current.Equals(otherValues.Current))
{
return false;
}
thisMoveNext = thisValues.MoveNext();
otherMoveNext = otherValues.MoveNext();
if (thisMoveNext != otherMoveNext)
{
return false;
}

@ -11,6 +11,8 @@ public class Address : ValueObject
public int Number { get; }
public string[] Tags { get; }
private Address()
{
}
@ -18,11 +20,13 @@ public class Address : ValueObject
public Address(
Guid cityId,
string street,
int number)
int number,
params string[] tags)
{
CityId = cityId;
Street = street;
Number = number;
Tags = tags;
}
//Requires to implement this method to return properties.
@ -31,5 +35,9 @@ public class Address : ValueObject
yield return Street;
yield return CityId;
yield return Number;
foreach (var tag in Tags)
{
yield return tag;
}
}
}

@ -10,8 +10,9 @@ public class ValueObject_Tests
public void ValueObjects_With_Same_Properties_Should_Be_Equals()
{
var cityId = Guid.NewGuid();
var address1 = new Address(cityId, "Baris Manco", 42);
var address2 = new Address(cityId, "Baris Manco", 42);
var address1 = new Address(cityId, "Baris Manco", 42, "home", "office");
var address2 = new Address(cityId, "Baris Manco", 42, "home", "office");
address1.ValueEquals(address2).ShouldBeTrue();
}
@ -25,5 +26,10 @@ public class ValueObject_Tests
var address2 = new Address(cityId, "Baris Manco", 43);
address1.ValueEquals(address2).ShouldBeFalse();
address1 = new Address(cityId, "Baris Manco", 42, "home", "office");
address2 = new Address(cityId, "Baris Manco", 42, "home");
address1.ValueEquals(address2).ShouldBeFalse();
}
}

Loading…
Cancel
Save