-
Notifications
You must be signed in to change notification settings - Fork 337
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
Kralizek
wants to merge
1
commit into
AutoFixture:master
Choose a base branch
from
Kralizek:autovalue
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
|
||
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
31
Src/AutoFixture.NUnit3.UnitTest/AutoValueAttributeUsageTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
.