PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/FluentAssertions.Specs/MethodInfoSelectorSpecs.cs

#
C# | 189 lines | 105 code | 30 blank | 54 comment | 2 complexity | f31e4ad9550af912a3a144f9c7bf585c MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using FluentAssertions.Assertions;
  5. using Internal.Main.Test;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. namespace FluentAssertions.specs
  8. {
  9. [TestClass]
  10. public class MethodInfoSelectorSpecs
  11. {
  12. [TestMethod]
  13. public void When_selecting_methods_from_types_in_an_assembly_it_should_return_the_applicable_methods()
  14. {
  15. //-------------------------------------------------------------------------------------------------------------------
  16. // Arrange
  17. //-------------------------------------------------------------------------------------------------------------------
  18. #if !WINRT
  19. Assembly assembly = typeof(ClassWithSomeAttribute).Assembly;
  20. #else
  21. Assembly assembly = typeof(ClassWithSomeAttribute).GetTypeInfo().Assembly;
  22. #endif
  23. //-------------------------------------------------------------------------------------------------------------------
  24. // Act
  25. //-------------------------------------------------------------------------------------------------------------------
  26. IEnumerable<MethodInfo> methods = assembly.Types()
  27. .ThatAreDecoratedWith<SomeAttribute>()
  28. .Methods();
  29. //-------------------------------------------------------------------------------------------------------------------
  30. // Assert
  31. //-------------------------------------------------------------------------------------------------------------------
  32. methods.Should()
  33. .HaveCount(2)
  34. .And.Contain(m => m.Name == "Method1")
  35. .And.Contain(m => m.Name == "Method2");
  36. }
  37. [TestMethod]
  38. public void When_selecting_methods_that_are_public_or_internal_it_should_return_only_the_applicable_methods()
  39. {
  40. //-------------------------------------------------------------------------------------------------------------------
  41. // Arrange
  42. //-------------------------------------------------------------------------------------------------------------------
  43. Type type = typeof (TestClassForMethodSelector);
  44. //-------------------------------------------------------------------------------------------------------------------
  45. // Act
  46. //-------------------------------------------------------------------------------------------------------------------
  47. IEnumerable<MethodInfo> methods = type.Methods().ThatArePublicOrInternal;
  48. //-------------------------------------------------------------------------------------------------------------------
  49. // Assert
  50. //-------------------------------------------------------------------------------------------------------------------
  51. const int PublicMethodCount = 2;
  52. const int InternalMethodCount = 1;
  53. methods.Should().HaveCount(PublicMethodCount + InternalMethodCount);
  54. }
  55. [TestMethod]
  56. public void When_selecting_methods_decorated_with_specific_attribute_it_should_return_only_the_applicable_methods()
  57. {
  58. //-------------------------------------------------------------------------------------------------------------------
  59. // Arrange
  60. //-------------------------------------------------------------------------------------------------------------------
  61. Type type = typeof (TestClassForMethodSelector);
  62. //-------------------------------------------------------------------------------------------------------------------
  63. // Act
  64. //-------------------------------------------------------------------------------------------------------------------
  65. IEnumerable<MethodInfo> methods = type.Methods().ThatAreDecoratedWith<DummyMethodAttribute>().ToArray();
  66. //-------------------------------------------------------------------------------------------------------------------
  67. // Assert
  68. //-------------------------------------------------------------------------------------------------------------------
  69. methods.Should().HaveCount(2);
  70. }
  71. [TestMethod]
  72. public void When_selecting_methods_that_return_a_specific_type_it_should_return_only_the_applicable_methods()
  73. {
  74. //-------------------------------------------------------------------------------------------------------------------
  75. // Arrange
  76. //-------------------------------------------------------------------------------------------------------------------
  77. Type type = typeof (TestClassForMethodSelector);
  78. //-------------------------------------------------------------------------------------------------------------------
  79. // Act
  80. //-------------------------------------------------------------------------------------------------------------------
  81. IEnumerable<MethodInfo> methods = type.Methods().ThatReturn<string>().ToArray();
  82. //-------------------------------------------------------------------------------------------------------------------
  83. // Assert
  84. //-------------------------------------------------------------------------------------------------------------------
  85. methods.Should().HaveCount(2);
  86. }
  87. [TestMethod]
  88. public void When_selecting_methods_without_return_value_it_should_return_only_the_applicable_methods()
  89. {
  90. //-------------------------------------------------------------------------------------------------------------------
  91. // Arrange
  92. //-------------------------------------------------------------------------------------------------------------------
  93. Type type = typeof (TestClassForMethodSelector);
  94. //-------------------------------------------------------------------------------------------------------------------
  95. // Act
  96. //-------------------------------------------------------------------------------------------------------------------
  97. IEnumerable<MethodInfo> methods = type.Methods().ThatReturnVoid.ToArray();
  98. //-------------------------------------------------------------------------------------------------------------------
  99. // Assert
  100. //-------------------------------------------------------------------------------------------------------------------
  101. methods.Should().HaveCount(4);
  102. }
  103. [TestMethod]
  104. public void When_combining_filters_to_filter_methods_it_should_return_only_the_applicable_methods()
  105. {
  106. //-------------------------------------------------------------------------------------------------------------------
  107. // Arrange
  108. //-------------------------------------------------------------------------------------------------------------------
  109. Type type = typeof (TestClassForMethodSelector);
  110. //-------------------------------------------------------------------------------------------------------------------
  111. // Act
  112. //-------------------------------------------------------------------------------------------------------------------
  113. IEnumerable<MethodInfo> methods = type.Methods()
  114. .ThatArePublicOrInternal
  115. .ThatReturnVoid
  116. .ToArray();
  117. //-------------------------------------------------------------------------------------------------------------------
  118. // Assert
  119. //-------------------------------------------------------------------------------------------------------------------
  120. methods.Should().HaveCount(2);
  121. }
  122. }
  123. #region Internal classes used in unit tests
  124. internal class TestClassForMethodSelector
  125. {
  126. public event EventHandler SomethingChanged = delegate { };
  127. public virtual void PublicVirtualVoidMethod()
  128. {
  129. }
  130. [DummyMethod]
  131. public virtual void PublicVirtualVoidMethodWithAttribute()
  132. {
  133. }
  134. internal virtual int InternalVirtualIntMethod()
  135. {
  136. return 0;
  137. }
  138. [DummyMethod]
  139. protected virtual void ProtectedVirtualVoidMethodWithAttribute()
  140. {
  141. }
  142. private void PrivateVoidDoNothing()
  143. {
  144. }
  145. protected virtual string ProtectedVirtualStringMethod()
  146. {
  147. return "";
  148. }
  149. private string PrivateStringMethod()
  150. {
  151. return "";
  152. }
  153. }
  154. [AttributeUsage(AttributeTargets.Method)]
  155. public class DummyMethodAttribute : Attribute
  156. {
  157. }
  158. #endregion
  159. }