diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..af90ada --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,24 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/dotnet +{ + "name": "C# (.NET)", + "image": "mcr.microsoft.com/devcontainers/dotnet:0-6.0", + "features": { + "ghcr.io/devcontainers/features/dotnet:1": {} + } + + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [5000, 5001], + + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "dotnet restore", + + // Configure tool-specific properties. + // "customizations": {}, + + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} diff --git a/Generator.Equals.Runtime/Attributes.cs b/Generator.Equals.Runtime/Attributes.cs index c7b662b..c8e6cbd 100644 --- a/Generator.Equals.Runtime/Attributes.cs +++ b/Generator.Equals.Runtime/Attributes.cs @@ -19,43 +19,43 @@ public class EquatableAttribute : Attribute } [Conditional("GENERATOR_EQUALS")] - [AttributeUsage(AttributeTargets.Property)] + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] public class DefaultEqualityAttribute : Attribute { } [Conditional("GENERATOR_EQUALS")] - [AttributeUsage(AttributeTargets.Property)] + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] public class OrderedEqualityAttribute : Attribute { } [Conditional("GENERATOR_EQUALS")] - [AttributeUsage(AttributeTargets.Property)] + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] public class IgnoreEqualityAttribute : Attribute { } [Conditional("GENERATOR_EQUALS")] - [AttributeUsage(AttributeTargets.Property)] + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] public class UnorderedEqualityAttribute : Attribute { } [Conditional("GENERATOR_EQUALS")] - [AttributeUsage(AttributeTargets.Property)] + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] public class ReferenceEqualityAttribute : Attribute { } [Conditional("GENERATOR_EQUALS")] - [AttributeUsage(AttributeTargets.Property)] + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] public class SetEqualityAttribute : Attribute { } [Conditional("GENERATOR_EQUALS")] - [AttributeUsage(AttributeTargets.Property)] + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] public class CustomEqualityAttribute: Attribute { public Type EqualityType { get; } diff --git a/Generator.Equals.SnapshotTests/Classes/CustomEquality.verified.cs b/Generator.Equals.SnapshotTests/Classes/CustomEquality.verified.cs index cc5df97..0f8acac 100644 --- a/Generator.Equals.SnapshotTests/Classes/CustomEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/CustomEquality.verified.cs @@ -43,9 +43,9 @@ public bool Equals(global::Generator.Equals.Tests.Classes.CustomEquality.Sample? { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::Generator.Equals.Tests.Classes.CustomEquality.Comparer1.Default.Equals(Name1!, other.Name1!) - && global::Generator.Equals.Tests.Classes.CustomEquality.Comparer2.Instance.Equals(Name2!, other.Name2!) - && new global::Generator.Equals.Tests.Classes.CustomEquality.LengthEqualityComparer().Equals(Name3!, other.Name3!) + && global::Generator.Equals.Tests.Classes.CustomEquality.Comparer1.Default.Equals(this.Name1!, other.Name1!) + && global::Generator.Equals.Tests.Classes.CustomEquality.Comparer2.Instance.Equals(this.Name2!, other.Name2!) + && new global::Generator.Equals.Tests.Classes.CustomEquality.LengthEqualityComparer().Equals(this.Name3!, other.Name3!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/DictionaryEquality.verified.cs b/Generator.Equals.SnapshotTests/Classes/DictionaryEquality.verified.cs index ec19b32..9d4cc9d 100644 --- a/Generator.Equals.SnapshotTests/Classes/DictionaryEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/DictionaryEquality.verified.cs @@ -43,7 +43,7 @@ public bool Equals(global::Generator.Equals.Tests.Classes.DictionaryEquality.Sam { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::Generator.Equals.DictionaryEqualityComparer.Default.Equals(Properties!, other.Properties!) + && global::Generator.Equals.DictionaryEqualityComparer.Default.Equals(this.Properties!, other.Properties!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/ExplicitMode.verified.cs b/Generator.Equals.SnapshotTests/Classes/ExplicitMode.verified.cs index 592e455..54d3346 100644 --- a/Generator.Equals.SnapshotTests/Classes/ExplicitMode.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/ExplicitMode.verified.cs @@ -43,7 +43,7 @@ public bool Equals(global::Generator.Equals.Tests.Classes.ExplicitMode.Sample? o { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/FieldEquality.Diagnostics.verified.txt b/Generator.Equals.SnapshotTests/Classes/FieldEquality.Diagnostics.verified.txt new file mode 100644 index 0000000..ad47dbb --- /dev/null +++ b/Generator.Equals.SnapshotTests/Classes/FieldEquality.Diagnostics.verified.txt @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/Generator.Equals.SnapshotTests/Classes/FieldEquality.verified.cs b/Generator.Equals.SnapshotTests/Classes/FieldEquality.verified.cs new file mode 100644 index 0000000..a59b008 --- /dev/null +++ b/Generator.Equals.SnapshotTests/Classes/FieldEquality.verified.cs @@ -0,0 +1,65 @@ +//HintName: Generator.Equals.Tests.Classes.FieldEquality.Sample.Generator.Equals.g.cs + +#nullable enable +#pragma warning disable CS0612,CS0618 + +namespace Generator.Equals.Tests.Classes +{ + partial class FieldEquality + { + partial class Sample : global::System.IEquatable + { + /// + /// Indicates whether the object on the left is equal to the object on the right. + /// + /// The left object + /// The right object + /// true if the objects are equal; otherwise, false. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] + public static bool operator ==( + global::Generator.Equals.Tests.Classes.FieldEquality.Sample? left, + global::Generator.Equals.Tests.Classes.FieldEquality.Sample? right) => + global::System.Collections.Generic.EqualityComparer.Default + .Equals(left, right); + + /// + /// Indicates whether the object on the left is not equal to the object on the right. + /// + /// The left object + /// The right object + /// true if the objects are not equal; otherwise, false. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] + public static bool operator !=(global::Generator.Equals.Tests.Classes.FieldEquality.Sample? left, global::Generator.Equals.Tests.Classes.FieldEquality.Sample? right) => + !(left == right); + + /// + [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.FieldEquality.Sample); + + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] + public bool Equals(global::Generator.Equals.Tests.Classes.FieldEquality.Sample? other) + { + return + !ReferenceEquals(other, null) && this.GetType() == other.GetType() + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this._addresses!, other._addresses!) + ; + } + + /// + [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._addresses!, + global::Generator.Equals.OrderedEqualityComparer.Default); + + return hashCode.ToHashCode(); + } + } + } +} diff --git a/Generator.Equals.SnapshotTests/Classes/GenericParameterEquality.verified.cs b/Generator.Equals.SnapshotTests/Classes/GenericParameterEquality.verified.cs index 613a5e9..3c9bdb5 100644 --- a/Generator.Equals.SnapshotTests/Classes/GenericParameterEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/GenericParameterEquality.verified.cs @@ -43,8 +43,8 @@ public bool Equals(global::Generator.Equals.Tests.Classes.GenericParameterEquali { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/IgnoreEquality.verified.cs b/Generator.Equals.SnapshotTests/Classes/IgnoreEquality.verified.cs index 9411167..3147ff0 100644 --- a/Generator.Equals.SnapshotTests/Classes/IgnoreEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/IgnoreEquality.verified.cs @@ -43,7 +43,7 @@ public bool Equals(global::Generator.Equals.Tests.Classes.IgnoreEquality.Sample? { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/IgnoreInheritedMembers.verified.cs b/Generator.Equals.SnapshotTests/Classes/IgnoreInheritedMembers.verified.cs index de5af84..6c60c5d 100644 --- a/Generator.Equals.SnapshotTests/Classes/IgnoreInheritedMembers.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/IgnoreInheritedMembers.verified.cs @@ -43,7 +43,7 @@ public bool Equals(global::Generator.Equals.Tests.Classes.IgnoreInheritedMembers { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/MultiplePartialsEquality.verified.cs b/Generator.Equals.SnapshotTests/Classes/MultiplePartialsEquality.verified.cs index 652dc4a..e77b37a 100644 --- a/Generator.Equals.SnapshotTests/Classes/MultiplePartialsEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/MultiplePartialsEquality.verified.cs @@ -43,9 +43,9 @@ public bool Equals(global::Generator.Equals.Tests.Classes.MultiplePartialsEquali { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(Addresses!, other.Addresses!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(FirstName!, other.FirstName!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(LastName!, other.LastName!) + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this.Addresses!, other.Addresses!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.FirstName!, other.FirstName!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.LastName!, other.LastName!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/NonSupportedMembers.verified.cs b/Generator.Equals.SnapshotTests/Classes/NonSupportedMembers.verified.cs index 7881c43..e45a70d 100644 --- a/Generator.Equals.SnapshotTests/Classes/NonSupportedMembers.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/NonSupportedMembers.verified.cs @@ -43,7 +43,7 @@ public bool Equals(global::Generator.Equals.Tests.Classes.NonSupportedMembers.Sa { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/NullableEquality.verified.cs b/Generator.Equals.SnapshotTests/Classes/NullableEquality.verified.cs index 4e020d5..313c17a 100644 --- a/Generator.Equals.SnapshotTests/Classes/NullableEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/NullableEquality.verified.cs @@ -43,7 +43,7 @@ public bool Equals(global::Generator.Equals.Tests.Classes.NullableEquality.Sampl { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(Addresses!, other.Addresses!) + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this.Addresses!, other.Addresses!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/ObsoleteClass.verified.cs b/Generator.Equals.SnapshotTests/Classes/ObsoleteClass.verified.cs index cc3e2a3..7c05aa7 100644 --- a/Generator.Equals.SnapshotTests/Classes/ObsoleteClass.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/ObsoleteClass.verified.cs @@ -43,7 +43,7 @@ public bool Equals(global::Generator.Equals.Tests.Classes.ObsoleteClass.Sample? { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Something!, other.Something!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Something!, other.Something!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/ObsoleteMembers.verified.cs b/Generator.Equals.SnapshotTests/Classes/ObsoleteMembers.verified.cs index c6270f2..bccd6fe 100644 --- a/Generator.Equals.SnapshotTests/Classes/ObsoleteMembers.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/ObsoleteMembers.verified.cs @@ -43,8 +43,8 @@ public bool Equals(global::Generator.Equals.Tests.Classes.ObsoleteMembers.Sample { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::System.Collections.Generic.EqualityComparer.Default.Equals(NoComment!, other.NoComment!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Comment!, other.Comment!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.NoComment!, other.NoComment!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Comment!, other.Comment!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/OrderedEquality.verified.cs b/Generator.Equals.SnapshotTests/Classes/OrderedEquality.verified.cs index 2e06ad9..1f7dfc1 100644 --- a/Generator.Equals.SnapshotTests/Classes/OrderedEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/OrderedEquality.verified.cs @@ -43,7 +43,7 @@ public bool Equals(global::Generator.Equals.Tests.Classes.OrderedEquality.Sample { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(Addresses!, other.Addresses!) + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this.Addresses!, other.Addresses!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/PrimitiveEquality.verified.cs b/Generator.Equals.SnapshotTests/Classes/PrimitiveEquality.verified.cs index 396d35d..dde635f 100644 --- a/Generator.Equals.SnapshotTests/Classes/PrimitiveEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/PrimitiveEquality.verified.cs @@ -43,8 +43,8 @@ public bool Equals(global::Generator.Equals.Tests.Classes.PrimitiveEquality.Samp { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/ReferenceEquality.verified.cs b/Generator.Equals.SnapshotTests/Classes/ReferenceEquality.verified.cs index 7431d9f..4ff11df 100644 --- a/Generator.Equals.SnapshotTests/Classes/ReferenceEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/ReferenceEquality.verified.cs @@ -43,7 +43,7 @@ public bool Equals(global::Generator.Equals.Tests.Classes.ReferenceEquality.Samp { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::Generator.Equals.ReferenceEqualityComparer.Default.Equals(Name!, other.Name!) + && global::Generator.Equals.ReferenceEqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/SetEquality.verified.cs b/Generator.Equals.SnapshotTests/Classes/SetEquality.verified.cs index 94e6bc8..a75f92d 100644 --- a/Generator.Equals.SnapshotTests/Classes/SetEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/SetEquality.verified.cs @@ -43,7 +43,7 @@ public bool Equals(global::Generator.Equals.Tests.Classes.SetEquality.Sample? ot { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::Generator.Equals.SetEqualityComparer.Default.Equals(Properties!, other.Properties!) + && global::Generator.Equals.SetEqualityComparer.Default.Equals(this.Properties!, other.Properties!) ; } diff --git a/Generator.Equals.SnapshotTests/Classes/UnorderedEquality.verified.cs b/Generator.Equals.SnapshotTests/Classes/UnorderedEquality.verified.cs index 8b5c538..486feab 100644 --- a/Generator.Equals.SnapshotTests/Classes/UnorderedEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Classes/UnorderedEquality.verified.cs @@ -43,7 +43,7 @@ public bool Equals(global::Generator.Equals.Tests.Classes.UnorderedEquality.Samp { return !ReferenceEquals(other, null) && this.GetType() == other.GetType() - && global::Generator.Equals.UnorderedEqualityComparer.Default.Equals(Properties!, other.Properties!) + && global::Generator.Equals.UnorderedEqualityComparer.Default.Equals(this.Properties!, other.Properties!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/CustomEquality.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/CustomEquality.verified.cs index 1cf0046..6be9427 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/CustomEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/CustomEquality.verified.cs @@ -14,9 +14,9 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.CustomEquality.Sample other) { return true - && global::Generator.Equals.Tests.RecordStructs.CustomEquality.Comparer1.Default.Equals(Name1!, other.Name1!) - && global::Generator.Equals.Tests.RecordStructs.CustomEquality.Comparer2.Instance.Equals(Name2!, other.Name2!) - && new global::Generator.Equals.Tests.RecordStructs.CustomEquality.LengthEqualityComparer().Equals(Name3!, other.Name3!) + && global::Generator.Equals.Tests.RecordStructs.CustomEquality.Comparer1.Default.Equals(this.Name1!, other.Name1!) + && global::Generator.Equals.Tests.RecordStructs.CustomEquality.Comparer2.Instance.Equals(this.Name2!, other.Name2!) + && new global::Generator.Equals.Tests.RecordStructs.CustomEquality.LengthEqualityComparer().Equals(this.Name3!, other.Name3!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/DictionaryEquality.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/DictionaryEquality.verified.cs index d889a2e..608e200 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/DictionaryEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/DictionaryEquality.verified.cs @@ -14,7 +14,7 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.DictionaryEquality.Sample other) { return true - && global::Generator.Equals.DictionaryEqualityComparer.Default.Equals(Properties!, other.Properties!) + && global::Generator.Equals.DictionaryEqualityComparer.Default.Equals(this.Properties!, other.Properties!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/ExplicitMode.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/ExplicitMode.verified.cs index 365df65..56f7050 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/ExplicitMode.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/ExplicitMode.verified.cs @@ -14,7 +14,7 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.ExplicitMode.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/FieldEquality.Diagnostics.verified.txt b/Generator.Equals.SnapshotTests/RecordStructs/FieldEquality.Diagnostics.verified.txt new file mode 100644 index 0000000..ad47dbb --- /dev/null +++ b/Generator.Equals.SnapshotTests/RecordStructs/FieldEquality.Diagnostics.verified.txt @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/Generator.Equals.SnapshotTests/RecordStructs/FieldEquality.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/FieldEquality.verified.cs new file mode 100644 index 0000000..baee95d --- /dev/null +++ b/Generator.Equals.SnapshotTests/RecordStructs/FieldEquality.verified.cs @@ -0,0 +1,35 @@ +//HintName: Generator.Equals.Tests.RecordsStructs.FieldEquality.Sample.Generator.Equals.g.cs + +#nullable enable +#pragma warning disable CS0612,CS0618 + +namespace Generator.Equals.Tests.RecordsStructs +{ + partial class FieldEquality + { + partial record struct Sample + { + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] + public bool Equals(global::Generator.Equals.Tests.RecordsStructs.FieldEquality.Sample other) + { + return true + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this._addresses!, other._addresses!) + ; + } + + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] + public override int GetHashCode() + { + var hashCode = new global::System.HashCode(); + + hashCode.Add( + this._addresses!, + global::Generator.Equals.OrderedEqualityComparer.Default); + + return hashCode.ToHashCode(); + } + } + } +} diff --git a/Generator.Equals.SnapshotTests/RecordStructs/GenericParameterEquality.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/GenericParameterEquality.verified.cs index 5a95f75..e688ea2 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/GenericParameterEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/GenericParameterEquality.verified.cs @@ -14,8 +14,8 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.GenericParameterEquality.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/IgnoreEquality.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/IgnoreEquality.verified.cs index 4b08bc4..73cb492 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/IgnoreEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/IgnoreEquality.verified.cs @@ -14,7 +14,7 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.IgnoreEquality.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/NonSupportedMembers.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/NonSupportedMembers.verified.cs index c800a0a..2f486b5 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/NonSupportedMembers.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/NonSupportedMembers.verified.cs @@ -14,7 +14,7 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.NonSupportedMembers.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/NullableEquality.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/NullableEquality.verified.cs index 4b6e9fe..1ed4a7c 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/NullableEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/NullableEquality.verified.cs @@ -14,7 +14,7 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.NullableEquality.Sample other) { return true - && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(Addresses!, other.Addresses!) + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this.Addresses!, other.Addresses!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/ObsoleteMembers.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/ObsoleteMembers.verified.cs index fad4dc4..9ec2838 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/ObsoleteMembers.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/ObsoleteMembers.verified.cs @@ -14,8 +14,8 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.ObsoleteMembers.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(NoComment!, other.NoComment!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Comment!, other.Comment!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.NoComment!, other.NoComment!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Comment!, other.Comment!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/ObsoleteRecord.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/ObsoleteRecord.verified.cs index df22c7d..0b50c9f 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/ObsoleteRecord.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/ObsoleteRecord.verified.cs @@ -14,7 +14,7 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.ObsoleteRecord.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/OrderedEquality.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/OrderedEquality.verified.cs index bcd929c..e7593f2 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/OrderedEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/OrderedEquality.verified.cs @@ -14,7 +14,7 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.OrderedEquality.Sample other) { return true - && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(Addresses!, other.Addresses!) + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this.Addresses!, other.Addresses!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/PrimitiveEquality.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/PrimitiveEquality.verified.cs index e67771b..cfcab2c 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/PrimitiveEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/PrimitiveEquality.verified.cs @@ -14,8 +14,8 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.PrimitiveEquality.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/ReferenceEquality.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/ReferenceEquality.verified.cs index 91f6f66..42da245 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/ReferenceEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/ReferenceEquality.verified.cs @@ -14,7 +14,7 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.ReferenceEquality.Sample other) { return true - && global::Generator.Equals.ReferenceEqualityComparer.Default.Equals(Name!, other.Name!) + && global::Generator.Equals.ReferenceEqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/SetEquality.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/SetEquality.verified.cs index fcff3a3..1c2ba90 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/SetEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/SetEquality.verified.cs @@ -14,7 +14,7 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.SetEquality.Sample other) { return true - && global::Generator.Equals.SetEqualityComparer.Default.Equals(Properties!, other.Properties!) + && global::Generator.Equals.SetEqualityComparer.Default.Equals(this.Properties!, other.Properties!) ; } diff --git a/Generator.Equals.SnapshotTests/RecordStructs/UnorderedEquality.verified.cs b/Generator.Equals.SnapshotTests/RecordStructs/UnorderedEquality.verified.cs index 30ac6b7..701d3d7 100644 --- a/Generator.Equals.SnapshotTests/RecordStructs/UnorderedEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/RecordStructs/UnorderedEquality.verified.cs @@ -14,7 +14,7 @@ partial record struct Sample public bool Equals(global::Generator.Equals.Tests.RecordStructs.UnorderedEquality.Sample other) { return true - && global::Generator.Equals.UnorderedEqualityComparer.Default.Equals(Properties!, other.Properties!) + && global::Generator.Equals.UnorderedEqualityComparer.Default.Equals(this.Properties!, other.Properties!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/CustomEquality.verified.cs b/Generator.Equals.SnapshotTests/Records/CustomEquality.verified.cs index 95a4c21..aaea912 100644 --- a/Generator.Equals.SnapshotTests/Records/CustomEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/CustomEquality.verified.cs @@ -15,9 +15,9 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.CustomEquality { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::Generator.Equals.Tests.Records.CustomEquality.Comparer1.Default.Equals(Name1!, other.Name1!) - && global::Generator.Equals.Tests.Records.CustomEquality.Comparer2.Instance.Equals(Name2!, other.Name2!) - && new global::Generator.Equals.Tests.Records.CustomEquality.LengthEqualityComparer().Equals(Name3!, other.Name3!) + && global::Generator.Equals.Tests.Records.CustomEquality.Comparer1.Default.Equals(this.Name1!, other.Name1!) + && global::Generator.Equals.Tests.Records.CustomEquality.Comparer2.Instance.Equals(this.Name2!, other.Name2!) + && new global::Generator.Equals.Tests.Records.CustomEquality.LengthEqualityComparer().Equals(this.Name3!, other.Name3!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/DictionaryEquality.verified.cs b/Generator.Equals.SnapshotTests/Records/DictionaryEquality.verified.cs index f81d4b5..981ef58 100644 --- a/Generator.Equals.SnapshotTests/Records/DictionaryEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/DictionaryEquality.verified.cs @@ -15,7 +15,7 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.DictionaryEqua { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::Generator.Equals.DictionaryEqualityComparer.Default.Equals(Properties!, other.Properties!) + && global::Generator.Equals.DictionaryEqualityComparer.Default.Equals(this.Properties!, other.Properties!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/ExplicitMode.verified.cs b/Generator.Equals.SnapshotTests/Records/ExplicitMode.verified.cs index 9b1604d..2a8c58c 100644 --- a/Generator.Equals.SnapshotTests/Records/ExplicitMode.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/ExplicitMode.verified.cs @@ -15,7 +15,7 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.ExplicitMode.S { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/FieldEquality.Diagnostics.verified.txt b/Generator.Equals.SnapshotTests/Records/FieldEquality.Diagnostics.verified.txt new file mode 100644 index 0000000..ad47dbb --- /dev/null +++ b/Generator.Equals.SnapshotTests/Records/FieldEquality.Diagnostics.verified.txt @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/Generator.Equals.SnapshotTests/Records/FieldEquality.verified.cs b/Generator.Equals.SnapshotTests/Records/FieldEquality.verified.cs new file mode 100644 index 0000000..ba85d6d --- /dev/null +++ b/Generator.Equals.SnapshotTests/Records/FieldEquality.verified.cs @@ -0,0 +1,37 @@ +//HintName: Generator.Equals.Tests.Records.FieldEquality.Sample.Generator.Equals.g.cs + +#nullable enable +#pragma warning disable CS0612,CS0618 + +namespace Generator.Equals.Tests.Records +{ + partial class FieldEquality + { + partial record Sample + { + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] + public virtual bool Equals(global::Generator.Equals.Tests.Records.FieldEquality.Sample? other) + { + return + !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this._addresses!, other._addresses!) + ; + } + + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] + public override int GetHashCode() + { + var hashCode = new global::System.HashCode(); + + hashCode.Add(this.EqualityContract); + hashCode.Add( + this._addresses!, + global::Generator.Equals.OrderedEqualityComparer.Default); + + return hashCode.ToHashCode(); + } + } + } +} diff --git a/Generator.Equals.SnapshotTests/Records/GenericParameterEquality.verified.cs b/Generator.Equals.SnapshotTests/Records/GenericParameterEquality.verified.cs index 63a1cba..31c3846 100644 --- a/Generator.Equals.SnapshotTests/Records/GenericParameterEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/GenericParameterEquality.verified.cs @@ -15,8 +15,8 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.GenericParamet { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/IgnoreEquality.verified.cs b/Generator.Equals.SnapshotTests/Records/IgnoreEquality.verified.cs index 4bcb995..6f4667a 100644 --- a/Generator.Equals.SnapshotTests/Records/IgnoreEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/IgnoreEquality.verified.cs @@ -15,7 +15,7 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.IgnoreEquality { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/IgnoreInheritedMembers.verified.cs b/Generator.Equals.SnapshotTests/Records/IgnoreInheritedMembers.verified.cs index 3fcf0ec..bb33fdc 100644 --- a/Generator.Equals.SnapshotTests/Records/IgnoreInheritedMembers.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/IgnoreInheritedMembers.verified.cs @@ -15,7 +15,7 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.IgnoreInherite { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/MultiplePartialsEquality.verified.cs b/Generator.Equals.SnapshotTests/Records/MultiplePartialsEquality.verified.cs index 559dd52..dc27423 100644 --- a/Generator.Equals.SnapshotTests/Records/MultiplePartialsEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/MultiplePartialsEquality.verified.cs @@ -15,9 +15,9 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.MultiplePartia { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(Addresses!, other.Addresses!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(FirstName!, other.FirstName!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(LastName!, other.LastName!) + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this.Addresses!, other.Addresses!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.FirstName!, other.FirstName!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.LastName!, other.LastName!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/NonSupportedMembers.verified.cs b/Generator.Equals.SnapshotTests/Records/NonSupportedMembers.verified.cs index e5e97a9..661a424 100644 --- a/Generator.Equals.SnapshotTests/Records/NonSupportedMembers.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/NonSupportedMembers.verified.cs @@ -15,7 +15,7 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.NonSupportedMe { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/NullableEquality.verified.cs b/Generator.Equals.SnapshotTests/Records/NullableEquality.verified.cs index b48515b..06f0c71 100644 --- a/Generator.Equals.SnapshotTests/Records/NullableEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/NullableEquality.verified.cs @@ -15,7 +15,7 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.NullableEquali { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(Addresses!, other.Addresses!) + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this.Addresses!, other.Addresses!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/ObsoleteMembers.verified.cs b/Generator.Equals.SnapshotTests/Records/ObsoleteMembers.verified.cs index 938717a..679d8e2 100644 --- a/Generator.Equals.SnapshotTests/Records/ObsoleteMembers.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/ObsoleteMembers.verified.cs @@ -15,8 +15,8 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.ObsoleteMember { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::System.Collections.Generic.EqualityComparer.Default.Equals(NoComment!, other.NoComment!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Comment!, other.Comment!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.NoComment!, other.NoComment!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Comment!, other.Comment!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/ObsoleteRecord.verified.cs b/Generator.Equals.SnapshotTests/Records/ObsoleteRecord.verified.cs index c66dbb8..3598f66 100644 --- a/Generator.Equals.SnapshotTests/Records/ObsoleteRecord.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/ObsoleteRecord.verified.cs @@ -15,7 +15,7 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.ObsoleteRecord { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/OrderedEquality.verified.cs b/Generator.Equals.SnapshotTests/Records/OrderedEquality.verified.cs index 76053ae..dc3a5e0 100644 --- a/Generator.Equals.SnapshotTests/Records/OrderedEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/OrderedEquality.verified.cs @@ -15,7 +15,7 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.OrderedEqualit { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(Addresses!, other.Addresses!) + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this.Addresses!, other.Addresses!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/PrimitiveEquality.verified.cs b/Generator.Equals.SnapshotTests/Records/PrimitiveEquality.verified.cs index fa91b3b..0c6a575 100644 --- a/Generator.Equals.SnapshotTests/Records/PrimitiveEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/PrimitiveEquality.verified.cs @@ -15,8 +15,8 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.PrimitiveEqual { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/ReferenceEquality.verified.cs b/Generator.Equals.SnapshotTests/Records/ReferenceEquality.verified.cs index 5da480d..46997c1 100644 --- a/Generator.Equals.SnapshotTests/Records/ReferenceEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/ReferenceEquality.verified.cs @@ -15,7 +15,7 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.ReferenceEqual { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::Generator.Equals.ReferenceEqualityComparer.Default.Equals(Name!, other.Name!) + && global::Generator.Equals.ReferenceEqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/SetEquality.verified.cs b/Generator.Equals.SnapshotTests/Records/SetEquality.verified.cs index 5f1a231..0637515 100644 --- a/Generator.Equals.SnapshotTests/Records/SetEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/SetEquality.verified.cs @@ -15,7 +15,7 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.SetEquality.Sa { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::Generator.Equals.SetEqualityComparer.Default.Equals(Properties!, other.Properties!) + && global::Generator.Equals.SetEqualityComparer.Default.Equals(this.Properties!, other.Properties!) ; } diff --git a/Generator.Equals.SnapshotTests/Records/UnorderedEquality.verified.cs b/Generator.Equals.SnapshotTests/Records/UnorderedEquality.verified.cs index edb0ccb..7d3e8ca 100644 --- a/Generator.Equals.SnapshotTests/Records/UnorderedEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Records/UnorderedEquality.verified.cs @@ -15,7 +15,7 @@ public virtual bool Equals(global::Generator.Equals.Tests.Records.UnorderedEqual { return !ReferenceEquals(other, null) && EqualityContract == other.EqualityContract - && global::Generator.Equals.UnorderedEqualityComparer.Default.Equals(Properties!, other.Properties!) + && global::Generator.Equals.UnorderedEqualityComparer.Default.Equals(this.Properties!, other.Properties!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/CustomEquality.verified.cs b/Generator.Equals.SnapshotTests/Structs/CustomEquality.verified.cs index 820e7bf..9539569 100644 --- a/Generator.Equals.SnapshotTests/Structs/CustomEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/CustomEquality.verified.cs @@ -42,9 +42,9 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.CustomEquality.Sample other) { return true - && global::Generator.Equals.Tests.Structs.CustomEquality.Comparer1.Default.Equals(Name1!, other.Name1!) - && global::Generator.Equals.Tests.Structs.CustomEquality.Comparer2.Instance.Equals(Name2!, other.Name2!) - && new global::Generator.Equals.Tests.Structs.CustomEquality.LengthEqualityComparer().Equals(Name3!, other.Name3!) + && global::Generator.Equals.Tests.Structs.CustomEquality.Comparer1.Default.Equals(this.Name1!, other.Name1!) + && global::Generator.Equals.Tests.Structs.CustomEquality.Comparer2.Instance.Equals(this.Name2!, other.Name2!) + && new global::Generator.Equals.Tests.Structs.CustomEquality.LengthEqualityComparer().Equals(this.Name3!, other.Name3!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/DictionaryEquality.verified.cs b/Generator.Equals.SnapshotTests/Structs/DictionaryEquality.verified.cs index 10ac4f4..14cae59 100644 --- a/Generator.Equals.SnapshotTests/Structs/DictionaryEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/DictionaryEquality.verified.cs @@ -42,7 +42,7 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.DictionaryEquality.Sample other) { return true - && global::Generator.Equals.DictionaryEqualityComparer.Default.Equals(Properties!, other.Properties!) + && global::Generator.Equals.DictionaryEqualityComparer.Default.Equals(this.Properties!, other.Properties!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/ExplicitMode.verified.cs b/Generator.Equals.SnapshotTests/Structs/ExplicitMode.verified.cs index 19e6a17..97f2e43 100644 --- a/Generator.Equals.SnapshotTests/Structs/ExplicitMode.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/ExplicitMode.verified.cs @@ -42,7 +42,7 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.ExplicitMode.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/FieldEquality.Diagnostics.verified.txt b/Generator.Equals.SnapshotTests/Structs/FieldEquality.Diagnostics.verified.txt new file mode 100644 index 0000000..ad47dbb --- /dev/null +++ b/Generator.Equals.SnapshotTests/Structs/FieldEquality.Diagnostics.verified.txt @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/Generator.Equals.SnapshotTests/Structs/FieldEquality.verified.cs b/Generator.Equals.SnapshotTests/Structs/FieldEquality.verified.cs new file mode 100644 index 0000000..bcbdaa8 --- /dev/null +++ b/Generator.Equals.SnapshotTests/Structs/FieldEquality.verified.cs @@ -0,0 +1,63 @@ +//HintName: Generator.Equals.Tests.Structs.FieldEquality.Sample.Generator.Equals.g.cs + +#nullable enable +#pragma warning disable CS0612,CS0618 + +namespace Generator.Equals.Tests.Structs +{ + partial class FieldEquality + { + partial struct Sample : global::System.IEquatable + { + /// + /// Indicates whether the object on the left is equal to the object on the right. + /// + /// The left object + /// The right object + /// true if the objects are equal; otherwise, false. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] + public static bool operator ==( + global::Generator.Equals.Tests.Structs.FieldEquality.Sample left, + global::Generator.Equals.Tests.Structs.FieldEquality.Sample right) => + global::System.Collections.Generic.EqualityComparer.Default + .Equals(left, right); + + /// + /// Indicates whether the object on the left is not equal to the object on the right. + /// + /// The left object + /// The right object + /// true if the objects are not equal; otherwise, false. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] + public static bool operator !=(global::Generator.Equals.Tests.Structs.FieldEquality.Sample left, global::Generator.Equals.Tests.Structs.FieldEquality.Sample right) => + !(left == right); + + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] + public override bool Equals(object? obj) => + obj is global::Generator.Equals.Tests.Structs.FieldEquality.Sample o && Equals(o); + + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] + public bool Equals(global::Generator.Equals.Tests.Structs.FieldEquality.Sample other) + { + return true + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this._addresses!, other._addresses!) + ; + } + + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")] + public override int GetHashCode() + { + var hashCode = new global::System.HashCode(); + + hashCode.Add( + this._addresses!, + global::Generator.Equals.OrderedEqualityComparer.Default); + + return hashCode.ToHashCode(); + } + } + } +} diff --git a/Generator.Equals.SnapshotTests/Structs/GenericParameterEquality.verified.cs b/Generator.Equals.SnapshotTests/Structs/GenericParameterEquality.verified.cs index 4f7c028..44af588 100644 --- a/Generator.Equals.SnapshotTests/Structs/GenericParameterEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/GenericParameterEquality.verified.cs @@ -42,8 +42,8 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.GenericParameterEquality.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/IgnoreEquality.verified.cs b/Generator.Equals.SnapshotTests/Structs/IgnoreEquality.verified.cs index 01a092c..66f5263 100644 --- a/Generator.Equals.SnapshotTests/Structs/IgnoreEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/IgnoreEquality.verified.cs @@ -42,7 +42,7 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.IgnoreEquality.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/MultiplePartialsEquality.verified.cs b/Generator.Equals.SnapshotTests/Structs/MultiplePartialsEquality.verified.cs index 79e54fd..d0db24d 100644 --- a/Generator.Equals.SnapshotTests/Structs/MultiplePartialsEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/MultiplePartialsEquality.verified.cs @@ -42,9 +42,9 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.MultiplePartialsEquality.Sample other) { return true - && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(Addresses!, other.Addresses!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(FirstName!, other.FirstName!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(LastName!, other.LastName!) + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this.Addresses!, other.Addresses!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.FirstName!, other.FirstName!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.LastName!, other.LastName!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/NonSupportedMembers.verified.cs b/Generator.Equals.SnapshotTests/Structs/NonSupportedMembers.verified.cs index 6ac40c4..5a8c650 100644 --- a/Generator.Equals.SnapshotTests/Structs/NonSupportedMembers.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/NonSupportedMembers.verified.cs @@ -42,7 +42,7 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.NonSupportedMembers.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/NullableEquality.verified.cs b/Generator.Equals.SnapshotTests/Structs/NullableEquality.verified.cs index 6621dba..e901329 100644 --- a/Generator.Equals.SnapshotTests/Structs/NullableEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/NullableEquality.verified.cs @@ -42,7 +42,7 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.NullableEquality.Sample other) { return true - && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(Addresses!, other.Addresses!) + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this.Addresses!, other.Addresses!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/ObsoleteMembers.verified.cs b/Generator.Equals.SnapshotTests/Structs/ObsoleteMembers.verified.cs index 4f1369d..efc5480 100644 --- a/Generator.Equals.SnapshotTests/Structs/ObsoleteMembers.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/ObsoleteMembers.verified.cs @@ -42,8 +42,8 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.ObsoleteMembers.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(NoComment!, other.NoComment!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Comment!, other.Comment!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.NoComment!, other.NoComment!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Comment!, other.Comment!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/ObsoleteStruct.verified.cs b/Generator.Equals.SnapshotTests/Structs/ObsoleteStruct.verified.cs index f3351c5..fc8ae6c 100644 --- a/Generator.Equals.SnapshotTests/Structs/ObsoleteStruct.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/ObsoleteStruct.verified.cs @@ -42,7 +42,7 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.ObsoleteStruct.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Something!, other.Something!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Something!, other.Something!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/OrderedEquality.verified.cs b/Generator.Equals.SnapshotTests/Structs/OrderedEquality.verified.cs index c7b58c8..0b73877 100644 --- a/Generator.Equals.SnapshotTests/Structs/OrderedEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/OrderedEquality.verified.cs @@ -42,7 +42,7 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.OrderedEquality.Sample other) { return true - && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(Addresses!, other.Addresses!) + && global::Generator.Equals.OrderedEqualityComparer.Default.Equals(this.Addresses!, other.Addresses!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/PrimitiveEquality.verified.cs b/Generator.Equals.SnapshotTests/Structs/PrimitiveEquality.verified.cs index e61413f..138b0fd 100644 --- a/Generator.Equals.SnapshotTests/Structs/PrimitiveEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/PrimitiveEquality.verified.cs @@ -42,8 +42,8 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.PrimitiveEquality.Sample other) { return true - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Name!, other.Name!) - && global::System.Collections.Generic.EqualityComparer.Default.Equals(Age!, other.Age!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Name!, other.Name!) + && global::System.Collections.Generic.EqualityComparer.Default.Equals(this.Age!, other.Age!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/ReferenceEquality.verified.cs b/Generator.Equals.SnapshotTests/Structs/ReferenceEquality.verified.cs index cfa7ed6..77045ce 100644 --- a/Generator.Equals.SnapshotTests/Structs/ReferenceEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/ReferenceEquality.verified.cs @@ -42,7 +42,7 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.ReferenceEquality.Sample other) { return true - && global::Generator.Equals.ReferenceEqualityComparer.Default.Equals(Name!, other.Name!) + && global::Generator.Equals.ReferenceEqualityComparer.Default.Equals(this.Name!, other.Name!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/SetEquality.verified.cs b/Generator.Equals.SnapshotTests/Structs/SetEquality.verified.cs index fbc714e..1ae3fc7 100644 --- a/Generator.Equals.SnapshotTests/Structs/SetEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/SetEquality.verified.cs @@ -42,7 +42,7 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.SetEquality.Sample other) { return true - && global::Generator.Equals.SetEqualityComparer.Default.Equals(Properties!, other.Properties!) + && global::Generator.Equals.SetEqualityComparer.Default.Equals(this.Properties!, other.Properties!) ; } diff --git a/Generator.Equals.SnapshotTests/Structs/UnorderedEquality.verified.cs b/Generator.Equals.SnapshotTests/Structs/UnorderedEquality.verified.cs index 4edea72..7691f57 100644 --- a/Generator.Equals.SnapshotTests/Structs/UnorderedEquality.verified.cs +++ b/Generator.Equals.SnapshotTests/Structs/UnorderedEquality.verified.cs @@ -42,7 +42,7 @@ public override bool Equals(object? obj) => public bool Equals(global::Generator.Equals.Tests.Structs.UnorderedEquality.Sample other) { return true - && global::Generator.Equals.UnorderedEqualityComparer.Default.Equals(Properties!, other.Properties!) + && global::Generator.Equals.UnorderedEqualityComparer.Default.Equals(this.Properties!, other.Properties!) ; } diff --git a/Generator.Equals.Tests/Classes/ExplicitMode.Sample.cs b/Generator.Equals.Tests/Classes/ExplicitMode.Sample.cs index 84a07f8..cda2028 100644 --- a/Generator.Equals.Tests/Classes/ExplicitMode.Sample.cs +++ b/Generator.Equals.Tests/Classes/ExplicitMode.Sample.cs @@ -5,8 +5,11 @@ public partial class ExplicitMode [Equatable(Explicit = true)] public partial class Sample { - public Sample(string name, int age) + private bool _flag; + + public Sample(string name, int age, bool flag) { + _flag = flag; Name = name; Age = age; } diff --git a/Generator.Equals.Tests/Classes/ExplicitMode.cs b/Generator.Equals.Tests/Classes/ExplicitMode.cs index eaa5dd4..b22de04 100644 --- a/Generator.Equals.Tests/Classes/ExplicitMode.cs +++ b/Generator.Equals.Tests/Classes/ExplicitMode.cs @@ -4,8 +4,8 @@ public partial class ExplicitMode { public class EqualsTest : EqualityTestCase { - public override object Factory1() => new Sample("Dave", 35); - public override object Factory2() => new Sample("John", 35); + public override object Factory1() => new Sample("Dave", 35, true); + public override object Factory2() => new Sample("John", 35, false); public override bool EqualsOperator(object value1, object value2) => (Sample) value1 == (Sample) value2; public override bool NotEqualsOperator(object value1, object value2) => (Sample) value1 != (Sample) value2; } @@ -13,8 +13,8 @@ public class EqualsTest : EqualityTestCase public class NotEqualsTest : EqualityTestCase { public override bool Expected => false; - public override object Factory1() => new Sample("Dave", 35); - public override object Factory2() => new Sample("John", 40); + public override object Factory1() => new Sample("Dave", 35, true); + public override object Factory2() => new Sample("John", 40, false); public override bool EqualsOperator(object value1, object value2) => (Sample) value1 == (Sample) value2; public override bool NotEqualsOperator(object value1, object value2) => (Sample) value1 != (Sample) value2; } diff --git a/Generator.Equals.Tests/Classes/FieldEquality.Sample.cs b/Generator.Equals.Tests/Classes/FieldEquality.Sample.cs new file mode 100644 index 0000000..2e109da --- /dev/null +++ b/Generator.Equals.Tests/Classes/FieldEquality.Sample.cs @@ -0,0 +1,16 @@ +namespace Generator.Equals.Tests.Classes +{ + public partial class FieldEquality + { + [Equatable] + public partial class Sample + { + public Sample(string[] addresses) + { + _addresses = addresses; + } + + [OrderedEquality] private readonly string[] _addresses; + } + } +} \ No newline at end of file diff --git a/Generator.Equals.Tests/Classes/FieldEquality.cs b/Generator.Equals.Tests/Classes/FieldEquality.cs new file mode 100644 index 0000000..63261e8 --- /dev/null +++ b/Generator.Equals.Tests/Classes/FieldEquality.cs @@ -0,0 +1,21 @@ +namespace Generator.Equals.Tests.Classes +{ + public partial class FieldEquality + { + public class EqualsTest : EqualityTestCase + { + public override object Factory1() => new Sample(new[] {"10 Downing St"}); + public override bool EqualsOperator(object value1, object value2) => (Sample) value1 == (Sample) value2; + public override bool NotEqualsOperator(object value1, object value2) => (Sample) value1 != (Sample) value2; + } + + public class NotEqualsTest : EqualityTestCase + { + public override bool Expected => false; + public override object Factory1() => new Sample(new[] {"10 Downing St"}); + public override object Factory2() => new Sample(new[] {"Bricklane"}); + public override bool EqualsOperator(object value1, object value2) => (Sample) value1 == (Sample) value2; + public override bool NotEqualsOperator(object value1, object value2) => (Sample) value1 != (Sample) value2; + } + } +} \ No newline at end of file diff --git a/Generator.Equals.Tests/RecordStructs/FieldEquality.Sample.cs b/Generator.Equals.Tests/RecordStructs/FieldEquality.Sample.cs new file mode 100644 index 0000000..2ec24bb --- /dev/null +++ b/Generator.Equals.Tests/RecordStructs/FieldEquality.Sample.cs @@ -0,0 +1,16 @@ +namespace Generator.Equals.Tests.RecordsStructs +{ + public partial class FieldEquality + { + [Equatable] + public partial record struct Sample + { + public Sample(string[] addresses) + { + _addresses = addresses; + } + + [OrderedEquality] private readonly string[] _addresses; + }; + } +} \ No newline at end of file diff --git a/Generator.Equals.Tests/RecordStructs/FieldEquality.cs b/Generator.Equals.Tests/RecordStructs/FieldEquality.cs new file mode 100644 index 0000000..da35e81 --- /dev/null +++ b/Generator.Equals.Tests/RecordStructs/FieldEquality.cs @@ -0,0 +1,21 @@ +namespace Generator.Equals.Tests.RecordsStructs +{ + public partial class FieldEquality + { + public class EqualsTest : EqualityTestCase + { + public override object Factory1() => new Sample(new[] {"10 Downing St"}); + public override bool EqualsOperator(object value1, object value2) => (Sample) value1 == (Sample) value2; + public override bool NotEqualsOperator(object value1, object value2) => (Sample) value1 != (Sample) value2; + } + + public class NotEqualsTest : EqualityTestCase + { + public override bool Expected => false; + public override object Factory1() => new Sample(new[] {"10 Downing St"}); + public override object Factory2() => new Sample(new[] {"Bricklane"}); + public override bool EqualsOperator(object value1, object value2) => (Sample) value1 == (Sample) value2; + public override bool NotEqualsOperator(object value1, object value2) => (Sample) value1 != (Sample) value2; + } + } +} \ No newline at end of file diff --git a/Generator.Equals.Tests/Records/FieldEquality.Sample.cs b/Generator.Equals.Tests/Records/FieldEquality.Sample.cs new file mode 100644 index 0000000..551e83f --- /dev/null +++ b/Generator.Equals.Tests/Records/FieldEquality.Sample.cs @@ -0,0 +1,16 @@ +namespace Generator.Equals.Tests.Records +{ + public partial class FieldEquality + { + [Equatable] + public partial record Sample + { + public Sample(string[] addresses) + { + _addresses = addresses; + } + + [OrderedEquality] private readonly string[] _addresses; + }; + } +} \ No newline at end of file diff --git a/Generator.Equals.Tests/Records/FieldEquality.cs b/Generator.Equals.Tests/Records/FieldEquality.cs new file mode 100644 index 0000000..85c35fc --- /dev/null +++ b/Generator.Equals.Tests/Records/FieldEquality.cs @@ -0,0 +1,21 @@ +namespace Generator.Equals.Tests.Records +{ + public partial class FieldEquality + { + public class EqualsTest : EqualityTestCase + { + public override object Factory1() => new Sample(new[] {"10 Downing St"}); + public override bool EqualsOperator(object value1, object value2) => (Sample) value1 == (Sample) value2; + public override bool NotEqualsOperator(object value1, object value2) => (Sample) value1 != (Sample) value2; + } + + public class NotEqualsTest : EqualityTestCase + { + public override bool Expected => false; + public override object Factory1() => new Sample(new[] {"10 Downing St"}); + public override object Factory2() => new Sample(new[] {"Bricklane"}); + public override bool EqualsOperator(object value1, object value2) => (Sample) value1 == (Sample) value2; + public override bool NotEqualsOperator(object value1, object value2) => (Sample) value1 != (Sample) value2; + } + } +} \ No newline at end of file diff --git a/Generator.Equals.Tests/Structs/ExplicitMode.Sample.cs b/Generator.Equals.Tests/Structs/ExplicitMode.Sample.cs index 82908ea..7b300ec 100644 --- a/Generator.Equals.Tests/Structs/ExplicitMode.Sample.cs +++ b/Generator.Equals.Tests/Structs/ExplicitMode.Sample.cs @@ -5,8 +5,11 @@ public partial class ExplicitMode [Equatable(Explicit = true)] public partial struct Sample { - public Sample(string name, int age) + private bool _flag; + + public Sample(string name, int age, bool flag) { + _flag = flag; Name = name; Age = age; } diff --git a/Generator.Equals.Tests/Structs/ExplicitMode.cs b/Generator.Equals.Tests/Structs/ExplicitMode.cs index 2ff146f..06511a3 100644 --- a/Generator.Equals.Tests/Structs/ExplicitMode.cs +++ b/Generator.Equals.Tests/Structs/ExplicitMode.cs @@ -4,8 +4,8 @@ public partial class ExplicitMode { public class EqualsTest : EqualityTestCase { - public override object Factory1() => new Sample("Dave", 35); - public override object Factory2() => new Sample("John", 35); + public override object Factory1() => new Sample("Dave", 35, true); + public override object Factory2() => new Sample("John", 35, false); public override bool EqualsOperator(object value1, object value2) => (Sample) value1 == (Sample) value2; public override bool NotEqualsOperator(object value1, object value2) => (Sample) value1 != (Sample) value2; } @@ -13,8 +13,8 @@ public class EqualsTest : EqualityTestCase public class NotEqualsTest : EqualityTestCase { public override bool Expected => false; - public override object Factory1() => new Sample("Dave", 35); - public override object Factory2() => new Sample("John", 40); + public override object Factory1() => new Sample("Dave", 35, true); + public override object Factory2() => new Sample("John", 40, false); public override bool EqualsOperator(object value1, object value2) => (Sample) value1 == (Sample) value2; public override bool NotEqualsOperator(object value1, object value2) => (Sample) value1 != (Sample) value2; } diff --git a/Generator.Equals.Tests/Structs/FieldEquality.Sample.cs b/Generator.Equals.Tests/Structs/FieldEquality.Sample.cs new file mode 100644 index 0000000..a08625b --- /dev/null +++ b/Generator.Equals.Tests/Structs/FieldEquality.Sample.cs @@ -0,0 +1,16 @@ +namespace Generator.Equals.Tests.Structs +{ + public partial class FieldEquality + { + [Equatable] + public partial struct Sample + { + public Sample(string[] addresses) + { + _addresses = addresses; + } + + [OrderedEquality] private readonly string[] _addresses; + }; + } +} \ No newline at end of file diff --git a/Generator.Equals.Tests/Structs/FieldEquality.cs b/Generator.Equals.Tests/Structs/FieldEquality.cs new file mode 100644 index 0000000..74d28f3 --- /dev/null +++ b/Generator.Equals.Tests/Structs/FieldEquality.cs @@ -0,0 +1,21 @@ +namespace Generator.Equals.Tests.Structs +{ + public partial class FieldEquality + { + public class EqualsTest : EqualityTestCase + { + public override object Factory1() => new Sample(new[] {"10 Downing St"}); + public override bool EqualsOperator(object value1, object value2) => (Sample) value1 == (Sample) value2; + public override bool NotEqualsOperator(object value1, object value2) => (Sample) value1 != (Sample) value2; + } + + public class NotEqualsTest : EqualityTestCase + { + public override bool Expected => false; + public override object Factory1() => new Sample(new[] {"10 Downing St"}); + public override object Factory2() => new Sample(new[] {"Bricklane"}); + public override bool EqualsOperator(object value1, object value2) => (Sample) value1 == (Sample) value2; + public override bool NotEqualsOperator(object value1, object value2) => (Sample) value1 != (Sample) value2; + } + } +} \ No newline at end of file diff --git a/Generator.Equals/ClassEqualityGenerator.cs b/Generator.Equals/ClassEqualityGenerator.cs index 7d3eefc..1486d67 100644 --- a/Generator.Equals/ClassEqualityGenerator.cs +++ b/Generator.Equals/ClassEqualityGenerator.cs @@ -49,10 +49,7 @@ static void BuildEquals( ? "!ReferenceEquals(other, null) && this.GetType() == other.GetType()" : $"base.Equals(other as {baseTypeName})"); - foreach (var property in symbol.GetProperties()) - { - BuildPropertyEquality(attributesMetadata, sb, level, property, explicitMode); - } + BuildMembersEquality(symbol, attributesMetadata, sb, level, explicitMode); sb.AppendLine(level, ";"); level--; @@ -60,6 +57,7 @@ static void BuildEquals( sb.AppendCloseBracket(ref level); } + static void BuildGetHashCode( ITypeSymbol symbol, AttributesMetadata attributesMetadata, @@ -82,17 +80,14 @@ static void BuildGetHashCode( ? "hashCode.Add(this.GetType());" : "hashCode.Add(base.GetHashCode());"); - foreach (var property in symbol.GetProperties()) - { - BuildPropertyHashCode(property, attributesMetadata, sb, level, explicitMode); - } + BuildMembersHashCode(symbol, attributesMetadata, sb, level, explicitMode); sb.AppendLine(level); sb.AppendLine(level, "return hashCode.ToHashCode();"); sb.AppendCloseBracket(ref level); } - + public static string Generate( ITypeSymbol symbol, AttributesMetadata attributesMetadata, diff --git a/Generator.Equals/EqualityGeneratorBase.cs b/Generator.Equals/EqualityGeneratorBase.cs index ea820b0..7f2082f 100644 --- a/Generator.Equals/EqualityGeneratorBase.cs +++ b/Generator.Equals/EqualityGeneratorBase.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Text; using Microsoft.CodeAnalysis; @@ -32,158 +33,222 @@ internal class EqualityGeneratorBase protected const string InheritDocComment = "/// "; - public static void BuildPropertyEquality(AttributesMetadata attributesMetadata, StringBuilder sb, int level, - IPropertySymbol property, bool explicitMode) + static void BuildEquality(AttributesMetadata attributesMetadata, StringBuilder sb, int level, + ISymbol memberSymbol, ITypeSymbol typeSymbol, bool explicitMode) { - if (property.HasAttribute(attributesMetadata.IgnoreEquality)) + if (memberSymbol.HasAttribute(attributesMetadata.IgnoreEquality)) return; - var propertyName = property.ToFQF(); + var propertyName = memberSymbol.ToFQF(); - var typeName = property.Type.ToNullableFQF(); + var typeName = typeSymbol.ToNullableFQF(); - if (property.HasAttribute(attributesMetadata.UnorderedEquality)) + if (memberSymbol.HasAttribute(attributesMetadata.UnorderedEquality)) { - var types = property.GetIDictionaryTypeArguments(); + var types = typeSymbol.GetIDictionaryTypeArguments(); if (types != null) { sb.AppendLine(level, - $"&& global::Generator.Equals.DictionaryEqualityComparer<{string.Join(", ", types.Value)}>.Default.Equals({propertyName}!, other.{propertyName}!)"); + $"&& global::Generator.Equals.DictionaryEqualityComparer<{string.Join(", ", types.Value)}>.Default.Equals(this.{propertyName}!, other.{propertyName}!)"); } else { - types = property.GetIEnumerableTypeArguments()!; + types = typeSymbol.GetIEnumerableTypeArguments()!; sb.AppendLine(level, - $"&& global::Generator.Equals.UnorderedEqualityComparer<{string.Join(", ", types.Value)}>.Default.Equals({propertyName}!, other.{propertyName}!)"); + $"&& global::Generator.Equals.UnorderedEqualityComparer<{string.Join(", ", types.Value)}>.Default.Equals(this.{propertyName}!, other.{propertyName}!)"); } } - else if (property.HasAttribute(attributesMetadata.OrderedEquality)) + else if (memberSymbol.HasAttribute(attributesMetadata.OrderedEquality)) { - var types = property.GetIEnumerableTypeArguments()!; + var types = typeSymbol.GetIEnumerableTypeArguments()!; sb.AppendLine(level, - $"&& global::Generator.Equals.OrderedEqualityComparer<{string.Join(", ", types.Value)}>.Default.Equals({propertyName}!, other.{propertyName}!)"); + $"&& global::Generator.Equals.OrderedEqualityComparer<{string.Join(", ", types.Value)}>.Default.Equals(this.{propertyName}!, other.{propertyName}!)"); } - else if (property.HasAttribute(attributesMetadata.ReferenceEquality)) + else if (memberSymbol.HasAttribute(attributesMetadata.ReferenceEquality)) { sb.AppendLine(level, - $"&& global::Generator.Equals.ReferenceEqualityComparer<{typeName}>.Default.Equals({propertyName}!, other.{propertyName}!)"); + $"&& global::Generator.Equals.ReferenceEqualityComparer<{typeName}>.Default.Equals(this.{propertyName}!, other.{propertyName}!)"); } - else if (property.HasAttribute(attributesMetadata.SetEquality)) + else if (memberSymbol.HasAttribute(attributesMetadata.SetEquality)) { - var types = property.GetIEnumerableTypeArguments()!; + var types = typeSymbol.GetIEnumerableTypeArguments()!; sb.AppendLine(level, - $"&& global::Generator.Equals.SetEqualityComparer<{string.Join(", ", types.Value)}>.Default.Equals({propertyName}!, other.{propertyName}!)"); + $"&& global::Generator.Equals.SetEqualityComparer<{string.Join(", ", types.Value)}>.Default.Equals(this.{propertyName}!, other.{propertyName}!)"); } - else if (property.HasAttribute(attributesMetadata.CustomEquality)) + else if (memberSymbol.HasAttribute(attributesMetadata.CustomEquality)) { - var attribute = property.GetAttribute(attributesMetadata.CustomEquality); + var attribute = memberSymbol.GetAttribute(attributesMetadata.CustomEquality); var comparerType = (INamedTypeSymbol) attribute?.ConstructorArguments[0].Value!; var comparerMemberName = (string) attribute?.ConstructorArguments[1].Value!; - if (comparerType.GetMembers().Any(x => x.Name == comparerMemberName && x.IsStatic) || comparerType.GetProperties().Any(x => x.Name == comparerMemberName && x.IsStatic)) + if (comparerType.GetMembers().Any(x => x.Name == comparerMemberName && x.IsStatic) || comparerType.GetPropertiesAndFields().Any(x => x.Name == comparerMemberName && x.IsStatic)) { sb.AppendLine(level, - $"&& {comparerType.ToFQF()}.{comparerMemberName}.Equals({propertyName}!, other.{propertyName}!)"); + $"&& {comparerType.ToFQF()}.{comparerMemberName}.Equals(this.{propertyName}!, other.{propertyName}!)"); } else { sb.AppendLine(level, - $"&& new {comparerType.ToFQF()}().Equals({propertyName}!, other.{propertyName}!)"); + $"&& new {comparerType.ToFQF()}().Equals(this.{propertyName}!, other.{propertyName}!)"); } } else if ( - !explicitMode || - property.HasAttribute(attributesMetadata.DefaultEquality)) + memberSymbol.HasAttribute(attributesMetadata.DefaultEquality) || + (!explicitMode && memberSymbol is IPropertySymbol) + ) { sb.AppendLine(level, - $"&& global::System.Collections.Generic.EqualityComparer<{typeName}>.Default.Equals({propertyName}!, other.{propertyName}!)"); + $"&& global::System.Collections.Generic.EqualityComparer<{typeName}>.Default.Equals(this.{propertyName}!, other.{propertyName}!)"); } } - - public static void BuildPropertyHashCode( - IPropertySymbol property, + + public static void BuildMembersEquality(ITypeSymbol symbol, AttributesMetadata attributesMetadata, StringBuilder sb, + int level, bool explicitMode, Predicate? filter = null) + { + foreach (var member in symbol.GetPropertiesAndFields()) + { + if (filter != null && !filter(member)) + continue; + + switch (member) + { + case IPropertySymbol propertySymbol: + BuildEquality(attributesMetadata, sb, level, propertySymbol, propertySymbol.Type, explicitMode); + break; + case IFieldSymbol fieldSymbol: + BuildEquality(attributesMetadata, sb, level, fieldSymbol, fieldSymbol.Type, explicitMode); + break; + default: + throw new NotSupportedException($"Member of type {member.GetType()} not supported"); + } + } + } + static void BuildHashCode( + ISymbol memberSymbol, + ITypeSymbol typeSymbol, AttributesMetadata attributesMetadata, StringBuilder sb, int level, bool explicitMode) { - if (property.HasAttribute(attributesMetadata.IgnoreEquality)) + if (memberSymbol.HasAttribute(attributesMetadata.IgnoreEquality)) return; - if (explicitMode && - !property.HasAttribute(attributesMetadata.DefaultEquality) && - !property.HasAttribute(attributesMetadata.UnorderedEquality) && - !property.HasAttribute(attributesMetadata.OrderedEquality) && - !property.HasAttribute(attributesMetadata.ReferenceEquality) && - !property.HasAttribute(attributesMetadata.SetEquality) && - !property.HasAttribute(attributesMetadata.CustomEquality)) - return; + var propertyName = memberSymbol.ToFQF(); - var propertyName = property.ToFQF(); + var typeName = typeSymbol.ToNullableFQF(); - var typeName = property.Type.ToNullableFQF(); + void BuildHashCodeAdd(Action action) + { + sb.AppendLine(level, $"hashCode.Add("); + level++; - sb.AppendLine(level, $"hashCode.Add("); - level++; - sb.AppendLine(level, $"this.{propertyName}!,"); - sb.AppendMargin(level); + sb.AppendLine(level, $"this.{propertyName}!,"); + sb.AppendMargin(level); - if (property.HasAttribute(attributesMetadata.UnorderedEquality)) - { - var types = property.GetIDictionaryTypeArguments(); + action(); - if (types != null) + sb.AppendLine(");"); + } + + if (memberSymbol.HasAttribute(attributesMetadata.UnorderedEquality)) + { + BuildHashCodeAdd(() => { - sb.Append( - $"global::Generator.Equals.DictionaryEqualityComparer<{string.Join(", ", types.Value)}>.Default"); - } - else + var types = typeSymbol.GetIDictionaryTypeArguments(); + + if (types != null) + { + sb.Append( + $"global::Generator.Equals.DictionaryEqualityComparer<{string.Join(", ", types.Value)}>.Default"); + } + else + { + types = typeSymbol.GetIEnumerableTypeArguments()!; + sb.Append( + $"global::Generator.Equals.UnorderedEqualityComparer<{string.Join(", ", types.Value)}>.Default"); + } + }); + } + else if (memberSymbol.HasAttribute(attributesMetadata.OrderedEquality)) + { + BuildHashCodeAdd(() => { - types = property.GetIEnumerableTypeArguments()!; + var types = typeSymbol.GetIEnumerableTypeArguments()!; sb.Append( - $"global::Generator.Equals.UnorderedEqualityComparer<{string.Join(", ", types.Value)}>.Default"); - } + $"global::Generator.Equals.OrderedEqualityComparer<{string.Join(", ", types.Value)}>.Default"); + }); } - else if (property.HasAttribute(attributesMetadata.OrderedEquality)) + else if (memberSymbol.HasAttribute(attributesMetadata.ReferenceEquality)) { - var types = property.GetIEnumerableTypeArguments()!; - sb.Append( - $"global::Generator.Equals.OrderedEqualityComparer<{string.Join(", ", types.Value)}>.Default"); + BuildHashCodeAdd(() => + { + sb.Append($"global::Generator.Equals.ReferenceEqualityComparer<{typeName}>.Default"); + }); } - else if (property.HasAttribute(attributesMetadata.ReferenceEquality)) + else if (memberSymbol.HasAttribute(attributesMetadata.SetEquality)) { - sb.Append($"global::Generator.Equals.ReferenceEqualityComparer<{typeName}>.Default"); + BuildHashCodeAdd(() => + { + var types = typeSymbol.GetIEnumerableTypeArguments()!; + sb.Append( + $"global::Generator.Equals.SetEqualityComparer<{string.Join(", ", types.Value)}>.Default"); + }); } - else if (property.HasAttribute(attributesMetadata.SetEquality)) + else if (memberSymbol.HasAttribute(attributesMetadata.CustomEquality)) { - var types = property.GetIEnumerableTypeArguments()!; - sb.Append( - $"global::Generator.Equals.SetEqualityComparer<{string.Join(", ", types.Value)}>.Default"); + BuildHashCodeAdd(() => + { + var attribute = memberSymbol.GetAttribute(attributesMetadata.CustomEquality); + var comparerType = (INamedTypeSymbol)attribute?.ConstructorArguments[0].Value!; + var comparerMemberName = (string)attribute?.ConstructorArguments[1].Value!; + + if (comparerType.GetMembers().Any(x => x.Name == comparerMemberName && x.IsStatic) || comparerType + .GetPropertiesAndFields().Any(x => x.Name == comparerMemberName && x.IsStatic)) + { + sb.Append($"{comparerType.ToFQF()}.{comparerMemberName}"); + } + else + { + sb.Append($"new {comparerType.ToFQF()}()"); + } + }); } - else if (property.HasAttribute(attributesMetadata.CustomEquality)) + else if ( + memberSymbol.HasAttribute(attributesMetadata.DefaultEquality) || + (!explicitMode && memberSymbol is IPropertySymbol) + ) { - var attribute = property.GetAttribute(attributesMetadata.CustomEquality); - var comparerType = (INamedTypeSymbol) attribute?.ConstructorArguments[0].Value!; - var comparerMemberName = (string) attribute?.ConstructorArguments[1].Value!; - - if (comparerType.GetMembers().Any(x => x.Name == comparerMemberName && x.IsStatic) || comparerType.GetProperties().Any(x => x.Name == comparerMemberName && x.IsStatic)) + BuildHashCodeAdd(() => { - sb.Append($"{comparerType.ToFQF()}.{comparerMemberName}"); - } - else - { - sb.Append($"new {comparerType.ToFQF()}()"); - } + sb.Append($"global::System.Collections.Generic.EqualityComparer<{typeName}>.Default"); + }); } - else + } + + public static void BuildMembersHashCode(ITypeSymbol symbol, AttributesMetadata attributesMetadata, StringBuilder sb, + int level, bool explicitMode, Predicate? filter = null) + { + foreach (var member in symbol.GetPropertiesAndFields()) { - sb.Append($"global::System.Collections.Generic.EqualityComparer<{typeName}>.Default"); + if (filter != null && !filter(member)) + continue; + + switch (member) + { + case IPropertySymbol propertySymbol: + BuildHashCode(propertySymbol, propertySymbol.Type, attributesMetadata, sb, level, explicitMode); + break; + case IFieldSymbol fieldSymbol: + BuildHashCode(fieldSymbol, fieldSymbol.Type, attributesMetadata, sb, level, explicitMode); + break; + default: + throw new NotSupportedException($"Member of type {member.GetType()} not supported"); + } } - - sb.AppendLine(");"); } } } \ No newline at end of file diff --git a/Generator.Equals/RecordEqualityGenerator.cs b/Generator.Equals/RecordEqualityGenerator.cs index 9318752..fe9e41e 100644 --- a/Generator.Equals/RecordEqualityGenerator.cs +++ b/Generator.Equals/RecordEqualityGenerator.cs @@ -29,16 +29,8 @@ static void BuildEquals( sb.AppendLine(level, baseTypeName == "object" || ignoreInheritedMembers ? "!ReferenceEquals(other, null) && EqualityContract == other.EqualityContract" : $"base.Equals(({baseTypeName}?)other)"); - - foreach (var property in symbol.GetProperties()) - { - var propertyName = property.ToFQF(); - - if (propertyName == "EqualityContract") - continue; - - BuildPropertyEquality(attributesMetadata, sb, level, property, explicitMode); - } + + BuildMembersEquality(symbol, attributesMetadata, sb, level, explicitMode, m => m.ToFQF() != "EqualityContract"); sb.AppendLine(level, ";"); level--; @@ -66,16 +58,8 @@ static void BuildGetHashCode( sb.AppendLine(level, baseTypeName == "object" || ignoreInheritedMembers ? "hashCode.Add(this.EqualityContract);" : "hashCode.Add(base.GetHashCode());"); - - foreach (var property in symbol.GetProperties()) - { - var propertyName = property.ToFQF(); - - if (propertyName == "EqualityContract") - continue; - - BuildPropertyHashCode(property, attributesMetadata, sb, level, explicitMode); - } + + BuildMembersHashCode(symbol, attributesMetadata, sb, level, explicitMode, m => m.ToFQF() != "EqualityContract"); sb.AppendLine(level); sb.AppendLine(level, "return hashCode.ToHashCode();"); diff --git a/Generator.Equals/RecordStructEqualityGenerator.cs b/Generator.Equals/RecordStructEqualityGenerator.cs index 32e75bd..cce8abe 100644 --- a/Generator.Equals/RecordStructEqualityGenerator.cs +++ b/Generator.Equals/RecordStructEqualityGenerator.cs @@ -22,10 +22,7 @@ static void BuildEquals( sb.AppendLine(level, "return true"); level++; - foreach (var property in symbol.GetProperties()) - { - BuildPropertyEquality(attributesMetadata, sb, level, property, explicitMode); - } + BuildMembersEquality(symbol, attributesMetadata, sb, level, explicitMode); sb.AppendLine(level, ";"); level--; @@ -47,10 +44,7 @@ static void BuildGetHashCode( sb.AppendLine(level, @"var hashCode = new global::System.HashCode();"); sb.AppendLine(level); - foreach (var property in symbol.GetProperties()) - { - BuildPropertyHashCode(property, attributesMetadata, sb, level, explicitMode); - } + BuildMembersHashCode(symbol, attributesMetadata, sb, level, explicitMode); sb.AppendLine(level); sb.AppendLine(level, "return hashCode.ToHashCode();"); diff --git a/Generator.Equals/StructEqualityGenerator.cs b/Generator.Equals/StructEqualityGenerator.cs index e5e946a..822bc8e 100644 --- a/Generator.Equals/StructEqualityGenerator.cs +++ b/Generator.Equals/StructEqualityGenerator.cs @@ -43,10 +43,7 @@ static void BuildEquals( sb.AppendLine(level, "return true"); level++; - foreach (var property in symbol.GetProperties()) - { - BuildPropertyEquality(attributesMetadata, sb, level, property, explicitMode); - } + BuildMembersEquality(symbol, attributesMetadata, sb, level, explicitMode); sb.AppendLine(level, ";"); level--; @@ -69,10 +66,7 @@ static void BuildGetHashCode( sb.AppendLine(level, @"var hashCode = new global::System.HashCode();"); sb.AppendLine(level); - foreach (var property in symbol.GetProperties()) - { - BuildPropertyHashCode(property, attributesMetadata, sb, level, explicitMode); - } + BuildMembersHashCode(symbol, attributesMetadata, sb, level, explicitMode); sb.AppendLine(level); sb.AppendLine(level, "return hashCode.ToHashCode();"); diff --git a/Generator.Equals/SymbolHelpers.cs b/Generator.Equals/SymbolHelpers.cs index 35e8ec5..6971fa6 100644 --- a/Generator.Equals/SymbolHelpers.cs +++ b/Generator.Equals/SymbolHelpers.cs @@ -8,17 +8,24 @@ namespace Generator.Equals { public static class SymbolHelpers { - public static IEnumerable GetProperties(this ITypeSymbol symbol) + public static IEnumerable GetPropertiesAndFields(this ITypeSymbol symbol) { - var properties = symbol + var members = symbol .GetMembers() - .OfType() - .Where(x => !x.IsStatic && !x.IsIndexer); + .Where(x => + { + return !x.IsStatic && x switch + { + IPropertySymbol { IsIndexer: false } => true, + IFieldSymbol { CanBeReferencedByName: true } => true, + _ => false + }; + }); - foreach (var property in properties) - yield return property; + foreach (var member in members) + yield return member; } - + // ReSharper disable once InconsistentNaming public static string ToNullableFQF(this ISymbol symbol) => symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat.WithMiscellaneousOptions( @@ -40,20 +47,20 @@ public static string ToFQF(this ISymbol symbol) => public static bool HasAttribute(this ISymbol symbol, INamedTypeSymbol attribute) => GetAttribute(symbol, attribute) != null; - public static INamedTypeSymbol? GetInterface(this IPropertySymbol property, string interfaceFqn) + public static INamedTypeSymbol? GetInterface(this ITypeSymbol symbol, string interfaceFqn) { - return property.Type.AllInterfaces + return symbol.AllInterfaces .FirstOrDefault(x => x.OriginalDefinition.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == interfaceFqn); } - public static ImmutableArray? GetIEnumerableTypeArguments(this IPropertySymbol property) + public static ImmutableArray? GetIEnumerableTypeArguments(this ITypeSymbol symbol) { - return property.GetInterface("global::System.Collections.Generic.IEnumerable")?.TypeArguments; + return symbol.GetInterface("global::System.Collections.Generic.IEnumerable")?.TypeArguments; } - public static ImmutableArray? GetIDictionaryTypeArguments(this IPropertySymbol property) + public static ImmutableArray? GetIDictionaryTypeArguments(this ITypeSymbol symbol) { - return property.GetInterface("global::System.Collections.Generic.IDictionary")?.TypeArguments; + return symbol.GetInterface("global::System.Collections.Generic.IDictionary")?.TypeArguments; } } } \ No newline at end of file diff --git a/README.md b/README.md index 601ce1b..83591e1 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ class Program } } ``` -Need more than records? Generator.Equals also support classes, structs and record structs. +Need more than records? Generator.Equals supports properties (and fields) also across classes, structs and record structs. ```c# using Generator.Equals; @@ -40,6 +40,9 @@ using Generator.Equals; [Equatable] partial class MyClass { + [DefaultEquality] + private int _secretNumber = 42; + [OrderedEquality] public string[] Fruits { get; set; } } @@ -66,6 +69,7 @@ Below is a list of all supported comparers. Would you like something else added? This is the comparer that's used when a property has no attributes indicating otherwise. The generated code will use ```EqualityComparer.Default``` for both equals and hashing operation. +> _Fields are not used in comparison unless explicitly annotated. To enable the default comparison for a field, annotate it with the `DefaultEquality` attribute._ ### IgnoreEquality @@ -159,6 +163,7 @@ argument to CustomEqualityAttribute should fulfill AT LEAST one of the following ## Advanced Options ### Explicit Mode + The generator allows you to explicitly specify which properties are used to generate the `IEquatable`. To do this, set the `Explicit` property of `EquatableAttribute` to `true` and specify the required properties using `DefaultEqualityAttribute` or other attributes.