/Main/FluentAssertions.Specs/MethodInfoAssertionSpecs.cs
C# | 217 lines | 128 code | 35 blank | 54 comment | 0 complexity | 9bc7a50aa8b5b0bf000ed14f52cdf95c MD5 | raw file
- using System;
-
- using FluentAssertions.Assertions;
-
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- namespace FluentAssertions.specs
- {
- [TestClass]
- public class MethodInfoAssertionSpecs
- {
- [TestMethod]
- public void When_asserting_methods_are_virtual_and_they_are_it_should_succeed()
- {
- //-------------------------------------------------------------------------------------------------------------------
- // Arrange
- //-------------------------------------------------------------------------------------------------------------------
- var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsVirtual));
-
- //-------------------------------------------------------------------------------------------------------------------
- // Act
- //-------------------------------------------------------------------------------------------------------------------
- Action act = () =>
- methodSelector.Should().BeVirtual();
-
- //-------------------------------------------------------------------------------------------------------------------
- // Assert
- //-------------------------------------------------------------------------------------------------------------------
- act.ShouldNotThrow();
- }
-
- [TestMethod]
- public void When_asserting_methods_are_virtual_but_non_virtual_methods_are_found_it_should_throw()
- {
- //-------------------------------------------------------------------------------------------------------------------
- // Arrange
- //-------------------------------------------------------------------------------------------------------------------
- var methodSelector = new MethodInfoSelector(typeof(ClassWithNonVirtualPublicMethods));
-
- //-------------------------------------------------------------------------------------------------------------------
- // Act
- //-------------------------------------------------------------------------------------------------------------------
- Action act = () =>
- methodSelector.Should().BeVirtual();
-
- //-------------------------------------------------------------------------------------------------------------------
- // Assert
- //-------------------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>();
- }
-
- [TestMethod]
- public void When_asserting_methods_are_virtual_but_non_virtual_methods_are_found_it_should_throw_with_descriptive_message()
- {
- //-------------------------------------------------------------------------------------------------------------------
- // Arrange
- //-------------------------------------------------------------------------------------------------------------------
- var methodSelector = new MethodInfoSelector(typeof(ClassWithNonVirtualPublicMethods));
-
- //-------------------------------------------------------------------------------------------------------------------
- // Act
- //-------------------------------------------------------------------------------------------------------------------
- Action act = () =>
- methodSelector.Should().BeVirtual("we want to test the error {0}", "message");
-
- //-------------------------------------------------------------------------------------------------------------------
- // Assert
- //-------------------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>()
- .WithMessage("Expected all selected methods" +
- " to be virtual because we want to test the error message," +
- " but the following methods are not virtual:\r\n" +
- "Void FluentAssertions.specs.ClassWithNonVirtualPublicMethods.PublicDoNothing\r\n" +
- "Void FluentAssertions.specs.ClassWithNonVirtualPublicMethods.InternalDoNothing\r\n" +
- "Void FluentAssertions.specs.ClassWithNonVirtualPublicMethods.ProtectedDoNothing");
- }
-
- [TestMethod]
- public void When_asserting_methods_are_decorated_with_attribute_and_it_is_it_should_succeed()
- {
- //-------------------------------------------------------------------------------------------------------------------
- // Arrange
- //-------------------------------------------------------------------------------------------------------------------
- var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsDecoratedWithDummyAttribute));
-
- //-------------------------------------------------------------------------------------------------------------------
- // Act
- //-------------------------------------------------------------------------------------------------------------------
- Action act = () =>
- methodSelector.Should().BeDecoratedWith<DummyMethodAttribute>();
-
- //-------------------------------------------------------------------------------------------------------------------
- // Assert
- //-------------------------------------------------------------------------------------------------------------------
- act.ShouldNotThrow();
- }
-
- [TestMethod]
- public void When_asserting_methods_are_decorated_with_attribute_and_they_are_not_it_should_throw()
- {
- //-------------------------------------------------------------------------------------------------------------------
- // Arrange
- //-------------------------------------------------------------------------------------------------------------------
- var methodSelector = new MethodInfoSelector(typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute))
- .ThatArePublicOrInternal;
-
- //-------------------------------------------------------------------------------------------------------------------
- // Act
- //-------------------------------------------------------------------------------------------------------------------
- Action act = () =>
- methodSelector.Should().BeDecoratedWith<DummyMethodAttribute>();
-
- //-------------------------------------------------------------------------------------------------------------------
- // Assert
- //-------------------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>();
- }
-
- [TestMethod]
- public void When_asserting_methods_are_decorated_with_attribute_and_they_are_not_it_should_throw_with_descriptive_message()
- {
- //-------------------------------------------------------------------------------------------------------------------
- // Arrange
- //-------------------------------------------------------------------------------------------------------------------
- var methodSelector = new MethodInfoSelector(typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute));
-
- //-------------------------------------------------------------------------------------------------------------------
- // Act
- //-------------------------------------------------------------------------------------------------------------------
- Action act = () =>
- methodSelector.Should().BeDecoratedWith<DummyMethodAttribute>("because we want to test the error {0}", "message");
-
- //-------------------------------------------------------------------------------------------------------------------
- // Assert
- //-------------------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>()
- .WithMessage("Expected all selected methods to be decorated with" +
- " FluentAssertions.specs.DummyMethodAttribute because we want to test the error message," +
- " but the following methods are not:\r\n" +
- "Void FluentAssertions.specs.ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.PublicDoNothing\r\n" +
- "Void FluentAssertions.specs.ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.ProtectedDoNothing\r\n" +
- "Void FluentAssertions.specs.ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.PrivateDoNothing");
- }
- }
-
- #region Internal classes used in unit tests
-
- internal class ClassWithAllMethodsVirtual
- {
- public virtual void PublicVirtualDoNothing()
- {
- }
-
- internal virtual void InternalVirtualDoNothing()
- {
- }
-
- protected virtual void ProtectedVirtualDoNothing()
- {
- }
- }
-
- internal interface IInterfaceWithPublicMethod
- {
- void PublicDoNothing();
- }
-
- internal class ClassWithNonVirtualPublicMethods : IInterfaceWithPublicMethod
- {
- public void PublicDoNothing()
- {
- }
-
- internal void InternalDoNothing()
- {
- }
-
- protected void ProtectedDoNothing()
- {
- }
- }
-
- internal class ClassWithAllMethodsDecoratedWithDummyAttribute
- {
- [DummyMethod]
- public void PublicDoNothing()
- {
- }
-
- [DummyMethod]
- protected void ProtectedDoNothing()
- {
- }
-
- [DummyMethod]
- private void PrivateDoNothing()
- {
- }
- }
-
- internal class ClassWithMethodsThatAreNotDecoratedWithDummyAttribute
- {
- public void PublicDoNothing()
- {
- }
-
- protected void ProtectedDoNothing()
- {
- }
-
- private void PrivateDoNothing()
- {
- }
- }
-
- #endregion
- }