private bool ObjectsEqual(object expected, object actual, ref Tolerance tolerance) { if (expected == null && actual == null) { return(true); } if (expected == null || actual == null) { return(false); } if (object.ReferenceEquals(expected, actual)) { return(true); } Type xType = expected.GetType(); Type yType = actual.GetType(); EqualityAdapter externalComparer = GetExternalComparer(expected, actual); if (externalComparer != null) { return(externalComparer.AreEqual(expected, actual)); } if (xType.IsArray && yType.IsArray && !compareAsCollection) { return(ArraysEqual((Array)expected, (Array)actual, ref tolerance)); } if (expected is IDictionary && actual is IDictionary) { return(DictionariesEqual((IDictionary)expected, (IDictionary)actual, ref tolerance)); } if (expected is IEnumerable && actual is IEnumerable && !(expected is string && actual is string)) { return(EnumerablesEqual((IEnumerable)expected, (IEnumerable)actual, ref tolerance)); } if (expected is string && actual is string) { return(StringsEqual((string)expected, (string)actual)); } if (expected is Stream && actual is Stream) { return(StreamsEqual((Stream)expected, (Stream)actual)); } if (expected is DirectoryInfo && actual is DirectoryInfo) { return(DirectoriesEqual((DirectoryInfo)expected, (DirectoryInfo)actual)); } if (Numerics.IsNumericType(expected) && Numerics.IsNumericType(actual)) { return(Numerics.AreEqual(expected, actual, ref tolerance)); } if (tolerance != null && tolerance.Value is TimeSpan) { var amount = (TimeSpan)tolerance.Value; if (expected is DateTime && actual is DateTime) { return(((DateTime)expected - (DateTime)actual).Duration() <= amount); } if (expected is TimeSpan && actual is TimeSpan) { return(((TimeSpan)expected - (TimeSpan)actual).Duration() <= amount); } } #if CLR_2_0 || CLR_4_0 if (FirstImplementsIEquatableOfSecond(xType, yType)) { return(InvokeFirstIEquatableEqualsSecond(expected, actual)); } else if (FirstImplementsIEquatableOfSecond(yType, xType)) { return(InvokeFirstIEquatableEqualsSecond(actual, expected)); } #endif return(expected.Equals(actual)); }
/// <summary> /// Flag the constraint to use the supplied EqualityAdapter. /// NOTE: For internal use only. /// </summary> /// <param name="adapter">The EqualityAdapter to use.</param> /// <returns>Self.</returns> internal CollectionItemsEqualConstraint Using(EqualityAdapter adapter) { comparer.ExternalComparers.Add(adapter); return(this); }
/// <summary> /// Flag the constraint to use the supplied Comparison object. /// </summary> /// <param name="comparer">The IComparer object to use.</param> /// <returns>Self.</returns> public CollectionItemsEqualConstraint Using <T>(Comparison <T> comparer) { return(Using(EqualityAdapter.For(comparer))); }
/// <summary> /// Flag the constraint to use the supplied IEqualityComparer object. /// </summary> /// <param name="comparer">The IComparer object to use.</param> /// <returns>Self.</returns> public CollectionItemsEqualConstraint Using(IEqualityComparer comparer) { return(Using(EqualityAdapter.For(comparer))); }
private ContainsConstraint AddAdapter(EqualityAdapter adapter) { equalityAdapters.Add(adapter); return(this); }
/// <summary> /// Flag the constraint to use the supplied IEqualityComparer object. /// </summary> /// <param name="comparer">The IComparer object to use.</param> /// <returns>Self.</returns> public ContainsConstraint Using <T>(IEqualityComparer <T> comparer) { return(AddAdapter(EqualityAdapter.For(comparer))); }
/// <summary> /// Flag the constraint to use the supplied IComparer object. /// </summary> /// <param name="comparer">The IComparer object to use.</param> /// <returns>Self.</returns> public ContainsConstraint Using(IComparer comparer) { return(AddAdapter(EqualityAdapter.For(comparer))); }
/// <summary> /// Flag the constraint to use the supplied IEqualityComparer object. /// </summary> /// <param name="comparer">The IComparer object to use.</param> /// <returns>Self.</returns> public EqualConstraint Using <T>(IEqualityComparer <T> comparer) { this.comparer.ExternalComparers.Add(EqualityAdapter.For(comparer)); return(this); }