IsNumericType() public static méthode

Checks the type of the object, returning true if the object is a numeric type.
public static IsNumericType ( Object obj ) : bool
obj Object The object to check
Résultat bool
Exemple #1
0
        private bool ObjectsEqual(object expected, object actual)
        {
            if (expected == null && actual == null)
            {
                return(true);
            }

            if (expected == null || actual == null)
            {
                return(false);
            }

            Type expectedType = expected.GetType();
            Type actualType   = actual.GetType();

            if (expectedType.IsArray && actualType.IsArray && !compareAsCollection)
            {
                return(ArraysEqual((Array)expected, (Array)actual));
            }

            if (expected is ICollection && actual is ICollection)
            {
                return(CollectionsEqual((ICollection)expected, (ICollection)actual));
            }

            if (expected is Stream && actual is Stream)
            {
                return(StreamsEqual((Stream)expected, (Stream)actual));
            }

            if (compareWith != null)
            {
                return(compareWith.Compare(expected, actual) == 0);
            }

            if (Numerics.IsNumericType(expected) && Numerics.IsNumericType(actual))
            {
                return(Numerics.AreEqual(expected, actual, tolerance));
            }

            if (expected is string && actual is string)
            {
                return(string.Compare((string)expected, (string)actual, caseInsensitive) == 0);
            }

            foreach (Type type in constraintHelpers.Keys)
            {
                if (type.IsInstanceOfType(expected) && type.IsInstanceOfType(actual))
                {
                    Type       constraintType = (Type)constraintHelpers[type];
                    Constraint constraint     = (Constraint)NUnitLite.Reflect.Construct(constraintType, expected);
                    return(constraint.Matches(actual));
                }
            }

            return(expected.Equals(actual));
        }
Exemple #2
0
        private bool ObjectsEqual(object expected, object actual)
        {
            if (expected == null && actual == null)
            {
                return(true);
            }

            if (expected == null || actual == null)
            {
                return(false);
            }

            Type expectedType = expected.GetType();
            Type actualType   = actual.GetType();

            if (expectedType.IsArray && actualType.IsArray && !compareAsCollection)
            {
                return(ArraysEqual((Array)expected, (Array)actual));
            }

            if (expected is ICollection && actual is ICollection)
            {
                return(CollectionsEqual((ICollection)expected, (ICollection)actual));
            }

            if (expected is Stream && actual is Stream)
            {
                return(StreamsEqual((Stream)expected, (Stream)actual));
            }

            if (compareWith != null)
            {
                return(compareWith.Compare(expected, actual) == 0);
            }

            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 (expected is string && actual is string)
            {
                return(StringsEqual((string)expected, (string)actual));
            }

            if (expected is DateTime && actual is DateTime && tolerance is TimeSpan)
            {
                return(((DateTime)expected - (DateTime)actual).Duration() <= (TimeSpan)tolerance);
            }

            return(expected.Equals(actual));
        }
Exemple #3
0
 private void CheckLinearAndNumeric()
 {
     if (mode != ToleranceMode.Linear)
     {
         throw new InvalidOperationException((mode == ToleranceMode.None) ? "Tolerance amount must be specified before setting mode" : "Tried to use multiple tolerance modes at the same time");
     }
     if (!Numerics.IsNumericType(amount))
     {
         throw new InvalidOperationException("A numeric tolerance is required");
     }
 }
Exemple #4
0
        private Range PercentRange(object value)
        {
            if (!Numerics.IsNumericType(Amount) || !Numerics.IsNumericType(value))
            {
                throw new InvalidOperationException("Cannot create range for a non-numeric value");
            }

            var v      = Convert.ToDouble(value);
            var offset = v * Convert.ToDouble(Amount) / 100.0;

            return(new Range(v - offset, v + offset));
        }
