/src/NUnit/framework/Constraints/ComparisonConstraints.cs
C# | 173 lines | 74 code | 17 blank | 82 comment | 10 complexity | 743597a0043b843d357e99ff0b2e136a MD5 | raw file
1// **************************************************************** 2// Copyright 2007, Charlie Poole 3// This is free software licensed under the NUnit license. You may 4// obtain a copy of the license at http://nunit.org 5// **************************************************************** 6 7using System; 8using System.Collections; 9#if NET_2_0 10using System.Collections.Generic; 11#endif 12 13namespace NUnit.Framework.Constraints 14{ 15 /// <summary> 16 /// Abstract base class for constraints that compare values to 17 /// determine if one is greater than, equal to or less than 18 /// the other. 19 /// </summary> 20 public abstract class ComparisonConstraint : Constraint 21 { 22 /// <summary> 23 /// The value against which a comparison is to be made 24 /// </summary> 25 protected object expected; 26 /// <summary> 27 /// If true, less than returns success 28 /// </summary> 29 protected bool ltOK = false; 30 /// <summary> 31 /// if true, equal returns success 32 /// </summary> 33 protected bool eqOK = false; 34 /// <summary> 35 /// if true, greater than returns success 36 /// </summary> 37 protected bool gtOK = false; 38 /// <summary> 39 /// The predicate used as a part of the description 40 /// </summary> 41 private string predicate; 42 43 /// <summary> 44 /// ComparisonAdapter to be used in making the comparison 45 /// </summary> 46 private ComparisonAdapter comparer = ComparisonAdapter.Default; 47 48 /// <summary> 49 /// Initializes a new instance of the <see cref="T:ComparisonConstraint"/> class. 50 /// </summary> 51 /// <param name="value">The value against which to make a comparison.</param> 52 /// <param name="ltOK">if set to <c>true</c> less succeeds.</param> 53 /// <param name="eqOK">if set to <c>true</c> equal succeeds.</param> 54 /// <param name="gtOK">if set to <c>true</c> greater succeeds.</param> 55 /// <param name="predicate">String used in describing the constraint.</param> 56 public ComparisonConstraint(object value, bool ltOK, bool eqOK, bool gtOK, string predicate) 57 : base(value) 58 { 59 this.expected = value; 60 this.ltOK = ltOK; 61 this.eqOK = eqOK; 62 this.gtOK = gtOK; 63 this.predicate = predicate; 64 } 65 66 /// <summary> 67 /// Test whether the constraint is satisfied by a given value 68 /// </summary> 69 /// <param name="actual">The value to be tested</param> 70 /// <returns>True for success, false for failure</returns> 71 public override bool Matches(object actual) 72 { 73 this.actual = actual; 74 75 if (expected == null) 76 throw new ArgumentException("Cannot compare using a null reference", "expected"); 77 78 if (actual == null) 79 throw new ArgumentException("Cannot compare to null reference", "actual"); 80 81 int icomp = comparer.Compare(expected, actual); 82 83 return icomp < 0 && gtOK || icomp == 0 && eqOK || icomp > 0 && ltOK; 84 } 85 86 /// <summary> 87 /// Write the constraint description to a MessageWriter 88 /// </summary> 89 /// <param name="writer">The writer on which the description is displayed</param> 90 public override void WriteDescriptionTo(MessageWriter writer) 91 { 92 writer.WritePredicate(predicate); 93 writer.WriteExpectedValue(expected); 94 } 95 96 /// <summary> 97 /// Modifies the constraint to use an IComparer and returns self 98 /// </summary> 99 public ComparisonConstraint Using(IComparer comparer) 100 { 101 this.comparer = ComparisonAdapter.For(comparer); 102 return this; 103 } 104 105#if NET_2_0 106 /// <summary> 107 /// Modifies the constraint to use an IComparer<T> and returns self 108 /// </summary> 109 public ComparisonConstraint Using<T>(IComparer<T> comparer) 110 { 111 this.comparer = ComparisonAdapter.For(comparer); 112 return this; 113 } 114 115 /// <summary> 116 /// Modifies the constraint to use a Comparison<T> and returns self 117 /// </summary> 118 public ComparisonConstraint Using<T>(Comparison<T> comparer) 119 { 120 this.comparer = ComparisonAdapter.For(comparer); 121 return this; 122 } 123#endif 124 } 125 126 /// <summary> 127 /// Tests whether a value is greater than the value supplied to its constructor 128 /// </summary> 129 public class GreaterThanConstraint : ComparisonConstraint 130 { 131 /// <summary> 132 /// Initializes a new instance of the <see cref="T:GreaterThanConstraint"/> class. 133 /// </summary> 134 /// <param name="expected">The expected value.</param> 135 public GreaterThanConstraint(object expected) : base(expected, false, false, true, "greater than") { } 136 } 137 138 /// <summary> 139 /// Tests whether a value is greater than or equal to the value supplied to its constructor 140 /// </summary> 141 public class GreaterThanOrEqualConstraint : ComparisonConstraint 142 { 143 /// <summary> 144 /// Initializes a new instance of the <see cref="T:GreaterThanOrEqualConstraint"/> class. 145 /// </summary> 146 /// <param name="expected">The expected value.</param> 147 public GreaterThanOrEqualConstraint(object expected) : base(expected, false, true, true, "greater than or equal to") { } 148 } 149 150 /// <summary> 151 /// Tests whether a value is less than the value supplied to its constructor 152 /// </summary> 153 public class LessThanConstraint : ComparisonConstraint 154 { 155 /// <summary> 156 /// Initializes a new instance of the <see cref="T:LessThanConstraint"/> class. 157 /// </summary> 158 /// <param name="expected">The expected value.</param> 159 public LessThanConstraint(object expected) : base(expected, true, false, false, "less than") { } 160 } 161 162 /// <summary> 163 /// Tests whether a value is less than or equal to the value supplied to its constructor 164 /// </summary> 165 public class LessThanOrEqualConstraint : ComparisonConstraint 166 { 167 /// <summary> 168 /// Initializes a new instance of the <see cref="T:LessThanOrEqualConstraint"/> class. 169 /// </summary> 170 /// <param name="expected">The expected value.</param> 171 public LessThanOrEqualConstraint(object expected) : base(expected, true, true, false, "less than or equal to") { } 172 } 173}