Skip to content

Commit 4160db7

Browse files
committed
Fixed bug in LoremIpsum plugin which why the same data was created multiple times.
1 parent 65aee74 commit 4160db7

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

ObjectFiller.Test/LoremIpsumPluginTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace ObjectFiller.Test
88
{
9+
using System.Collections.Generic;
10+
911
[TestClass]
1012
public class LoremIpsumPluginTest
1113
{
@@ -74,5 +76,22 @@ public void Test_With_LoremIpsum_Seed_Settings()
7476
Assert.IsNotNull(b1);
7577
Assert.AreEqual(b.ISBN, b1.ISBN);
7678
}
79+
80+
[TestMethod]
81+
public void LoremIpsum_should_provide_different_data()
82+
{
83+
var alowedDelta = 2;
84+
85+
var filler = new Filler<Book>();
86+
filler.Setup()
87+
.OnProperty(foo => foo.Description)
88+
.Use(new Lipsum(LipsumFlavor.LoremIpsum));
89+
90+
var resultElements = filler.Create(100);
91+
92+
var groupedResult = resultElements.GroupBy(x => x.Description);
93+
94+
Assert.AreEqual(100, groupedResult.Count(), alowedDelta);
95+
}
7796
}
7897
}

ObjectFiller/Plugins/String/Lipsum.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class Lipsum : IRandomizerPlugin<string>
3939
private readonly int maxSentences;
4040
private readonly int minWords;
4141
private readonly int maxWords;
42-
private readonly int seed;
42+
private readonly int? seed;
4343

4444
/// <summary>
4545
/// Words for the standard lorem ipsum text.
@@ -130,6 +130,8 @@ public class Lipsum : IRandomizerPlugin<string>
130130
/// </summary>
131131
private readonly Dictionary<LipsumFlavor, string[]> map;
132132

133+
private System.Random random;
134+
133135
/// <summary>
134136
/// Initializes a new instance of the <see cref="Lipsum"/> class.
135137
/// </summary>
@@ -152,7 +154,7 @@ public class Lipsum : IRandomizerPlugin<string>
152154
/// The max words of the generated text.
153155
/// </param>
154156
/// <param name="seed">
155-
/// The seed for the random to get the same result with the same seed.
157+
/// The seed for randomizer to get the same result with the same seed.
156158
/// </param>
157159
public Lipsum(LipsumFlavor flavor, int paragraphs = 3, int minSentences = 3, int maxSentences = 8,
158160
int minWords = 10, int maxWords = 50, int? seed = null)
@@ -172,7 +174,8 @@ public Lipsum(LipsumFlavor flavor, int paragraphs = 3, int minSentences = 3, int
172174
{ LipsumFlavor.LeMasque, LeMasque }
173175
};
174176

175-
this.seed = seed.HasValue ? seed.Value : Environment.TickCount;
177+
this.seed = seed;
178+
this.random = new System.Random();
176179
}
177180

178181
/// <summary>
@@ -181,20 +184,23 @@ public Lipsum(LipsumFlavor flavor, int paragraphs = 3, int minSentences = 3, int
181184
/// <returns>Random data for type <see cref="T"/></returns>
182185
public string GetValue()
183186
{
184-
System.Random rnd = new System.Random(this.seed);
185-
var array = this.map[this.flavor];
187+
if (this.seed.HasValue)
188+
{
189+
this.random = new System.Random(this.seed.Value);
190+
}
186191

192+
var array = this.map[this.flavor];
187193
var result = new StringBuilder();
188194

189195
for (var i = 0; i < this.paragraphs; i++)
190196
{
191-
var sentences = rnd.Next(this.minSentences, this.maxSentences + 1);
197+
var sentences = this.random.Next(this.minSentences, this.maxSentences + 1);
192198
for (var j = 0; j < sentences; j++)
193199
{
194-
var words = rnd.Next(this.minWords, this.maxWords + 1);
200+
var words = this.random.Next(this.minWords, this.maxWords + 1);
195201
for (var k = 0; k < words; k++)
196202
{
197-
var word = array[rnd.Next(array.Length)];
203+
var word = array[this.random.Next(array.Length)];
198204
if (k == 0)
199205
{
200206
word = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(word);

0 commit comments

Comments
 (0)