PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/src/NUnit/framework/Constraints/ThrowsConstraint.cs

#
C# | 195 lines | 103 code | 22 blank | 70 comment | 18 complexity | 292da6c21f5c9f7c96db957b8e639903 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. #region ThrowsConstraint
  10. /// <summary>
  11. /// ThrowsConstraint is used to test the exception thrown by
  12. /// a delegate by applying a constraint to it.
  13. /// </summary>
  14. public class ThrowsConstraint : PrefixConstraint
  15. {
  16. private Exception caughtException;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="T:ThrowsConstraint"/> class,
  19. /// using a constraint to be applied to the exception.
  20. /// </summary>
  21. /// <param name="baseConstraint">A constraint to apply to the caught exception.</param>
  22. public ThrowsConstraint(Constraint baseConstraint)
  23. : base(baseConstraint) { }
  24. /// <summary>
  25. /// Get the actual exception thrown - used by Assert.Throws.
  26. /// </summary>
  27. public Exception ActualException
  28. {
  29. get { return caughtException; }
  30. }
  31. #region Constraint Overrides
  32. /// <summary>
  33. /// Executes the code of the delegate and captures any exception.
  34. /// If a non-null base constraint was provided, it applies that
  35. /// constraint to the exception.
  36. /// </summary>
  37. /// <param name="actual">A delegate representing the code to be tested</param>
  38. /// <returns>True if an exception is thrown and the constraint succeeds, otherwise false</returns>
  39. public override bool Matches(object actual)
  40. {
  41. TestDelegate code = actual as TestDelegate;
  42. if (code == null)
  43. throw new ArgumentException(
  44. string.Format("The actual value must be a TestDelegate but was {0}",actual.GetType().Name), "actual");
  45. this.caughtException = null;
  46. try
  47. {
  48. code();
  49. }
  50. catch (Exception ex)
  51. {
  52. this.caughtException = ex;
  53. }
  54. if (this.caughtException == null)
  55. return false;
  56. return baseConstraint == null || baseConstraint.Matches(caughtException);
  57. }
  58. #if NET_2_0
  59. /// <summary>
  60. /// Converts an ActualValueDelegate to a TestDelegate
  61. /// before calling the primary overload.
  62. /// </summary>
  63. /// <param name="del"></param>
  64. /// <returns></returns>
  65. public override bool Matches(ActualValueDelegate del)
  66. {
  67. TestDelegate testDelegate = new TestDelegate(delegate { del(); });
  68. return Matches((object)testDelegate);
  69. }
  70. #endif
  71. /// <summary>
  72. /// Write the constraint description to a MessageWriter
  73. /// </summary>
  74. /// <param name="writer">The writer on which the description is displayed</param>
  75. public override void WriteDescriptionTo(MessageWriter writer)
  76. {
  77. if (baseConstraint == null)
  78. writer.WritePredicate("an exception");
  79. else
  80. baseConstraint.WriteDescriptionTo(writer);
  81. }
  82. /// <summary>
  83. /// Write the actual value for a failing constraint test to a
  84. /// MessageWriter. The default implementation simply writes
  85. /// the raw value of actual, leaving it to the writer to
  86. /// perform any formatting.
  87. /// </summary>
  88. /// <param name="writer">The writer on which the actual value is displayed</param>
  89. public override void WriteActualValueTo(MessageWriter writer)
  90. {
  91. if (caughtException == null)
  92. writer.Write("no exception thrown");
  93. else if (baseConstraint != null)
  94. baseConstraint.WriteActualValueTo(writer);
  95. else
  96. writer.WriteActualValue(caughtException);
  97. }
  98. #endregion
  99. /// <summary>
  100. /// Returns the string representation of this constraint
  101. /// </summary>
  102. protected override string GetStringRepresentation()
  103. {
  104. if (baseConstraint == null)
  105. return "<throws>";
  106. return base.GetStringRepresentation();
  107. }
  108. }
  109. #endregion
  110. #region ThrowsNothingConstraint
  111. /// <summary>
  112. /// ThrowsNothingConstraint tests that a delegate does not
  113. /// throw an exception.
  114. /// </summary>
  115. public class ThrowsNothingConstraint : Constraint
  116. {
  117. private Exception caughtException;
  118. /// <summary>
  119. /// Test whether the constraint is satisfied by a given value
  120. /// </summary>
  121. /// <param name="actual">The value to be tested</param>
  122. /// <returns>True if no exception is thrown, otherwise false</returns>
  123. public override bool Matches(object actual)
  124. {
  125. TestDelegate code = actual as TestDelegate;
  126. if (code == null)
  127. throw new ArgumentException("The actual value must be a TestDelegate", "actual");
  128. this.caughtException = null;
  129. try
  130. {
  131. code();
  132. }
  133. catch (Exception ex)
  134. {
  135. this.caughtException = ex;
  136. }
  137. return this.caughtException == null;
  138. }
  139. #if NET_2_0
  140. /// <summary>
  141. /// Converts an ActualValueDelegate to a TestDelegate
  142. /// before calling the primary overload.
  143. /// </summary>
  144. /// <param name="del"></param>
  145. /// <returns></returns>
  146. public override bool Matches(ActualValueDelegate del)
  147. {
  148. TestDelegate testDelegate = new TestDelegate(delegate { del(); });
  149. return Matches((object)testDelegate);
  150. }
  151. #endif
  152. /// <summary>
  153. /// Write the constraint description to a MessageWriter
  154. /// </summary>
  155. /// <param name="writer">The writer on which the description is displayed</param>
  156. public override void WriteDescriptionTo(MessageWriter writer)
  157. {
  158. writer.Write(string.Format("No Exception to be thrown"));
  159. }
  160. /// <summary>
  161. /// Write the actual value for a failing constraint test to a
  162. /// MessageWriter. The default implementation simply writes
  163. /// the raw value of actual, leaving it to the writer to
  164. /// perform any formatting.
  165. /// </summary>
  166. /// <param name="writer">The writer on which the actual value is displayed</param>
  167. public override void WriteActualValueTo(MessageWriter writer)
  168. {
  169. writer.WriteActualValue( this.caughtException.GetType() );
  170. }
  171. }
  172. #endregion
  173. }