Skip to content

Commit

Permalink
making attribute interfaces for issue #12
Browse files Browse the repository at this point in the history
  • Loading branch information
ipjohnson committed Jun 1, 2017
1 parent b72cb81 commit 1d27f0c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
26 changes: 21 additions & 5 deletions src/SimpleFixture/Attributes/AttributeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public static IEnumerable<T> GetAttributes<T>(ParameterInfo parameterInfo) where
return returnList;
}

/// <summary>
/// Get data for parameter info
/// </summary>
/// <param name="parameterInfo"></param>
/// <returns></returns>
public static object[] GetData(ParameterInfo parameterInfo)
{
var fixture = CreateFixture(parameterInfo);
Expand Down Expand Up @@ -353,11 +358,22 @@ private static T Freeze<T>(Fixture fixture, ParameterInfo parameter, FreezeAttri
/// <returns></returns>
public static Fixture CreateFixture(MethodInfo testMethod)
{
var attribute = AttributeHelper.GetAttribute<FixtureCreationAttribute>(testMethod);
var attribute = GetAttribute<IFixtureCreationAttribute>(testMethod);

var fixture = attribute != null ? attribute.CreateFixture() : new Fixture();
Fixture fixture;

if (attribute != null)
{
fixture = attribute.CreateFixture();
}
else
{
var configurationAttribute = GetAttribute<IFixtureConfigurationAttribute>(testMethod);

fixture = new Fixture(configurationAttribute?.ProvideConfiguration(testMethod));
}

var initializeAttributes = AttributeHelper.GetAttributes<FixtureInitializationAttribute>(testMethod);
var initializeAttributes = GetAttributes<IFixtureInitializationAttribute>(testMethod);

foreach (var initializeAttribute in initializeAttributes)
{
Expand All @@ -374,11 +390,11 @@ public static Fixture CreateFixture(MethodInfo testMethod)
/// <returns></returns>
public static Fixture CreateFixture(ParameterInfo parameterInfo)
{
var attribute = AttributeHelper.GetAttribute<FixtureCreationAttribute>(parameterInfo);
var attribute = GetAttribute<IFixtureCreationAttribute>(parameterInfo);

var fixture = attribute != null ? attribute.CreateFixture() : new Fixture();

var initializeAttributes = AttributeHelper.GetAttributes<FixtureInitializationAttribute>(parameterInfo);
var initializeAttributes = GetAttributes<IFixtureInitializationAttribute>(parameterInfo);

foreach (var initializeAttribute in initializeAttributes)
{
Expand Down
9 changes: 7 additions & 2 deletions src/SimpleFixture/Attributes/DataConfigurationAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@

namespace SimpleFixture.Attributes
{
public abstract class DataConfigurationAttribute : Attribute
public interface IFixtureConfigurationAttribute
{
public abstract DefaultFixtureConfiguration ProvideConfiguration(MethodInfo method);
IFixtureConfiguration ProvideConfiguration(MethodInfo method);
}

public abstract class DataConfigurationAttribute : Attribute, IFixtureConfigurationAttribute
{
public abstract IFixtureConfiguration ProvideConfiguration(MethodInfo method);
}
}
22 changes: 21 additions & 1 deletion src/SimpleFixture/Attributes/FixtureCreationAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,28 @@

namespace SimpleFixture.Attributes
{
public abstract class FixtureCreationAttribute : Attribute
/// <summary>
///
/// </summary>
public interface IFixtureCreationAttribute
{
/// <summary>
/// Create fixture
/// </summary>
/// <returns></returns>
Fixture CreateFixture();
}


/// <summary>
/// Attribute for creating fixture
/// </summary>
public abstract class FixtureCreationAttribute : Attribute, IFixtureCreationAttribute
{
/// <summary>
/// Create fixture
/// </summary>
/// <returns></returns>
public abstract Fixture CreateFixture();
}
}
14 changes: 13 additions & 1 deletion src/SimpleFixture/Attributes/FixtureInitializationAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

namespace SimpleFixture.Attributes
{
/// <summary>
/// attributes that implement this interface will be called to initialize fixture
/// </summary>
public interface IFixtureInitializationAttribute
{
/// <summary>
///
/// </summary>
/// <param name="fixture"></param>
void Initialize(Fixture fixture);
}

/// <summary>
/// Attribute for initializing fixture
/// </summary>
public abstract class FixtureInitializationAttribute : Attribute
public abstract class FixtureInitializationAttribute : Attribute, IFixtureInitializationAttribute
{
/// <summary>
/// Initialize fixture
Expand Down

0 comments on commit 1d27f0c

Please sign in to comment.