PageRenderTime 48ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/FluentAssertions.Specs/MethodInfoAssertionSpecs.cs

#
C# | 217 lines | 128 code | 35 blank | 54 comment | 0 complexity | 9bc7a50aa8b5b0bf000ed14f52cdf95c 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 MethodInfoAssertionSpecs
  8. {
  9. [TestMethod]
  10. public void When_asserting_methods_are_virtual_and_they_are_it_should_succeed()
  11. {
  12. //-------------------------------------------------------------------------------------------------------------------
  13. // Arrange
  14. //-------------------------------------------------------------------------------------------------------------------
  15. var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsVirtual));
  16. //-------------------------------------------------------------------------------------------------------------------
  17. // Act
  18. //-------------------------------------------------------------------------------------------------------------------
  19. Action act = () =>
  20. methodSelector.Should().BeVirtual();
  21. //-------------------------------------------------------------------------------------------------------------------
  22. // Assert
  23. //-------------------------------------------------------------------------------------------------------------------
  24. act.ShouldNotThrow();
  25. }
  26. [TestMethod]
  27. public void When_asserting_methods_are_virtual_but_non_virtual_methods_are_found_it_should_throw()
  28. {
  29. //-------------------------------------------------------------------------------------------------------------------
  30. // Arrange
  31. //-------------------------------------------------------------------------------------------------------------------
  32. var methodSelector = new MethodInfoSelector(typeof(ClassWithNonVirtualPublicMethods));
  33. //-------------------------------------------------------------------------------------------------------------------
  34. // Act
  35. //-------------------------------------------------------------------------------------------------------------------
  36. Action act = () =>
  37. methodSelector.Should().BeVirtual();
  38. //-------------------------------------------------------------------------------------------------------------------
  39. // Assert
  40. //-------------------------------------------------------------------------------------------------------------------
  41. act.ShouldThrow<AssertFailedException>();
  42. }
  43. [TestMethod]
  44. public void When_asserting_methods_are_virtual_but_non_virtual_methods_are_found_it_should_throw_with_descriptive_message()
  45. {
  46. //-------------------------------------------------------------------------------------------------------------------
  47. // Arrange
  48. //-------------------------------------------------------------------------------------------------------------------
  49. var methodSelector = new MethodInfoSelector(typeof(ClassWithNonVirtualPublicMethods));
  50. //-------------------------------------------------------------------------------------------------------------------
  51. // Act
  52. //-------------------------------------------------------------------------------------------------------------------
  53. Action act = () =>
  54. methodSelector.Should().BeVirtual("we want to test the error {0}", "message");
  55. //-------------------------------------------------------------------------------------------------------------------
  56. // Assert
  57. //-------------------------------------------------------------------------------------------------------------------
  58. act.ShouldThrow<AssertFailedException>()
  59. .WithMessage("Expected all selected methods" +
  60. " to be virtual because we want to test the error message," +
  61. " but the following methods are not virtual:\r\n" +
  62. "Void FluentAssertions.specs.ClassWithNonVirtualPublicMethods.PublicDoNothing\r\n" +
  63. "Void FluentAssertions.specs.ClassWithNonVirtualPublicMethods.InternalDoNothing\r\n" +
  64. "Void FluentAssertions.specs.ClassWithNonVirtualPublicMethods.ProtectedDoNothing");
  65. }
  66. [TestMethod]
  67. public void When_asserting_methods_are_decorated_with_attribute_and_it_is_it_should_succeed()
  68. {
  69. //-------------------------------------------------------------------------------------------------------------------
  70. // Arrange
  71. //-------------------------------------------------------------------------------------------------------------------
  72. var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsDecoratedWithDummyAttribute));
  73. //-------------------------------------------------------------------------------------------------------------------
  74. // Act
  75. //-------------------------------------------------------------------------------------------------------------------
  76. Action act = () =>
  77. methodSelector.Should().BeDecoratedWith<DummyMethodAttribute>();
  78. //-------------------------------------------------------------------------------------------------------------------
  79. // Assert
  80. //-------------------------------------------------------------------------------------------------------------------
  81. act.ShouldNotThrow();
  82. }
  83. [TestMethod]
  84. public void When_asserting_methods_are_decorated_with_attribute_and_they_are_not_it_should_throw()
  85. {
  86. //-------------------------------------------------------------------------------------------------------------------
  87. // Arrange
  88. //-------------------------------------------------------------------------------------------------------------------
  89. var methodSelector = new MethodInfoSelector(typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute))
  90. .ThatArePublicOrInternal;
  91. //-------------------------------------------------------------------------------------------------------------------
  92. // Act
  93. //-------------------------------------------------------------------------------------------------------------------
  94. Action act = () =>
  95. methodSelector.Should().BeDecoratedWith<DummyMethodAttribute>();
  96. //-------------------------------------------------------------------------------------------------------------------
  97. // Assert
  98. //-------------------------------------------------------------------------------------------------------------------
  99. act.ShouldThrow<AssertFailedException>();
  100. }
  101. [TestMethod]
  102. public void When_asserting_methods_are_decorated_with_attribute_and_they_are_not_it_should_throw_with_descriptive_message()
  103. {
  104. //-------------------------------------------------------------------------------------------------------------------
  105. // Arrange
  106. //-------------------------------------------------------------------------------------------------------------------
  107. var methodSelector = new MethodInfoSelector(typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute));
  108. //-------------------------------------------------------------------------------------------------------------------
  109. // Act
  110. //-------------------------------------------------------------------------------------------------------------------
  111. Action act = () =>
  112. methodSelector.Should().BeDecoratedWith<DummyMethodAttribute>("because we want to test the error {0}", "message");
  113. //-------------------------------------------------------------------------------------------------------------------
  114. // Assert
  115. //-------------------------------------------------------------------------------------------------------------------
  116. act.ShouldThrow<AssertFailedException>()
  117. .WithMessage("Expected all selected methods to be decorated with" +
  118. " FluentAssertions.specs.DummyMethodAttribute because we want to test the error message," +
  119. " but the following methods are not:\r\n" +
  120. "Void FluentAssertions.specs.ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.PublicDoNothing\r\n" +
  121. "Void FluentAssertions.specs.ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.ProtectedDoNothing\r\n" +
  122. "Void FluentAssertions.specs.ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.PrivateDoNothing");
  123. }
  124. }
  125. #region Internal classes used in unit tests
  126. internal class ClassWithAllMethodsVirtual
  127. {
  128. public virtual void PublicVirtualDoNothing()
  129. {
  130. }
  131. internal virtual void InternalVirtualDoNothing()
  132. {
  133. }
  134. protected virtual void ProtectedVirtualDoNothing()
  135. {
  136. }
  137. }
  138. internal interface IInterfaceWithPublicMethod
  139. {
  140. void PublicDoNothing();
  141. }
  142. internal class ClassWithNonVirtualPublicMethods : IInterfaceWithPublicMethod
  143. {
  144. public void PublicDoNothing()
  145. {
  146. }
  147. internal void InternalDoNothing()
  148. {
  149. }
  150. protected void ProtectedDoNothing()
  151. {
  152. }
  153. }
  154. internal class ClassWithAllMethodsDecoratedWithDummyAttribute
  155. {
  156. [DummyMethod]
  157. public void PublicDoNothing()
  158. {
  159. }
  160. [DummyMethod]
  161. protected void ProtectedDoNothing()
  162. {
  163. }
  164. [DummyMethod]
  165. private void PrivateDoNothing()
  166. {
  167. }
  168. }
  169. internal class ClassWithMethodsThatAreNotDecoratedWithDummyAttribute
  170. {
  171. public void PublicDoNothing()
  172. {
  173. }
  174. protected void ProtectedDoNothing()
  175. {
  176. }
  177. private void PrivateDoNothing()
  178. {
  179. }
  180. }
  181. #endregion
  182. }