Ejemplo n.º 1
0
        public void Clients_DoNotHaveToCareAboutNulls()
        {
            var notNull   = new EqualitySubject("a", 1, 1m);
            var chainable = new DelegatedEqualizer <EqualitySubject>((x, y) => x.I.Equals(x.I), x => x.I.GetHashCode());

            Assert.That(chainable.Equals(notNull, null), Is.False);
            Assert.That(chainable.Equals(null, notNull), Is.False);
            Assert.That(chainable.Equals(null, null), Is.True);
        }
Ejemplo n.º 2
0
        public void CanonicalHasherl_SameValue_AsResharperImplementation()
        {
            var baseline = new EqualitySubject {
                I = 243, D = 298.75m, S = "dGG"
            };

            int actual = Hasher.Canonical(baseline.I, baseline.D, baseline.S);

            Assert.That(actual, Is.EqualTo(baseline.GetHashCode()));
        }
        public void Inheritors_DoNotHaveToCareAboutNulls()
        {
            var notNull = new EqualitySubject {
                S = "A"
            };

            var chainable = new PropertySEqualizer();

            Assert.That(chainable.Equals(notNull, null), Is.False);
            Assert.That(chainable.Equals(null, notNull), Is.False);
            Assert.That(chainable.Equals(null, null), Is.True);
        }
Ejemplo n.º 4
0
        public void Chaining_IsDestructive()
        {
            EqualitySubject x1 = new EqualitySubject("x", 1, 1m), x2 = new EqualitySubject("x", 1, 2m);

            ChainableEqualizer <EqualitySubject> sAndI = new DelegatedEqualizer <EqualitySubject>((x, y) => x.S.Equals(y.S), Hasher.Zero)
                                                         .Then((x, y) => x.I.Equals(y.I), Hasher.Zero);

            Assert.That(sAndI.Equals(x1, x2), Is.True);

            var allProp = sAndI.Then(Eq <EqualitySubject> .By((x, y) => x.D.Equals(y.D), x => x.D.GetHashCode()));

            Assert.That(allProp.Equals(x1, x2), Is.False);
            Assert.That(sAndI.Equals(x1, x2), Is.False);
        }
        public void Explore()
        {
            PropertySEqualizer inheritor = new PropertySEqualizer();

            Assert.That(inheritor, Is.InstanceOf <IEqualityComparer <EqualitySubject> >(), "is a proper comparer");

            EqualitySubject one = new EqualitySubject("1", 1, 1m), two = new EqualitySubject("1", 2, 2m);

            Assert.That(inheritor.Equals(one, two), Is.True, "x.S == y.S");
            Assert.That(inheritor.GetHashCode(one), Is.EqualTo(one.S.GetHashCode()));

            ChainableEqualizer <EqualitySubject> chained = inheritor.Then(new PropertyIEqualizer());

            Assert.That(one, Is.Not.EqualTo(two).Using(chained), "x.I != y.I");
        }
Ejemplo n.º 6
0
        public void Explore()
        {
            Func <EqualitySubject, EqualitySubject, bool> compareI = (x, y) => x.I.Equals(y.I);
            var subject = new DelegatedEqualizer <EqualitySubject>(compareI, s => s.I.GetHashCode());
            var x1      = new EqualitySubject("x", 1, 1m);

            Assert.That(subject.Equals(x1, new EqualitySubject("y", 1, 2m)), Is.True);

            var custom = new DelegatedEqualizer <EqualitySubject>((x, y) => x.I.Equals(y.I), s => s.I.GetHashCode());

            Assert.That(custom.GetHashCode(x1), Is.EqualTo(x1.I.GetHashCode()));
            custom = new DelegatedEqualizer <EqualitySubject>((x, y) => x.I.Equals(y.I), s => Hasher.Default(s.I));
            Assert.That(custom.GetHashCode(x1), Is.EqualTo(x1.I.GetHashCode()));
            var @default = new DelegatedEqualizer <EqualitySubject>((x, y) => x.I.Equals(y.I), Hasher.Default);

            Assert.That(@default.GetHashCode(x1), Is.EqualTo(x1.GetHashCode()));
            var zero = new DelegatedEqualizer <EqualitySubject>((x, y) => x.I.Equals(y.I), Hasher.Zero);

            Assert.That(zero.GetHashCode(x1), Is.EqualTo(0));


            IEqualityComparer <EqualitySubject> bySAndI = new DelegatedEqualizer <EqualitySubject>((x, y) => x.S.Equals(y.S), Hasher.Zero)
                                                          .Then(new DelegatedEqualizer <EqualitySubject>((x, y) => x.I.Equals(y.I), Hasher.Default));

            bySAndI = new DelegatedEqualizer <EqualitySubject>((x, y) => x.S.Equals(y.S), Hasher.Zero)
                      .Then((x, y) => x.I == y.I, Hasher.Default);
            bySAndI = Eq <EqualitySubject> .By((x, y) => x.S.Equals(y.S), Hasher.Zero)
                      .Then((x, y) => x == y, Hasher.Default);

            IComparer <EqualitySubject> comparer = Cmp <EqualitySubject> .By(x => x.I);

            IEqualityComparer <EqualitySubject> eq         = new DelegatedEqualizer <EqualitySubject>(comparer, Hasher.Zero);
            Comparison <EqualitySubject>        comparison = (x, y) => x.D.CompareTo(y.D);

            eq = new DelegatedEqualizer <EqualitySubject>(comparison, Hasher.Zero);
        }