Exemple #5
0
        /// <summary>
        /// Tests that the current Tolerance is linear with a
        /// numeric value, throwing an exception if it is not.
        /// </summary>
        private void CheckLinearAndNumeric()
        {
            if (Mode != ToleranceMode.Linear)
            {
                throw new InvalidOperationException(Mode == ToleranceMode.Unset
                    ? ModeMustFollowTolerance
                    : MultipleToleranceModes);
            }

            if (!Numerics.IsNumericType(Amount))
            {
                throw new InvalidOperationException(NumericToleranceRequired);
            }
        }
        /// <summary>
        /// Compares two objects
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int Compare(object x, object y)
        {
            if (x == null)
            {
                return(y == null ? 0 : -1);
            }
            else if (y == null)
            {
                return(+1);
            }

            if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
            {
                return(Numerics.Compare(x, y));
            }

            Type xType = x.GetType();
            Type yType = y.GetType();

            // If we use BindingFlags.ExactBinding it will prevent us finding CompareTo(object)
            // It however also prevents finding CompareTo(TBase) when called with TDerived
            // Nor will it find CompareTo(int) when called with a short.
            // We fallback to explicitly exclude CompareTo(object)
            bool IsIComparable(MethodInfo method) => method.GetParameters()[0].ParameterType == typeof(object);

            MethodInfo method = xType.GetMethod("CompareTo", new Type[] { yType });

            if (method != null && !IsIComparable(method))
            {
                return((int)method.Invoke(x, new object[] { y }));
            }

            method = yType.GetMethod("CompareTo", new Type[] { xType });
            if (method != null && !IsIComparable(method))
            {
                return(-(int)method.Invoke(y, new object[] { x }));
            }

            if (x is IComparable)
            {
                return(((IComparable)x).CompareTo(y));
            }

            if (y is IComparable)
            {
                return(-((IComparable)y).CompareTo(x));
            }

            throw new ArgumentException("Neither value implements IComparable or IComparable<T>");
        }
        /// <summary>
        /// Compares two objects
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int Compare(object x, object y)
        {
            if (x == null)
            {
                return(y == null ? 0 : -1);
            }
            else if (y == null)
            {
                return(+1);
            }

            if (x is char && y is char)
            {
                return((char)x == (char)y ? 0 : 1);
            }

            if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
            {
                return(Numerics.Compare(x, y));
            }

            if (x is IComparable)
            {
                return(((IComparable)x).CompareTo(y));
            }

            if (y is IComparable)
            {
                return(-((IComparable)y).CompareTo(x));
            }

            Type xType = x.GetType();
            Type yType = y.GetType();

            MethodInfo method = xType.GetMethod("CompareTo", new Type[] { yType });

            if (method != null)
            {
                return((int)method.Invoke(x, new object[] { y }));
            }

            method = yType.GetMethod("CompareTo", new Type[] { xType });
            if (method != null)
            {
                return(-(int)method.Invoke(y, new object[] { x }));
            }

            throw new ArgumentException("Neither value implements IComparable or IComparable<T>");
        }
Exemple #8
0
        public int Compare(object x, object y)
        {
            if (x == null)
            {
                return((y != null) ? (-1) : 0);
            }
            if (y == null)
            {
                return(1);
            }
            if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
            {
                return(Numerics.Compare(x, y));
            }
            if (x is IComparable)
            {
                return(((IComparable)x).CompareTo(y));
            }
            if (y is IComparable)
            {
                return(-((IComparable)y).CompareTo(x));
            }
            Type       type   = x.GetType();
            Type       type2  = y.GetType();
            MethodInfo method = type.GetMethod("CompareTo", new Type[1] {
                type2
            });

            if (method != null)
            {
                return((int)method.Invoke(x, new object[1] {
                    y
                }));
            }
            method = type2.GetMethod("CompareTo", new Type[1] {
                type
            });
            if (method != null)
            {
                return(-(int)method.Invoke(y, new object[1] {
                    x
                }));
            }
            throw new ArgumentException("Neither value implements IComparable or IComparable<T>");
        }
