Skip to content

Commit

Permalink
fix - reg - Fixed equals operator for parts
Browse files Browse the repository at this point in the history
---

We've fixed a regression where all values don't get compared as expected.

---

Type: fix
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Mar 31, 2024
1 parent 1a23dbf commit dcaa8d5
Show file tree
Hide file tree
Showing 28 changed files with 163 additions and 84 deletions.
6 changes: 3 additions & 3 deletions VisualCard.Tests/ContactData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ public static class ContactData
ORG:Organization
TITLE:Title
NOTE:Notes
X-AIM;HOME:IM
X-MSN;HOME:Windows LIVE
X-YAHOO;HOME:Yahoo
X-AIM;TYPE=HOME:IM
X-MSN;TYPE=HOME:Windows LIVE
X-YAHOO;TYPE=HOME:Yahoo
END:VCARD

BEGIN:VCARD
Expand Down
10 changes: 7 additions & 3 deletions VisualCard/Parts/BaseCardPartInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,22 @@ public abstract class BaseCardPartInfo : IEquatable<BaseCardPartInfo>
public bool Equals(BaseCardPartInfo source, BaseCardPartInfo target)
{
// We can't perform this operation on null.
if (source is null)
if (source is null || target is null)
return false;

// Check all the properties
return
source.Arguments.SequenceEqual(target.Arguments) &&
source.ElementTypes.SequenceEqual(target.ElementTypes) &&
source.AltId == target.AltId &&
source.ValueType == target.ValueType
source.ValueType == target.ValueType &&
EqualsInternal(source, target);
;
}

/// <inheritdoc/>
public override bool Equals(object obj) =>
base.Equals(obj);
Equals((BaseCardPartInfo)obj);

/// <inheritdoc/>
public override int GetHashCode()
Expand All @@ -102,6 +103,9 @@ public override int GetHashCode()
public static bool operator !=(BaseCardPartInfo left, BaseCardPartInfo right) =>
!(left == right);

internal virtual bool EqualsInternal(BaseCardPartInfo source, BaseCardPartInfo target) =>
true;

internal abstract BaseCardPartInfo FromStringVcardInternal(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion);

internal abstract string ToStringVcardInternal(Version cardVersion);
Expand Down
4 changes: 2 additions & 2 deletions VisualCard/Parts/Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void SaveTo(string path)

/// <inheritdoc/>
public override bool Equals(object obj) =>
base.Equals(obj);
Equals((Card)obj);

/// <summary>
/// Checks to see if both the cards are equal
Expand All @@ -217,7 +217,7 @@ public void SaveTo(string path)
public bool Equals(Card source, Card target)
{
// We can't perform this operation on null.
if (source is null)
if (source is null || target is null)
return false;

// Check all the properties
Expand Down
31 changes: 29 additions & 2 deletions VisualCard/Parts/Comparers/PartComparison.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,23 @@ internal static class PartComparison
bool equal = source.All(kvp =>
{
bool exists = target.TryGetValue(kvp.Key, out List<BaseCardPartInfo> parts);
bool partsEqual = kvp.Value.SequenceEqual(parts);
return exists && partsEqual;
if (!exists)
return false;
// Verify the lists
if (!VerifyLists(kvp.Value, parts))
return false;
// Now, compare between two parts
List<bool> results = [];
for (int i = 0; i < parts.Count; i++)
{
BaseCardPartInfo sourcePart = kvp.Value[i];
BaseCardPartInfo targetPart = parts[i];
bool equals = sourcePart == targetPart;
results.Add(equals);
}
return !results.Contains(false);
});
return equal;
}
Expand All @@ -92,6 +107,18 @@ internal static class PartComparison
return equal;
}

private static bool VerifyLists<TValue>(
IList<TValue> source,
IList<TValue> target)
{
if (source == null || target == null)
return false;

if (source.Count != target.Count)
return false;
return true;
}

