PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/framework/Constraints/ComparisonConstraints.cs

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