PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/framework/Constraints/TypeConstraints.cs

#
C# | 183 lines | 77 code | 16 blank | 90 comment | 10 complexity | a8e4f1bf731402d3415308a9473edb74 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. /// TypeConstraint is the abstract base for constraints
  11. /// that take a Type as their expected value.
  12. /// </summary>
  13. public abstract class TypeConstraint : Constraint
  14. {
  15. /// <summary>
  16. /// The expected Type used by the constraint
  17. /// </summary>
  18. protected Type expectedType;
  19. /// <summary>
  20. /// Construct a TypeConstraint for a given Type
  21. /// </summary>
  22. /// <param name="type"></param>
  23. public TypeConstraint(Type type) : base(type)
  24. {
  25. this.expectedType = type;
  26. }
  27. /// <summary>
  28. /// Write the actual value for a failing constraint test to a
  29. /// MessageWriter. TypeConstraints override this method to write
  30. /// the name of the type.
  31. /// </summary>
  32. /// <param name="writer">The writer on which the actual value is displayed</param>
  33. public override void WriteActualValueTo(MessageWriter writer)
  34. {
  35. writer.WriteActualValue( actual == null ? null : actual.GetType() );
  36. }
  37. }
  38. /// <summary>
  39. /// ExactTypeConstraint is used to test that an object
  40. /// is of the exact type provided in the constructor
  41. /// </summary>
  42. public class ExactTypeConstraint : TypeConstraint
  43. {
  44. /// <summary>
  45. /// Construct an ExactTypeConstraint for a given Type
  46. /// </summary>
  47. /// <param name="type">The expected Type.</param>
  48. public ExactTypeConstraint(Type type) : base( type )
  49. {
  50. this.DisplayName = "typeof";
  51. }
  52. /// <summary>
  53. /// Test that an object is of the exact type specified
  54. /// </summary>
  55. /// <param name="actual">The actual value.</param>
  56. /// <returns>True if the tested object is of the exact type provided, otherwise false.</returns>
  57. public override bool Matches(object actual)
  58. {
  59. this.actual = actual;
  60. return actual != null && actual.GetType() == this.expectedType;
  61. }
  62. /// <summary>
  63. /// Write the description of this constraint to a MessageWriter
  64. /// </summary>
  65. /// <param name="writer">The MessageWriter to use</param>
  66. public override void WriteDescriptionTo(MessageWriter writer)
  67. {
  68. writer.WriteExpectedValue(expectedType);
  69. }
  70. }
  71. /// <summary>
  72. /// InstanceOfTypeConstraint is used to test that an object
  73. /// is of the same type provided or derived from it.
  74. /// </summary>
  75. public class InstanceOfTypeConstraint : TypeConstraint
  76. {
  77. /// <summary>
  78. /// Construct an InstanceOfTypeConstraint for the type provided
  79. /// </summary>
  80. /// <param name="type">The expected Type</param>
  81. public InstanceOfTypeConstraint(Type type) : base(type)
  82. {
  83. this.DisplayName = "instanceof";
  84. }
  85. /// <summary>
  86. /// Test whether an object is of the specified type or a derived type
  87. /// </summary>
  88. /// <param name="actual">The object to be tested</param>
  89. /// <returns>True if the object is of the provided type or derives from it, otherwise false.</returns>
  90. public override bool Matches(object actual)
  91. {
  92. this.actual = actual;
  93. return actual != null && expectedType.IsInstanceOfType(actual);
  94. }
  95. /// <summary>
  96. /// Write a description of this constraint to a MessageWriter
  97. /// </summary>
  98. /// <param name="writer">The MessageWriter to use</param>
  99. public override void WriteDescriptionTo(MessageWriter writer)
  100. {
  101. writer.WritePredicate("instance of");
  102. writer.WriteExpectedValue(expectedType);
  103. }
  104. }
  105. /// <summary>
  106. /// AssignableFromConstraint is used to test that an object
  107. /// can be assigned from a given Type.
  108. /// </summary>
  109. public class AssignableFromConstraint : TypeConstraint
  110. {
  111. /// <summary>
  112. /// Construct an AssignableFromConstraint for the type provided
  113. /// </summary>
  114. /// <param name="type"></param>
  115. public AssignableFromConstraint(Type type) : base(type) { }
  116. /// <summary>
  117. /// Test whether an object can be assigned from the specified type
  118. /// </summary>
  119. /// <param name="actual">The object to be tested</param>
  120. /// <returns>True if the object can be assigned a value of the expected Type, otherwise false.</returns>
  121. public override bool Matches(object actual)
  122. {
  123. this.actual = actual;
  124. return actual != null && actual.GetType().IsAssignableFrom(expectedType);
  125. }
  126. /// <summary>
  127. /// Write a description of this constraint to a MessageWriter
  128. /// </summary>
  129. /// <param name="writer">The MessageWriter to use</param>
  130. public override void WriteDescriptionTo(MessageWriter writer)
  131. {
  132. writer.WritePredicate("assignable from");
  133. writer.WriteExpectedValue(expectedType);
  134. }
  135. }
  136. /// <summary>
  137. /// AssignableToConstraint is used to test that an object
  138. /// can be assigned to a given Type.
  139. /// </summary>
  140. public class AssignableToConstraint : TypeConstraint
  141. {
  142. /// <summary>
  143. /// Construct an AssignableToConstraint for the type provided
  144. /// </summary>
  145. /// <param name="type"></param>
  146. public AssignableToConstraint(Type type) : base(type) { }
  147. /// <summary>
  148. /// Test whether an object can be assigned to the specified type
  149. /// </summary>
  150. /// <param name="actual">The object to be tested</param>
  151. /// <returns>True if the object can be assigned a value of the expected Type, otherwise false.</returns>
  152. public override bool Matches(object actual)
  153. {
  154. this.actual = actual;
  155. return actual != null && expectedType.IsAssignableFrom(actual.GetType());
  156. }
  157. /// <summary>
  158. /// Write a description of this constraint to a MessageWriter
  159. /// </summary>
  160. /// <param name="writer">The MessageWriter to use</param>
  161. public override void WriteDescriptionTo(MessageWriter writer)
  162. {
  163. writer.WritePredicate("assignable to");
  164. writer.WriteExpectedValue(expectedType);
  165. }
  166. }
  167. }