Esempio n. 1
0
        public void TestToleranceDefault()
        {
            GlobalSettings.DefaultFloatingPointTolerance = 0.5d;
            var defaultTolerance = Tolerance.Default;

            Assert.IsTrue(defaultTolerance.IsUnsetOrDefault);
            Assert.IsTrue(_comparer.AreEqual(2.0d, 2.1d, ref defaultTolerance));
        }
        public static bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state, NUnitEqualityComparer equalityComparer)
        {
            // Issue #70 - EquivalentTo isn't compatible with IgnoreCase for dictionaries
            if (!(x is DictionaryEntry xDictionaryEntry) || !(y is DictionaryEntry yDictionaryEntry))
            {
                return(null);
            }

            var keyTolerance = Tolerance.Exact;

            return(equalityComparer.AreEqual(xDictionaryEntry.Key, yDictionaryEntry.Key, ref keyTolerance, state.PushComparison(x, y)) &&
                   equalityComparer.AreEqual(xDictionaryEntry.Value, yDictionaryEntry.Value, ref tolerance, state.PushComparison(x, y)));
        }
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            if (!(x is IDictionary) || !(y is IDictionary))
            {
                return(null);
            }

            IDictionary xDictionary = (IDictionary)x;
            IDictionary yDictionary = (IDictionary)y;

            if (xDictionary.Count != yDictionary.Count)
            {
                return(false);
            }

            CollectionTally tally = new CollectionTally(_equalityComparer, xDictionary.Keys);

            tally.TryRemove(yDictionary.Keys);
            if ((tally.Result.MissingItems.Count > 0) || (tally.Result.ExtraItems.Count > 0))
            {
                return(false);
            }

            foreach (object key in xDictionary.Keys)
            {
                if (!_equalityComparer.AreEqual(xDictionary[key], yDictionary[key], ref tolerance, state.PushComparison(x, y)))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 4
0
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            Type xType = x.GetType();
            Type yType = y.GetType();

            if (!IsCorrectType(xType) || !IsCorrectType(yType))
            {
                return(null);
            }

            int numberOfGenericArgs = xType.GetGenericArguments().Length;

            if (numberOfGenericArgs != yType.GetGenericArguments().Length)
            {
                return(false);
            }

            for (int i = 0; i < numberOfGenericArgs; i++)
            {
                string propertyName = i < 7 ? "Item" + (i + 1) : "Rest";
                object xItem        = GetValue(xType, propertyName, x);
                object yItem        = GetValue(yType, propertyName, y);

                bool comparison = _equalityComparer.AreEqual(xItem, yItem, ref tolerance, state.PushComparison(x, y));
                if (!comparison)
                {
                    return(false);
                }
            }

            return(true);
        }
        public bool?Equal(object x, object y, ref Tolerance tolerance, bool topLevelComparison = true)
        {
            // Issue #70 - EquivalentTo isn't compatible with IgnoreCase for dictionaries
            if (!(x is DictionaryEntry) || !(y is DictionaryEntry))
            {
                return(null);
            }

            DictionaryEntry xDictionaryEntry = (DictionaryEntry)x;
            DictionaryEntry yDictionaryEntry = (DictionaryEntry)y;

            var keyTolerance = Tolerance.Exact;

            return(_equalityComparer.AreEqual(xDictionaryEntry.Key, yDictionaryEntry.Key, ref keyTolerance, false) &&
                   _equalityComparer.AreEqual(xDictionaryEntry.Value, yDictionaryEntry.Value, ref tolerance, false));
        }
Esempio n. 6
0
        public bool?Equal(object x, object y, ref Tolerance tolerance)
        {
            Type xType = x.GetType();
            Type yType = y.GetType();

            string xTypeName = GetTypeNameWithoutGenerics(xType.FullName);
            string yTypeName = GetTypeNameWithoutGenerics(yType.FullName);

            if (xTypeName != NameOfType || yTypeName != NameOfType)
            {
                return(null);
            }

            int numberOfGenericArgs = xType.GetGenericArguments().Length;

            if (numberOfGenericArgs != yType.GetGenericArguments().Length)
            {
                return(false);
            }

            for (int i = 0; i < numberOfGenericArgs; i++)
            {
                string propertyName = i < 7 ? "Item" + (i + 1) : "Rest";
                object xItem        = GetValue(xType, propertyName, x);
                object yItem        = GetValue(yType, propertyName, y);

                bool comparison = _equalityComparer.AreEqual(xItem, yItem, ref tolerance);
                if (!comparison)
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 7
0
        public bool?Equal(object x, object y, ref Tolerance tolerance)
        {
            if (!(x is IDictionary) || !(y is IDictionary))
            {
                return(null);
            }

            IDictionary xDictionary = (IDictionary)x;
            IDictionary yDictionary = (IDictionary)y;

            if (xDictionary.Count != yDictionary.Count)
            {
                return(false);
            }

            CollectionTally tally = new CollectionTally(_equalityComparer, xDictionary.Keys);

            if (!tally.TryRemove(yDictionary.Keys) || tally.Count > 0)
            {
                return(false);
            }

            foreach (object key in xDictionary.Keys)
            {
                if (!_equalityComparer.AreEqual(xDictionary[key], yDictionary[key], ref tolerance))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio 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));
        }
Esempio n. 9
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));
        }