Exemple #9
0
        /// <summary>
        /// Compares two objects
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int Compare(object x, object y)
        {
            if (x == null)
            {
                return(y == null ? 0 : -1);
            }
            else if (y == null)
            {
                return(+1);
            }

            if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
            {
                return(Numerics.Compare(x, y));
            }

            Type xType = x.GetType();
            Type yType = y.GetType();
Exemple #10
0
 private static bool IsSpecialComparisonType(Type type)
 {
     if (type.IsGenericType)
     {
         return(type.FullName.StartsWith("System.Collections.Generic.KeyValuePair`2", StringComparison.Ordinal));
     }
     else if (Numerics.IsNumericType(type))
     {
         return(true);
     }
     else
     {
         return
             (type == typeof(string) ||
              type == typeof(char) ||
              type == typeof(DateTimeOffset) ||
              type == typeof(DictionaryEntry));
     }
 }
Exemple #11
0
        private bool ObjectsEqual(object expected, object actual)
        {
            if (expected == null && actual == null)
            {
                return(true);
            }

            if (expected == null || actual == null)
            {
                return(false);
            }

            if (expected is string && actual is string)
            {
                return(string.Compare((string)expected, (string)actual, caseInsensitive) == 0);
            }

            if (expected is DateTime && actual is DateTime && tolerance is TimeSpan)
            {
                return(((DateTime)expected - (DateTime)actual).Duration() <= (TimeSpan)tolerance);
            }

            if (expected is bool && actual is bool)
            {
                return(expected.Equals(actual));
            }

            if (FitToInt(expected))
            {
                return(Convert.ToInt32(expected) == Convert.ToInt32(actual));
            }

            if (expected is uint)
            {
                return(Convert.ToUInt32(expected) == Convert.ToUInt32(actual));
            }

            if (expected is long)
            {
                if (actual is ulong)
                {
                    return((ulong)(long)expected == (ulong)actual);
                }
                return((long)expected == Convert.ToInt64(actual));
            }

            if (expected is ulong)
            {
                if (actual is long)
                {
                    return((long)(ulong)expected == (long)actual);
                }
                return((ulong)expected == Convert.ToUInt64(actual));
            }

            if (Numerics.IsNumericType(expected) && Numerics.IsNumericType(actual))
            {
                return(Numerics.AreEqual(expected, actual, ref tolerance));
            }

            Type expectedType = expected.GetType();
            Type actualType   = actual.GetType();

            if (expectedType.IsArray && actualType.IsArray && !compareAsCollection)
            {
                return(ArraysEqual((Array)expected, (Array)actual));
            }

            if (expected is ICollection && actual is ICollection)
            {
                return(CollectionsEqual((ICollection)expected, (ICollection)actual));
            }

            //if (expected is Stream && actual is Stream)
            //    return StreamsEqual((Stream)expected, (Stream)actual);

            if (compareWith != null)
            {
                return(compareWith.Compare(expected, actual) == 0);
            }

            return(expected.Equals(actual));
        }
Exemple #12
0
        /// <summary>
        /// Compares two objects for equality within a tolerance.
        /// </summary>
        public bool AreEqual(object x, object y, ref Tolerance tolerance)
        {
            this.failurePoints = new List <FailurePoint>();

            if (x == null && y == null)
            {
                return(true);
            }

            if (x == null || y == null)
            {
                return(false);
            }

            if (object.ReferenceEquals(x, y))
            {
                return(true);
            }

            Type xType = x.GetType();
            Type yType = y.GetType();

            Type xGenericTypeDefinition = xType.IsGenericType ? xType.GetGenericTypeDefinition() : null;
            Type yGenericTypeDefinition = yType.IsGenericType ? yType.GetGenericTypeDefinition() : null;

            EqualityAdapter externalComparer = GetExternalComparer(x, y);

            if (externalComparer != null)
            {
                return(externalComparer.AreEqual(x, y));
            }

            if (xType.IsArray && yType.IsArray && !compareAsCollection)
            {
                return(ArraysEqual((Array)x, (Array)y, ref tolerance));
            }

            if (x is IDictionary && y is IDictionary)
            {
                return(DictionariesEqual((IDictionary)x, (IDictionary)y, ref tolerance));
            }

            // Issue #70 - EquivalentTo isn't compatible with IgnoreCase for dictionaries
            if (x is DictionaryEntry && y is DictionaryEntry)
            {
                return(DictionaryEntriesEqual((DictionaryEntry)x, (DictionaryEntry)y, ref tolerance));
            }

            // IDictionary<,> will eventually try to compare it's key value pairs when using CollectionTally
            if (xGenericTypeDefinition == typeof(KeyValuePair <,>) &&
                yGenericTypeDefinition == typeof(KeyValuePair <,>))
            {
                var    keyTolerance = Tolerance.Exact;
                object xKey         = xType.GetProperty("Key").GetValue(x, null);
                object yKey         = yType.GetProperty("Key").GetValue(y, null);
                object xValue       = xType.GetProperty("Value").GetValue(x, null);
                object yValue       = yType.GetProperty("Value").GetValue(y, null);

                return(AreEqual(xKey, yKey, ref keyTolerance) && AreEqual(xValue, yValue, ref tolerance));
            }

            //if (x is ICollection && y is ICollection)
            //    return CollectionsEqual((ICollection)x, (ICollection)y, ref tolerance);

            if (x is string && y is string)
            {
                return(StringsEqual((string)x, (string)y));
            }

            if (x is IEnumerable && y is IEnumerable)
            {
                return(EnumerablesEqual((IEnumerable)x, (IEnumerable)y, ref tolerance));
            }

            if (x is Stream && y is Stream)
            {
                return(StreamsEqual((Stream)x, (Stream)y));
            }

            if (x is char && y is char)
            {
                return(CharsEqual((char)x, (char)y));
            }

#if !PORTABLE
            if (x is DirectoryInfo && y is DirectoryInfo)
            {
                return(DirectoriesEqual((DirectoryInfo)x, (DirectoryInfo)y));
            }
#endif

            if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
            {
                return(Numerics.AreEqual(x, y, ref tolerance));
            }

#if !NETCF
            if (x is DateTimeOffset && y is DateTimeOffset)
            {
                bool result;

                DateTimeOffset xAsOffset = (DateTimeOffset)x;
                DateTimeOffset yAsOffset = (DateTimeOffset)y;

                if (tolerance != null && tolerance.Value is TimeSpan)
                {
                    TimeSpan amount = (TimeSpan)tolerance.Value;
                    result = (xAsOffset - yAsOffset).Duration() <= amount;
                }
                else
                {
                    result = xAsOffset == yAsOffset;
                }

                if (result && WithSameOffset)
                {
                    result = xAsOffset.Offset == yAsOffset.Offset;
                }

                return(result);
            }
#endif

            if (tolerance != null && tolerance.Value is TimeSpan)
            {
                TimeSpan amount = (TimeSpan)tolerance.Value;

                if (x is DateTime && y is DateTime)
                {
                    return(((DateTime)x - (DateTime)y).Duration() <= amount);
                }

                if (x is TimeSpan && y is TimeSpan)
                {
                    return(((TimeSpan)x - (TimeSpan)y).Duration() <= amount);
                }
            }

            if (FirstImplementsIEquatableOfSecond(xType, yType))
            {
                return(InvokeFirstIEquatableEqualsSecond(x, y));
            }
            else if (xType != yType && FirstImplementsIEquatableOfSecond(yType, xType))
            {
                return(InvokeFirstIEquatableEqualsSecond(y, x));
            }

            return(x.Equals(y));
        }
        public bool AreEqual(object x, object y, ref Tolerance tolerance)
        {
            failurePoints = new ObjectList();
            if (x == null && y == null)
            {
                return(true);
            }
            if (x == null || y == null)
            {
                return(false);
            }
            if (object.ReferenceEquals(x, y))
            {
                return(true);
            }
            Type            type             = x.GetType();
            Type            type2            = y.GetType();
            EqualityAdapter externalComparer = GetExternalComparer(x, y);

            if (externalComparer != null)
            {
                return(externalComparer.AreEqual(x, y));
            }
            if (type.IsArray && type2.IsArray && !compareAsCollection)
            {
                return(ArraysEqual((Array)x, (Array)y, ref tolerance));
            }
            if (x is IDictionary && y is IDictionary)
            {
                return(DictionariesEqual((IDictionary)x, (IDictionary)y, ref tolerance));
            }
            if (x is IEnumerable && y is IEnumerable && (!(x is string) || !(y is string)))
            {
                return(EnumerablesEqual((IEnumerable)x, (IEnumerable)y, ref tolerance));
            }
            if (x is string && y is string)
            {
                return(StringsEqual((string)x, (string)y));
            }
            if (x is Stream && y is Stream)
            {
                return(StreamsEqual((Stream)x, (Stream)y));
            }
            if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
            {
                return(Numerics.AreEqual(x, y, ref tolerance));
            }
            if (tolerance != null && tolerance.Value is TimeSpan)
            {
                TimeSpan timeSpan = (TimeSpan)tolerance.Value;
                if (x is DateTime && y is DateTime)
                {
                    return(((DateTime)x - (DateTime)y).Duration() <= timeSpan);
                }
                if (x is TimeSpan && y is TimeSpan)
                {
                    return(((TimeSpan)x - (TimeSpan)y).Duration() <= timeSpan);
                }
            }
            return(x.Equals(y));
        }
        /// <summary>
        /// Compares two objects for equality within a tolerance.
        /// </summary>
        public bool AreEqual(object x, object y, ref Tolerance tolerance)
        {
            this.failurePoints = new ArrayList();

            if (x == null && y == null)
            {
                return(true);
            }

            if (x == null || y == null)
            {
                return(false);
            }

            if (object.ReferenceEquals(x, y))
            {
                return(true);
            }

            Type xType = x.GetType();
            Type yType = y.GetType();

            EqualityAdapter externalComparer = GetExternalComparer(x, y);

            if (externalComparer != null)
            {
                return(externalComparer.AreEqual(x, y));
            }

            if (xType.IsArray && yType.IsArray && !compareAsCollection)
            {
                return(ArraysEqual((Array)x, (Array)y, ref tolerance));
            }

            if (x is IDictionary && y is IDictionary)
            {
                return(DictionariesEqual((IDictionary)x, (IDictionary)y, ref tolerance));
            }

            //if (x is ICollection && y is ICollection)
            //    return CollectionsEqual((ICollection)x, (ICollection)y, ref tolerance);

            if (x is IEnumerable && y is IEnumerable && !(x is string && y is string))
            {
                return(EnumerablesEqual((IEnumerable)x, (IEnumerable)y, ref tolerance));
            }

            if (x is string && y is string)
            {
                return(StringsEqual((string)x, (string)y));
            }

            if (x is Stream && y is Stream)
            {
                return(StreamsEqual((Stream)x, (Stream)y));
            }

            if (x is DirectoryInfo && y is DirectoryInfo)
            {
                return(DirectoriesEqual((DirectoryInfo)x, (DirectoryInfo)y));
            }

            if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
            {
                return(Numerics.AreEqual(x, y, ref tolerance));
            }

            if (tolerance != null && tolerance.Value is TimeSpan)
            {
                TimeSpan amount = (TimeSpan)tolerance.Value;

                if (x is DateTime && y is DateTime)
                {
                    return(((DateTime)x - (DateTime)y).Duration() <= amount);
                }

                if (x is TimeSpan && y is TimeSpan)
                {
                    return(((TimeSpan)x - (TimeSpan)y).Duration() <= amount);
                }
            }

#if CLR_2_0 || CLR_4_0
            if (FirstImplementsIEquatableOfSecond(xType, yType))
            {
                return(InvokeFirstIEquatableEqualsSecond(x, y));
            }
            else if (FirstImplementsIEquatableOfSecond(yType, xType))
            {
                return(InvokeFirstIEquatableEqualsSecond(y, x));
            }
#endif

            return(x.Equals(y));
        }
        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 !PORTABLE
            if (expected is DirectoryInfo && actual is DirectoryInfo)
            {
                return(DirectoriesEqual((DirectoryInfo)expected, (DirectoryInfo)actual));
            }
#endif

            if (Numerics.IsNumericType(expected) && Numerics.IsNumericType(actual))
            {
                return(Numerics.AreEqual(expected, actual, ref tolerance));
            }

            if (tolerance != null && tolerance.Value is TimeSpan)
            {
                TimeSpan 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) && !NETCF
            if (FirstImplementsIEquatableOfSecond(xType, yType))
            {
                return(InvokeFirstIEquatableEqualsSecond(expected, actual));
            }
            else if (FirstImplementsIEquatableOfSecond(yType, xType))
            {
                return(InvokeFirstIEquatableEqualsSecond(actual, expected));
            }
#endif

            return(expected.Equals(actual));
        }
