-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from diegofrata/deep-equality
Fix comparison to support deep equality on non-sealed types.
- Loading branch information
Showing
84 changed files
with
912 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Generator.Equals | ||
{ | ||
public class DefaultEqualityComparer<T> : IEqualityComparer<T> | ||
{ | ||
public static DefaultEqualityComparer<T> Default { get; }= new DefaultEqualityComparer<T>(); | ||
|
||
private static readonly IEqualityComparer<T> _underlying; | ||
|
||
static DefaultEqualityComparer() | ||
{ | ||
if (typeof(T).IsSealed) | ||
_underlying = EqualityComparer<T>.Default; | ||
else | ||
_underlying = new ObjectEqualityComparer(); | ||
} | ||
|
||
public bool Equals(T x, T y) => _underlying.Equals(x, y); | ||
|
||
public int GetHashCode(T obj) => _underlying.GetHashCode(obj); | ||
|
||
private class ObjectEqualityComparer : IEqualityComparer<T> | ||
{ | ||
public bool Equals(T x, T y) => object.Equals(x, y); | ||
|
||
public int GetHashCode(T obj) => obj!.GetHashCode(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...lity.00Generator.Equals.Tests.Classes.DeepEquality.Manager.Generator.Equals.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//HintName: Generator.Equals.Tests.Classes.DeepEquality.Manager.Generator.Equals.g.cs | ||
|
||
#nullable enable | ||
#pragma warning disable CS0612,CS0618 | ||
|
||
namespace Generator.Equals.Tests.Classes | ||
{ | ||
partial class DeepEquality | ||
{ | ||
partial class Manager : global::System.IEquatable<Manager> | ||
{ | ||
/// <summary> | ||
/// Indicates whether the object on the left is equal to the object on the right. | ||
/// </summary> | ||
/// <param name="left">The left object</param> | ||
/// <param name="right">The right object</param> | ||
/// <returns>true if the objects are equal; otherwise, false.</returns> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public static bool operator ==( | ||
global::Generator.Equals.Tests.Classes.DeepEquality.Manager? left, | ||
global::Generator.Equals.Tests.Classes.DeepEquality.Manager? right) => | ||
global::Generator.Equals.DefaultEqualityComparer<global::Generator.Equals.Tests.Classes.DeepEquality.Manager?>.Default | ||
.Equals(left, right); | ||
|
||
/// <summary> | ||
/// Indicates whether the object on the left is not equal to the object on the right. | ||
/// </summary> | ||
/// <param name="left">The left object</param> | ||
/// <param name="right">The right object</param> | ||
/// <returns>true if the objects are not equal; otherwise, false.</returns> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public static bool operator !=(global::Generator.Equals.Tests.Classes.DeepEquality.Manager? left, global::Generator.Equals.Tests.Classes.DeepEquality.Manager? right) => | ||
!(left == right); | ||
|
||
/// <inheritdoc/> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public override bool Equals(object? obj) => | ||
Equals(obj as global::Generator.Equals.Tests.Classes.DeepEquality.Manager); | ||
|
||
/// <inheritdoc/> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public bool Equals(global::Generator.Equals.Tests.Classes.DeepEquality.Manager? other) | ||
{ | ||
return | ||
base.Equals(other as global::Generator.Equals.Tests.Classes.DeepEquality.Person) | ||
&& global::Generator.Equals.DefaultEqualityComparer<global::System.String>.Default.Equals(this.Department!, other.Department!) | ||
; | ||
} | ||
|
||
/// <inheritdoc/> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public override int GetHashCode() | ||
{ | ||
var hashCode = new global::System.HashCode(); | ||
|
||
hashCode.Add(base.GetHashCode()); | ||
hashCode.Add( | ||
this.Department!, | ||
global::Generator.Equals.DefaultEqualityComparer<global::System.String>.Default); | ||
|
||
return hashCode.ToHashCode(); | ||
} | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...ality.01Generator.Equals.Tests.Classes.DeepEquality.Person.Generator.Equals.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//HintName: Generator.Equals.Tests.Classes.DeepEquality.Person.Generator.Equals.g.cs | ||
|
||
#nullable enable | ||
#pragma warning disable CS0612,CS0618 | ||
|
||
namespace Generator.Equals.Tests.Classes | ||
{ | ||
partial class DeepEquality | ||
{ | ||
partial class Person : global::System.IEquatable<Person> | ||
{ | ||
/// <summary> | ||
/// Indicates whether the object on the left is equal to the object on the right. | ||
/// </summary> | ||
/// <param name="left">The left object</param> | ||
/// <param name="right">The right object</param> | ||
/// <returns>true if the objects are equal; otherwise, false.</returns> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public static bool operator ==( | ||
global::Generator.Equals.Tests.Classes.DeepEquality.Person? left, | ||
global::Generator.Equals.Tests.Classes.DeepEquality.Person? right) => | ||
global::Generator.Equals.DefaultEqualityComparer<global::Generator.Equals.Tests.Classes.DeepEquality.Person?>.Default | ||
.Equals(left, right); | ||
|
||
/// <summary> | ||
/// Indicates whether the object on the left is not equal to the object on the right. | ||
/// </summary> | ||
/// <param name="left">The left object</param> | ||
/// <param name="right">The right object</param> | ||
/// <returns>true if the objects are not equal; otherwise, false.</returns> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public static bool operator !=(global::Generator.Equals.Tests.Classes.DeepEquality.Person? left, global::Generator.Equals.Tests.Classes.DeepEquality.Person? right) => | ||
!(left == right); | ||
|
||
/// <inheritdoc/> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public override bool Equals(object? obj) => | ||
Equals(obj as global::Generator.Equals.Tests.Classes.DeepEquality.Person); | ||
|
||
/// <inheritdoc/> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public bool Equals(global::Generator.Equals.Tests.Classes.DeepEquality.Person? other) | ||
{ | ||
return | ||
!ReferenceEquals(other, null) && this.GetType() == other.GetType() | ||
&& global::Generator.Equals.DefaultEqualityComparer<global::System.Int32>.Default.Equals(this.Age!, other.Age!) | ||
; | ||
} | ||
|
||
/// <inheritdoc/> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public override int GetHashCode() | ||
{ | ||
var hashCode = new global::System.HashCode(); | ||
|
||
hashCode.Add(this.GetType()); | ||
hashCode.Add( | ||
this.Age!, | ||
global::Generator.Equals.DefaultEqualityComparer<global::System.Int32>.Default); | ||
|
||
return hashCode.ToHashCode(); | ||
} | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...ality.02Generator.Equals.Tests.Classes.DeepEquality.Sample.Generator.Equals.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//HintName: Generator.Equals.Tests.Classes.DeepEquality.Sample.Generator.Equals.g.cs | ||
|
||
#nullable enable | ||
#pragma warning disable CS0612,CS0618 | ||
|
||
namespace Generator.Equals.Tests.Classes | ||
{ | ||
partial class DeepEquality | ||
{ | ||
partial class Sample : global::System.IEquatable<Sample> | ||
{ | ||
/// <summary> | ||
/// Indicates whether the object on the left is equal to the object on the right. | ||
/// </summary> | ||
/// <param name="left">The left object</param> | ||
/// <param name="right">The right object</param> | ||
/// <returns>true if the objects are equal; otherwise, false.</returns> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public static bool operator ==( | ||
global::Generator.Equals.Tests.Classes.DeepEquality.Sample? left, | ||
global::Generator.Equals.Tests.Classes.DeepEquality.Sample? right) => | ||
global::Generator.Equals.DefaultEqualityComparer<global::Generator.Equals.Tests.Classes.DeepEquality.Sample?>.Default | ||
.Equals(left, right); | ||
|
||
/// <summary> | ||
/// Indicates whether the object on the left is not equal to the object on the right. | ||
/// </summary> | ||
/// <param name="left">The left object</param> | ||
/// <param name="right">The right object</param> | ||
/// <returns>true if the objects are not equal; otherwise, false.</returns> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public static bool operator !=(global::Generator.Equals.Tests.Classes.DeepEquality.Sample? left, global::Generator.Equals.Tests.Classes.DeepEquality.Sample? right) => | ||
!(left == right); | ||
|
||
/// <inheritdoc/> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public override bool Equals(object? obj) => | ||
Equals(obj as global::Generator.Equals.Tests.Classes.DeepEquality.Sample); | ||
|
||
/// <inheritdoc/> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public bool Equals(global::Generator.Equals.Tests.Classes.DeepEquality.Sample? other) | ||
{ | ||
return | ||
!ReferenceEquals(other, null) && this.GetType() == other.GetType() | ||
&& global::Generator.Equals.DefaultEqualityComparer<global::Generator.Equals.Tests.Classes.DeepEquality.Person>.Default.Equals(this.Person!, other.Person!) | ||
; | ||
} | ||
|
||
/// <inheritdoc/> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public override int GetHashCode() | ||
{ | ||
var hashCode = new global::System.HashCode(); | ||
|
||
hashCode.Add(this.GetType()); | ||
hashCode.Add( | ||
this.Person!, | ||
global::Generator.Equals.DefaultEqualityComparer<global::Generator.Equals.Tests.Classes.DeepEquality.Person>.Default); | ||
|
||
return hashCode.ToHashCode(); | ||
} | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
Generator.Equals.SnapshotTests/Classes/DeepEquality.Diagnostics.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
65 changes: 65 additions & 0 deletions
65
...t6_0.00Generator.Equals.Tests.Classes.DeepEquality.Manager.Generator.Equals.g.received.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//HintName: Generator.Equals.Tests.Classes.DeepEquality.Manager.Generator.Equals.g.cs | ||
|
||
#nullable enable | ||
#pragma warning disable CS0612,CS0618 | ||
|
||
namespace Generator.Equals.Tests.Classes | ||
{ | ||
partial class DeepEquality | ||
{ | ||
partial class Manager : global::System.IEquatable<Manager> | ||
{ | ||
/// <summary> | ||
/// Indicates whether the object on the left is equal to the object on the right. | ||
/// </summary> | ||
/// <param name="left">The left object</param> | ||
/// <param name="right">The right object</param> | ||
/// <returns>true if the objects are equal; otherwise, false.</returns> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public static bool operator ==( | ||
global::Generator.Equals.Tests.Classes.DeepEquality.Manager? left, | ||
global::Generator.Equals.Tests.Classes.DeepEquality.Manager? right) => | ||
global::Generator.Equals.DefaultEqualityComparer<global::Generator.Equals.Tests.Classes.DeepEquality.Manager?>.Default | ||
.Equals(left, right); | ||
|
||
/// <summary> | ||
/// Indicates whether the object on the left is not equal to the object on the right. | ||
/// </summary> | ||
/// <param name="left">The left object</param> | ||
/// <param name="right">The right object</param> | ||
/// <returns>true if the objects are not equal; otherwise, false.</returns> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public static bool operator !=(global::Generator.Equals.Tests.Classes.DeepEquality.Manager? left, global::Generator.Equals.Tests.Classes.DeepEquality.Manager? right) => | ||
!(left == right); | ||
|
||
/// <inheritdoc/> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public override bool Equals(object? obj) => | ||
Equals(obj as global::Generator.Equals.Tests.Classes.DeepEquality.Manager); | ||
|
||
/// <inheritdoc/> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public bool Equals(global::Generator.Equals.Tests.Classes.DeepEquality.Manager? other) | ||
{ | ||
return | ||
base.Equals(other as global::Generator.Equals.Tests.Classes.DeepEquality.Person) | ||
&& global::Generator.Equals.DefaultEqualityComparer<global::System.String>.Default.Equals(this.Department!, other.Department!) | ||
; | ||
} | ||
|
||
/// <inheritdoc/> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] | ||
public override int GetHashCode() | ||
{ | ||
var hashCode = new global::System.HashCode(); | ||
|
||
hashCode.Add(base.GetHashCode()); | ||
hashCode.Add( | ||
this.Department!, | ||
global::Generator.Equals.DefaultEqualityComparer<global::System.String>.Default); | ||
|
||
return hashCode.ToHashCode(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.