PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 302 lines | 150 code | 33 blank | 119 comment | 0 complexity | 5c0e8de9c70bd992b11a94f379214970 MD5 | raw file
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Xml.Serialization;
  5. using FluentAssertions.Common;
  6. namespace FluentAssertions.Assertions
  7. {
  8. /// <summary>
  9. /// Contains a number of methods to assert that an <see cref="object"/> is in the expected state.
  10. /// </summary>
  11. [DebuggerNonUserCode]
  12. public class ObjectAssertions : ReferenceTypeAssertions<object, ObjectAssertions>
  13. {
  14. protected internal ObjectAssertions(object value)
  15. {
  16. Subject = value;
  17. }
  18. /// <summary>
  19. /// Asserts that the value of an object equals another object when using it's <see cref="object.Equals(object)" /> method.
  20. /// </summary>
  21. /// <param name="expected">The expected value</param>
  22. public AndConstraint<ObjectAssertions> Be(object expected)
  23. {
  24. return Be(expected, String.Empty);
  25. }
  26. /// <summary>
  27. /// Asserts that an object equals another object using its <see cref="object.Equals(object)" /> implementation.
  28. /// </summary>
  29. /// <param name="expected">The expected value</param>
  30. /// <param name="reason">
  31. /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
  32. /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
  33. /// </param>
  34. /// <param name="reasonArgs">
  35. /// Zero or more objects to format using the placeholders in <see cref="reason" />.
  36. /// </param>
  37. public AndConstraint<ObjectAssertions> Be(object expected, string reason, params object[] reasonArgs)
  38. {
  39. Execute.Verification
  40. .BecauseOf(reason, reasonArgs)
  41. .ForCondition(Subject.IsSameOrEqualTo(expected))
  42. .FailWith("Expected " + Verification.SubjectNameOr("object") + " to be {0}{reason}, but found {1}.", expected,
  43. Subject);
  44. return new AndConstraint<ObjectAssertions>(this);
  45. }
  46. /// <summary>
  47. /// Asserts that an object does not equal another object using it's <see cref="object.Equals(object)" /> method.
  48. /// </summary>
  49. /// <param name="unexpected">The unexpected value</param>
  50. public AndConstraint<ObjectAssertions> NotBe(object unexpected)
  51. {
  52. return NotBe(unexpected, String.Empty);
  53. }
  54. /// <summary>
  55. /// Asserts that an object does not equal another object using it's <see cref="object.Equals(object)" /> method.
  56. /// </summary>
  57. /// <param name="unexpected">The unexpected value</param>
  58. /// <param name="reason">
  59. /// A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
  60. /// start with the word <i>because</i>, it is prepended to the message.
  61. /// </param>
  62. /// <param name="reasonArgs">
  63. /// Zero or more values to use for filling in any <see cref="string.Format(string,object[])" /> compatible placeholders.
  64. /// </param>
  65. public AndConstraint<ObjectAssertions> NotBe(object unexpected, string reason, params object[] reasonArgs)
  66. {
  67. Execute.Verification
  68. .ForCondition(!Subject.IsSameOrEqualTo(unexpected))
  69. .BecauseOf(reason, reasonArgs)
  70. .FailWith("Did not expect object to be equal to {0}{reason}.", unexpected);
  71. return new AndConstraint<ObjectAssertions>(this);
  72. }
  73. /// <summary>
  74. /// Asserts that an object reference refers to the exact same object as another object reference.
  75. /// </summary>
  76. /// <param name="expected">The expected object</param>
  77. public AndConstraint<ObjectAssertions> BeSameAs(object expected)
  78. {
  79. return BeSameAs(expected, String.Empty);
  80. }
  81. /// <summary>
  82. /// Asserts that an object reference refers to the exact same object as another object reference.
  83. /// </summary>
  84. /// <param name="expected">The expected object</param>
  85. /// <param name="reason">
  86. /// A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
  87. /// start with the word <i>because</i>, it is prepended to the message.
  88. /// </param>
  89. /// <param name="reasonArgs">
  90. /// Zero or more values to use for filling in any <see cref="string.Format(string,object[])" /> compatible placeholders.
  91. /// </param>
  92. public AndConstraint<ObjectAssertions> BeSameAs(object expected, string reason, params object[] reasonArgs)
  93. {
  94. Execute.Verification
  95. .UsingLineBreaks
  96. .ForCondition(ReferenceEquals(Subject, expected))
  97. .BecauseOf(reason, reasonArgs)
  98. .FailWith("Expected reference to object {0}{reason}, but found object {1}.", expected, Subject);
  99. return new AndConstraint<ObjectAssertions>(this);
  100. }
  101. /// <summary>
  102. /// Asserts that an object reference refers to a different object than another object reference refers to.
  103. /// </summary>
  104. /// <param name="unexpected">The unexpected object</param>
  105. public AndConstraint<ObjectAssertions> NotBeSameAs(object unexpected)
  106. {
  107. return NotBeSameAs(unexpected, String.Empty);
  108. }
  109. /// <summary>
  110. /// Asserts that an object reference refers to a different object than another object reference refers to.
  111. /// </summary>
  112. /// <param name="unexpected">The unexpected object</param>
  113. /// <param name="reason">
  114. /// A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
  115. /// start with the word <i>because</i>, it is prepended to the message.
  116. /// </param>
  117. /// <param name="reasonArgs">
  118. /// Zero or more values to use for filling in any <see cref="string.Format(string,object[])" /> compatible placeholders.
  119. /// </param>
  120. public AndConstraint<ObjectAssertions> NotBeSameAs(object unexpected, string reason,
  121. params object[] reasonArgs)
  122. {
  123. Execute.Verification
  124. .UsingLineBreaks
  125. .ForCondition(!ReferenceEquals(Subject, unexpected))
  126. .BecauseOf(reason, reasonArgs)
  127. .FailWith("Did not expect reference to object {0}{reason}.", unexpected);
  128. return new AndConstraint<ObjectAssertions>(this);
  129. }
  130. /// <summary>
  131. /// Asserts that the object is <c>null</c>.
  132. /// </summary>
  133. public AndConstraint<ObjectAssertions> BeNull()
  134. {
  135. return BeNull(String.Empty);
  136. }
  137. /// <summary>
  138. /// Asserts that the object is <c>null</c>.
  139. /// </summary>
  140. /// <param name="reason">
  141. /// A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
  142. /// start with the word <i>because</i>, it is prepended to the message.
  143. /// </param>
  144. /// <param name="reasonArgs">
  145. /// Zero or more values to use for filling in any <see cref="string.Format(string,object[])" /> compatible placeholders.
  146. /// </param>
  147. public AndConstraint<ObjectAssertions> BeNull(string reason, params object[] reasonArgs)
  148. {
  149. Execute.Verification
  150. .ForCondition(ReferenceEquals(Subject, null))
  151. .BecauseOf(reason, reasonArgs)
  152. .FailWith("Expected <null>{reason}, but found {0}.", Subject);
  153. return new AndConstraint<ObjectAssertions>(this);
  154. }
  155. /// <summary>
  156. /// Asserts that the object is not <c>null</c>.
  157. /// </summary>
  158. public AndConstraint<ObjectAssertions> NotBeNull()
  159. {
  160. return NotBeNull(String.Empty);
  161. }
  162. /// <summary>
  163. /// Asserts that the object is not <c>null</c>.
  164. /// </summary>
  165. /// <param name="reason">
  166. /// A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
  167. /// start with the word <i>because</i>, it is prepended to the message.
  168. /// </param>
  169. /// <param name="reasonArgs">
  170. /// Zero or more values to use for filling in any <see cref="string.Format(string,object[])" /> compatible placeholders.
  171. /// </param>
  172. public AndConstraint<ObjectAssertions> NotBeNull(string reason, params object[] reasonArgs)
  173. {
  174. Execute.Verification
  175. .ForCondition(!ReferenceEquals(Subject, null))
  176. .BecauseOf(reason, reasonArgs)
  177. .FailWith("Expected non-null value{reason}, but found <null>.");
  178. return new AndConstraint<ObjectAssertions>(this);
  179. }
  180. #if !SILVERLIGHT
  181. /// <summary>
  182. /// Asserts that an object can be serialized and deserialized using the binary serializer and that it stills retains
  183. /// the values of all properties.
  184. /// </summary>
  185. public AndConstraint<ObjectAssertions> BeBinarySerializable()
  186. {
  187. return BeBinarySerializable(string.Empty);
  188. }
  189. /// <summary>
  190. /// Asserts that an object can be serialized and deserialized using the binary serializer and that it stills retains
  191. /// the values of all properties.
  192. /// </summary>
  193. /// <param name="reason">
  194. /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
  195. /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
  196. /// </param>
  197. /// <param name="reasonArgs">
  198. /// Zero or more objects to format using the placeholders in <see cref="reason" />.
  199. /// </param>
  200. public AndConstraint<ObjectAssertions> BeBinarySerializable(string reason, params object[] reasonArgs)
  201. {
  202. try
  203. {
  204. object deserializedObject = CreateCloneUsingBinarySerializer(Subject);
  205. deserializedObject.ShouldHave().AllRuntimeProperties().EqualTo(Subject);
  206. }
  207. catch (Exception exc)
  208. {
  209. Execute.Verification
  210. .BecauseOf(reason, reasonArgs)
  211. .FailWith("Expected {0} to be serializable{reason}, but serialization failed with:\r\n\r\n{1}", Subject,
  212. exc.Message);
  213. }
  214. return new AndConstraint<ObjectAssertions>(this);
  215. }
  216. private static object CreateCloneUsingBinarySerializer(object subject)
  217. {
  218. var stream = new MemoryStream();
  219. var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  220. binaryFormatter.Serialize(stream, subject);
  221. stream.Position = 0;
  222. return binaryFormatter.Deserialize(stream);
  223. }
  224. #endif
  225. /// <summary>
  226. /// Asserts that an object can be serialized and deserialized using the XML serializer and that it stills retains
  227. /// the values of all properties.
  228. /// </summary>
  229. public AndConstraint<ObjectAssertions> BeXmlSerializable()
  230. {
  231. return BeXmlSerializable(string.Empty);
  232. }
  233. /// <summary>
  234. /// Asserts that an object can be serialized and deserialized using the XML serializer and that it stills retains
  235. /// the values of all properties.
  236. /// </summary>
  237. /// <param name="reason">
  238. /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
  239. /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
  240. /// </param>
  241. /// <param name="reasonArgs">
  242. /// Zero or more objects to format using the placeholders in <see cref="reason" />.
  243. /// </param>
  244. public AndConstraint<ObjectAssertions> BeXmlSerializable(string reason, params object[] reasonArgs)
  245. {
  246. try
  247. {
  248. object deserializedObject = CreateCloneUsingXmlSerializer(Subject);
  249. deserializedObject.ShouldHave().AllRuntimeProperties().EqualTo(Subject);
  250. }
  251. catch (Exception exc)
  252. {
  253. Execute.Verification
  254. .BecauseOf(reason, reasonArgs)
  255. .FailWith("Expected {0} to be serializable{reason}, but serialization failed with:\r\n\r\n{1}", Subject,
  256. exc.Message);
  257. }
  258. return new AndConstraint<ObjectAssertions>(this);
  259. }
  260. private static object CreateCloneUsingXmlSerializer(object subject)
  261. {
  262. var stream = new MemoryStream();
  263. var binaryFormatter = new XmlSerializer(subject.GetType());
  264. binaryFormatter.Serialize(stream, subject);
  265. stream.Position = 0;
  266. return binaryFormatter.Deserialize(stream);
  267. }
  268. }
  269. }