Skip to content

Setups can now be created without a filler and you can encapsulate setups with other setups. #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions ObjectFiller.Test/SetupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public class Parent

public class Child
{
public int Value { get; set; }
public int IntValue { get; set; }

public string StringValue { get; set; }
}

[TestMethod]
Expand All @@ -24,10 +26,10 @@ public void RandomizerCreatesObjectsBasedOnPreviouseSetups()
int givenValue = Randomizer<int>.Create();

var childFiller = new Filler<Child>();
var childSetup = childFiller.Setup().OnProperty(x => x.Value).Use(givenValue).Result;
var childSetup = childFiller.Setup().OnProperty(x => x.IntValue).Use(givenValue).Result;

var child = Randomizer<Child>.Create(childSetup);
Assert.AreEqual(givenValue, child.Value);
Assert.AreEqual(givenValue, child.IntValue);
}

[TestMethod]
Expand All @@ -36,13 +38,13 @@ public void UseSetupsAgainForPropertyConfigurations()
int givenValue = Randomizer<int>.Create();

var childFiller = new Filler<Child>();
var childSetup = childFiller.Setup().OnProperty(x => x.Value).Use(givenValue).Result;
var childSetup = childFiller.Setup().OnProperty(x => x.IntValue).Use(givenValue).Result;

var parentFiller = new Filler<Parent>();
parentFiller.Setup().OnProperty(x => x.Child).Use(childSetup);

var parent = parentFiller.Create();
Assert.AreEqual(givenValue, parent.Child.Value);
Assert.AreEqual(givenValue, parent.Child.IntValue);
}

[TestMethod]
Expand All @@ -51,13 +53,13 @@ public void UseSetupsAgainForTypeConfigurations()
int givenValue = Randomizer<int>.Create();

var childFiller = new Filler<Child>();
var childSetup = childFiller.Setup().OnProperty(x => x.Value).Use(givenValue).Result;
var childSetup = childFiller.Setup().OnProperty(x => x.IntValue).Use(givenValue).Result;

var parentFiller = new Filler<Parent>();
parentFiller.Setup().OnType<Child>().Use(childSetup);

var parent = parentFiller.Create();
Assert.AreEqual(givenValue, parent.Child.Value);
Assert.AreEqual(givenValue, parent.Child.IntValue);
}

[TestMethod]
Expand All @@ -66,16 +68,34 @@ public void UseSetupsAgain()
int givenValue = Randomizer<int>.Create();

var firstChildFiller = new Filler<Child>();
var childSetup = firstChildFiller.Setup().OnProperty(x => x.Value).Use(givenValue).Result;
var childSetup = firstChildFiller.Setup().OnProperty(x => x.IntValue).Use(givenValue).Result;

var secondChildFiller = new Filler<Child>();
secondChildFiller.Setup(childSetup);

var child = secondChildFiller.Create();

Assert.AreEqual(givenValue, child.Value);
Assert.AreEqual(givenValue, child.IntValue);
}

[TestMethod]
public void SetupsCanBeCreatedWithFactoryMethod()
{
var childSetup = FillerSetup.Create<Child>().OnProperty(x => x.IntValue).Use(42).Result;

var child = Randomizer<Child>.Create(childSetup);
Assert.AreEqual(42, child.IntValue);
}

[TestMethod]
public void SetupsCanBeCreatedWithFactoryMethodBasedOnExistingSetupManager()
{
var childSetup = FillerSetup.Create<Child>().OnProperty(x => x.IntValue).Use(42).Result;
childSetup = FillerSetup.Create<Child>(childSetup).OnProperty(x => x.StringValue).Use("Juchu").Result;

var child = Randomizer<Child>.Create(childSetup);
Assert.AreEqual(42, child.IntValue);
Assert.AreEqual("Juchu", child.StringValue);
}
}
}
24 changes: 24 additions & 0 deletions ObjectFiller/Setup/FillerSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,29 @@ public FillerSetup()
/// Gets the <see cref="FillerSetupItem"/> for a specific <see cref="Type"/>
/// </summary>
internal Dictionary<Type, FillerSetupItem> TypeToFillerSetup { get; private set; }

/// <summary>
/// Creates a setup for a specific type.
/// </summary>
/// <typeparam name="TTarget">The type which is configured.</typeparam>
/// <returns>Setup you can use for Filler and Randomizer.</returns>
public static FluentFillerApi<TTarget> Create<TTarget>() where TTarget : class
{
return new FluentFillerApi<TTarget>(new SetupManager());
}

/// <summary>
/// Creates a setup based uppon another setup
/// </summary>
/// <typeparam name="TTarget">The type which is configured.</typeparam>
/// <param name="baseSetup">The setup which is used as basis of the new one.</param>
/// <returns>Setup you can use for Filler and Randomizer.</returns>
public static FluentFillerApi<TTarget> Create<TTarget>(FillerSetup baseSetup) where TTarget : class
{
var setupManager = new SetupManager();
setupManager.FillerSetup = baseSetup ?? new FillerSetup();

return new FluentFillerApi<TTarget>(setupManager);
}
}
}