forked from AutoFixture/AutoFixture
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Specifications, Builders and Customizations to target issue:
- Loading branch information
1 parent
9df6684
commit ebda500
Showing
17 changed files
with
483 additions
and
3 deletions.
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
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,37 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Ploeh.AutoFixture.Kernel | ||
{ | ||
/// <summary> | ||
/// A specification that evaluates whether a request is a request for a type without public constructors. | ||
/// </summary> | ||
public class NoConstructorSpecification : IRequestSpecification | ||
{ | ||
private readonly IMethodQuery modestConstructorQuery; | ||
|
||
public NoConstructorSpecification() | ||
{ | ||
modestConstructorQuery = new ModestConstructorQuery(); | ||
} | ||
|
||
/// <summary> | ||
/// Evaluates a request for a specimen. | ||
/// </summary> | ||
/// <param name="request">The specimen request.</param> | ||
/// <returns> | ||
/// <see langword="true"/> if <paramref name="request"/> is a <see cref="Type"/> that represents a type without public constructors; | ||
/// otherwise, <see langword="false"/>. | ||
/// </returns> | ||
public bool IsSatisfiedBy(object request) | ||
{ | ||
if (request == null) | ||
{ | ||
throw new ArgumentNullException("request"); | ||
} | ||
|
||
var type = request as Type; | ||
return type != null && !modestConstructorQuery.SelectMethods(type).Any(); | ||
} | ||
} | ||
} |
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,35 @@ | ||
using System; | ||
|
||
namespace Ploeh.AutoFixture.Kernel | ||
{ | ||
/// <summary> | ||
/// A specification that evaluates whether a request is a request for a value type such as a custom structure. | ||
/// </summary> | ||
public class StructureSpecification : IRequestSpecification | ||
{ | ||
|
||
/// <summary> | ||
/// Evaluates a request for a specimen. | ||
/// </summary> | ||
/// <param name="request">The specimen request.</param> | ||
/// <returns> | ||
/// <see langword="true"/> if <paramref name="request"/> is a <see cref="Type"/> that represents a custom structure; | ||
/// otherwise, <see langword="false"/>. | ||
/// </returns> | ||
public bool IsSatisfiedBy(object request) | ||
{ | ||
if (request == null) | ||
{ | ||
throw new ArgumentNullException("request"); | ||
} | ||
|
||
var type = request as Type; | ||
return type != null && IsStruct(type); | ||
} | ||
|
||
private bool IsStruct(Type type) | ||
{ | ||
return type.IsValueType && !type.IsEnum && !type.IsPrimitive; | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Src/AutoFixture/Kernel/StructureWithoutConstructorSpecimenBuilder.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,43 @@ | ||
using System.Globalization; | ||
|
||
namespace Ploeh.AutoFixture.Kernel | ||
{ | ||
/// <summary> | ||
/// Throws an <see cref="ObjectCreationException"/> when one tries to create Structure without explicit parametrized constructor. | ||
/// Possibly notifying about bad design (mutable value type). | ||
/// </summary> | ||
/// <remarks> | ||
/// <para> | ||
/// This <see cref="ISpecimenBuilder"/> can be used with proper filtering <see cref="IRequestSpecification"/> to throw exceptions only on | ||
/// structures without constructors. Will throw an exception instead of letting the | ||
/// containing builder return a <see cref="NoSpecimen"/> instance when it can't satisfy a | ||
/// request or generic exception being thrown. | ||
/// </para> | ||
/// </remarks> | ||
public class StructureWithoutConstructorSpecimenBuilder : ISpecimenBuilder | ||
{ | ||
/// <summary> | ||
/// Throws an <see cref="ObjectCreationException"/>. | ||
/// </summary> | ||
/// <param name="request">The request that describes what to create.</param> | ||
/// <param name="context"> | ||
/// A context that can be used to create other specimens. Not used. | ||
/// </param> | ||
/// <returns> | ||
/// This method never returns. It always throws an <see cref="ObjectCreationException"/>. | ||
/// </returns> | ||
public object Create(object request, ISpecimenContext context) | ||
{ | ||
throw new ObjectCreationException( | ||
string.Format( | ||
CultureInfo.CurrentCulture, | ||
@"AutoFixture was unable to create an instance from {0}, since it's a value type with no explicit, parameterized constructors. Are you attempting to create an instance of a mutable value type? If so, you should strongly consider changing the design of the value type. However, if you are unable to do so, you can add the SupportValueTypeDefaultConstructors customizations to your Fixture instance: | ||
var fixture = new Fixture(); | ||
var customization = new {1}(); | ||
customization.Customize(fixture);", | ||
request, typeof(SupportStructureDefaultConstructorsCustomization).Name | ||
) | ||
); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
Src/AutoFixture/SupportStructureDefaultConstructorsCustomization.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,26 @@ | ||
using System; | ||
|
||
namespace Ploeh.AutoFixture | ||
|
||
{ | ||
/// <summary> | ||
/// A customization that changes how custom <see langword="struct"/> are generated. Uses <see cref="SupportStructureDefaultConstructorsGenerator"/>. | ||
/// </summary> | ||
public class SupportStructureDefaultConstructorsCustomization : ICustomization | ||
{ | ||
/// <summary> | ||
/// Customizes specified fixture by adding <see cref="SupportStructureDefaultConstructorsGenerator"/> as a default stragety for | ||
/// creating new custom <see langword="struct"/> that has only default constructor. | ||
/// </summary> | ||
/// <param name="fixture">The fixture to customize.</param> | ||
public void Customize(IFixture fixture) | ||
{ | ||
if (fixture == null) | ||
{ | ||
throw new ArgumentNullException("fixture"); | ||
} | ||
|
||
fixture.Customizations.Add(new SupportStructureDefaultConstructorsGenerator()); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
Src/AutoFixture/SupportStructureDefaultConstructorsGenerator.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,35 @@ | ||
using System; | ||
using Ploeh.AutoFixture.Kernel; | ||
|
||
namespace Ploeh.AutoFixture | ||
{ | ||
/// <summary> | ||
/// Creates new <see langword="struct"/>. | ||
/// </summary> | ||
public class SupportStructureDefaultConstructorsGenerator : ISpecimenBuilder | ||
{ | ||
/// <summary> | ||
/// Creates a new <see langword="struct"/>. | ||
/// </summary> | ||
/// <param name="request">The request that describes what to create.</param> | ||
/// <param name="context">A context that can be used to create other specimens. Not used</param> | ||
/// <returns> | ||
/// The requested struct if possible; otherwise a <see cref="NoSpecimen"/> instance. | ||
/// </returns> | ||
public object Create(object request, ISpecimenContext context) | ||
{ | ||
Type type = request as Type; | ||
if (type == null || !IsCustomStructure(type)) | ||
{ | ||
return new NoSpecimen(request); | ||
} | ||
|
||
return Activator.CreateInstance(type); | ||
} | ||
|
||
private bool IsCustomStructure(Type type) | ||
{ | ||
return type.IsValueType && !type.IsEnum && !type.IsPrimitive; | ||
} | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
Src/AutoFixtureUnitTest/Kernel/NoConstructorSpecificationTest.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,53 @@ | ||
using System; | ||
using Ploeh.AutoFixture.Kernel; | ||
using Ploeh.TestTypeFoundation; | ||
using Xunit; | ||
using Xunit.Extensions; | ||
|
||
namespace Ploeh.AutoFixtureUnitTest.Kernel | ||
{ | ||
public class NoConstructorSpecificationTest | ||
{ | ||
[Fact] | ||
public void SutIsRequestSpecification() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var sut = new NoConstructorSpecification(); | ||
// Verify outcome | ||
Assert.IsAssignableFrom<IRequestSpecification>(sut); | ||
// Teardown | ||
} | ||
|
||
[Fact] | ||
public void IsSatisfiedByNullThrows() | ||
{ | ||
// Fixture setup | ||
var sut = new NoConstructorSpecification(); | ||
// Exercise system and verify outcome | ||
Assert.Throws<ArgumentNullException>(() => sut.IsSatisfiedBy(null)); | ||
// Teardown | ||
} | ||
|
||
[Theory] | ||
[InlineData("Ploeh", false)] | ||
[InlineData(1, false)] | ||
[InlineData(typeof(object), false)] | ||
[InlineData(typeof(string), false)] | ||
[InlineData(typeof(AbstractType), true)] | ||
[InlineData(typeof(IInterface), true)] | ||
[InlineData(typeof(StructType), false)] | ||
[InlineData(typeof(char), true)] | ||
[InlineData(typeof(ActivityScope), true)] | ||
public void IsSatisfiedByReturnsCorrectResult(object request, bool expectedResult) | ||
{ | ||
// Fixture setup | ||
var sut = new NoConstructorSpecification(); | ||
// Exercise system | ||
var result = sut.IsSatisfiedBy(request); | ||
// Verify outcome | ||
Assert.Equal(expectedResult, result); | ||
// Teardown | ||
} | ||
} | ||
} |
Oops, something went wrong.