Example #1
0
 public override void ValidateActualValue(object actual)
 {
     Guard.ArgumentNotNull(actual, nameof(actual));
     if (!(actual is TExpected))
     {
         Guard.ArgumentValid(
             Numerics.IsNumericType(actual) && Numerics.IsNumericType(typeof(TExpected)),
             "Range comparisons are only supported for numeric types.",
             nameof(actual));
     }
 }
 public override void ValidateActualValue(object actual)
 {
     Guard.ArgumentNotNull(actual, nameof(actual));
     if (!(actual is TExpected))
     {
         Guard.ArgumentValid(
             Numerics.IsNumericType(actual) && Numerics.IsNumericType(typeof(TExpected)),
             "Comparisons between objects of different types require both to be numeric types.",
             nameof(actual));
     }
 }
Example #3
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.Default
                    ? ModeMustFollowTolerance
                    : MultipleToleranceModes);
            }

            if (!Numerics.IsNumericType(Amount))
            {
                throw new InvalidOperationException(NumericToleranceRequired);
            }
        }
Example #4
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));
            }

            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>");
        }
Example #5
0
 public void FailsOnDecimalAbovePercentage()
 {
     Assert.Throws <AssertionException>(() => Assert.IsTrue(Numerics.AreEqual(10000m, 11500m, ref _tenPercent)));
 }
Example #6
0
 public void FailsOnIntegralsOutsideOfPercentage(object value)
 {
     Assert.Throws <AssertionException>(() => Assert.IsTrue(Numerics.AreEqual(10000, value, ref _tenPercent)));
 }
Example #7
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));
 }
Example #8
0
 public void CanMatchIntegralsWithPercentage(object value)
 {
     Assert.IsTrue(Numerics.AreEqual(10000, value, ref _tenPercent));
 }
Example #9
0
 public void CanMatchDecimalWithoutToleranceMode()
 {
     Assert.IsTrue(Numerics.AreEqual(123m, 123m, ref _zeroTolerance));
 }
Example #10
0
 public void CanMatchWithoutToleranceMode <T>(T value)
 {
     Assert.IsTrue(Numerics.AreEqual(value, value, ref _zeroTolerance));
 }