Exemple #16
0
        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));
            }

            // Issue #70 - EquivalentTo isn't compatible with IgnoreCase for dictionaries
            if (expected is DictionaryEntry && actual is DictionaryEntry)
            {
                return(DictionaryEntriesEqual((DictionaryEntry)expected, (DictionaryEntry)actual, ref tolerance));
            }

#if CLR_2_0 || CLR_4_0
            // IDictionary<,> will eventually try to compare it's key value pairs when using CollectionTally
            if (xType.IsGenericType && xType.GetGenericTypeDefinition() == typeof(KeyValuePair <,>) &&
                yType.IsGenericType && yType.GetGenericTypeDefinition() == typeof(KeyValuePair <,>))
            {
                Tolerance keyTolerance = new Tolerance(0);
                object    xKey         = xType.GetProperty("Key").GetValue(expected, null);
                object    yKey         = yType.GetProperty("Key").GetValue(actual, null);
                object    xValue       = xType.GetProperty("Value").GetValue(expected, null);
                object    yValue       = yType.GetProperty("Value").GetValue(actual, null);

                return(AreEqual(xKey, yKey, ref keyTolerance) && AreEqual(xValue, yValue, ref tolerance));
            }
#endif

            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 char && actual is char)
            {
                return(CharsEqual((char)expected, (char)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)
            {
                TimeSpan amount = (TimeSpan)tolerance.Value;

                if (expected is DateTime && actual is DateTime)
                {
                    return(((DateTime)expected - (DateTime)actual).Duration() <= amount);
                }

#if CLR_2_0 || CLR_4_0
                if (expected is DateTimeOffset && actual is DateTimeOffset)
                {
                    return(((DateTimeOffset)expected - (DateTimeOffset)actual).Duration() <= amount);
                }
#endif

                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));
        }
