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

/src/NUnit/framework/Constraints/PrefixConstraints.cs

#
C# | 226 lines | 112 code | 25 blank | 89 comment | 8 complexity | 1c95c8cea6f0c0692aa30215b34ec0c9 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. namespace NUnit.Framework.Constraints
  9. {
  10. #region PrefixConstraint
  11. /// <summary>
  12. /// Abstract base class used for prefixes
  13. /// </summary>
  14. public abstract class PrefixConstraint : Constraint
  15. {
  16. /// <summary>
  17. /// The base constraint
  18. /// </summary>
  19. protected Constraint baseConstraint;
  20. /// <summary>
  21. /// Construct given a base constraint
  22. /// </summary>
  23. /// <param name="resolvable"></param>
  24. protected PrefixConstraint(IResolveConstraint resolvable) : base(resolvable)
  25. {
  26. if ( resolvable != null )
  27. this.baseConstraint = resolvable.Resolve();
  28. }
  29. }
  30. #endregion
  31. #region NotConstraint
  32. /// <summary>
  33. /// NotConstraint negates the effect of some other constraint
  34. /// </summary>
  35. public class NotConstraint : PrefixConstraint
  36. {
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="T:NotConstraint"/> class.
  39. /// </summary>
  40. /// <param name="baseConstraint">The base constraint to be negated.</param>
  41. public NotConstraint(Constraint baseConstraint)
  42. : base( baseConstraint ) { }
  43. /// <summary>
  44. /// Test whether the constraint is satisfied by a given value
  45. /// </summary>
  46. /// <param name="actual">The value to be tested</param>
  47. /// <returns>True for if the base constraint fails, false if it succeeds</returns>
  48. public override bool Matches(object actual)
  49. {
  50. this.actual = actual;
  51. return !baseConstraint.Matches(actual);
  52. }
  53. /// <summary>
  54. /// Write the constraint description to a MessageWriter
  55. /// </summary>
  56. /// <param name="writer">The writer on which the description is displayed</param>
  57. public override void WriteDescriptionTo( MessageWriter writer )
  58. {
  59. writer.WritePredicate( "not" );
  60. baseConstraint.WriteDescriptionTo( writer );
  61. }
  62. /// <summary>
  63. /// Write the actual value for a failing constraint test to a MessageWriter.
  64. /// </summary>
  65. /// <param name="writer">The writer on which the actual value is displayed</param>
  66. public override void WriteActualValueTo(MessageWriter writer)
  67. {
  68. baseConstraint.WriteActualValueTo (writer);
  69. }
  70. }
  71. #endregion
  72. #region AllItemsConstraint
  73. /// <summary>
  74. /// AllItemsConstraint applies another constraint to each
  75. /// item in a collection, succeeding if they all succeed.
  76. /// </summary>
  77. public class AllItemsConstraint : PrefixConstraint
  78. {
  79. /// <summary>
  80. /// Construct an AllItemsConstraint on top of an existing constraint
  81. /// </summary>
  82. /// <param name="itemConstraint"></param>
  83. public AllItemsConstraint(Constraint itemConstraint)
  84. : base( itemConstraint )
  85. {
  86. this.DisplayName = "all";
  87. }
  88. /// <summary>
  89. /// Apply the item constraint to each item in the collection,
  90. /// failing if any item fails.
  91. /// </summary>
  92. /// <param name="actual"></param>
  93. /// <returns></returns>
  94. public override bool Matches(object actual)
  95. {
  96. this.actual = actual;
  97. if ( !(actual is IEnumerable) )
  98. throw new ArgumentException( "The actual value must be an IEnumerable", "actual" );
  99. foreach(object item in (IEnumerable)actual)
  100. if (!baseConstraint.Matches(item))
  101. return false;
  102. return true;
  103. }
  104. /// <summary>
  105. /// Write a description of this constraint to a MessageWriter
  106. /// </summary>
  107. /// <param name="writer"></param>
  108. public override void WriteDescriptionTo(MessageWriter writer)
  109. {
  110. writer.WritePredicate("all items");
  111. baseConstraint.WriteDescriptionTo(writer);
  112. }
  113. }
  114. #endregion
  115. #region SomeItemsConstraint
  116. /// <summary>
  117. /// SomeItemsConstraint applies another constraint to each
  118. /// item in a collection, succeeding if any of them succeeds.
  119. /// </summary>
  120. public class SomeItemsConstraint : PrefixConstraint
  121. {
  122. /// <summary>
  123. /// Construct a SomeItemsConstraint on top of an existing constraint
  124. /// </summary>
  125. /// <param name="itemConstraint"></param>
  126. public SomeItemsConstraint(Constraint itemConstraint)
  127. : base( itemConstraint )
  128. {
  129. this.DisplayName = "some";
  130. }
  131. /// <summary>
  132. /// Apply the item constraint to each item in the collection,
  133. /// succeeding if any item succeeds.
  134. /// </summary>
  135. /// <param name="actual"></param>
  136. /// <returns></returns>
  137. public override bool Matches(object actual)
  138. {
  139. this.actual = actual;
  140. if ( !(actual is IEnumerable) )
  141. throw new ArgumentException( "The actual value must be an IEnumerable", "actual" );
  142. foreach(object item in (IEnumerable)actual)
  143. if (baseConstraint.Matches(item))
  144. return true;
  145. return false;
  146. }
  147. /// <summary>
  148. /// Write a description of this constraint to a MessageWriter
  149. /// </summary>
  150. /// <param name="writer"></param>
  151. public override void WriteDescriptionTo(MessageWriter writer)
  152. {
  153. writer.WritePredicate("some item");
  154. baseConstraint.WriteDescriptionTo(writer);
  155. }
  156. }
  157. #endregion
  158. #region NoItemConstraint
  159. /// <summary>
  160. /// NoItemConstraint applies another constraint to each
  161. /// item in a collection, failing if any of them succeeds.
  162. /// </summary>
  163. public class NoItemConstraint : PrefixConstraint
  164. {
  165. /// <summary>
  166. /// Construct a SomeItemsConstraint on top of an existing constraint
  167. /// </summary>
  168. /// <param name="itemConstraint"></param>
  169. public NoItemConstraint(Constraint itemConstraint)
  170. : base( itemConstraint )
  171. {
  172. this.DisplayName = "none";
  173. }
  174. /// <summary>
  175. /// Apply the item constraint to each item in the collection,
  176. /// failing if any item fails.
  177. /// </summary>
  178. /// <param name="actual"></param>
  179. /// <returns></returns>
  180. public override bool Matches(object actual)
  181. {
  182. this.actual = actual;
  183. if ( !(actual is IEnumerable) )
  184. throw new ArgumentException( "The actual value must be an IEnumerable", "actual" );
  185. foreach(object item in (IEnumerable)actual)
  186. if (baseConstraint.Matches(item))
  187. return false;
  188. return true;
  189. }
  190. /// <summary>
  191. /// Write a description of this constraint to a MessageWriter
  192. /// </summary>
  193. /// <param name="writer"></param>
  194. public override void WriteDescriptionTo(MessageWriter writer)
  195. {
  196. writer.WritePredicate("no item");
  197. baseConstraint.WriteDescriptionTo(writer);
  198. }
  199. }
  200. #endregion
  201. }