예제 #1
0
 public override bool Matches(object actual)
 {
     base.actual = actual;
     if (from == null || to == null || actual == null)
     {
         throw new ArgumentException("Cannot compare using a null reference", "actual");
     }
     return(comparer.Compare(from, actual) <= 0 && comparer.Compare(to, actual) >= 0);
 }
예제 #2
0
        /// <summary>
        /// Test whether the constraint is satisfied by a given value
        /// </summary>
        /// <param name="actual">The value to be tested</param>
        /// <returns>True for success, false for failure</returns>
        public override ConstraintResult ApplyTo <TActual>(TActual actual)
        {
            if (from == null || to == null || actual == null)
            {
                throw new ArgumentException("Cannot compare using a null reference", "actual");
            }

            bool isInsideRange = comparer.Compare(from, actual) <= 0 && comparer.Compare(to, actual) >= 0;

            return(new ConstraintResult(this, actual, isInsideRange));
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RangeConstraint"/> class.
        /// </summary>
        /// <remarks>from must be less than or equal to true</remarks>
        /// <param name="from">Inclusive beginning of the range. Must be less than or equal to to.</param>
        /// <param name="to">Inclusive end of the range. Must be greater than or equal to from.</param>
        public RangeConstraint(IComparable from, IComparable to) : base(from, to)
        {
            // Issue #21 - https://github.com/nunit/nunit-framework/issues/21
            // from must be less than or equal to to
            if (comparer.Compare(from, to) > 0)
            {
                throw new ArgumentException("from must be less than to");
            }

            this.from = from;
            this.to   = to;
        }
예제 #4
0
        public override bool Matches(object actual)
        {
            base.actual = actual;
            if (expected == null)
            {
                throw new ArgumentException("Cannot compare using a null reference", "expected");
            }
            if (actual == null)
            {
                throw new ArgumentException("Cannot compare to null reference", "actual");
            }
            int num = comparer.Compare(expected, actual);

            return((num < 0 && greaterComparisonResult) || (num == 0 && equalComparisonResult) || (num > 0 && lessComparisonResult));
        }
        /// <summary>
        /// Test whether the constraint is satisfied by a given value
        /// </summary>
        /// <param name="actual">The value to be tested</param>
        /// <returns>True for success, false for failure</returns>
        public override bool Matches(object actual)
        {
            this.actual = actual;

            if (expected == null)
            {
                throw new ArgumentException("Cannot compare using a null reference", "expected");
            }

            if (actual == null)
            {
                throw new ArgumentException("Cannot compare to null reference", "actual");
            }

            int icomp = comparer.Compare(expected, actual);

            return(icomp < 0 && gtOK || icomp == 0 && eqOK || icomp > 0 && ltOK);
        }
예제 #6
0
        /// <summary>
        /// Test whether the constraint is satisfied by a given value
        /// </summary>
        /// <param name="actual">The value to be tested</param>
        /// <returns>True for success, false for failure</returns>
        public override ConstraintResult ApplyTo <TActual>(TActual actual)
        {
            if (expected == null)
            {
                throw new ArgumentException("Cannot compare using a null reference", "expected");
            }

            if (actual == null)
            {
                throw new ArgumentException("Cannot compare to null reference", "actual");
            }

            int icomp = comparer.Compare(expected, actual);

            bool hasSucceeded = icomp < 0 && greaterComparisonResult || icomp == 0 && equalComparisonResult || icomp > 0 && lessComparisonResult;

            return(new ConstraintResult(this, actual, hasSucceeded));
        }
예제 #7
0
        /// <summary>
        /// Test whether the collection is ordered
        /// </summary>
        /// <param name="actual"></param>
        /// <returns></returns>
        protected override bool doMatch(IEnumerable actual)
        {
            object previous = null;
            int    index    = 0;

            foreach (object obj in actual)
            {
                object objToCompare = obj;
                if (obj == null)
                {
                    throw new ArgumentNullException("actual", "Null value at index " + index.ToString());
                }

                if (this.propertyName != null)
                {
                    PropertyInfo prop = obj.GetType().GetProperty(propertyName);
                    objToCompare = prop.GetValue(obj, null);
                    if (objToCompare == null)
                    {
                        throw new ArgumentNullException("actual", "Null property value at index " + index.ToString());
                    }
                }

                if (previous != null)
                {
                    //int comparisonResult = comparer.Compare(al[i], al[i + 1]);
                    int comparisonResult = comparer.Compare(previous, objToCompare);

                    if (descending && comparisonResult < 0)
                    {
                        return(false);
                    }
                    if (!descending && comparisonResult > 0)
                    {
                        return(false);
                    }
                }

                previous = objToCompare;
                index++;
            }

            return(true);
        }
예제 #8
0
        protected override bool doMatch(IEnumerable actual)
        {
            object obj = null;
            int    num = 0;

            foreach (object item in actual)
            {
                object obj2 = item;
                if (item == null)
                {
                    throw new ArgumentNullException("actual", "Null value at index " + num);
                }
                if (propertyName != null)
                {
                    PropertyInfo property = item.GetType().GetProperty(propertyName);
                    obj2 = property.GetValue(item, null);
                    if (obj2 == null)
                    {
                        throw new ArgumentNullException("actual", "Null property value at index " + num);
                    }
                }
                if (obj != null)
                {
                    int num2 = comparer.Compare(obj, obj2);
                    if (descending && num2 < 0)
                    {
                        return(false);
                    }
                    if (!descending && num2 > 0)
                    {
                        return(false);
                    }
                }
                obj = obj2;
                num++;
            }
            return(true);
        }
예제 #9
0
 public override bool ObjectsEqual(object x, object y)
 {
     return(comparer.Compare(x, y) == 0);
 }
예제 #10
0
 /// <summary>
 /// Perform the comparison
 /// </summary>
 protected override bool PerformComparison(ComparisonAdapter comparer, object actual, object expected, Tolerance tolerance)
 {
     return(comparer.Compare(actual, tolerance.ApplyToValue(expected).LowerBound) > 0);
 }
 public static void CanCompareWithNull(ComparisonAdapter adapter)
 {
     Assert.That(adapter.Compare(null, "a"), Is.LessThan(0));
     Assert.That(adapter.Compare("a", null), Is.GreaterThan(0));
     Assert.That(adapter.Compare(null, null), Is.Zero);
 }