AreEqual() 개인적인 정적인 메소드

private static AreEqual ( decimal expected, decimal actual, NUnit.Framework.Constraints.Tolerance tolerance ) : bool
expected decimal
actual decimal
tolerance NUnit.Framework.Constraints.Tolerance
리턴 bool
예제 #1
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.GetTypeInfo().IsGenericType ? xType.GetGenericTypeDefinition() : null;
            Type yGenericTypeDefinition = yType.GetTypeInfo().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 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 (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);
            }

            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);
                }
            }

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

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

            return(x.Equals(y));
        }
예제 #2
0
 public void CanMatchDecimalWithPercentage()
 {
     Assert.IsTrue(Numerics.AreEqual(10000m, 9500m, ref tenPercent));
     Assert.IsTrue(Numerics.AreEqual(10000m, 10000m, ref tenPercent));
     Assert.IsTrue(Numerics.AreEqual(10000m, 10500m, ref tenPercent));
 }
예제 #3
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));
            }

            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)
            {
                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
            if (FirstImplementsIEquatableOfSecond(xType, yType))
            {
                return(InvokeFirstIEquatableEqualsSecond(expected, actual));
            }
            else if (FirstImplementsIEquatableOfSecond(yType, xType))
            {
                return(InvokeFirstIEquatableEqualsSecond(actual, expected));
            }
#endif

            return(expected.Equals(actual));
        }
예제 #4
0
 public void CanMatchDecimalWithoutToleranceMode()
 {
     Assert.IsTrue(Numerics.AreEqual(123m, 123m, ref zeroTolerance));
 }
예제 #5
0
 public void CanMatchIntegralsWithPercentage(object value)
 {
     Assert.IsTrue(Numerics.AreEqual(10000, value, ref tenPercent));
 }
예제 #6
0
 public void FailsOnDecimalAbovePercentage()
 {
     Assert.Throws <AssertionException>(() => Assert.IsTrue(Numerics.AreEqual(10000m, 11500m, ref tenPercent)));
 }
예제 #7
0
 public void CanMatchWithoutToleranceMode(object value)
 {
     Assert.IsTrue(Numerics.AreEqual(value, value, ref zeroTolerance));
 }
예제 #8
0
        /// <summary>
        /// Compares two objects for equality.
        /// </summary>
        public bool ObjectsEqual(object x, object y)
        {
            this.failurePoints = new ArrayList();

            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));
        }
예제 #9
0
 public void FailsOnIntegralsOutsideOfPercentage(object value)
 {
     Assert.Throws <AssertionException>(() => Assert.IsTrue(Numerics.AreEqual(10000, value, ref tenPercent)));
 }
예제 #10
0
        /// <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 (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));
            }

            // 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));
        }
예제 #12
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));
        }
예제 #13
0
 public void FailsOnIntegralsOutsideOfPercentage(object value)
 {
     Assert.IsTrue(Numerics.AreEqual(10000, value, ref tenPercent));
 }
예제 #14
0
 public void FailsOnDecimalAbovePercentage()
 {
     Assert.IsTrue(Numerics.AreEqual(10000m, 11500m, ref tenPercent));
 }