PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Releases/1.7.0/FluentAssertions.Specs/TypeAssertionSpecs.cs

#
C# | 406 lines | 190 code | 68 blank | 148 comment | 0 complexity | d5803a9d8aa85168bbf445654018f52d MD5 | raw file
  1. using System;
  2. using FluentAssertions.Assertions;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. namespace FluentAssertions.specs
  5. {
  6. [TestClass]
  7. public class TypeAssertionSpecs
  8. {
  9. #region Be
  10. [TestMethod]
  11. public void When_type_is_equal_to_the_same_type_it_should_succeed()
  12. {
  13. //-------------------------------------------------------------------------------------------------------------------
  14. // Arrange
  15. //-------------------------------------------------------------------------------------------------------------------
  16. Type type = typeof (ClasWithAttribute);
  17. Type sameType = typeof (ClasWithAttribute);
  18. //-------------------------------------------------------------------------------------------------------------------
  19. // Act
  20. //-------------------------------------------------------------------------------------------------------------------
  21. Action act = () =>
  22. type.Should().Be(sameType);
  23. //-------------------------------------------------------------------------------------------------------------------
  24. // Assert
  25. //-------------------------------------------------------------------------------------------------------------------
  26. act.ShouldNotThrow();
  27. }
  28. [TestMethod]
  29. public void When_type_is_equal_to_another_type_it_should_throw()
  30. {
  31. //-------------------------------------------------------------------------------------------------------------------
  32. // Arrange
  33. //-------------------------------------------------------------------------------------------------------------------
  34. Type type = typeof (ClasWithAttribute);
  35. Type differentType = typeof (ClasWithoutAttribute);
  36. //-------------------------------------------------------------------------------------------------------------------
  37. // Act
  38. //-------------------------------------------------------------------------------------------------------------------
  39. Action act = () =>
  40. type.Should().Be(differentType);
  41. //-------------------------------------------------------------------------------------------------------------------
  42. // Assert
  43. //-------------------------------------------------------------------------------------------------------------------
  44. act.ShouldThrow<AssertFailedException>();
  45. }
  46. [TestMethod]
  47. public void When_type_is_equal_to_another_type_it_should_throw_with_descriptive_message()
  48. {
  49. //-------------------------------------------------------------------------------------------------------------------
  50. // Arrange
  51. //-------------------------------------------------------------------------------------------------------------------
  52. Type type = typeof (ClasWithAttribute);
  53. Type differentType = typeof (ClasWithoutAttribute);
  54. //-------------------------------------------------------------------------------------------------------------------
  55. // Act
  56. //-------------------------------------------------------------------------------------------------------------------
  57. Action act = () =>
  58. type.Should().Be(differentType, "because we want to test the error {0}", "message");
  59. //-------------------------------------------------------------------------------------------------------------------
  60. // Assert
  61. //-------------------------------------------------------------------------------------------------------------------
  62. act.ShouldThrow<AssertFailedException>().WithMessage(
  63. "Expected type to be FluentAssertions.specs.ClasWithoutAttribute" +
  64. " because we want to test the error message, but found FluentAssertions.specs.ClasWithAttribute.");
  65. }
  66. [TestMethod]
  67. public void When_type_is_equal_to_same_type_from_different_assembly_it_should_throw_with_assembly_qualified_name()
  68. {
  69. //-------------------------------------------------------------------------------------------------------------------
  70. // Arrange
  71. //-------------------------------------------------------------------------------------------------------------------
  72. #pragma warning disable 436 // disable the warning on conflicting types, as this is the intention for the spec
  73. Type typeFromThisAssembly = typeof(ObjectAssertions);
  74. Type typeFromOtherAssembly = typeof (TypeAssertions).Assembly.GetType("FluentAssertions.Assertions.ObjectAssertions");
  75. #pragma warning restore 436
  76. //-------------------------------------------------------------------------------------------------------------------
  77. // Act
  78. //-------------------------------------------------------------------------------------------------------------------
  79. Action act = () =>
  80. typeFromThisAssembly.Should().Be(typeFromOtherAssembly, "because we want to test the error {0}", "message");
  81. //-------------------------------------------------------------------------------------------------------------------
  82. // Assert
  83. //-------------------------------------------------------------------------------------------------------------------
  84. const string expectedMessage =
  85. "Expected type to be [FluentAssertions.Assertions.ObjectAssertions, FluentAssertions*]" +
  86. " because we want to test the error message, but found " +
  87. "[FluentAssertions.Assertions.ObjectAssertions, FluentAssertions*].";
  88. act.ShouldThrow<AssertFailedException>().WithMessage(expectedMessage, ComparisonMode.Wildcard);
  89. }
  90. [TestMethod]
  91. public void When_type_is_equal_to_the_same_type_using_generics_it_should_succeed()
  92. {
  93. //-------------------------------------------------------------------------------------------------------------------
  94. // Arrange
  95. //-------------------------------------------------------------------------------------------------------------------
  96. Type type = typeof (ClasWithAttribute);
  97. //-------------------------------------------------------------------------------------------------------------------
  98. // Act
  99. //-------------------------------------------------------------------------------------------------------------------
  100. Action act = () =>
  101. type.Should().Be<ClasWithAttribute>();
  102. //-------------------------------------------------------------------------------------------------------------------
  103. // Assert
  104. //-------------------------------------------------------------------------------------------------------------------
  105. act.ShouldNotThrow();
  106. }
  107. [TestMethod]
  108. public void When_type_is_equal_to_another_type_using_generics_it_should_throw()
  109. {
  110. //-------------------------------------------------------------------------------------------------------------------
  111. // Arrange
  112. //-------------------------------------------------------------------------------------------------------------------
  113. Type type = typeof (ClasWithAttribute);
  114. //-------------------------------------------------------------------------------------------------------------------
  115. // Act
  116. //-------------------------------------------------------------------------------------------------------------------
  117. Action act = () =>
  118. type.Should().Be<ClasWithoutAttribute>();
  119. //-------------------------------------------------------------------------------------------------------------------
  120. // Assert
  121. //-------------------------------------------------------------------------------------------------------------------
  122. act.ShouldThrow<AssertFailedException>();
  123. }
  124. [TestMethod]
  125. public void When_type_is_equal_to_another_type_using_generics_it_should_throw_with_descriptive_message()
  126. {
  127. //-------------------------------------------------------------------------------------------------------------------
  128. // Arrange
  129. //-------------------------------------------------------------------------------------------------------------------
  130. Type type = typeof (ClasWithAttribute);
  131. //-------------------------------------------------------------------------------------------------------------------
  132. // Act
  133. //-------------------------------------------------------------------------------------------------------------------
  134. Action act = () =>
  135. type.Should().Be<ClasWithoutAttribute>("because we want to test the error {0}", "message");
  136. //-------------------------------------------------------------------------------------------------------------------
  137. // Assert
  138. //-------------------------------------------------------------------------------------------------------------------
  139. act.ShouldThrow<AssertFailedException>()
  140. .WithMessage("Expected type to be FluentAssertions.specs.ClasWithoutAttribute because we want to test " +
  141. "the error message, but found FluentAssertions.specs.ClasWithAttribute.");
  142. }
  143. #endregion
  144. #region NotBe
  145. [TestMethod]
  146. public void When_type_is_not_equal_to_the_another_type_it_should_succeed()
  147. {
  148. //-------------------------------------------------------------------------------------------------------------------
  149. // Arrange
  150. //-------------------------------------------------------------------------------------------------------------------
  151. Type type = typeof (ClasWithAttribute);
  152. Type otherType = typeof (ClasWithoutAttribute);
  153. //-------------------------------------------------------------------------------------------------------------------
  154. // Act
  155. //-------------------------------------------------------------------------------------------------------------------
  156. Action act = () =>
  157. type.Should().NotBe(otherType);
  158. //-------------------------------------------------------------------------------------------------------------------
  159. // Assert
  160. //-------------------------------------------------------------------------------------------------------------------
  161. act.ShouldNotThrow();
  162. }
  163. [TestMethod]
  164. public void When_type_is_not_equal_to_the_same_type_it_should_throw()
  165. {
  166. //-------------------------------------------------------------------------------------------------------------------
  167. // Arrange
  168. //-------------------------------------------------------------------------------------------------------------------
  169. Type type = typeof (ClasWithAttribute);
  170. Type sameType = typeof (ClasWithAttribute);
  171. //-------------------------------------------------------------------------------------------------------------------
  172. // Act
  173. //-------------------------------------------------------------------------------------------------------------------
  174. Action act = () =>
  175. type.Should().NotBe(sameType);
  176. //-------------------------------------------------------------------------------------------------------------------
  177. // Assert
  178. //-------------------------------------------------------------------------------------------------------------------
  179. act.ShouldThrow<AssertFailedException>();
  180. }
  181. [TestMethod]
  182. public void When_type_is_not_equal_to_the_same_type_it_should_throw_with_descriptive_message()
  183. {
  184. //-------------------------------------------------------------------------------------------------------------------
  185. // Arrange
  186. //-------------------------------------------------------------------------------------------------------------------
  187. Type type = typeof (ClasWithAttribute);
  188. Type sameType = typeof (ClasWithAttribute);
  189. //-------------------------------------------------------------------------------------------------------------------
  190. // Act
  191. //-------------------------------------------------------------------------------------------------------------------
  192. Action act = () =>
  193. type.Should().NotBe(sameType, "because we want to test the error {0}", "message");
  194. //-------------------------------------------------------------------------------------------------------------------
  195. // Assert
  196. //-------------------------------------------------------------------------------------------------------------------
  197. act.ShouldThrow<AssertFailedException>()
  198. .WithMessage("Expected type not to be [FluentAssertions.specs.ClasWithAttribute*]" +
  199. " because we want to test the error message.", ComparisonMode.Wildcard);
  200. }
  201. [TestMethod]
  202. public void When_type_is_not_equal_to_another_type_using_generics_it_should_succeed()
  203. {
  204. //-------------------------------------------------------------------------------------------------------------------
  205. // Arrange
  206. //-------------------------------------------------------------------------------------------------------------------
  207. Type type = typeof (ClasWithAttribute);
  208. //-------------------------------------------------------------------------------------------------------------------
  209. // Act
  210. //-------------------------------------------------------------------------------------------------------------------
  211. Action act = () =>
  212. type.Should().NotBe<ClasWithoutAttribute>();
  213. //-------------------------------------------------------------------------------------------------------------------
  214. // Assert
  215. //-------------------------------------------------------------------------------------------------------------------
  216. act.ShouldNotThrow();
  217. }
  218. [TestMethod]
  219. public void When_type_is_not_equal_to_the_same_type_using_generics_it_should_throw()
  220. {
  221. //-------------------------------------------------------------------------------------------------------------------
  222. // Arrange
  223. //-------------------------------------------------------------------------------------------------------------------
  224. Type type = typeof (ClasWithAttribute);
  225. //-------------------------------------------------------------------------------------------------------------------
  226. // Act
  227. //-------------------------------------------------------------------------------------------------------------------
  228. Action act = () =>
  229. type.Should().NotBe<ClasWithAttribute>();
  230. //-------------------------------------------------------------------------------------------------------------------
  231. // Assert
  232. //-------------------------------------------------------------------------------------------------------------------
  233. act.ShouldThrow<AssertFailedException>();
  234. }
  235. [TestMethod]
  236. public void When_type_is_not_equal_to_the_same_type_using_generics_it_should_throw_with_descriptive_message()
  237. {
  238. //-------------------------------------------------------------------------------------------------------------------
  239. // Arrange
  240. //-------------------------------------------------------------------------------------------------------------------
  241. Type type = typeof (ClasWithAttribute);
  242. //-------------------------------------------------------------------------------------------------------------------
  243. // Act
  244. //-------------------------------------------------------------------------------------------------------------------
  245. Action act = () =>
  246. type.Should().NotBe<ClasWithAttribute>("because we want to test the error {0}", "message");
  247. //-------------------------------------------------------------------------------------------------------------------
  248. // Assert
  249. //-------------------------------------------------------------------------------------------------------------------
  250. act.ShouldThrow<AssertFailedException>()
  251. .WithMessage("Expected type not to be [FluentAssertions.specs.ClasWithAttribute*] because we want to test " +
  252. "the error message.", ComparisonMode.Wildcard);
  253. }
  254. #endregion
  255. #region BeDecoratedWith
  256. [TestMethod]
  257. public void When_type_is_decorated_with_a_specific_attribute_and_it_is_it_should_succeed()
  258. {
  259. //-------------------------------------------------------------------------------------------------------------------
  260. // Arrange
  261. //-------------------------------------------------------------------------------------------------------------------
  262. Type typeWithAttribute = typeof (ClasWithAttribute);
  263. //-------------------------------------------------------------------------------------------------------------------
  264. // Act
  265. //-------------------------------------------------------------------------------------------------------------------
  266. Action act = () =>
  267. typeWithAttribute.Should().BeDecoratedWith<DummyClassAttribute>();
  268. //-------------------------------------------------------------------------------------------------------------------
  269. // Assert
  270. //-------------------------------------------------------------------------------------------------------------------
  271. act.ShouldNotThrow();
  272. }
  273. [TestMethod]
  274. public void When_type_is_decorated_with_an_attribute_and_it_is_not_it_should_throw()
  275. {
  276. //-------------------------------------------------------------------------------------------------------------------
  277. // Arrange
  278. //-------------------------------------------------------------------------------------------------------------------
  279. Type typeWithoutAttribute = typeof (ClasWithoutAttribute);
  280. //-------------------------------------------------------------------------------------------------------------------
  281. // Act
  282. //-------------------------------------------------------------------------------------------------------------------
  283. Action act = () =>
  284. typeWithoutAttribute.Should().BeDecoratedWith<DummyClassAttribute>();
  285. //-------------------------------------------------------------------------------------------------------------------
  286. // Assert
  287. //-------------------------------------------------------------------------------------------------------------------
  288. act.ShouldThrow<AssertFailedException>();
  289. }
  290. [TestMethod]
  291. public void When_type_is_decorated_with_an_attribute_and_it_is_not_it_should_throw_with_descriptive_message()
  292. {
  293. //-------------------------------------------------------------------------------------------------------------------
  294. // Arrange
  295. //-------------------------------------------------------------------------------------------------------------------
  296. Type typeWithoutAttribute = typeof (ClasWithoutAttribute);
  297. //-------------------------------------------------------------------------------------------------------------------
  298. // Act
  299. //-------------------------------------------------------------------------------------------------------------------
  300. Action act = () =>
  301. typeWithoutAttribute.Should().BeDecoratedWith<DummyClassAttribute>("because we want to test the error {0}",
  302. "message");
  303. //-------------------------------------------------------------------------------------------------------------------
  304. // Assert
  305. //-------------------------------------------------------------------------------------------------------------------
  306. act.ShouldThrow<AssertFailedException>()
  307. .WithMessage("Expected type FluentAssertions.specs.ClasWithoutAttribute to be decorated with " +
  308. "FluentAssertions.specs.DummyClassAttribute because we want to test the error message, but the attribute " +
  309. "was not found.");
  310. }
  311. #endregion
  312. }
  313. #region Internal classes used in unit tests
  314. [DummyClass]
  315. public class ClasWithAttribute
  316. {
  317. }
  318. public class ClasWithoutAttribute
  319. {
  320. }
  321. [AttributeUsage(AttributeTargets.Class)]
  322. public class DummyClassAttribute : Attribute
  323. {
  324. }
  325. #endregion
  326. }
  327. namespace FluentAssertions.Assertions
  328. {
  329. #pragma warning disable 436 // disable the warning on conflicting types, as this is the intention for the spec
  330. /// <summary>
  331. /// A class that intentianalty has the exact same name and namespace as the ObjectAssertions from the FluentAssertions
  332. /// assembly. This class is used to test the behavior of comparisons on such types.
  333. /// </summary>
  334. internal class ObjectAssertions
  335. {
  336. }
  337. #pragma warning restore 436
  338. }