NUnitEqualityComparer encapsulates NUnit's handling of equality tests between objects.
Inheritance: INUnitEqualityComparer
Exemplo n.º 1
0
        public void InheritingAndOverridingIEquatable()
        {
            var obj1 = new InheritingEquatableObject {
                SomeProperty = 1, OtherProperty = 2
            };
            var obj2 = new InheritingEquatableObject {
                SomeProperty = 1, OtherProperty = 2
            };
            var obj3 = new InheritingEquatableObject {
                SomeProperty = 1, OtherProperty = 3
            };
            var obj4 = new InheritingEquatableObject {
                SomeProperty = 4, OtherProperty = 2
            };

            Assert.That(obj1, Is.EqualTo(obj2));
            Assert.That(obj1, Is.Not.EqualTo(obj3));
            Assert.That(obj1, Is.Not.EqualTo(obj4));

            var n         = new NUnitEqualityComparer();
            var tolerance = Tolerance.Exact;

            Assert.That(n.AreEqual(obj1, obj2, ref tolerance), Is.True);
            Assert.That(n.AreEqual(obj2, obj1, ref tolerance), Is.True);
            Assert.That(n.AreEqual(obj1, obj3, ref tolerance), Is.False);
            Assert.That(n.AreEqual(obj3, obj1, ref tolerance), Is.False);
            Assert.That(n.AreEqual(obj1, obj4, ref tolerance), Is.False);
            Assert.That(n.AreEqual(obj4, obj1, ref tolerance), Is.False);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Construct a CollectionTally object from a comparer and a collection
        /// </summary>
        public CollectionTally(NUnitEqualityComparer comparer, IEnumerable c)
        {
            this.comparer = comparer;

            foreach (object o in c)
                list.Add(o);
        }
Exemplo n.º 3
0
        public void CanCompareArrayContainingSelfToSelf()
        {
            object[] array = new object[1];
            array[0] = array;
            NUnitEqualityComparer comparer = new NUnitEqualityComparer();

            Assert.True(comparer.ObjectsEqual(array, array));
        }
Exemplo n.º 4
0
 public CollectionTally(NUnitEqualityComparer comparer, IEnumerable c)
 {
     this.comparer = comparer;
     foreach (object item in c)
     {
         list.Add(item);
     }
 }
Exemplo n.º 5
0
        public void SelfContainedItemDoesntRecurseForever <T>(T x, ICollection y)
        {
            var equalityComparer = new NUnitEqualityComparer();
            var tolerance        = Tolerance.Default;

            equalityComparer.ExternalComparers.Add(new DetectRecursionComparer(30));

            Assert.DoesNotThrow(() => equalityComparer.AreEqual(x, y, ref tolerance));
        }
Exemplo n.º 6
0
        /// <summary>Construct a CollectionTally object from a comparer and a collection.</summary>
        /// <param name="comparer">The comparer to use for equality.</param>
        /// <param name="c">The expected collection to compare against.</param>
        public CollectionTally(NUnitEqualityComparer comparer, IEnumerable c)
        {
            this.comparer = comparer;

            foreach (object o in c)
            {
                _missingItems.Add(o);
            }
        }
Exemplo n.º 7
0
        public void TestToleranceDefault()
        {
            var defaultTolerance = Tolerance.Default;

            Assert.IsTrue(defaultTolerance.IsUnsetOrDefault);

            var comparer = new NUnitEqualityComparer();

            Assert.IsTrue(comparer.AreEqual(2.0d, 2.1d, ref defaultTolerance));
        }
Exemplo n.º 8
0
        public void TestToleranceExact()
        {
            var noneTolerance = Tolerance.Exact;

            Assert.IsFalse(noneTolerance.IsUnsetOrDefault);

            var comparer = new NUnitEqualityComparer();

            Assert.IsFalse(comparer.AreEqual(2.0d, 2.1d, ref noneTolerance));
        }
Exemplo n.º 9
0
        public void SelfContainedItemFoundInCollection <T>(T x, ICollection y)
        {
            var equalityComparer = new NUnitEqualityComparer();
            var tolerance        = Tolerance.Default;
            var equality         = equalityComparer.AreEqual(x, y, ref tolerance);

            Assert.IsFalse(equality);
            Assert.Contains(x, y);
            Assert.That(y, Contains.Item(x));
            Assert.That(y, Does.Contain(x));
        }
Exemplo n.º 10
0
        /// <summary>Construct a CollectionTally object from a comparer and a collection.</summary>
        /// <param name="comparer">The comparer to use for equality.</param>
        /// <param name="c">The expected collection to compare against.</param>
        public CollectionTally(NUnitEqualityComparer comparer, IEnumerable c)
        {
            _missingItems = new Dictionary <object, int>(new TallyEqualityComparer(comparer));

            var index = 0;

            foreach (object o in c)
            {
                _missingItems.Add(o, index++);
            }
        }
Exemplo n.º 11
0
        /// <summary>Construct a CollectionTally object from a comparer and a collection.</summary>
        /// <param name="comparer">The comparer to use for equality.</param>
        /// <param name="c">The expected collection to compare against.</param>
        public CollectionTally(NUnitEqualityComparer comparer, IEnumerable c)
        {
            this.comparer = comparer;

            _missingItems = ToArrayList(c);

            if (c.IsSortable())
            {
                _isSortable = TrySort(ref _missingItems);
            }
        }
Exemplo n.º 12
0
        public void SelfContainedDuplicateItemsAreCompared()
        {
            var equalityComparer = new NUnitEqualityComparer();
            var equalInstance1   = new[] { 1 };
            var equalInstance2   = new[] { 1 };

            var x = new[] { equalInstance1, equalInstance1 };
            var y = new[] { equalInstance2, equalInstance2 };

            Assert.True(equalityComparer.AreEqual(x, y, ref tolerance));
        }
Exemplo n.º 13
0
        /// <summary>Construct a CollectionTally object from a comparer and a collection.</summary>
        /// <param name="comparer">The comparer to use for equality.</param>
        /// <param name="c">The expected collection to compare against.</param>
        public CollectionTally(NUnitEqualityComparer comparer, IEnumerable c)
        {
            this._comparer = comparer;

            _missingItems = ToArrayList(c);

            if (c.IsSortable())
            {
                _missingItems.Sort();
                _isSortable = true;
            }
        }
Exemplo n.º 14
0
        public void ImplementingIEquatableOnABaseClassOrInterfaceThroughInterface()
        {
            IEquatableObject obj1 = new InheritedEquatableObject {
                SomeProperty = 1
            };
            IEquatableObject obj2 = new InheritedEquatableObject {
                SomeProperty = 1
            };

            var n         = new NUnitEqualityComparer();
            var tolerance = Tolerance.Exact;

            Assert.That(n.AreEqual(obj1, obj2, ref tolerance), Is.True);
            Assert.That(n.AreEqual(obj2, obj1, ref tolerance), Is.True);
        }
Exemplo n.º 15
0
        public void ImplementingIEquatableDirectlyOnTheClass()
        {
            var obj1 = new EquatableObject {
                SomeProperty = 1
            };
            var obj2 = new EquatableObject {
                SomeProperty = 1
            };

            var n         = new NUnitEqualityComparer();
            var tolerance = Tolerance.Exact;

            Assert.That(n.AreEqual(obj1, obj2, ref tolerance), Is.True);
            Assert.That(n.AreEqual(obj2, obj1, ref tolerance), Is.True);
        }
Exemplo n.º 16
0
        public void CanHandleMultipleImplementationsOfIEquatable()
        {
            IEquatableObject obj1 = new InheritedEquatableObject {
                SomeProperty = 1
            };
            IEquatableObject obj2 = new MultipleIEquatables {
                SomeProperty = 1
            };
            var obj3 = new EquatableObject {
                SomeProperty = 1
            };

            var n         = new NUnitEqualityComparer();
            var tolerance = Tolerance.Exact;

            Assert.That(n.AreEqual(obj1, obj2, ref tolerance), Is.True);
            Assert.That(n.AreEqual(obj2, obj1, ref tolerance), Is.True);
            Assert.That(n.AreEqual(obj1, obj3, ref tolerance), Is.False);
            Assert.That(n.AreEqual(obj3, obj1, ref tolerance), Is.False);
            Assert.That(n.AreEqual(obj2, obj3, ref tolerance), Is.True);
            Assert.That(n.AreEqual(obj3, obj2, ref tolerance), Is.True);
        }
Exemplo n.º 17
0
 public void Setup()
 {
     tolerance = Tolerance.Default;
     comparer  = new NUnitEqualityComparer();
 }
Exemplo n.º 18
0
        /// <summary>
        /// Displays a single line showing the point in the expected and actual
        /// arrays at which the comparison failed. If the arrays have different
        /// structures or dimensions, both values are shown.
        /// </summary>
        /// <param name="writer">The MessageWriter on which to display</param>
        /// <param name="expected">The expected array</param>
        /// <param name="actual">The actual array</param>
        /// <param name="failurePoint">Index of the failure point in the underlying collections</param>
        /// <param name="indent">The indentation level for the message line</param>
        private void DisplayFailurePoint(MessageWriter writer, IEnumerable expected, IEnumerable actual, NUnitEqualityComparer.FailurePoint failurePoint, int indent)
        {
            Array expectedArray = expected as Array;
            Array actualArray = actual as Array;

            int expectedRank = expectedArray != null ? expectedArray.Rank : 1;
            int actualRank = actualArray != null ? actualArray.Rank : 1;

            bool useOneIndex = expectedRank == actualRank;

            if (expectedArray != null && actualArray != null)
                for (int r = 1; r < expectedRank && useOneIndex; r++)
                    if (expectedArray.GetLength(r) != actualArray.GetLength(r))
                        useOneIndex = false;

            int[] expectedIndices = MsgUtils.GetArrayIndicesFromCollectionIndex(expected, failurePoint.Position);
            if (useOneIndex)
            {
                writer.WriteMessageLine(indent, ValuesDiffer_1, MsgUtils.GetArrayIndicesAsString(expectedIndices));
            }
            else
            {
                int[] actualIndices = MsgUtils.GetArrayIndicesFromCollectionIndex(actual, failurePoint.Position);
                writer.WriteMessageLine(indent, ValuesDiffer_2,
                    MsgUtils.GetArrayIndicesAsString(expectedIndices), MsgUtils.GetArrayIndicesAsString(actualIndices));
            }
        }
 public void Setup()
 {
     tolerance = Tolerance.Empty;
     comparer  = new NUnitEqualityComparer();
 }
Exemplo n.º 20
0
 public TallyEqualityComparer(NUnitEqualityComparer comparer)
 {
     _comparer = comparer;
 }
Exemplo n.º 21
0
 public void TearDown()
 {
     GlobalSettings.DefaultFloatingPointTolerance = 0d;
     _comparer = new NUnitEqualityComparer();
 }