Exemple #17
0
        /// <summary>
        /// Compares two objects for equality.
        /// </summary>
        public bool ObjectsEqual(object x, object y)
        {
            this.failurePoints = new List <object>();

            if (x == null && y == null)
            {
                return(true);
            }

            if (x == null || y == null)
            {
                return(false);
            }

            Type xType = x.GetType();
            Type yType = y.GetType();

            if (xType.IsArray && yType.IsArray && !compareAsCollection)
            {
                return(ArraysEqual((Array)x, (Array)y));
            }

            if (x is ICollection && y is ICollection)
            {
                return(CollectionsEqual((ICollection)x, (ICollection)y));
            }

            if (x is IEnumerable && y is IEnumerable && !(x is string && y is string))
            {
                return(EnumerablesEqual((IEnumerable)x, (IEnumerable)y));
            }

            if (externalComparer != null)
            {
                return(externalComparer.ObjectsEqual(x, y));
            }

            if (x is string && y is string)
            {
                return(StringsEqual((string)x, (string)y));
            }

            if (x is Stream && y is Stream)
            {
                return(StreamsEqual((Stream)x, (Stream)y));
            }

            if (x is DirectoryInfo && y is DirectoryInfo)
            {
                return(DirectoriesEqual((DirectoryInfo)x, (DirectoryInfo)y));
            }

            if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
            {
                return(Numerics.AreEqual(x, y, ref tolerance));
            }

            if (tolerance != null && tolerance.Value is TimeSpan)
            {
                TimeSpan amount = (TimeSpan)tolerance.Value;

                if (x is DateTime && y is DateTime)
                {
                    return(((DateTime)x - (DateTime)y).Duration() <= amount);
                }

                if (x is TimeSpan && y is TimeSpan)
                {
                    return(((TimeSpan)x - (TimeSpan)y).Duration() <= amount);
                }
            }

            return(x.Equals(y));
        }