1 // ****************************************************************
\r
2 // Copyright 2007, Charlie Poole
\r
3 // This is free software licensed under the NUnit license. You may
\r
4 // obtain a copy of the license at http://nunit.org/?p=license&r=2.4
\r
5 // ****************************************************************
\r
9 namespace NUnit.Framework.Constraints
\r
12 /// Abstract base class for constraints that compare values to
\r
13 /// determine if one is greater than, equal to or less than
\r
16 public abstract class ComparisonConstraint : Constraint
\r
19 /// The value against which a comparison is to be made
\r
21 protected IComparable expected;
\r
23 /// If true, less than returns success
\r
25 protected bool ltOK = false;
\r
27 /// if true, equal returns success
\r
29 protected bool eqOK = false;
\r
31 /// if true, greater than returns success
\r
33 protected bool gtOK = false;
\r
35 /// The predicate used as a part of the description
\r
37 private string predicate;
\r
40 /// Initializes a new instance of the <see cref="T:ComparisonConstraint"/> class.
\r
42 /// <param name="value">The value against which to make a comparison.</param>
\r
43 /// <param name="ltOK">if set to <c>true</c> less succeeds.</param>
\r
44 /// <param name="eqOK">if set to <c>true</c> equal succeeds.</param>
\r
45 /// <param name="gtOK">if set to <c>true</c> greater succeeds.</param>
\r
46 /// <param name="predicate">String used in describing the constraint.</param>
\r
47 public ComparisonConstraint(IComparable value, bool ltOK, bool eqOK, bool gtOK, string predicate)
\r
49 this.expected = value;
\r
53 this.predicate = predicate;
\r
57 /// Test whether the constraint is satisfied by a given value
\r
59 /// <param name="actual">The value to be tested</param>
\r
60 /// <returns>True for success, false for failure</returns>
\r
61 public override bool Matches(object actual)
\r
63 this.actual = actual;
\r
65 int icomp = Numerics.Compare( expected, actual );
\r
66 return icomp < 0 && gtOK || icomp == 0 && eqOK || icomp > 0 && ltOK;
\r
70 /// Write the constraint description to a MessageWriter
\r
72 /// <param name="writer">The writer on which the description is displayed</param>
\r
73 public override void WriteDescriptionTo(MessageWriter writer)
\r
75 writer.WritePredicate(predicate);
\r
76 writer.WriteExpectedValue(expected);
\r
81 /// Tests whether a value is greater than the value supplied to its constructor
\r
83 public class GreaterThanConstraint : ComparisonConstraint
\r
86 /// Initializes a new instance of the <see cref="T:GreaterThanConstraint"/> class.
\r
88 /// <param name="expected">The expected value.</param>
\r
89 public GreaterThanConstraint(IComparable expected) : base(expected, false, false, true, "greater than") { }
\r
93 /// Tests whether a value is greater than or equal to the value supplied to its constructor
\r
95 public class GreaterThanOrEqualConstraint : ComparisonConstraint
\r
98 /// Initializes a new instance of the <see cref="T:GreaterThanOrEqualConstraint"/> class.
\r
100 /// <param name="expected">The expected value.</param>
\r
101 public GreaterThanOrEqualConstraint(IComparable expected) : base(expected, false, true, true, "greater than or equal to") { }
\r
105 /// Tests whether a value is less than the value supplied to its constructor
\r
107 public class LessThanConstraint : ComparisonConstraint
\r
110 /// Initializes a new instance of the <see cref="T:LessThanConstraint"/> class.
\r
112 /// <param name="expected">The expected value.</param>
\r
113 public LessThanConstraint(IComparable expected) : base(expected, true, false, false, "less than") { }
\r
117 /// Tests whether a value is less than or equal to the value supplied to its constructor
\r
119 public class LessThanOrEqualConstraint : ComparisonConstraint
\r
122 /// Initializes a new instance of the <see cref="T:LessThanOrEqualConstraint"/> class.
\r
124 /// <param name="expected">The expected value.</param>
\r
125 public LessThanOrEqualConstraint(IComparable expected) : base(expected, true, true, false, "less than or equal to") { }
\r