PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/framework/Constraints/RangeConstraint.cs

#
C# | 92 lines | 48 code | 12 blank | 32 comment | 6 complexity | 065cbe59d9c01bf5a8a807b5d76f16ae MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2008, 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. /// RangeConstraint tests whethe two values are within a
  15. /// specified range.
  16. /// </summary>
  17. public class RangeConstraint : Constraint
  18. {
  19. private IComparable from;
  20. private IComparable to;
  21. private ComparisonAdapter comparer = ComparisonAdapter.Default;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="T:RangeConstraint"/> class.
  24. /// </summary>
  25. /// <param name="from">From.</param>
  26. /// <param name="to">To.</param>
  27. public RangeConstraint(IComparable from, IComparable to) : base( from, to )
  28. {
  29. this.from = from;
  30. this.to = to;
  31. }
  32. /// <summary>
  33. /// Test whether the constraint is satisfied by a given value
  34. /// </summary>
  35. /// <param name="actual">The value to be tested</param>
  36. /// <returns>True for success, false for failure</returns>
  37. public override bool Matches(object actual)
  38. {
  39. this.actual = actual;
  40. if ( from == null || to == null || actual == null)
  41. throw new ArgumentException( "Cannot compare using a null reference", "expected" );
  42. return comparer.Compare(from, actual) <= 0 &&
  43. comparer.Compare(to, actual) >= 0;
  44. }
  45. /// <summary>
  46. /// Write the constraint description to a MessageWriter
  47. /// </summary>
  48. /// <param name="writer">The writer on which the description is displayed</param>
  49. public override void WriteDescriptionTo(MessageWriter writer)
  50. {
  51. writer.Write("in range ({0},{1})", from, to);
  52. }
  53. /// <summary>
  54. /// Modifies the constraint to use an IComparer and returns self.
  55. /// </summary>
  56. public RangeConstraint Using(IComparer comparer)
  57. {
  58. this.comparer = ComparisonAdapter.For(comparer);
  59. return this;
  60. }
  61. #if NET_2_0
  62. /// <summary>
  63. /// Modifies the constraint to use an IComparer&lt;T&gt; and returns self.
  64. /// </summary>
  65. public RangeConstraint Using<T>(IComparer<T> comparer)
  66. {
  67. this.comparer = ComparisonAdapter.For(comparer);
  68. return this;
  69. }
  70. /// <summary>
  71. /// Modifies the constraint to use a Comparison&lt;T&gt; and returns self.
  72. /// </summary>
  73. public RangeConstraint Using<T>(Comparison<T> comparer)
  74. {
  75. this.comparer = ComparisonAdapter.For(comparer);
  76. return this;
  77. }
  78. #endif
  79. }
  80. }