public void Custom_IEquatable_implementation_is_not_used_yet()
        {
            var x = new TrueEquatable {
                Property = 1
            };
            var y = new TrueEquatable {
                Property = 2
            };

            var comparer = new ComparerBuilder().GetEqualityComparer <TrueEquatable>();

            comparer.Equals(x, y).Should().BeFalse();
        }
        public void Cycle_detection_in_multiple_threads_works()
        {
            Helper.Parallel(() => {
                var comparer = new ComparerBuilder().GetEqualityComparer <OneSealed>();

                var one             = _fixture.Create <OneSealed>();
                var other           = _fixture.Create <OneSealed>();
                one.Two.Three.One   = one;
                other.Two.Three.One = other;

                using (new AssertionScope()) {
                    comparer.Equals(one, other).Should().BeFalse();
                    comparer.GetHashCode(one).Should().NotBe(comparer.GetHashCode(other));
                }
            });
        }
Ejemplo n.º 3
0
        public void Custom_comparer_should_be_used_for_structs()
        {
            Test(() => {
                var x = _fixture.Create <ComparableStruct <string> >();
                var y = _fixture.Create <ComparableStruct <string> >();

                var comparer = new ComparerBuilder(c => c
                                                   .SetCustomEqualityComparer <SampleStructCustomEqualityComparer>())
                               .GetEqualityComparer <ComparableStruct <string> >();

                using (new AssertionScope()) {
                    comparer.Equals(x, y).Should().BeTrue();
                    comparer.GetHashCode(x).Should().Be(0);
                }
            });
        }
        public void No_cycle_when_identical_objects_in_collection()
        {
            var one = new SelfSealed {
                Value = 1
            };

            one.Second = new SelfSealed {
                First = one,
                Value = 1
            };
            var two = new SelfSealed {
                Value = 1
            };

            two.Second = new SelfSealed {
                First = two,
                Value = 1
            };
            var x = new[] {
                one,
                one,
                one
            };

            var other = new SelfSealed {
                Second = new SelfSealed {
                    First = new SelfSealed {
                        Value = 4
                    },
                    Value = 4
                },
                Value = 3
            };
            var y = new[] {
                two,
                two,
                other
            };

            var comparer = new ComparerBuilder().For <SelfSealed>().GetEqualityComparer <SelfSealed[]>();

            using (new AssertionScope()) {
                comparer.Equals(x, y).Should().BeFalse();
                comparer.GetHashCode(x).Should().Be(271135911);
                comparer.GetHashCode(y).Should().Be(194708355);
            }
        }
Ejemplo n.º 5
0
        public void Custom_comparer_defined_as_a_type_should_be_used()
        {
            Test(() => {
                var x = _fixture.Create <SampleObject <ComparableStruct <string> > >();
                var y = _fixture.Create <SampleObject <ComparableStruct <string> > >();
                var expectedCustomHash = HashCodeCombiner.Combine(0, 0);

                var comparer = new ComparerBuilder(c => c
                                                   .SetCustomEqualityComparer <SampleStructCustomEqualityComparer>())
                               .GetEqualityComparer <SampleObject <ComparableStruct <string> > >();

                using (new AssertionScope()) {
                    comparer.Equals(x, y).Should().BeTrue();
                    comparer.GetHashCode(x).Should().Be(expectedCustomHash);
                }
            });
        }
Ejemplo n.º 6
0
        public void Custom_comparer_should_be_used_for_collection_when_defined()
        {
            Test(() => {
                var x = _fixture.Create <ComparableObject <int[]> >();
                var y = _fixture.Create <ComparableObject <int[]> >();

                var referenceComparer = new ComparableObjectEqualityComparer <int[]>(new CustomizableEqualityComparer <int[]>(
                                                                                         (a, b) => a is null && b is null || !(a is null || b is null), _ => 0));
                var expected = referenceComparer.Equals(x, y);

                var equalsIsUsed = false;
                var hashIsUsed   = false;
                var comparer     = new ComparerBuilder(c => c
                                                       .SetDefaultCollectionsOrderIgnoring(_fixture.Create <bool>())
                                                       .SetCustomEqualityComparer(new CustomizableEqualityComparer <int>(
                                                                                      (__, _) => {
                    equalsIsUsed = true;
                    return(true);
                },
                                                                                      _ => {
                    hashIsUsed = true;
                    return(0);
                })))
                                   .GetEqualityComparer <ComparableObject <int[]> >();

                var actualEquals = comparer.Equals(x, y);
                var hashX        = comparer.GetHashCode(x);
                var hashY        = comparer.GetHashCode(y);

                using (new AssertionScope()) {
                    actualEquals.Should().Be(expected);
                    hashX.Should().NotBe(0);
                    hashY.Should().NotBe(0);
                    var fieldIsNull        = x.Field is null || y.Field is null;
                    var propertyIsNull     = x.Property is null || y.Property is null;
                    var fieldsAreNulls     = x.Field is null && y.Field is null;
                    var propertiesAreNulls = x.Property is null && y.Property is null;
                    equalsIsUsed.Should().Be(!fieldIsNull || fieldsAreNulls && !propertyIsNull, $"null checks are used.\n{x}\n{y}");
                    hashIsUsed.Should().Be(!fieldsAreNulls || !propertiesAreNulls, $"null checks are used.\n{x}\n{y}");
                }
            });
        }
        public void Should_use_delayed_comparison()
        {
            var x = _fixture.CreateMany <SampleEqualityStruct <EnumSmall?>?>().ToArray();
            var y = _fixture.CreateMany <SampleEqualityStruct <EnumSmall?>?>().ToArray();

            var referenceComparer = new CollectionEqualityComparer <SampleEqualityStruct <EnumSmall?>?>(
                new NullableEqualityComparer <SampleEqualityStruct <EnumSmall?> >());

            var comparer = new ComparerBuilder().GetEqualityComparer <object>();

            var expectedHashX = referenceComparer.GetHashCode(x);
            var expectedHashY = referenceComparer.GetHashCode(y);
            var actualHashX   = comparer.GetHashCode(x);
            var actualHashY   = comparer.GetHashCode(y);
            var expected      = referenceComparer.Equals(x, y);
            var actual        = comparer.Equals(x, y);

            using (new AssertionScope()) {
                actualHashX.Should().Be(expectedHashX);
                actualHashY.Should().Be(expectedHashY);
                actual.Should().Be(expected);
            }
        }