Skip to content

Commit f0eb68b

Browse files
authored
Merge pull request #86 from Tynamix/FEATURE_83_Explicit_Setup
#83 implemented
2 parents 103cce3 + ffdead9 commit f0eb68b

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

Tynamix.ObjectFiller.Test/SetupTests.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using Xunit;
2+
using Xunit;
33

44
namespace ObjectFiller.Test
55
{
@@ -11,6 +11,8 @@ public class SetupTests
1111
public class Parent
1212
{
1313
public Child Child { get; set; }
14+
15+
public int? SomeId { get; set; }
1416
}
1517

1618
public class Child
@@ -20,14 +22,46 @@ public class Child
2022
public string StringValue { get; set; }
2123
}
2224

25+
[Fact]
26+
public void ExplicitSetupShallJustFillPropertiesWhichAreSetUpEvenInSubtypes()
27+
{
28+
Filler<Parent> filler = new Filler<Parent>();
29+
filler.Setup(true)
30+
.SetupFor<Child>().OnProperty(x => x.IntValue).Use(new IntRange(1, 20));
31+
32+
var parent = filler.Create();
33+
34+
Assert.NotNull(parent);
35+
Assert.NotNull(parent.Child);
36+
Assert.Null(parent.SomeId);
37+
Assert.InRange(parent.Child.IntValue, 1, 20);
38+
Assert.Null(parent.Child.StringValue);
39+
}
40+
41+
[Fact]
42+
public void ExplicitSetupShallJustFillPropertiesWhichAreSetUpAndNoInstanceShallCreateForSubTypesIfNotSetup()
43+
{
44+
Filler<Parent> filler = new Filler<Parent>();
45+
filler.Setup(true)
46+
.OnProperty(x => x.SomeId).Use(new IntRange(1, 20));
47+
48+
var parent = filler.Create();
49+
50+
Assert.NotNull(parent);
51+
Assert.Null(parent.Child);
52+
Assert.NotNull(parent.SomeId);
53+
}
54+
55+
56+
2357
[Fact]
2458
public void RandomizerCreatesObjectsBasedOnPreviouseSetups()
2559
{
2660
int givenValue = Randomizer<int>.Create();
2761

2862
var childFiller = new Filler<Child>();
2963
var childSetup = childFiller.Setup().OnProperty(x => x.IntValue).Use(givenValue).Result;
30-
64+
3165
var child = Randomizer<Child>.Create(childSetup);
3266
Assert.Equal(givenValue, child.IntValue);
3367
}

Tynamix.ObjectFiller/Filler.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public class Filler<T>
3434
/// </summary>
3535
private readonly SetupManager setupManager;
3636

37+
/// <summary>
38+
/// True when the filler will just fill properties which are set up explicit
39+
/// </summary>
40+
private bool justConfiguredProperties;
41+
3742
#endregion
3843

3944
#region Constructors and Destructors
@@ -117,7 +122,17 @@ public T Fill(T instanceToFill)
117122
/// <returns>Fluent API setup</returns>
118123
public FluentFillerApi<T> Setup()
119124
{
120-
return this.Setup(null);
125+
return this.Setup(false);
126+
}
127+
128+
/// <summary>
129+
/// Call this to start the setup for the <see cref="Filler{T}"/>
130+
/// </summary>
131+
/// <param name="explicitSetup">True if just properties shall get filled which configured in filler setup.</param>
132+
/// <returns>Fluent API setup</returns>
133+
public FluentFillerApi<T> Setup(bool explicitSetup)
134+
{
135+
return this.Setup(null, explicitSetup);
121136
}
122137

123138
/// <summary>
@@ -131,12 +146,29 @@ public FluentFillerApi<T> Setup()
131146
/// Fluent API Setup
132147
/// </returns>
133148
public FluentFillerApi<T> Setup(FillerSetup fillerSetupToUse)
149+
{
150+
return this.Setup(fillerSetupToUse, false);
151+
}
152+
153+
/// <summary>
154+
/// Call this to start the setup for the <see cref="Filler{T}"/> and use a setup which you created
155+
/// before with the <see cref="IFluentApi{TTargetObject,TTargetType}"/>
156+
/// </summary>
157+
/// <param name="fillerSetupToUse">
158+
/// FillerSetup to use
159+
/// </param>
160+
/// <param name="explicitSetup">True if just properties shall get filled which configured in filler setup.</param>
161+
/// <returns>
162+
/// Fluent API Setup
163+
/// </returns>
164+
public FluentFillerApi<T> Setup(FillerSetup fillerSetupToUse, bool explicitSetup)
134165
{
135166
if (fillerSetupToUse != null)
136167
{
137168
this.setupManager.FillerSetup = fillerSetupToUse;
138169
}
139170

171+
this.justConfiguredProperties = explicitSetup;
140172
return new FluentFillerApi<T>(this.setupManager);
141173
}
142174

@@ -648,6 +680,12 @@ private void FillInternal(object objectToFill, HashStack<Type> typeTracker = nul
648680
continue;
649681
}
650682

683+
if (this.justConfiguredProperties
684+
&& !this.setupManager.FillerSetup.TypeToFillerSetup.ContainsKey(property.PropertyType))
685+
{
686+
continue;
687+
}
688+
651689
object filledObject = this.CreateAndFillObject(property.PropertyType, currentSetup, typeTracker);
652690

653691
this.SetPropertyValue(property, objectToFill, filledObject);

Tynamix.ObjectFiller/Randomizer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ internal static Func<T> CreateFactoryMethod(FillerSetup setup)
144144

145145
if (setup != null)
146146
{
147-
var setupMethod = objectFiller.GetType().GetMethods().Single(x => x.Name == "Setup" && x.GetParameters().Any());
147+
var setupMethod = objectFiller.GetType().GetMethods()
148+
.Where(x => x.Name == "Setup" && x.GetParameters().Length == 1)
149+
.Single(x => x.GetParameters().First().ParameterType == typeof(FillerSetup));
150+
148151
setupMethod.Invoke(objectFiller, new[] { setup });
149152
}
150153

0 commit comments

Comments
 (0)