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] Added AutoValueAttribute to NUnit3 glue library #1012

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="NUnit" Version="3.7.0" Condition=" '$(TargetFramework)'=='netcoreapp1.1' " />
<PackageReference Include="NUnit" Version="3.0.1" Condition=" '$(TargetFramework)'=='net452' " />

<PackageReference Include="NUnit3TestAdapter" Version="3.8.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
</ItemGroup>

Expand Down
32 changes: 32 additions & 0 deletions Src/AutoFixture.NUnit3.UnitTest/AutoValueAttributeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using TestTypeFoundation;

namespace AutoFixture.NUnit3.UnitTest
{
[TestFixture]
public class AutoValueAttributeTest
{
[Test]
[InlineAutoData(typeof(int))]
[InlineAutoData(typeof(string))]
[InlineAutoData(typeof(DoubleParameterType<int, string>))]
public void AutoValueAttribute_generates_a_model(Type typeToGenerate, AutoValueAttribute sut)
{
var mockParameter = new Mock<IParameterInfo>();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zvirja I added a dependency on Moq to quickly write this test. If you have better ways to solve this scenario, I'll make the proper changes.

I was too lazy to create a fake implementation of IParameterInfo.


mockParameter.SetupGet(p => p.ParameterType).Returns(typeToGenerate);

var values = sut.GetData(mockParameter.Object);

Assert.That(values, Has.Exactly(1).InstanceOf(typeToGenerate));
}
}
}
31 changes: 31 additions & 0 deletions Src/AutoFixture.NUnit3.UnitTest/AutoValueAttributeUsageTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using NUnit.Framework;
using TestTypeFoundation;

namespace AutoFixture.NUnit3.UnitTest {
[TestFixture]
public class AutoValueAttributeUsageTest
{
[Test]
public void AutoValueAttribute_should_provide_a_fixed_Model(
[Values] bool flag,
[Range(1, 5)] int statusCode,
[AutoValue] DoubleParameterType<int, string> model
)
{
Assert.That(model, Is.Not.Null);
}

[Test]
public void AutoValueAttribute_should_provide_a_simple_type([AutoValue] int value)
{
Assert.Pass();
}

[Test]
public void AutoValueAttribute_should_provide_a_complex_type([AutoValue] DoubleParameterType<int, string> value)
{
Assert.Pass();
}

}
}
1 change: 0 additions & 1 deletion Src/AutoFixture.NUnit3.UnitTest/DependencyConstraints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public void AutoFixtureNUnit3DoesNotReference(string assemblyName)
[InlineAutoData("FakeItEasy")]
[InlineAutoData("Foq")]
[InlineAutoData("FsCheck")]
[InlineAutoData("Moq")]
[InlineAutoData("NSubstitute")]
[InlineAutoData("Rhino.Mocks")]
[InlineAutoData("Unquote")]
Expand Down
65 changes: 65 additions & 0 deletions Src/AutoFixture.NUnit3/AutoValueAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using AutoFixture.Kernel;
using NUnit.Framework;
using NUnit.Framework.Interfaces;

namespace AutoFixture.NUnit3
{
/// <summary>
/// This attribute uses AutoFixture to generate values for unit test parameters.
/// This implementation is based on DataAttribute and IParameterDataSource of NUnit3
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "This attribute is the root of a potential attribute hierarchy.")]
[AttributeUsage(AttributeTargets.Parameter)]
public class AutoValueAttribute : DataAttribute, IParameterDataSource
{
private readonly Lazy<IFixture> _fixtureLazy;
private IFixture Fixture => _fixtureLazy.Value;

/// <summary>
/// Constructs a <see cref="AutoValueAttribute"/>
/// </summary>
public AutoValueAttribute() : this (() => new Fixture())
{

}

/// <summary>
/// Initializes a new instance of the <see cref="AutoValueAttribute"/> class
/// with the supplied <paramref name="fixtureBuilder"/>. Fixture will be created
/// on demand using the provided factory
/// </summary>
/// <param name="fixtureBuilder">The fixture factory used to construct the fixture.</param>
protected AutoValueAttribute(Func<IFixture> fixtureBuilder)
{
if (fixtureBuilder == null)
{
throw new ArgumentNullException(nameof(fixtureBuilder));
}

_fixtureLazy = new Lazy<IFixture>(fixtureBuilder, LazyThreadSafetyMode.PublicationOnly);
}

/// <summary>
/// Constructs a parameter from a given <see cref="IParameterInfo"/>
/// </summary>
/// <param name="parameter">A representation of the parameter to create</param>
/// <returns>Exactly an instance of the given <see cref="IParameterInfo"/></returns>
public IEnumerable GetData(IParameterInfo parameter)
{
if (parameter == null)
{
throw new ArgumentNullException(nameof(parameter));
}

var context = new SpecimenContext(Fixture);

var obj = context.Resolve(parameter.ParameterType);

return new [] {obj};
}
}
}