PageRenderTime 24ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/framework/Constraints/BasicConstraints.cs

#
C# | 118 lines | 52 code | 13 blank | 53 comment | 11 complexity | 346a13d101de5a176ba298bc462cad95 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. namespace NUnit.Framework.Constraints
  8. {
  9. /// <summary>
  10. /// BasicConstraint is the abstract base for constraints that
  11. /// perform a simple comparison to a constant value.
  12. /// </summary>
  13. public abstract class BasicConstraint : Constraint
  14. {
  15. private object expected;
  16. private string description;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="T:BasicConstraint"/> class.
  19. /// </summary>
  20. /// <param name="expected">The expected.</param>
  21. /// <param name="description">The description.</param>
  22. public BasicConstraint(object expected, string description)
  23. {
  24. this.expected = expected;
  25. this.description = description;
  26. }
  27. /// <summary>
  28. /// Test whether the constraint is satisfied by a given value
  29. /// </summary>
  30. /// <param name="actual">The value to be tested</param>
  31. /// <returns>True for success, false for failure</returns>
  32. public override bool Matches(object actual)
  33. {
  34. this.actual = actual;
  35. if (actual == null && expected == null)
  36. return true;
  37. if (actual == null || expected == null)
  38. return false;
  39. return expected.Equals(actual);
  40. }
  41. /// <summary>
  42. /// Write the constraint description to a MessageWriter
  43. /// </summary>
  44. /// <param name="writer">The writer on which the description is displayed</param>
  45. public override void WriteDescriptionTo(MessageWriter writer)
  46. {
  47. writer.Write(description);
  48. }
  49. }
  50. /// <summary>
  51. /// NullConstraint tests that the actual value is null
  52. /// </summary>
  53. public class NullConstraint : BasicConstraint
  54. {
  55. /// <summary>
  56. /// Initializes a new instance of the <see cref="T:NullConstraint"/> class.
  57. /// </summary>
  58. public NullConstraint() : base(null, "null") { }
  59. }
  60. /// <summary>
  61. /// TrueConstraint tests that the actual value is true
  62. /// </summary>
  63. public class TrueConstraint : BasicConstraint
  64. {
  65. /// <summary>
  66. /// Initializes a new instance of the <see cref="T:TrueConstraint"/> class.
  67. /// </summary>
  68. public TrueConstraint() : base(true, "True") { }
  69. }
  70. /// <summary>
  71. /// FalseConstraint tests that the actual value is false
  72. /// </summary>
  73. public class FalseConstraint : BasicConstraint
  74. {
  75. /// <summary>
  76. /// Initializes a new instance of the <see cref="T:FalseConstraint"/> class.
  77. /// </summary>
  78. public FalseConstraint() : base(false, "False") { }
  79. }
  80. /// <summary>
  81. /// NaNConstraint tests that the actual value is a double or float NaN
  82. /// </summary>
  83. public class NaNConstraint : Constraint
  84. {
  85. /// <summary>
  86. /// Test that the actual value is an NaN
  87. /// </summary>
  88. /// <param name="actual"></param>
  89. /// <returns></returns>
  90. public override bool Matches(object actual)
  91. {
  92. this.actual = actual;
  93. return actual is double && double.IsNaN((double)actual)
  94. || actual is float && float.IsNaN((float)actual);
  95. }
  96. /// <summary>
  97. /// Write the constraint description to a specified writer
  98. /// </summary>
  99. /// <param name="writer"></param>
  100. public override void WriteDescriptionTo(MessageWriter writer)
  101. {
  102. writer.Write("NaN");
  103. }
  104. }
  105. }