PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/FluentAssertions.Specs/TypeAssertionSpecs.cs

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