Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Renamed Blueprint to interface naming and added variation support

  • Loading branch information...
commit 13887af177fb2db59d0fc5aca40252729ceeb373 1 parent dce1abe
@lcrodriguez lcrodriguez authored
View
36 Plant.Core/BasePlant.cs
@@ -4,12 +4,13 @@
using System.Reflection;
using Properties = System.Collections.Generic.IDictionary<Plant.Core.PropertyData, object>;
using Blueprints = System.Collections.Generic.Dictionary<System.Type, System.Collections.Generic.IDictionary<Plant.Core.PropertyData, object>>;
-
+using Variations = System.Collections.Generic.Dictionary<string, System.Collections.Generic.IDictionary<Plant.Core.PropertyData, object>>;
namespace Plant.Core
{
public class BasePlant
{
+ private readonly Variations propertyVariations = new Variations();
private readonly Blueprints propertyBlueprints = new Blueprints();
private readonly Blueprints constructorBlueprints = new Blueprints();
private readonly IDictionary<Type, CreationStrategy> creationStrategies = new Dictionary<Type, CreationStrategy>();
@@ -57,12 +58,17 @@ private static T CreateInstanceWithEmptyConstructor<T>()
return Activator.CreateInstance<T>();
}
+ public virtual T Create<T>(string variation)
+ {
+ return Create<T>(null, variation);
+ }
+
public virtual T Create<T>(T userSpecifiedProperties)
{
return Create<T>((object)userSpecifiedProperties);
}
- public virtual T Create<T>(object userSpecifiedProperties = null)
+ public virtual T Create<T>(object userSpecifiedProperties = null, string variation = null)
{
var userSpecifiedPropertyList = ToPropertyList(userSpecifiedProperties);
@@ -71,12 +77,22 @@ public virtual T Create<T>(object userSpecifiedProperties = null)
constructedObject = CreateViaConstructor<T>(userSpecifiedPropertyList);
else
constructedObject = CreateViaProperties<T>(userSpecifiedPropertyList);
-
+
+ UpdateProperties<T>(constructedObject, variation);
+
if (postBuildActions.ContainsKey(typeof(T)))
((Action<T>)postBuildActions[typeof (T)])(constructedObject);
return constructedObject;
}
+
+ private void UpdateProperties<T>(T constructedObject, string variation)
+ {
+ if (string.IsNullOrEmpty(variation))
+ return;
+
+ SetProperties(propertyVariations[string.Format("{0}{1}", typeof(T), variation)], constructedObject);
+ }
private CreationStrategy StrategyFor<T>()
{
@@ -156,6 +172,16 @@ public virtual void DefinePropertiesOf<T>(object defaults)
sequenceValues.Add(typeof(T), 0);
}
+ public virtual void DefineVariationOf<T>(string variation, T defaults)
+ {
+ DefineVariationOf<T>(variation, (object)defaults);
+ }
+
+ public virtual void DefineVariationOf<T>(string variation, object defaults)
+ {
+ propertyVariations.Add(string.Format("{0}{1}", typeof(T), variation), ToPropertyList(defaults));
+ }
+
public void DefineConstructionOf<T>(object defaults, Action<T> afterCtorPopulation)
{
DefineConstructionOf<T>(defaults);
@@ -182,10 +208,10 @@ private void AddDefaultsTo<T>(Blueprints blueprints, object defaults)
public BasePlant WithBlueprintsFromAssemblyOf<T>()
{
var assembly = typeof(T).Assembly;
- var blueprintTypes = assembly.GetTypes().Where(t => typeof(Blueprint).IsAssignableFrom(t));
+ var blueprintTypes = assembly.GetTypes().Where(t => typeof(IBlueprint).IsAssignableFrom(t));
blueprintTypes.ToList().ForEach(blueprintType =>
{
- var blueprint = (Blueprint)Activator.CreateInstance(blueprintType);
+ var blueprint = (IBlueprint)Activator.CreateInstance(blueprintType);
blueprint.SetupPlant(this);
});
return this;
View
7 Plant.Core/Blueprint.cs
@@ -1,7 +0,0 @@
-namespace Plant.Core
-{
- public interface Blueprint
- {
- void SetupPlant(BasePlant p);
- }
-}
View
7 Plant.Core/IBlueprint.cs
@@ -0,0 +1,7 @@
+namespace Plant.Core
+{
+ public interface IBlueprint
+ {
+ void SetupPlant(BasePlant p);
+ }
+}
View
2  Plant.Core/Plant.Core.csproj
@@ -41,7 +41,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BasePlant.cs" />
- <Compile Include="Blueprint.cs" />
+ <Compile Include="IBlueprint.cs" />
<Compile Include="CreationStrategy.cs" />
<Compile Include="PropertyData.cs" />
<Compile Include="LazyPropertyHasWrongTypeException.cs" />
View
26 Plant.Tests/BasePlantTest.cs
@@ -11,6 +11,30 @@ namespace Plant.Tests
public class BasePlantTest
{
[Test]
+ public void Should_Create_Variation_Of_Specified_Type()
+ {
+ var plant = new BasePlant();
+ plant.DefinePropertiesOf<Person>(new { FirstName = "" });
+ plant.DefineVariationOf<Person>("My",new { FirstName = "My" });
+ plant.DefineVariationOf<Person>("Her", new { FirstName = "Her" });
+
+ Assert.IsInstanceOf(typeof(Person), plant.Create<Person>());
+ Assert.IsInstanceOf(typeof(Person), plant.Create<Person>("My"));
+ Assert.IsInstanceOf(typeof(Person), plant.Create<Person>("Her"));
+ }
+
+ [Test]
+ public void Should_Create_Variation_Of_Specified_Type_With_Correct_Data()
+ {
+ var plant = new BasePlant();
+ plant.DefinePropertiesOf<Person>(new { FirstName = "" });
+ plant.DefineVariationOf<Person>("My", new { FirstName = "My" });
+
+ var person = plant.Create<Person>("My");
+ Assert.AreEqual("My", person.FirstName);
+ }
+
+ [Test]
public void Should_Create_Instance_Of_Specified_Type()
{
var plant = new BasePlant();
@@ -200,7 +224,7 @@ public void Should_increment_values_in_a_sequence_with_ctor_construction()
}
namespace TestBlueprints
{
- class TestBlueprint : Blueprint
+ class TestBlueprint : IBlueprint
{
public void SetupPlant(BasePlant plant)
{
Please sign in to comment.
Something went wrong with that request. Please try again.