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

/src/NUnit/framework/Constraints/BinaryOperations.cs

#
C# | 148 lines | 71 code | 14 blank | 63 comment | 3 complexity | 7c50d2f6503dba6d5ad7dc1048f7a589 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. namespace NUnit.Framework.Constraints
  8. {
  9. /// <summary>
  10. /// BinaryConstraint is the abstract base of all constraints
  11. /// that combine two other constraints in some fashion.
  12. /// </summary>
  13. public abstract class BinaryConstraint : Constraint
  14. {
  15. /// <summary>
  16. /// The first constraint being combined
  17. /// </summary>
  18. protected Constraint left;
  19. /// <summary>
  20. /// The second constraint being combined
  21. /// </summary>
  22. protected Constraint right;
  23. /// <summary>
  24. /// Construct a BinaryConstraint from two other constraints
  25. /// </summary>
  26. /// <param name="left">The first constraint</param>
  27. /// <param name="right">The second constraint</param>
  28. public BinaryConstraint(Constraint left, Constraint right) : base(left, right)
  29. {
  30. this.left = left;
  31. this.right = right;
  32. }
  33. }
  34. /// <summary>
  35. /// AndConstraint succeeds only if both members succeed.
  36. /// </summary>
  37. public class AndConstraint : BinaryConstraint
  38. {
  39. private enum FailurePoint
  40. {
  41. None,
  42. Left,
  43. Right
  44. };
  45. private FailurePoint failurePoint;
  46. /// <summary>
  47. /// Create an AndConstraint from two other constraints
  48. /// </summary>
  49. /// <param name="left">The first constraint</param>
  50. /// <param name="right">The second constraint</param>
  51. public AndConstraint(Constraint left, Constraint right) : base(left, right) { }
  52. /// <summary>
  53. /// Apply both member constraints to an actual value, succeeding
  54. /// succeeding only if both of them succeed.
  55. /// </summary>
  56. /// <param name="actual">The actual value</param>
  57. /// <returns>True if the constraints both succeeded</returns>
  58. public override bool Matches(object actual)
  59. {
  60. this.actual = actual;
  61. failurePoint = left.Matches(actual)
  62. ? right.Matches(actual)
  63. ? FailurePoint.None
  64. : FailurePoint.Right
  65. : FailurePoint.Left;
  66. return failurePoint == FailurePoint.None;
  67. }
  68. /// <summary>
  69. /// Write a description for this contraint to a MessageWriter
  70. /// </summary>
  71. /// <param name="writer">The MessageWriter to receive the description</param>
  72. public override void WriteDescriptionTo(MessageWriter writer)
  73. {
  74. left.WriteDescriptionTo(writer);
  75. writer.WriteConnector("and");
  76. right.WriteDescriptionTo(writer);
  77. }
  78. /// <summary>
  79. /// Write the actual value for a failing constraint test to a
  80. /// MessageWriter. The default implementation simply writes
  81. /// the raw value of actual, leaving it to the writer to
  82. /// perform any formatting.
  83. /// </summary>
  84. /// <param name="writer">The writer on which the actual value is displayed</param>
  85. public override void WriteActualValueTo(MessageWriter writer)
  86. {
  87. switch (failurePoint)
  88. {
  89. case FailurePoint.Left:
  90. left.WriteActualValueTo(writer);
  91. break;
  92. case FailurePoint.Right:
  93. right.WriteActualValueTo(writer);
  94. break;
  95. default:
  96. base.WriteActualValueTo(writer);
  97. break;
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// OrConstraint succeeds if either member succeeds
  103. /// </summary>
  104. public class OrConstraint : BinaryConstraint
  105. {
  106. /// <summary>
  107. /// Create an OrConstraint from two other constraints
  108. /// </summary>
  109. /// <param name="left">The first constraint</param>
  110. /// <param name="right">The second constraint</param>
  111. public OrConstraint(Constraint left, Constraint right) : base(left, right) { }
  112. /// <summary>
  113. /// Apply the member constraints to an actual value, succeeding
  114. /// succeeding as soon as one of them succeeds.
  115. /// </summary>
  116. /// <param name="actual">The actual value</param>
  117. /// <returns>True if either constraint succeeded</returns>
  118. public override bool Matches(object actual)
  119. {
  120. this.actual = actual;
  121. return left.Matches(actual) || right.Matches(actual);
  122. }
  123. /// <summary>
  124. /// Write a description for this contraint to a MessageWriter
  125. /// </summary>
  126. /// <param name="writer">The MessageWriter to receive the description</param>
  127. public override void WriteDescriptionTo(MessageWriter writer)
  128. {
  129. left.WriteDescriptionTo(writer);
  130. writer.WriteConnector("or");
  131. right.WriteDescriptionTo(writer);
  132. }
  133. }
  134. }