private static bool VerifyDicts<TKey, TValue>(
IDictionary<TKey, TValue> source,
IDictionary<TKey, TValue> target)
Expand Down
8 changes: 5 additions & 3 deletions VisualCard/Parts/Implementations/AddressInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[

/// <inheritdoc/>
public override bool Equals(object obj) =>
base.Equals(obj);
Equals((AddressInfo)obj);

/// <summary>
/// Checks to see if both the parts are equal
Expand All @@ -142,12 +142,11 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
public bool Equals(AddressInfo source, AddressInfo target)
{
// We can't perform this operation on null.
if (source is null)
if (source is null || target is null)
return false;

// Check all the properties
return
base.Equals(source, target) &&
source.PostOfficeBox == target.PostOfficeBox &&
source.ExtendedAddress == target.ExtendedAddress &&
source.StreetAddress == target.StreetAddress &&
Expand Down Expand Up @@ -181,6 +180,9 @@ public override int GetHashCode()
public static bool operator !=(AddressInfo left, AddressInfo right) =>
!(left == right);

internal override bool EqualsInternal(BaseCardPartInfo source, BaseCardPartInfo target) =>
((AddressInfo)source) == ((AddressInfo)target);

internal AddressInfo() :
base()
{ }
Expand Down
8 changes: 5 additions & 3 deletions VisualCard/Parts/Implementations/AgentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[

/// <inheritdoc/>
public override bool Equals(object obj) =>
base.Equals(obj);
Equals((AgentInfo)obj);

/// <summary>
/// Checks to see if both the parts are equal
Expand All @@ -107,12 +107,11 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
public bool Equals(AgentInfo source, AgentInfo target)
{
// We can't perform this operation on null.
if (source is null)
if (source is null || target is null)
return false;

// Check all the properties
return
base.Equals(source, target) &&
source.AgentCards == target.AgentCards
;
}
Expand All @@ -134,6 +133,9 @@ public override int GetHashCode()
public static bool operator !=(AgentInfo left, AgentInfo right) =>
!(left == right);

internal override bool EqualsInternal(BaseCardPartInfo source, BaseCardPartInfo target) =>
((AgentInfo)source) == ((AgentInfo)target);

internal AgentInfo() :
base()
{ }
Expand Down
8 changes: 5 additions & 3 deletions VisualCard/Parts/Implementations/AnniversaryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[

/// <inheritdoc/>
public override bool Equals(object obj) =>
base.Equals(obj);
Equals((AnniversaryInfo)obj);

/// <summary>
/// Checks to see if both the parts are equal
Expand All @@ -84,12 +84,11 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
public bool Equals(AnniversaryInfo source, AnniversaryInfo target)
{
// We can't perform this operation on null.
if (source is null)
if (source is null || target is null)
return false;

// Check all the properties
return
base.Equals(source, target) &&
source.Anniversary == target.Anniversary
;
}
Expand All @@ -111,6 +110,9 @@ public override int GetHashCode()
public static bool operator !=(AnniversaryInfo left, AnniversaryInfo right) =>
!(left == right);

internal override bool EqualsInternal(BaseCardPartInfo source, BaseCardPartInfo target) =>
((AnniversaryInfo)source) == ((AnniversaryInfo)target);

internal AnniversaryInfo() { }

internal AnniversaryInfo(int altId, string[] arguments, string[] elementTypes, string valueType, DateTime? anniversary) :
Expand Down
8 changes: 5 additions & 3 deletions VisualCard/Parts/Implementations/BirthDateInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[

/// <inheritdoc/>
public override bool Equals(object obj) =>
base.Equals(obj);
Equals((BirthDateInfo)obj);

/// <summary>
/// Checks to see if both the parts are equal
Expand All @@ -85,12 +85,11 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
public bool Equals(BirthDateInfo source, BirthDateInfo target)
{
// We can't perform this operation on null.
if (source is null)
if (source is null || target is null)
return false;

// Check all the properties
return
base.Equals(source, target) &&
source.BirthDate == target.BirthDate
;
}
Expand All @@ -112,6 +111,9 @@ public override int GetHashCode()
public static bool operator !=(BirthDateInfo left, BirthDateInfo right) =>
!(left == right);

internal override bool EqualsInternal(BaseCardPartInfo source, BaseCardPartInfo target) =>
((BirthDateInfo)source) == ((BirthDateInfo)target);

internal BirthDateInfo() { }

internal BirthDateInfo(int altId, string[] arguments, string[] elementTypes, string valueType, DateTime? birth) :
Expand Down
8 changes: 5 additions & 3 deletions VisualCard/Parts/Implementations/CategoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[

/// <inheritdoc/>
public override bool Equals(object obj) =>
base.Equals(obj);
Equals((CategoryInfo)obj);

/// <summary>
/// Checks to see if both the parts are equal
Expand All @@ -78,12 +78,11 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
public bool Equals(CategoryInfo source, CategoryInfo target)
{
// We can't perform this operation on null.
if (source is null)
if (source is null || target is null)
return false;

// Check all the properties
return
base.Equals(source, target) &&
source.Category == target.Category
;
}
Expand All @@ -105,6 +104,9 @@ public override int GetHashCode()
public static bool operator !=(CategoryInfo left, CategoryInfo right) =>
!(left == right);

internal override bool EqualsInternal(BaseCardPartInfo source, BaseCardPartInfo target) =>
((CategoryInfo)source) == ((CategoryInfo)target);

internal CategoryInfo() { }

internal CategoryInfo(int altId, string[] arguments, string[] elementTypes, string valueType, string[] category) :
Expand Down
8 changes: 5 additions & 3 deletions VisualCard/Parts/Implementations/EmailInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[

/// <inheritdoc/>
public override bool Equals(object obj) =>
base.Equals(obj);
Equals((EmailInfo)obj);

/// <summary>
/// Checks to see if both the parts are equal
Expand All @@ -104,12 +104,11 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
public bool Equals(EmailInfo source, EmailInfo target)
{
// We can't perform this operation on null.
if (source is null)
if (source is null || target is null)
return false;

// Check all the properties
return
base.Equals(source, target) &&
source.ContactEmailAddress == target.ContactEmailAddress
;
}
Expand All @@ -131,6 +130,9 @@ public override int GetHashCode()
public static bool operator !=(EmailInfo left, EmailInfo right) =>
!(left == right);

internal override bool EqualsInternal(BaseCardPartInfo source, BaseCardPartInfo target) =>
((EmailInfo)source) == ((EmailInfo)target);

internal EmailInfo() { }

internal EmailInfo(int altId, string[] arguments, string[] elementTypes, string valueType, string contactEmailAddress) :
Expand Down
8 changes: 5 additions & 3 deletions VisualCard/Parts/Implementations/GenderInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[

/// <inheritdoc/>
public override bool Equals(object obj) =>
base.Equals(obj);
Equals((GenderInfo)obj);

/// <summary>
/// Checks to see if both the parts are equal
Expand All @@ -104,12 +104,11 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
public bool Equals(GenderInfo source, GenderInfo target)
{
// We can't perform this operation on null.
if (source is null)
if (source is null || target is null)
return false;

// Check all the properties
return
base.Equals(source, target) &&
source.Gender == target.Gender &&
source.GenderDescription == target.GenderDescription
;
Expand All @@ -133,6 +132,9 @@ public override int GetHashCode()
public static bool operator !=(GenderInfo left, GenderInfo right) =>
!(left == right);

internal override bool EqualsInternal(BaseCardPartInfo source, BaseCardPartInfo target) =>
((GenderInfo)source) == ((GenderInfo)target);

internal GenderInfo() { }

internal GenderInfo(int altId, string[] arguments, string[] elementTypes, string valueType, Gender gender, string genderDescription) :
Expand Down
8 changes: 5 additions & 3 deletions VisualCard/Parts/Implementations/GeoInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[

/// <inheritdoc/>
public override bool Equals(object obj) =>
base.Equals(obj);
Equals((GeoInfo)obj);

/// <summary>
/// Checks to see if both the parts are equal
Expand All @@ -95,12 +95,11 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
public bool Equals(GeoInfo source, GeoInfo target)
{
// We can't perform this operation on null.
if (source is null)
if (source is null || target is null)
return false;

// Check all the properties
return
base.Equals(source, target) &&
source.Geo == target.Geo
;
}
Expand All @@ -122,6 +121,9 @@ public override int GetHashCode()
public static bool operator !=(GeoInfo left, GeoInfo right) =>
!(left == right);

internal override bool EqualsInternal(BaseCardPartInfo source, BaseCardPartInfo target) =>
((GeoInfo)source) == ((GeoInfo)target);

internal GeoInfo() { }

internal GeoInfo(int altId, string[] arguments, string[] elementTypes, string valueType, string geo) :
Expand Down
Loading

0 comments on commit dcaa8d5

Please sign in to comment.