コード例 #1
0
        public void After_change_custom_comparer_new_dynamic_comparer_should_be_created()
        {
            Test(() => {
                var x = _fixture.Create <Tuple <int, string> >();
                var y = _fixture.Create <Tuple <int, string> >();
                var expectedEqualsInt    = x.Item1.Equals(y.Item1);
                var expectedEqualsString = x.Item2?.Equals(y.Item2) ?? y.Item2 == null;
                var expectedHashInt      = HashCodeCombiner.Combine(x.Item1.GetHashCode(), 0);
                var expectedHashString   = HashCodeCombiner.Combine(0, x.Item2?.GetHashCode() ?? 0);

                var builder               = new ComparerBuilder(c => c.SetCustomEqualityComparer(new CustomizableEqualityComparer <string>((__, _) => true, _ => 0)));
                var comparerForIntOnly    = builder.GetEqualityComparer <Tuple <int, string> >();
                var comparerForStringOnly =
                    builder.Configure(c => c.SetCustomEqualityComparer <string>(null)
                                      .SetCustomEqualityComparer(new CustomizableEqualityComparer <int>((__, _) => true, _ => 0)))
                    .GetEqualityComparer <Tuple <int, string> >();

                using (new AssertionScope()) {
                    comparerForIntOnly.Equals(x, y).Should().Be(expectedEqualsInt, "comparison is based on int field");
                    comparerForStringOnly.Equals(x, y).Should().Be(expectedEqualsString, "comparison is based on string field");
                    comparerForIntOnly.GetHashCode(x).Should().Be(expectedHashInt);
                    comparerForStringOnly.GetHashCode(x).Should().Be(expectedHashString);
                }
            });
        }
コード例 #2
0
        public void After_clean_custom_comparer_for_value_type_dynamic_comparer_should_be_created()
        {
            var x = _fixture.Create <SampleObject <SampleStruct <string> > >();
            var y = _fixture.Create <SampleObject <SampleStruct <string> > >();

            var reference = new SampleObjectComparer <SampleStruct <string> >(new SampleStructComparer <string>());
            var expected  = reference.Compare(x, y);

            var builder   = new ComparerBuilder(c => c.SetCustomComparer <SampleStructCustomComparer>());
            var comparer1 = builder.GetComparer <SampleObject <SampleStruct <string> > >();
            var comparer2 = builder.Configure(c => c.SetCustomComparer <SampleStruct <string> >(null))
                            .GetComparer <SampleObject <SampleStruct <string> > >();

            using (new AssertionScope()) {
                comparer1.Compare(x, y).Should().Be(0);
                comparer2.Compare(x, y).Normalize().Should().Be(expected.Normalize());
            }
        }
コード例 #3
0
        public void After_change_custom_comparer_new_dynamic_comparer_should_be_created()
        {
            Test(() => {
                var x         = _fixture.Create <Tuple <int, string> >();
                var y         = _fixture.Create <Tuple <int, string> >();
                var expected1 = x.Item1.CompareTo(y.Item1);
                var expected2 = x.Item2?.CompareTo(y.Item2) ?? -y.Item2?.CompareTo(null) ?? 0;

                var builder   = new ComparerBuilder(c => c.SetCustomComparer(new CustomizableComparer <string>((__, _) => 0)));
                var comparer1 = builder.GetComparer <Tuple <int, string> >();
                var comparer2 =
                    builder.Configure(c => c.SetCustomComparer <string>(null)
                                      .SetCustomComparer(new CustomizableComparer <int>((__, _) => 0)))
                    .GetComparer <Tuple <int, string> >();

                using (new AssertionScope()) {
                    comparer1.Compare(x, y).Normalize().Should().Be(expected1.Normalize());
                    comparer2.Compare(x, y).Normalize().Should().Be(expected2.Normalize());
                }
            });
        }
コード例 #4
0
        public void After_clean_custom_comparer_for_value_type_dynamic_comparer_should_be_created()
        {
            var x = _fixture.Create <ComparableObject <ComparableStruct <string> > >();
            var y = _fixture.Create <ComparableObject <ComparableStruct <string> > >();

            var reference          = new ComparableObjectEqualityComparer <ComparableStruct <string> >(new ComparableStructEqualityComparer <string>());
            var expectedEquals     = reference.Equals(x, y);
            var expectedHash       = reference.GetHashCode(x);
            var expectedCustomHash = HashCodeCombiner.Combine(0, 0);

            var builder         = new ComparerBuilder(c => c.SetCustomEqualityComparer <SampleStructCustomEqualityComparer>());
            var comparerCustom  = builder.GetEqualityComparer <ComparableObject <ComparableStruct <string> > >();
            var comparerDefault =
                builder.Configure(c => c.SetCustomEqualityComparer <ComparableStruct <string> >(null))
                .GetEqualityComparer <ComparableObject <ComparableStruct <string> > >();

            using (new AssertionScope()) {
                comparerCustom.Equals(x, y).Should().BeTrue();
                comparerCustom.GetHashCode(x).Should().Be(expectedCustomHash);
                comparerDefault.Equals(x, y).Should().Be(expectedEquals);
                comparerDefault.GetHashCode(x).Should().Be(expectedHash);
            }
        }