Esempio n. 10
0
        public void ComparerAreEqual()
        {
            var       comparer  = new NUnitEqualityComparer();
            Tolerance tolerance = new Tolerance(0);

            for (int i = 0; i < 1000000; i++)
            {
                var x = 10;
                var y = 10;
                comparer.AreEqual(x, y, ref tolerance);
            }
        }
Esempio n. 11
0
        public bool?Equal(object x, object y, ref Tolerance tolerance, bool topLevelComparison = true)
        {
            // IDictionary<,> will eventually try to compare its key value pairs when using CollectionTally
            Type xType = x.GetType();
            Type yType = y.GetType();

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

            if (xGenericTypeDefinition != typeof(KeyValuePair <,>) ||
                yGenericTypeDefinition != typeof(KeyValuePair <,>))
            {
                return(null);
            }

            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(_equalityComparer.AreEqual(xKey, yKey, ref keyTolerance, false) &&
                   _equalityComparer.AreEqual(xValue, yValue, ref tolerance, false));
        }
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            if (!(x is IEnumerable xIEnumerable) || !(y is IEnumerable yIEnumerable))
            {
                return(null);
            }

            var expectedEnum = xIEnumerable.GetEnumerator();

            using (expectedEnum as IDisposable)
            {
                var actualEnum = yIEnumerable.GetEnumerator();
                using (actualEnum as IDisposable)
                {
                    for (int count = 0; ; count++)
                    {
                        bool expectedHasData = expectedEnum.MoveNext();
                        bool actualHasData   = actualEnum.MoveNext();

                        if (!expectedHasData && !actualHasData)
                        {
                            return(true);
                        }

                        if (expectedHasData != actualHasData ||
                            !_equalityComparer.AreEqual(expectedEnum.Current, actualEnum.Current, ref tolerance, state.PushComparison(x, y)))
                        {
                            NUnitEqualityComparer.FailurePoint fp = new NUnitEqualityComparer.FailurePoint();
                            fp.Position        = count;
                            fp.ExpectedHasData = expectedHasData;
                            if (expectedHasData)
                            {
                                fp.ExpectedValue = expectedEnum.Current;
                            }
                            fp.ActualHasData = actualHasData;
                            if (actualHasData)
                            {
                                fp.ActualValue = actualEnum.Current;
                            }
                            _equalityComparer.FailurePoints.Insert(0, fp);
                            return(false);
                        }
                    }
                }
            }
        }
    public override bool Matches(object actual)
    {
        // set the base class value so it appears in the error message
        this.actual = actual;
        Tolerance tolerance = Tolerance.Empty;

        // Loop through the expected values and return true on first match
        foreach (object value in expected)
        {
            if (comparer.AreEqual(value, actual, ref tolerance))
            {
                return(true);
            }
        }
        // No matches, return false
        return(false);
    }
Esempio n. 14
0
            public override bool Equals(object other)
            {
                var left  = obj;
                var right = other;

                if (left is string == false && right is string)
                {
                    right = Alvis.TestHelper.SmartConvert(right, left.GetType());
                }

                if (comparison.HasFlag(ObjectComparison.Unordered))
                {
                    left  = Sorted(left);
                    right = Sorted(right);
                }

                var tolerance = Tolerance.Default;

                return(comparer.AreEqual(left, right, ref tolerance));
            }
Esempio n. 15
0
        public bool?Equal(object x, object y, ref Tolerance tolerance, bool topLevelComparison = true)
        {
            if (!(x is IEnumerable) || !(y is IEnumerable))
            {
                return(null);
            }

            IEnumerable xIEnumerable = (IEnumerable)x;
            IEnumerable yIEnumerable = (IEnumerable)y;

            IEnumerator expectedEnum = null;
            IEnumerator actualEnum   = null;

            try
            {
                expectedEnum = xIEnumerable.GetEnumerator();
                actualEnum   = yIEnumerable.GetEnumerator();

                int count;
                for (count = 0; ; count++)
                {
                    bool expectedHasData = expectedEnum.MoveNext();
                    bool actualHasData   = actualEnum.MoveNext();

                    if (!expectedHasData && !actualHasData)
                    {
                        return(true);
                    }

                    if (expectedHasData != actualHasData ||
                        !_equalityComparer.AreEqual(expectedEnum.Current, actualEnum.Current, ref tolerance, topLevelComparison: false))
                    {
                        NUnitEqualityComparer.FailurePoint fp = new NUnitEqualityComparer.FailurePoint();
                        fp.Position        = count;
                        fp.ExpectedHasData = expectedHasData;
                        if (expectedHasData)
                        {
                            fp.ExpectedValue = expectedEnum.Current;
                        }
                        fp.ActualHasData = actualHasData;
                        if (actualHasData)
                        {
                            fp.ActualValue = actualEnum.Current;
                        }
                        _equalityComparer.FailurePoints.Insert(0, fp);
                        return(false);
                    }
                }
            }
            finally
            {
                var expectedDisposable = expectedEnum as IDisposable;
                if (expectedDisposable != null)
                {
                    expectedDisposable.Dispose();
                }

                var actualDisposable = actualEnum as IDisposable;
                if (actualDisposable != null)
                {
                    actualDisposable.Dispose();
                }
            }
        }
Esempio n. 16
0
 public override ConstraintResult ApplyTo <TActual>(TActual actual)
 {
     return(new EqualAstConstraintResult(this, actual, _comparer.AreEqual(_expected, actual, ref _tolerance)));
 }
 public new bool Equals(object x, object y)
 {
     return(_comparer.AreEqual(x, y, ref _tolerance));
 }