PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/framework/Constraints/PropertyConstraint.cs

#
C# | 168 lines | 82 code | 22 blank | 64 comment | 14 complexity | 1bb7f2e3989fd74eb02854f10a66f8d6 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. using System.Reflection;
  9. namespace NUnit.Framework.Constraints
  10. {
  11. /// <summary>
  12. /// PropertyExistsConstraint tests that a named property
  13. /// exists on the object provided through Match.
  14. ///
  15. /// Originally, PropertyConstraint provided this feature
  16. /// in addition to making optional tests on the vaue
  17. /// of the property. The two constraints are now separate.
  18. /// </summary>
  19. public class PropertyExistsConstraint : Constraint
  20. {
  21. private string name;
  22. Type actualType;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="T:PropertyExistConstraint"/> class.
  25. /// </summary>
  26. /// <param name="name">The name of the property.</param>
  27. public PropertyExistsConstraint(string name) : base(name)
  28. {
  29. this.name = name;
  30. }
  31. /// <summary>
  32. /// Test whether the property exists for a given object
  33. /// </summary>
  34. /// <param name="actual">The object to be tested</param>
  35. /// <returns>True for success, false for failure</returns>
  36. public override bool Matches(object actual)
  37. {
  38. this.actual = actual;
  39. if (actual == null)
  40. throw new ArgumentNullException("actual");
  41. this.actualType = actual as Type;
  42. if (actualType == null)
  43. actualType = actual.GetType();
  44. PropertyInfo property = actualType.GetProperty(name,
  45. BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty);
  46. return property != null;
  47. }
  48. /// <summary>
  49. /// Write the constraint description to a MessageWriter
  50. /// </summary>
  51. /// <param name="writer">The writer on which the description is displayed</param>
  52. public override void WriteDescriptionTo(MessageWriter writer)
  53. {
  54. writer.Write("property " + name);
  55. }
  56. /// <summary>
  57. /// Write the actual value for a failing constraint test to a
  58. /// MessageWriter.
  59. /// </summary>
  60. /// <param name="writer">The writer on which the actual value is displayed</param>
  61. public override void WriteActualValueTo(MessageWriter writer)
  62. {
  63. writer.WriteActualValue(actualType);
  64. }
  65. /// <summary>
  66. /// Returns the string representation of the constraint.
  67. /// </summary>
  68. /// <returns></returns>
  69. protected override string GetStringRepresentation()
  70. {
  71. return string.Format("<propertyexists {0}>", name);
  72. }
  73. }
  74. /// <summary>
  75. /// PropertyConstraint extracts a named property and uses
  76. /// its value as the actual value for a chained constraint.
  77. /// </summary>
  78. public class PropertyConstraint : PrefixConstraint
  79. {
  80. private string name;
  81. private object propValue;
  82. /// <summary>
  83. /// Initializes a new instance of the <see cref="T:PropertyConstraint"/> class.
  84. /// </summary>
  85. /// <param name="name">The name.</param>
  86. /// <param name="baseConstraint">The constraint to apply to the property.</param>
  87. public PropertyConstraint(string name, Constraint baseConstraint)
  88. : base( baseConstraint )
  89. {
  90. this.name = name;
  91. }
  92. /// <summary>
  93. /// Test whether the constraint is satisfied by a given value
  94. /// </summary>
  95. /// <param name="actual">The value to be tested</param>
  96. /// <returns>True for success, false for failure</returns>
  97. public override bool Matches(object actual)
  98. {
  99. this.actual = actual;
  100. if (actual == null)
  101. throw new ArgumentNullException("actual");
  102. Type actualType = actual as Type;
  103. if ( actualType == null )
  104. actualType = actual.GetType();
  105. PropertyInfo property = actualType.GetProperty(name,
  106. BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty);
  107. if (property == null)
  108. throw new ArgumentException(string.Format("Property {0} was not found",name), "name");
  109. propValue = property.GetValue( actual, null );
  110. return baseConstraint.Matches( propValue );
  111. }
  112. /// <summary>
  113. /// Write the constraint description to a MessageWriter
  114. /// </summary>
  115. /// <param name="writer">The writer on which the description is displayed</param>
  116. public override void WriteDescriptionTo(MessageWriter writer)
  117. {
  118. writer.WritePredicate( "property " + name );
  119. if (baseConstraint != null)
  120. {
  121. if (baseConstraint is EqualConstraint)
  122. writer.WritePredicate("equal to");
  123. baseConstraint.WriteDescriptionTo(writer);
  124. }
  125. }
  126. /// <summary>
  127. /// Write the actual value for a failing constraint test to a
  128. /// MessageWriter. The default implementation simply writes
  129. /// the raw value of actual, leaving it to the writer to
  130. /// perform any formatting.
  131. /// </summary>
  132. /// <param name="writer">The writer on which the actual value is displayed</param>
  133. public override void WriteActualValueTo(MessageWriter writer)
  134. {
  135. writer.WriteActualValue(propValue);
  136. }
  137. /// <summary>
  138. /// Returns the string representation of the constraint.
  139. /// </summary>
  140. /// <returns></returns>
  141. protected override string GetStringRepresentation()
  142. {
  143. return string.Format("<property {0} {1}>", name, baseConstraint);
  144. }
  145. }
  146. }