public void Replaced_comparable_object_is_compared_with_custom_implementation()
        {
            var comparer = new ComparerBuilder().GetComparer <SampleObject <ComparableBaseObject <EnumSmall> > >();
            var fixture  = FixtureBuilder.GetInstance();

            var one = new SampleObject <ComparableBaseObject <EnumSmall> > {
                Property = fixture.Create <ComparableChildObject <EnumSmall> >()
            };

            comparer.Compare(one, one.DeepClone()).Should().Be(0);

            for (var i = 0; i < Constants.SmallCount; i++)
            {
                one.Property = fixture.Create <ComparableChildObject <EnumSmall> >();
                var other = new SampleObject <ComparableBaseObject <EnumSmall> > {
                    Property = fixture.Create <ComparableChildObject <EnumSmall> >()
                };

                var expected = one.Property.CompareTo(other.Property).Normalize();
                var actual   = comparer.Compare(one, other).Normalize();

                actual.Should().Be(expected);
            }

            ComparableChildObject <EnumSmall> .UsedCompareTo.Should().BeTrue();
        }
        public void Null_enumerator_pass()
        {
            var x        = new EnumerableStruct <int>(null);
            var comparer = new ComparerBuilder().GetComparer <EnumerableStruct <int> >();

            Assert.Throws <NullReferenceException>(() => comparer.Compare(x, x));
            Assert.Throws <NullReferenceException>(() => comparer.Compare(default, default));
        public void Empty_structs_should_be_equal()
        {
            var comparer = new ComparerBuilder().GetComparer <DummyStruct>();

            var actual = comparer.Compare(new DummyStruct(), new DummyStruct());

            actual.Should().Be(0);
        }
        public void Enumerable_structs_with_nullables_are_comparable()
        {
            var referenceComparer =
                new CollectionComparer <SampleStruct <int?>?>(
                    new NullableComparer <SampleStruct <int?> >(new SampleStructComparer <int?>()));
            var comparer = new ComparerBuilder().GetComparer <EnumerableStruct <SampleStruct <int?>?> >();

            Helper.Parallel(() => {
                var x = _fixture.Create <EnumerableStruct <SampleStruct <int?>?> >();
                var y = _fixture.Create <EnumerableStruct <SampleStruct <int?>?> >();

                var expectedEquals = referenceComparer.Compare(x, y);
                var equals         = comparer.Compare(x, y);

                using (new AssertionScope()) {
                    comparer.Compare(x, x).Should().Be(0);
                    equals.Should().Be(expectedEquals);
                }
            });
        }
        public void Enumerables_are_not_equal()
        {
            var x = new List <int>(new[] { 1, 2, 3 });
            var y = new List <int>(new[] { 2, 3, 1 });

            var comparer = new ComparerBuilder().GetComparer <IEnumerable <int> >();

            var result = comparer.Compare(x, y);

            result.Should().Be(-1);
        }
        public void Enumerable_structs_are_comparable()
        {
            var referenceComparer = new CollectionComparer <int>();
            var x        = _fixture.Create <EnumerableStruct <int> >();
            var y        = _fixture.Create <EnumerableStruct <int> >();
            var expected = referenceComparer.Compare(x, y);

            var comparer = new ComparerBuilder().GetComparer <EnumerableStruct <int> >();

            var result = comparer.Compare(x, y);

            result.Should().Be(expected);
        }
        public void Custom_comparer_defined_as_a_type_should_be_used()
        {
            Test(() => {
                var x = _fixture.Create <SampleObject <SampleStruct <string> > >();
                var y = _fixture.Create <SampleObject <SampleStruct <string> > >();

                var comparer =
                    new ComparerBuilder(c => c.SetCustomComparer <SampleStructCustomComparer>())
                    .GetComparer <SampleObject <SampleStruct <string> > >();

                comparer.Compare(x, y).Should().Be(0);
            });
        }
        public void Should_use_delayed_comparison()
        {
            var x = Fixture.CreateMany <SampleStruct <EnumSmall?>?>().ToArray();
            var y = Fixture.CreateMany <SampleStruct <EnumSmall?>?>().ToArray();

            var referenceComparer = new CollectionComparer <SampleStruct <EnumSmall?>?>(new NullableComparer <SampleStruct <EnumSmall?> >(new SampleStructComparer <EnumSmall?>()));
            var comparer          = new ComparerBuilder().GetComparer <object>();

            var expected = referenceComparer.Compare(x, y).Normalize();
            var actual   = comparer.Compare(x, y).Normalize();

            actual.Should().Be(expected);
        }
        public void Compare_enumerables_of_enumerables()
        {
            var collectionComparer = new CollectionComparer <List <int?> >(new CollectionComparer <int?>());
            var referenceComparer  = new CollectionComparer <EnumerableStruct <List <int?> >?>(
                new NullableComparer <EnumerableStruct <List <int?> > >(
                    new CustomizableComparer <EnumerableStruct <List <int?> > >((a, b) =>
                                                                                collectionComparer.Compare(a, b))));
            var comparer = new ComparerBuilder().GetComparer <IEnumerable <EnumerableStruct <List <int?> >?> >();

            Helper.Parallel(() => {
                var x = _fixture.CreateMany <EnumerableStruct <List <int?> >?>().RandomNulls().ToList();
                var y = _fixture.CreateMany <EnumerableStruct <List <int?> >?>().RandomNulls().ToList();

                var expectedEquals = referenceComparer.Compare(x, y);
                var equals         = comparer.Compare(x, y);

                using (new AssertionScope()) {
                    comparer.Compare(x, x).Should().Be(0);
                    equals.Should().Be(expectedEquals);
                }
            });
        }
        public void Custom_comparable_implementation_should_return_negative_when_first_argument_isnull()
        {
            var one = new SampleObject <ComparableBaseObject <EnumSmall> > {
                Property = FixtureBuilder.GetInstance().Create <ComparableBaseObject <EnumSmall> >()
            };

            var other = one.DeepClone();

            one.Property = null;

            var comparer = new ComparerBuilder().GetComparer <SampleObject <ComparableBaseObject <EnumSmall> > >();

            comparer.Compare(one, other).Should().BeNegative();
        }
Example #11
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 expected = SelfSealed.Comparer.Compare(x, y);
            var comparer = new ComparerBuilder()
                           .For <SelfSealed>(c => c.IgnoreMember(o => o.Id))
                           .GetComparer <SelfSealed[]>();
            var actual = comparer.Compare(x, y);

            using (new AssertionScope()) {
                expected.Should().Be(-1);
                actual.Should().Be(-1);
            }
        }
        public void Delayed_comparison_on_sample_object()
        {
            var x = new SampleObject <EnumSmall?> {
                Field = null
            };
            var y = new SampleObject <EnumSmall?> {
                Field = EnumSmall.One
            };

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

            var actual = comparer.Compare(x, y);

            actual.Should().Be(-1);
        }
