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); }
/// <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); }
public void CanCompareArrayContainingSelfToSelf() { object[] array = new object[1]; array[0] = array; NUnitEqualityComparer comparer = new NUnitEqualityComparer(); Assert.True(comparer.ObjectsEqual(array, array)); }
public CollectionTally(NUnitEqualityComparer comparer, IEnumerable c) { this.comparer = comparer; foreach (object item in c) { list.Add(item); } }
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)); }
/// <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); } }
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)); }
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)); }
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)); }
/// <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++); } }
/// <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); } }
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)); }
/// <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; } }
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); }
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); }
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); }
public void Setup() { tolerance = Tolerance.Default; comparer = new NUnitEqualityComparer(); }
/// <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(); }
public TallyEqualityComparer(NUnitEqualityComparer comparer) { _comparer = comparer; }
public void TearDown() { GlobalSettings.DefaultFloatingPointTolerance = 0d; _comparer = new NUnitEqualityComparer(); }