1 // ***********************************************************************
2 // Copyright (c) 2008 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
30 using System.Collections;
31 using System.Collections.Generic;
33 namespace NUnit.Framework.Constraints
36 /// RangeConstraint tests whether two _values are within a
39 public class RangeConstraint : Constraint
41 private readonly IComparable from;
42 private readonly IComparable to;
44 private ComparisonAdapter comparer = ComparisonAdapter.Default;
47 /// Initializes a new instance of the <see cref="RangeConstraint"/> class.
49 /// <remarks>from must be less than or equal to true</remarks>
50 /// <param name="from">Inclusive beginning of the range. Must be less than or equal to to.</param>
51 /// <param name="to">Inclusive end of the range. Must be greater than or equal to from.</param>
52 public RangeConstraint(IComparable from, IComparable to) : base( from, to )
54 // Issue #21 - https://github.com/nunit/nunit-framework/issues/21
55 // from must be less than or equal to to
56 if (comparer.Compare(from, to) > 0)
57 throw new ArgumentException( "from must be less than to" );
64 /// Gets text describing a constraint
66 public override string Description
68 get { return string.Format("in range ({0},{1})", from, to); }
72 /// Test whether the constraint is satisfied by a given value
74 /// <param name="actual">The value to be tested</param>
75 /// <returns>True for success, false for failure</returns>
76 public override ConstraintResult ApplyTo<TActual>(TActual actual)
78 if ( from == null || to == null || actual == null)
79 throw new ArgumentException( "Cannot compare using a null reference", "actual" );
81 bool isInsideRange = comparer.Compare(from, actual) <= 0 && comparer.Compare(to, actual) >= 0;
82 return new ConstraintResult(this, actual, isInsideRange);
86 /// Modifies the constraint to use an <see cref="IComparer"/> and returns self.
88 public RangeConstraint Using(IComparer comparer)
90 this.comparer = ComparisonAdapter.For(comparer);
95 /// Modifies the constraint to use an <see cref="IComparer{T}"/> and returns self.
97 public RangeConstraint Using<T>(IComparer<T> comparer)
99 this.comparer = ComparisonAdapter.For(comparer);
104 /// Modifies the constraint to use a <see cref="Comparison{T}"/> and returns self.
106 public RangeConstraint Using<T>(Comparison<T> comparer)
108 this.comparer = ComparisonAdapter.For(comparer);