Merge branch 'master' of git://github.com/mono/mono
[mono.git] / mcs / nunit24 / NUnitFramework / framework / Constraints / ComparisonConstraints.cs
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
6 \r
7 using System;\r
8 \r
9 namespace NUnit.Framework.Constraints\r
10 {\r
11     /// <summary>\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
14     /// the other.\r
15     /// </summary>\r
16     public abstract class ComparisonConstraint : Constraint\r
17     {\r
18         /// <summary>\r
19         /// The value against which a comparison is to be made\r
20         /// </summary>\r
21         protected IComparable expected;\r
22         /// <summary>\r
23         /// If true, less than returns success\r
24         /// </summary>\r
25         protected bool ltOK = false;\r
26         /// <summary>\r
27         /// if true, equal returns success\r
28         /// </summary>\r
29         protected bool eqOK = false;\r
30         /// <summary>\r
31         /// if true, greater than returns success\r
32         /// </summary>\r
33         protected bool gtOK = false;\r
34         /// <summary>\r
35         /// The predicate used as a part of the description\r
36         /// </summary>\r
37         private string predicate;\r
38 \r
39         /// <summary>\r
40         /// Initializes a new instance of the <see cref="T:ComparisonConstraint"/> class.\r
41         /// </summary>\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
48         {\r
49             this.expected = value;\r
50             this.ltOK = ltOK;\r
51             this.eqOK = eqOK;\r
52             this.gtOK = gtOK;\r
53             this.predicate = predicate;\r
54         }\r
55 \r
56         /// <summary>\r
57         /// Test whether the constraint is satisfied by a given value\r
58         /// </summary>\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
62         {\r
63             this.actual = actual;\r
64 \r
65                         int icomp = Numerics.Compare( expected, actual );\r
66             return icomp < 0 && gtOK || icomp == 0 && eqOK || icomp > 0 && ltOK;\r
67         }\r
68 \r
69         /// <summary>\r
70         /// Write the constraint description to a MessageWriter\r
71         /// </summary>\r
72         /// <param name="writer">The writer on which the description is displayed</param>\r
73         public override void WriteDescriptionTo(MessageWriter writer)\r
74         {\r
75             writer.WritePredicate(predicate);\r
76             writer.WriteExpectedValue(expected);\r
77         }\r
78     }\r
79 \r
80     /// <summary>\r
81     /// Tests whether a value is greater than the value supplied to its constructor\r
82     /// </summary>\r
83     public class GreaterThanConstraint : ComparisonConstraint\r
84     {\r
85         /// <summary>\r
86         /// Initializes a new instance of the <see cref="T:GreaterThanConstraint"/> class.\r
87         /// </summary>\r
88         /// <param name="expected">The expected value.</param>\r
89         public GreaterThanConstraint(IComparable expected) : base(expected, false, false, true, "greater than") { }\r
90     }\r
91 \r
92     /// <summary>\r
93     /// Tests whether a value is greater than or equal to the value supplied to its constructor\r
94     /// </summary>\r
95     public class GreaterThanOrEqualConstraint : ComparisonConstraint\r
96     {\r
97         /// <summary>\r
98         /// Initializes a new instance of the <see cref="T:GreaterThanOrEqualConstraint"/> class.\r
99         /// </summary>\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
102     }\r
103 \r
104     /// <summary>\r
105     /// Tests whether a value is less than the value supplied to its constructor\r
106     /// </summary>\r
107     public class LessThanConstraint : ComparisonConstraint\r
108     {\r
109         /// <summary>\r
110         /// Initializes a new instance of the <see cref="T:LessThanConstraint"/> class.\r
111         /// </summary>\r
112         /// <param name="expected">The expected value.</param>\r
113         public LessThanConstraint(IComparable expected) : base(expected, true, false, false, "less than") { }\r
114     }\r
115 \r
116     /// <summary>\r
117     /// Tests whether a value is less than or equal to the value supplied to its constructor\r
118     /// </summary>\r
119     public class LessThanOrEqualConstraint : ComparisonConstraint\r
120     {\r
121         /// <summary>\r
122         /// Initializes a new instance of the <see cref="T:LessThanOrEqualConstraint"/> class.\r
123         /// </summary>\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
126     }\r
127 }\r