Skip to content

Commit c17dcc8

Browse files
committed
Merge pull request #11 from Tynamix/FEATURE_Enumerator_Plugin
Feature enumerator plugin
2 parents f4f23c8 + 94d62f0 commit c17dcc8

File tree

7 files changed

+115
-0
lines changed

7 files changed

+115
-0
lines changed

ObjectFiller.Test/ListFillingTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System;
12
using System.Collections.Generic;
3+
using System.Linq;
24
using Microsoft.VisualStudio.TestTools.UnitTesting;
35
using ObjectFiller.Test.TestPoco.ListTest;
46
using Tynamix.ObjectFiller;
@@ -24,6 +26,27 @@ public void TestFillAllListsExceptArray()
2426
Assert.IsNotNull(entity.EntityIList);
2527
}
2628

29+
[TestMethod]
30+
public void TestUseEnumerable()
31+
{
32+
Filler<EntityCollection> eFiller = new Filler<EntityCollection>();
33+
eFiller.Setup()
34+
.ListItemCount(20)
35+
.OnProperty(x => x.EntityArray, x => x.EntityICollection,
36+
x => x.EntityIList, x => x.ObservableCollection,
37+
x => x.EntityIEnumerable).IgnoreIt()
38+
.SetupFor<Entity>()
39+
.OnProperty(x => x.Id).Use(Enumerable.Range(1, 22).Select(x => (int)Math.Pow(2, x)));
40+
41+
EntityCollection ec = eFiller.Create();
42+
43+
for (int i = 0; i < ec.EntityList.Count; i++)
44+
{
45+
int lastPowNum = (int)Math.Pow(2, i + 1);
46+
Assert.AreEqual(lastPowNum, ec.EntityList[i].Id);
47+
}
48+
}
49+
2750
[TestMethod]
2851
public void TestFillList()
2952
{

ObjectFiller.Test/PersonFillingTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using Microsoft.VisualStudio.TestTools.UnitTesting;
45
using ObjectFiller.Test.TestPoco.Person;
56
using Tynamix.ObjectFiller;
@@ -26,6 +27,25 @@ public void TestFillPerson()
2627

2728
}
2829

30+
[TestMethod]
31+
public void TestFillPersonWithEnumerable()
32+
{
33+
Filler<Person> pFiller = new Filler<Person>();
34+
35+
pFiller.Setup()
36+
.OnType<IAddress>().CreateInstanceOf<Address>()
37+
.OnProperty(x => x.Age).Use(Enumerable.Range(18, 60));
38+
39+
40+
Person filledPerson = pFiller.Create();
41+
42+
Assert.IsNotNull(filledPerson.Address);
43+
Assert.IsNotNull(filledPerson.Addresses);
44+
Assert.IsNotNull(filledPerson.StringToIAddress);
45+
Assert.IsNotNull(filledPerson.SureNames);
46+
47+
}
48+
2949
[TestMethod]
3050
public void TestNameListStringRandomizer()
3151
{

ObjectFiller/ObjectFiller.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
<SubType>Designer</SubType>
7171
</EmbeddedResource>
7272
</ItemGroup>
73+
<ItemGroup>
74+
<Compile Include="Plugins\EnumeratorPlugin.cs" />
75+
</ItemGroup>
7376
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7477
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
7578
Other similar extension points exist, see Microsoft.Common.targets.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Tynamix.ObjectFiller
5+
{
6+
internal class EnumeratorPlugin<T> : IRandomizerPlugin<T>
7+
{
8+
private readonly IEnumerable<T> _enumerable;
9+
private IEnumerator<T> _enumerator;
10+
11+
12+
public EnumeratorPlugin(IEnumerable<T> enumerable)
13+
{
14+
_enumerable = enumerable;
15+
}
16+
17+
public T GetValue()
18+
{
19+
// First time?
20+
if (_enumerator == null)
21+
{
22+
_enumerator = _enumerable.GetEnumerator();
23+
}
24+
25+
// Advance, try to recover if we hit end-of-enumeration
26+
var hasNext = _enumerator.MoveNext();
27+
if (!hasNext)
28+
{
29+
_enumerator = _enumerable.GetEnumerator();
30+
hasNext = _enumerator.MoveNext();
31+
32+
if (!hasNext)
33+
{
34+
throw new Exception("Unable to get next value from enumeration " + _enumerable);
35+
}
36+
}
37+
38+
return _enumerator.Current;
39+
}
40+
}
41+
}

ObjectFiller/Setup/FluentPropertyApi.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ public FluentFillerApi<TTargetObject> Use(IRandomizerPlugin<TTargetType> randomi
7171
return Use(randomizerPlugin.GetValue);
7272
}
7373

74+
/// <summary>
75+
/// Use this function if you want to use an IEnumerable for the datageneration.
76+
/// With that you can generate random data in a specific order, with include, exclude and all the other stuff
77+
/// what is possible with IEnumerables and linq
78+
/// </summary>
79+
/// <param name="enumerable">An IEnumerable with items of type <typeparam name="TTargetObject"/> which will be used to fill the data.</param>
80+
/// <returns>Main FluentFiller API</returns>
81+
public FluentFillerApi<TTargetObject> Use(IEnumerable<TTargetType> enumerable)
82+
{
83+
return Use(new EnumeratorPlugin<TTargetType>(enumerable));
84+
}
85+
7486
/// <summary>
7587
/// Ignores the entity for which the fluent setup is made (Type, Property)
7688
/// </summary>

ObjectFiller/Setup/FluentTypeApi.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace Tynamix.ObjectFiller
45
{
@@ -39,6 +40,11 @@ public FluentFillerApi<TTargetObject> Use(IRandomizerPlugin<TTargetType> randomi
3940
return _callback;
4041
}
4142

43+
public FluentFillerApi<TTargetObject> Use(IEnumerable<TTargetType> enumerable)
44+
{
45+
return Use(new EnumeratorPlugin<TTargetType>(enumerable));
46+
}
47+
4248
/// <summary>
4349
/// Ignores the entity for which the fluent setup is made (Type, Property)
4450
/// </summary>

ObjectFiller/Setup/IFluentApi.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace Tynamix.ObjectFiller
45
{
@@ -24,6 +25,15 @@ internal interface IFluentApi<TTargetObject, TTargetType> where TTargetObject :
2425
/// <returns>Main FluentFiller API</returns>
2526
FluentFillerApi<TTargetObject> Use(IRandomizerPlugin<TTargetType> randomizerPlugin);
2627

28+
/// <summary>
29+
/// Use this function if you want to use an IEnumerable for the datageneration.
30+
/// With that you can generate random data in a specific order, with include, exclude and all the other stuff
31+
/// what is possible with IEnumerables and linq
32+
/// </summary>
33+
/// <param name="enumerable">An IEnumerable with items of type <typeparam name="TTargetObject"/> which will be used to fill the data.</param>
34+
/// <returns>Main FluentFiller API</returns>
35+
FluentFillerApi<TTargetObject> Use(IEnumerable<TTargetType> enumerable);
36+
2737
/// <summary>
2838
/// Ignores the entity for which the fluent setup is made (Type, Property)
2939
/// </summary>

0 commit comments

Comments
 (0)