Skip to content

Instantly share code, notes, and snippets.

@lkaczanowski
Last active February 17, 2016 08:10
Show Gist options
  • Save lkaczanowski/e8d3ece4fc05f3721181 to your computer and use it in GitHub Desktop.
Save lkaczanowski/e8d3ece4fc05f3721181 to your computer and use it in GitHub Desktop.
Extensions for AutoFixture
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;
}
}
}
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