Skip to content

Commit 58dff07

Browse files
committed
Merge pull request #19 from Tynamix/ENHANCE_RandomListPlugin
Improved the random list plugin a lot!
2 parents 0fe3ab6 + 6925980 commit 58dff07

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

ObjectFiller.Test/ObjectFillerTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ public void TestFillPerson()
1919
.OnType<IAddress>().CreateInstanceOf<Address>()
2020
.OnType<string>().Use(new MnemonicString(10))
2121
.OnProperty(person => person.FirstName).Use(new MnemonicString(1))
22-
.OnProperty(person => person.LastName).Use(new RandomListItem<string>(new List<string>() { "Maik", "Tom", "Anton" }))
22+
.OnProperty(person => person.LastName).Use(new RandomListItem<string>("Maik", "Tom", "Anton"))
2323
.OnProperty(person => person.Age).Use(() => new Random().Next(12, 83))
2424
.SetupFor<Address>()
2525
.OnProperty(x => x.City, x => x.Country).IgnoreIt();
2626

2727
Person pFilled = filler.Fill(p);
28+
29+
Assert.IsTrue(new List<string>() { "Maik", "Tom", "Anton" }.Contains(pFilled.LastName));
2830
}
2931

3032
[TestMethod]

ObjectFiller/Plugins/RandomListItem.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Linq;
45

56
namespace Tynamix.ObjectFiller
67
{
78
public class RandomListItem<T> : IRandomizerPlugin<T>
89
{
9-
private readonly List<T> _allAvailableValues;
10+
private readonly T[] _allAvailableValues;
1011

11-
public RandomListItem(List<T> allAvailableValues)
12+
public RandomListItem(params T[] allAvailableValues)
1213
{
13-
if (allAvailableValues == null || allAvailableValues.Count == 0)
14+
if (allAvailableValues == null || allAvailableValues.Length == 0)
1415
{
15-
const string message = "List in RandomListItem ranomizer can not be empty!";
16-
Debug.WriteLine("ObjectFiller: " + message);
17-
throw new ArgumentException(message);
16+
const string message = "List in RandomListItem ranomizer can not be empty!";
17+
Debug.WriteLine("ObjectFiller: " + message);
18+
throw new ArgumentException(message);
1819
}
19-
_allAvailableValues = allAvailableValues;
20+
_allAvailableValues = allAvailableValues;
21+
}
22+
23+
public RandomListItem(IEnumerable<T> allAvailableValues)
24+
: this(allAvailableValues.ToArray())
25+
{
2026

2127
}
2228

29+
2330
public T GetValue()
2431
{
25-
int rndmListIndex = Random.Next(0, _allAvailableValues.Count);
32+
int rndmListIndex = Random.Next(0, _allAvailableValues.Length - 1);
2633
return _allAvailableValues[rndmListIndex];
2734
}
2835
}

0 commit comments

Comments
 (0)