Example #13
0
        public void Cycle_detection_in_multiple_threads_works()
        {
            Helper.Parallel(() => {
                var comparer = new ComparerBuilder().GetComparer <OneSealed>();

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

                var expected = one.Value.CompareTo(other.Value);
                var actual   = comparer.Compare(one, other);

                actual.Should().Be(expected);
            });
        }
        private static void Ignoring_order_does_not_add_side_effect_for <TElement>()
        {
            var comparer = new ComparerBuilder(c => c.SetDefaultCollectionsOrderIgnoring(true)).GetComparer <TElement[]>();

            var fixture = FixtureBuilder.GetInstance();
            var sample  = fixture.Create <TElement[]>();
            var clone   = sample.DeepClone();

            var elements = FixtureBuilder.GetSimpleInstance().CreateMany <TElement>().ToArray();

            comparer.Compare(sample, elements)
            .Should()
            .NotBe(0);

            sample.ShouldBeSameOrder(clone);
        }
        public void Custom_comparer_should_be_used_for_collection_when_defined()
        {
            Test(() => {
                var x = _fixture.Create <SampleObject <int[]> >();
                var y = _fixture.Create <SampleObject <int[]> >();

                var referenceComparer = new SampleObjectComparer <int[]>(new CustomizableComparer <int[]>((a, b) => {
                    if (a == b)
                    {
                        return(0);
                    }

                    if (b is null)
                    {
                        return(1);
                    }

                    if (a is null)
                    {
                        return(-1);
                    }

                    return(0);
                }));
                var expected = referenceComparer.Compare(x, y);

                var comparer =
                    new ComparerBuilder(c => c.SetDefaultCollectionsOrderIgnoring(_fixture.Create <bool>())
                                        .SetCustomComparer(new CustomizableComparer <int>((__, _) => 0)))
                    .GetComparer <SampleObject <int[]> >();

                var actual = comparer.Compare(x, y);

                actual.Should().Be(expected);
            });
        }