PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Releases/1.7.0/FluentAssertions.Net35/Assertions/XDocumentAssertions.cs

http://fluentassertions.codeplex.com
C# | 223 lines | 94 code | 22 blank | 107 comment | 5 complexity | 6e16f6e8a13e8015c1b0b789ef41d2a6 MD5 | raw file
  1. using System.Diagnostics;
  2. using System.Xml.Linq;
  3. namespace FluentAssertions.Assertions
  4. {
  5. /// <summary>
  6. /// Contains a number of methods to assert that an <see cref="XDocument"/> is in the expected state.
  7. /// </summary>
  8. [DebuggerNonUserCode]
  9. public class XDocumentAssertions
  10. {
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="XDocumentAssertions" /> class.
  13. /// </summary>
  14. protected internal XDocumentAssertions(XDocument document)
  15. {
  16. Subject = document;
  17. }
  18. /// <summary>
  19. /// Gets the object which value is being asserted.
  20. /// </summary>
  21. public XDocument Subject { get; private set; }
  22. /// <summary>
  23. /// Asserts that the current <see cref="XDocument"/> equals the <paramref name="expected"/> document,
  24. /// using its <see cref="object.Equals(object)" /> implementation.
  25. /// </summary>
  26. /// <param name="expected">The expected document</param>
  27. public AndConstraint<XDocumentAssertions> Be(XDocument expected)
  28. {
  29. return Be(expected, string.Empty);
  30. }
  31. /// <summary>
  32. /// Asserts that the current <see cref="XDocument"/> equals the <paramref name="expected"/> document,
  33. /// using its <see cref="object.Equals(object)" /> implementation.
  34. /// </summary>
  35. /// <param name="expected">The expected document</param>
  36. /// <param name="reason">
  37. /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
  38. /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
  39. /// </param>
  40. /// <param name="reasonArgs">
  41. /// Zero or more objects to format using the placeholders in <see cref="reason" />.
  42. /// </param>
  43. public AndConstraint<XDocumentAssertions> Be(XDocument expected, string reason, params object [] reasonArgs)
  44. {
  45. Execute.Verification
  46. .ForCondition(Subject.Equals(expected))
  47. .BecauseOf(reason, reasonArgs)
  48. .FailWith("Expected XML document to be {0}{reason}, but found {1}", expected, Subject);
  49. return new AndConstraint<XDocumentAssertions>(this);
  50. }
  51. /// <summary>
  52. /// Asserts that the current <see cref="XDocument"/> does not equal the <paramref name="unexpected"/> document,
  53. /// using its <see cref="object.Equals(object)" /> implementation.
  54. /// </summary>
  55. /// <param name="unexpected">The unexpected document</param>
  56. public AndConstraint<XDocumentAssertions> NotBe(XDocument unexpected)
  57. {
  58. return NotBe(unexpected, string.Empty);
  59. }
  60. /// <summary>
  61. /// Asserts that the current <see cref="XDocument"/> does not equal the <paramref name="unexpected"/> document,
  62. /// using its <see cref="object.Equals(object)" /> implementation.
  63. /// </summary>
  64. /// <param name="unexpected">The unexpected document</param>
  65. /// <param name="reason">
  66. /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
  67. /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
  68. /// </param>
  69. /// <param name="reasonArgs">
  70. /// Zero or more objects to format using the placeholders in <see cref="reason" />.
  71. /// </param>
  72. public AndConstraint<XDocumentAssertions> NotBe(XDocument unexpected, string reason, params object [] reasonArgs)
  73. {
  74. Execute.Verification
  75. .ForCondition(!Subject.Equals(unexpected))
  76. .BecauseOf(reason, reasonArgs)
  77. .FailWith("Did not expect XML document to be {0}{reason}.", unexpected);
  78. return new AndConstraint<XDocumentAssertions>(this);
  79. }
  80. /// <summary>
  81. /// Asserts that the <see cref="XDocument"/> is <c>null</c>.
  82. /// </summary>
  83. public AndConstraint<XDocumentAssertions> BeNull()
  84. {
  85. return BeNull(string.Empty);
  86. }
  87. /// <summary>
  88. /// Asserts that the <see cref="XDocument"/> is <c>null</c>.
  89. /// </summary>
  90. /// <param name="reason">
  91. /// A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
  92. /// start with the word <i>because</i>, it is prepended to the message.
  93. /// </param>
  94. /// <param name="reasonArgs">
  95. /// Zero or more values to use for filling in any <see cref="string.Format(string,object[])" /> compatible placeholders.
  96. /// </param>
  97. public AndConstraint<XDocumentAssertions> BeNull(string reason, params object [] reasonArgs)
  98. {
  99. Execute.Verification
  100. .ForCondition(ReferenceEquals(Subject, null))
  101. .BecauseOf(reason, reasonArgs)
  102. .FailWith("Expected XML document to be <null>{reason}, but found {0}.", Subject);
  103. return new AndConstraint<XDocumentAssertions>(this);
  104. }
  105. /// <summary>
  106. /// Asserts that the <see cref="XDocument"/> is not <c>null</c>.
  107. /// </summary>
  108. public AndConstraint<XDocumentAssertions> NotBeNull()
  109. {
  110. return NotBeNull(string.Empty);
  111. }
  112. /// <summary>
  113. /// Asserts that the <see cref="XDocument"/> is not <c>null</c>.
  114. /// </summary>
  115. /// <param name="reason">
  116. /// A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
  117. /// start with the word <i>because</i>, it is prepended to the message.
  118. /// </param>
  119. /// <param name="reasonArgs">
  120. /// Zero or more values to use for filling in any <see cref="string.Format(string,object[])" /> compatible placeholders.
  121. /// </param>
  122. public AndConstraint<XDocumentAssertions> NotBeNull(string reason, params object [] reasonArgs)
  123. {
  124. Execute.Verification
  125. .ForCondition(!ReferenceEquals(Subject, null))
  126. .BecauseOf(reason, reasonArgs)
  127. .FailWith("Did not expect XML document to be <null>{reason}.");
  128. return new AndConstraint<XDocumentAssertions>(this);
  129. }
  130. /// <summary>
  131. /// Asserts that the current <see cref="XDocument"/> has a root element with the specified
  132. /// <paramref name="expected"/> name.
  133. /// </summary>
  134. /// <param name="expected">The name of the expected root element of the current document.</param>
  135. public AndConstraint<XDocumentAssertions> HaveRoot(string expected)
  136. {
  137. return HaveRoot(expected, string.Empty);
  138. }
  139. /// <summary>
  140. /// Asserts that the current <see cref="XDocument"/> has a root element with the specified
  141. /// <paramref name="expected"/> name.
  142. /// </summary>
  143. /// <param name="expected">The name of the expected root element of the current document.</param>
  144. /// <param name="reason">
  145. /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
  146. /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
  147. /// </param>
  148. /// <param name="reasonArgs">
  149. /// Zero or more objects to format using the placeholders in <see cref="reason" />.
  150. /// </param>
  151. public AndConstraint<XDocumentAssertions> HaveRoot(string expected, string reason, params object[] reasonArgs)
  152. {
  153. XElement root = Subject.Root;
  154. Execute.Verification
  155. .ForCondition((root != null) && root.Name == expected)
  156. .BecauseOf(reason, reasonArgs)
  157. .FailWith("Expected XML document to have root element <" + expected + ">{reason}" +
  158. ", but found {0}.", Subject);
  159. return new AndConstraint<XDocumentAssertions>(this);
  160. }
  161. /// <summary>
  162. /// Asserts that the <see cref="XDocument.Root"/> element of the current <see cref="XDocument"/> has a direct
  163. /// child element with the specified <paramref name="expected"/> name.
  164. /// </summary>
  165. /// <param name="expected">
  166. /// The name of the expected child element of the current document's Root <see cref="XDocument.Root"/> element.
  167. /// </param>
  168. public AndConstraint<XDocumentAssertions> HaveElement(string expected)
  169. {
  170. return HaveElement(expected, string.Empty);
  171. }
  172. /// <summary>
  173. /// Asserts that the <see cref="XDocument.Root"/> element of the current <see cref="XDocument"/> has a direct
  174. /// child element with the specified <paramref name="expected"/> name.
  175. /// </summary>
  176. /// <param name="expected">
  177. /// The name of the expected child element of the current document's Root <see cref="XDocument.Root"/> element.
  178. /// </param>
  179. /// <param name="reason">
  180. /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
  181. /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
  182. /// </param>
  183. /// <param name="reasonArgs">
  184. /// Zero or more objects to format using the placeholders in <see cref="reason" />.
  185. /// </param>
  186. public AndConstraint<XDocumentAssertions> HaveElement(string expected, string reason, params object[] reasonArgs)
  187. {
  188. Execute.Verification
  189. .ForCondition(Subject.Root != null)
  190. .BecauseOf(reason, reasonArgs)
  191. .FailWith("Expected XML document {0} to have root element with child <" + expected + ">{reason}" +
  192. ", but XML document has no Root element.", Subject);
  193. Execute.Verification
  194. .ForCondition(Subject.Root.Element(expected) != null)
  195. .BecauseOf(reason, reasonArgs)
  196. .FailWith("Expected XML document {0} to have root element with child <" + expected + ">{reason}" +
  197. ", but no such child element was found.", Subject);
  198. return new AndConstraint<XDocumentAssertions>(this);
  199. }
  200. }
  201. }