Last active
February 17, 2016 08:10
-
-
Save lkaczanowski/e8d3ece4fc05f3721181 to your computer and use it in GitHub Desktop.
Extensions for AutoFixture
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
namespace AutoFixtureExtensions | |
{ | |
using Ploeh.AutoFixture; | |
using Ploeh.AutoFixture.AutoMoq; | |
using Ploeh.AutoFixture.Kernel; | |
public class SpecificArgumentsConstructorQuery : IMethodQuery | |
{ | |
private readonly Type[] _types; | |
public SpecificArgumentsConstructorQuery(params Type[] types) | |
{ | |
if (types == null) | |
{ | |
throw new ArgumentNullException("types"); | |
} | |
_types = types; | |
} | |
public IEnumerable<IMethod> SelectMethods(Type type) | |
{ | |
if (type == null) | |
{ | |
throw new ArgumentNullException("type"); | |
} | |
var constructorMethods = type.GetConstructors() | |
.Where(x => HaveSameParameters(x.GetParameters())) | |
.Select(x => new ConstructorMethod(x)); | |
return constructorMethods; | |
} | |
private bool HaveSameParameters(ParameterInfo[] parameterInfos) | |
{ | |
if (parameterInfos.Length != _types.Length) | |
{ | |
return false; | |
} | |
return _types.Where((constructorArgumentType, index) => parameterInfos[index].ParameterType != constructorArgumentType).Any() == false; | |
} | |
} | |
} |
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
namespace AutofixtureIdiomsExtensions | |
{ | |
using System; | |
using Ploeh.AutoFixture.Idioms; | |
using Ploeh.AutoFixture.Kernel; | |
public class FullGuardClauseAssertion : GuardClauseAssertion | |
{ | |
public FullGuardClauseAssertion(ISpecimenBuilder builder) | |
: base( | |
builder, | |
new CompositeBehaviorExpectation( | |
new NullReferenceBehaviorExpectation(), | |
new EmptyStringBehaviorExpectation(), | |
new NullStringBehaviorExpectation(), | |
new WhitespaceStringBehaviorExpectation())) | |
{ | |
} | |
} | |
public class NullReferenceBehaviorExpectation : IBehaviorExpectation | |
{ | |
public void Verify(IGuardClauseCommand command) | |
{ | |
if (command.RequestedType.FullName == "System.String" | |
|| (command.RequestedType.IsClass == false && command.RequestedType.IsInterface == false)) | |
{ | |
return; | |
} | |
try | |
{ | |
command.Execute((object)null); | |
} | |
catch (ArgumentException) | |
{ | |
return; | |
} | |
catch (Exception exception) | |
{ | |
throw command.CreateException("null", exception); | |
} | |
throw command.CreateException("null"); | |
} | |
} | |
public class EmptyStringBehaviorExpectation : IBehaviorExpectation | |
{ | |
public void Verify(IGuardClauseCommand command) | |
{ | |
if (command.RequestedType.FullName != "System.String") | |
{ | |
return; | |
} | |
try | |
{ | |
command.Execute(string.Empty); | |
} | |
catch (ArgumentException) | |
{ | |
return; | |
} | |
catch (Exception exception) | |
{ | |
throw command.CreateException("empty", exception); | |
} | |
throw command.CreateException("empty"); | |
} | |
} | |
public class NullStringBehaviorExpectation : IBehaviorExpectation | |
{ | |
public void Verify(IGuardClauseCommand command) | |
{ | |
if (command.RequestedType.FullName != "System.String") | |
{ | |
return; | |
} | |
try | |
{ | |
command.Execute((string)null); | |
} | |
catch (ArgumentException) | |
{ | |
return; | |
} | |
catch (Exception exception) | |
{ | |
throw command.CreateException("null", exception); | |
} | |
throw command.CreateException("null"); | |
} | |
} | |
public class WhitespaceStringBehaviorExpectation : IBehaviorExpectation | |
{ | |
public void Verify(IGuardClauseCommand command) | |
{ | |
if (command.RequestedType.FullName != "System.String") | |
{ | |
return; | |
} | |
try | |
{ | |
command.Execute(" "); | |
} | |
catch (ArgumentException) | |
{ | |
return; | |
} | |
catch (Exception exception) | |
{ | |
throw command.CreateException("whitespace", exception); | |
} | |
throw command.CreateException("whitespace"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment