From c7413184b96a89cec3d1457917e238be9e9c09b6 Mon Sep 17 00:00:00 2001 From: Michal Kowal Date: Wed, 2 May 2018 11:05:45 +0200 Subject: [PATCH] Add GreedyMockAttribute (#766) --- Src/AutoMoq/GreedyMockAttribute.cs | 30 +++++++++++++ Src/AutoMoq/MockCustomizeAttribute.cs | 19 ++++++++ .../DelegatingMockCustomization.cs | 19 ++++++++ .../DelegatingMockCustomizeAttribute.cs | 20 +++++++++ .../GreedyMockAttributeTest.cs | 45 +++++++++++++++++++ .../MockCustomizeAttributeTest.cs | 38 ++++++++++++++++ 6 files changed, 171 insertions(+) create mode 100644 Src/AutoMoq/GreedyMockAttribute.cs create mode 100644 Src/AutoMoq/MockCustomizeAttribute.cs create mode 100644 Src/AutoMoqUnitTest/DelegatingMockCustomization.cs create mode 100644 Src/AutoMoqUnitTest/DelegatingMockCustomizeAttribute.cs create mode 100644 Src/AutoMoqUnitTest/GreedyMockAttributeTest.cs create mode 100644 Src/AutoMoqUnitTest/MockCustomizeAttributeTest.cs diff --git a/Src/AutoMoq/GreedyMockAttribute.cs b/Src/AutoMoq/GreedyMockAttribute.cs new file mode 100644 index 000000000..f380b1a33 --- /dev/null +++ b/Src/AutoMoq/GreedyMockAttribute.cs @@ -0,0 +1,30 @@ +using System; +using System.Reflection; + +namespace AutoFixture.AutoMoq +{ + /// + /// An attribute that can be applied to parameters in an AutoDataAttribute-driven + /// Theory to indicate that the parameter value should be created using the most greedy + /// constructor that can be satisfied by an . + /// + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] + public sealed class GreedyMockAttribute : MockCustomizeAttribute + { + /// + /// Gets a customization that associates a with the + /// of the parameter. + /// + /// The parameter for which the customization is requested. + /// + /// A customization that associates a with the + /// of the parameter. + /// + public override ICustomization GetCustomization(ParameterInfo parameter) + { + if (parameter == null) throw new ArgumentNullException(nameof(parameter)); + + return new ConstructorCustomization(parameter.ParameterType, new GreedyMockConstructorQuery()); + } + } +} diff --git a/Src/AutoMoq/MockCustomizeAttribute.cs b/Src/AutoMoq/MockCustomizeAttribute.cs new file mode 100644 index 000000000..c470a003d --- /dev/null +++ b/Src/AutoMoq/MockCustomizeAttribute.cs @@ -0,0 +1,19 @@ +using System; +using System.Reflection; + +namespace AutoFixture.AutoMoq +{ + /// + /// Base Mock class for customizing parameters in methods decorated with AutoDataAttribute + /// + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true)] + public abstract class MockCustomizeAttribute : Attribute, IParameterCustomizationSource + { + /// + /// Gets a customization for a parameter. + /// + /// The parameter for which the customization is requested. + /// + public abstract ICustomization GetCustomization(ParameterInfo parameter); + } +} diff --git a/Src/AutoMoqUnitTest/DelegatingMockCustomization.cs b/Src/AutoMoqUnitTest/DelegatingMockCustomization.cs new file mode 100644 index 000000000..6b296a2cd --- /dev/null +++ b/Src/AutoMoqUnitTest/DelegatingMockCustomization.cs @@ -0,0 +1,19 @@ +using System; + +namespace AutoFixture.AutoMoq.UnitTest +{ + internal class DelegatingMockCustomization : ICustomization + { + internal DelegatingMockCustomization() + { + this.OnCustomize = f => { }; + } + + public void Customize(IFixture fixture) + { + this.OnCustomize(fixture); + } + + internal Action OnCustomize { get; set; } + } +} diff --git a/Src/AutoMoqUnitTest/DelegatingMockCustomizeAttribute.cs b/Src/AutoMoqUnitTest/DelegatingMockCustomizeAttribute.cs new file mode 100644 index 000000000..9579fec10 --- /dev/null +++ b/Src/AutoMoqUnitTest/DelegatingMockCustomizeAttribute.cs @@ -0,0 +1,20 @@ +using System; +using System.Reflection; + +namespace AutoFixture.AutoMoq.UnitTest +{ + internal class DelegatingMockCustomizeAttribute : MockCustomizeAttribute + { + internal DelegatingMockCustomizeAttribute() + { + this.OnGetCustomization = p => new DelegatingMockCustomization(); + } + + public override ICustomization GetCustomization(ParameterInfo parameter) + { + return this.OnGetCustomization(parameter); + } + + internal Func OnGetCustomization { get; set; } + } +} diff --git a/Src/AutoMoqUnitTest/GreedyMockAttributeTest.cs b/Src/AutoMoqUnitTest/GreedyMockAttributeTest.cs new file mode 100644 index 000000000..827cc89a2 --- /dev/null +++ b/Src/AutoMoqUnitTest/GreedyMockAttributeTest.cs @@ -0,0 +1,45 @@ +using System; +using System.Linq; +using System.Reflection; +using TestTypeFoundation; +using Xunit; + +namespace AutoFixture.AutoMoq.UnitTest +{ + public class GreedyMockAttributeTest + { + [Fact] + public void SutIsAttribute() + { + // Arrange + // Act + var sut = new GreedyMockAttribute(); + // Assert + Assert.IsAssignableFrom(sut); + } + + [Fact] + public void GetCustomizationFromNullParamterThrows() + { + // Arrange + var sut = new GreedyMockAttribute(); + // Act & assert + Assert.Throws(() => + sut.GetCustomization(null)); + } + + [Fact] + public void GetCustomizationReturnsCorrectResult() + { + // Arrange + var sut = new GreedyMockAttribute(); + var parameter = typeof(TypeWithOverloadedMembers).GetMethod("DoSomething", new[] { typeof(object) }).GetParameters().Single(); + // Act + var result = sut.GetCustomization(parameter); + // Assert + var invoker = Assert.IsAssignableFrom(result); + Assert.Equal(parameter.ParameterType, invoker.TargetType); + Assert.IsAssignableFrom(invoker.Query); + } + } +} diff --git a/Src/AutoMoqUnitTest/MockCustomizeAttributeTest.cs b/Src/AutoMoqUnitTest/MockCustomizeAttributeTest.cs new file mode 100644 index 000000000..c6982459f --- /dev/null +++ b/Src/AutoMoqUnitTest/MockCustomizeAttributeTest.cs @@ -0,0 +1,38 @@ +using System; +using Xunit; + +namespace AutoFixture.AutoMoq.UnitTest +{ + public class MockCustomizeAttributeTest + { + [Fact] + public void TestableSutIsSut() + { + // Arrange + // Act + var sut = new DelegatingMockCustomizeAttribute(); + // Assert + Assert.IsAssignableFrom(sut); + } + + [Fact] + public void SutIsAttribute() + { + // Arrange + // Act + var sut = new DelegatingMockCustomizeAttribute(); + // Assert + Assert.IsAssignableFrom(sut); + } + + [Fact] + public void SutImplementsIParameterCustomizationSource() + { + // Arrange + // Act + var sut = new DelegatingMockCustomizeAttribute(); + // Assert + Assert.IsAssignableFrom(sut); + } + } +}