Skip to content

Commit f1f3dfb

Browse files
committed
Merge pull request #79 from Tynamix/70_Collectionizer_plugin
#70 Collectionizer implemented
2 parents ce9db60 + 9579bfb commit f1f3dfb

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Tynamix.ObjectFiller;
4+
using Xunit;
5+
6+
namespace ObjectFiller.Test
7+
{
8+
public class CollectionizerPoco
9+
{
10+
public IEnumerable<string> MnemonicStrings { get; set; }
11+
12+
public List<int> IntRange { get; set; }
13+
}
14+
15+
public class CollectionizerTest
16+
{
17+
[Fact]
18+
public void TestMnemonicStringPlugin()
19+
{
20+
var filler = new Filler<CollectionizerPoco>();
21+
22+
filler.Setup()
23+
.OnProperty(x => x.MnemonicStrings)
24+
.Use(new Collectionizer<string, MnemonicString>(new MnemonicString(1, 20, 25), 3, 10));
25+
26+
var collection = filler.Create();
27+
Assert.True(collection.MnemonicStrings.Count() >= 3 && collection.MnemonicStrings.Count() <= 10);
28+
Assert.True(collection.MnemonicStrings.All(x => x.Length >= 20 && x.Length <= 25));
29+
}
30+
31+
[Fact]
32+
public void TestIntRangePlugin()
33+
{
34+
var filler = new Filler<CollectionizerPoco>();
35+
36+
filler.Setup()
37+
.OnProperty(x => x.IntRange)
38+
.Use(new Collectionizer<int, IntRange>(new IntRange(10, 15), 3, 10));
39+
40+
var collection = filler.Create();
41+
Assert.True(collection.IntRange.Count >= 3 && collection.IntRange.Count() <= 10);
42+
Assert.True(collection.IntRange.All(x => x >= 10 && x <= 15));
43+
}
44+
}
45+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System.Collections.Generic;
2+
3+
namespace Tynamix.ObjectFiller
4+
{
5+
/// <summary>
6+
/// The collectionizer can be used to create <see cref="List{T}"/> based on a <see cref="IRandomizerPlugin{T}"/>.
7+
/// </summary>
8+
/// <typeparam name="T">Typeparameter of of the target List</typeparam>
9+
/// <typeparam name="TRandomizer">Plugin which will be used to create the List</typeparam>
10+
public class Collectionizer<T, TRandomizer> : IRandomizerPlugin<List<T>>
11+
where TRandomizer : IRandomizerPlugin<T>, new()
12+
{
13+
private readonly IRandomizerPlugin<T> randomizerToUse;
14+
private readonly int minCount;
15+
private readonly int maxCount;
16+
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="Collectionizer{T,TRandomizer}"/> class.
19+
/// </summary>
20+
public Collectionizer()
21+
: this(new TRandomizer())
22+
{
23+
}
24+
25+
/// <summary>
26+
/// Initializes a new instance of the <see cref="Collectionizer{T,TRandomizer}"/> class.
27+
/// </summary>
28+
/// <param name="minCount">Min count of list items</param>
29+
public Collectionizer(uint minCount)
30+
: this(new TRandomizer(), minCount)
31+
{
32+
}
33+
34+
/// <summary>
35+
/// Initializes a new instance of the <see cref="Collectionizer{T,TRandomizer}"/> class.
36+
/// </summary>
37+
/// <param name="minCount">Min count of list items</param>
38+
/// <param name="maxCount">Max count of list items</param>
39+
public Collectionizer(uint minCount, uint maxCount)
40+
: this(new TRandomizer(), minCount, maxCount)
41+
{
42+
}
43+
44+
/// <summary>
45+
/// Initializes a new instance of the <see cref="Collectionizer{T,TRandomizer}"/> class.
46+
/// </summary>
47+
/// <param name="randomizerToUse">The randomizer which will be used. Use this if you want
48+
/// to specifiy how the randomizer to use should work</param>
49+
public Collectionizer(TRandomizer randomizerToUse)
50+
: this(randomizerToUse, 10)
51+
{
52+
53+
}
54+
55+
/// <summary>
56+
/// Initializes a new instance of the <see cref="Collectionizer{T,TRandomizer}"/> class.
57+
/// </summary>
58+
/// <param name="randomizerToUse">The randomizer which will be used. Use this if you want
59+
/// to specifiy how the randomizer to use should work</param>
60+
/// <param name="minCount">Min count of list items</param>
61+
public Collectionizer(TRandomizer randomizerToUse, uint minCount)
62+
: this(randomizerToUse, minCount, 50)
63+
{
64+
}
65+
66+
/// <summary>
67+
/// Initializes a new instance of the <see cref="Collectionizer{T,TRandomizer}"/> class.
68+
/// </summary>
69+
/// <param name="randomizerToUse">The randomizer which will be used. Use this if you want
70+
/// to specifiy how the randomizer to use should work</param>
71+
/// <param name="minCount">Min count of list items</param>
72+
/// <param name="maxCount">Max count of list items</param>
73+
public Collectionizer(TRandomizer randomizerToUse, uint minCount, uint maxCount)
74+
{
75+
if (randomizerToUse == null)
76+
{
77+
randomizerToUse = new TRandomizer();
78+
}
79+
80+
if (minCount > maxCount)
81+
{
82+
maxCount = minCount;
83+
minCount = maxCount;
84+
}
85+
86+
this.randomizerToUse = randomizerToUse;
87+
this.minCount = (int)minCount;
88+
this.maxCount = (int)maxCount;
89+
}
90+
91+
92+
/// <summary>
93+
/// Gets random data for type <see cref="T"/>
94+
/// </summary>
95+
/// <returns>Random data for type <see cref="T"/></returns>
96+
public List<T> GetValue()
97+
{
98+
var count = Randomizer<int>.Create(new IntRange(this.minCount, this.maxCount));
99+
List<T> result = new List<T>();
100+
101+
for (int i = 0; i < count; i++)
102+
{
103+
result.Add(this.randomizerToUse.GetValue());
104+
}
105+
106+
return result;
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)