1 // ***********************************************************************
2 // Copyright (c) 2007 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 /// Abstract base class for constraints that compare _values to
37 /// determine if one is greater than, equal to or less than
40 public abstract class ComparisonConstraint : Constraint
43 /// The value against which a comparison is to be made
45 protected object expected;
47 /// If true, less than returns success
49 protected bool lessComparisonResult = false;
51 /// if true, equal returns success
53 protected bool equalComparisonResult = false;
55 /// if true, greater than returns success
57 protected bool greaterComparisonResult = false;
60 /// ComparisonAdapter to be used in making the comparison
62 private ComparisonAdapter comparer = ComparisonAdapter.Default;
65 /// Initializes a new instance of the <see cref="ComparisonConstraint"/> class.
67 /// <param name="value">The value against which to make a comparison.</param>
68 /// <param name="lessComparisonResult">if set to <c>true</c> less succeeds.</param>
69 /// <param name="equalComparisonResult">if set to <c>true</c> equal succeeds.</param>
70 /// <param name="greaterComparisonResult">if set to <c>true</c> greater succeeds.</param>
71 /// <param name="predicate">String used in describing the constraint.</param>
72 protected ComparisonConstraint(object value, bool lessComparisonResult, bool equalComparisonResult, bool greaterComparisonResult, string predicate)
75 this.expected = value;
76 this.lessComparisonResult = lessComparisonResult;
77 this.equalComparisonResult = equalComparisonResult;
78 this.greaterComparisonResult = greaterComparisonResult;
79 this.Description = predicate + " " + MsgUtils.FormatValue(expected);
83 /// Test whether the constraint is satisfied by a given value
85 /// <param name="actual">The value to be tested</param>
86 /// <returns>True for success, false for failure</returns>
87 public override ConstraintResult ApplyTo<TActual>(TActual actual)
90 throw new ArgumentException("Cannot compare using a null reference", "expected");
93 throw new ArgumentException("Cannot compare to null reference", "actual");
95 int icomp = comparer.Compare(expected, actual);
97 bool hasSucceeded = icomp < 0 && greaterComparisonResult || icomp == 0 && equalComparisonResult || icomp > 0 && lessComparisonResult;
98 return new ConstraintResult(this, actual, hasSucceeded);
102 /// Modifies the constraint to use an <see cref="IComparer"/> and returns self
104 /// <param name="comparer">The comparer used for comparison tests</param>
105 /// <returns>A constraint modified to use the given comparer</returns>
106 public ComparisonConstraint Using(IComparer comparer)
108 this.comparer = ComparisonAdapter.For(comparer);
113 /// Modifies the constraint to use an <see cref="IComparer{T}"/> and returns self
115 /// <param name="comparer">The comparer used for comparison tests</param>
116 /// <returns>A constraint modified to use the given comparer</returns>
117 public ComparisonConstraint Using<T>(IComparer<T> comparer)
119 this.comparer = ComparisonAdapter.For(comparer);
124 /// Modifies the constraint to use a <see cref="Comparison{T}"/> and returns self
126 /// <param name="comparer">The comparer used for comparison tests</param>
127 /// <returns>A constraint modified to use the given comparer</returns>
128 public ComparisonConstraint Using<T>(Comparison<T> comparer)
130 this.comparer = ComparisonAdapter.For(comparer);