Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add GreedyMockAttribute #1033

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions Src/AutoMoq/GreedyMockAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Reflection;

namespace AutoFixture.AutoMoq
{
/// <summary>
/// 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 <see cref="IFixture"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class GreedyMockAttribute : MockCustomizeAttribute
{
/// <summary>
/// Gets a customization that associates a <see cref="GreedyMockConstructorQuery"/> with the
/// <see cref="Type"/> of the parameter.
/// </summary>
/// <param name="parameter">The parameter for which the customization is requested.</param>
/// <returns>
/// A customization that associates a <see cref="GreedyMockConstructorQuery"/> with the
/// <see cref="Type"/> of the parameter.
/// </returns>
public override ICustomization GetCustomization(ParameterInfo parameter)
{
if (parameter == null) throw new ArgumentNullException(nameof(parameter));

return new ConstructorCustomization(parameter.ParameterType, new GreedyMockConstructorQuery());
}
}
}
19 changes: 19 additions & 0 deletions Src/AutoMoq/MockCustomizeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Reflection;

namespace AutoFixture.AutoMoq
{
/// <summary>
/// Base Mock class for customizing parameters in methods decorated with AutoDataAttribute
/// </summary>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true)]
public abstract class MockCustomizeAttribute : Attribute, IParameterCustomizationSource
{
/// <summary>
/// Gets a customization for a parameter.
/// </summary>
/// <param name="parameter">The parameter for which the customization is requested.</param>
/// <returns></returns>
public abstract ICustomization GetCustomization(ParameterInfo parameter);
}
}
19 changes: 19 additions & 0 deletions Src/AutoMoqUnitTest/DelegatingMockCustomization.cs
Original file line number Diff line number Diff line change
@@ -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<IFixture> OnCustomize { get; set; }
}
}
20 changes: 20 additions & 0 deletions Src/AutoMoqUnitTest/DelegatingMockCustomizeAttribute.cs
Original file line number Diff line number Diff line change
@@ -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<ParameterInfo, ICustomization> OnGetCustomization { get; set; }
}
}
45 changes: 45 additions & 0 deletions Src/AutoMoqUnitTest/GreedyMockAttributeTest.cs
Original file line number Diff line number Diff line change
@@ -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<MockCustomizeAttribute>(sut);
}

[Fact]
public void GetCustomizationFromNullParamterThrows()
{
// Arrange
var sut = new GreedyMockAttribute();
// Act & assert
Assert.Throws<ArgumentNullException>(() =>
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<ConstructorCustomization>(result);
Assert.Equal(parameter.ParameterType, invoker.TargetType);
Assert.IsAssignableFrom<GreedyMockConstructorQuery>(invoker.Query);
}
}
}
38 changes: 38 additions & 0 deletions Src/AutoMoqUnitTest/MockCustomizeAttributeTest.cs
Original file line number Diff line number Diff line change
@@ -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<MockCustomizeAttribute>(sut);
}

[Fact]
public void SutIsAttribute()
{
// Arrange
// Act
var sut = new DelegatingMockCustomizeAttribute();
// Assert
Assert.IsAssignableFrom<Attribute>(sut);
}

[Fact]
public void SutImplementsIParameterCustomizationSource()
{
// Arrange
// Act
var sut = new DelegatingMockCustomizeAttribute();
// Assert
Assert.IsAssignableFrom<IParameterCustomizationSource>(sut);
}
}
}