Skip to content

Commit 9eea6a1

Browse files
committed
Added Pick functionality to builders
1 parent 6b04558 commit 9eea6a1

File tree

7 files changed

+164
-0
lines changed

7 files changed

+164
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Linq;
2+
using Shouldly;
3+
using TestStack.Dossier.Lists;
4+
using TestStack.Dossier.Picking;
5+
using TestStack.Dossier.Tests.TestHelpers.Objects.Entities;
6+
using Xunit;
7+
8+
namespace TestStack.Dossier.Tests.Picking
9+
{
10+
public class PickingTests
11+
{
12+
[Fact]
13+
public void RandomItemFrom_should_add_items_from_list_randomly()
14+
{
15+
var addresses = Builder<Address>.CreateListOfSize(15).BuildList();
16+
var customers = Builder<Customer>
17+
.CreateListOfSize(15)
18+
.All()
19+
.Set(x => x.PostalAddress, Pick.RandomItemFrom(addresses).Next)
20+
.BuildList();
21+
22+
var uniqueAddresses = customers.Select(x => x.PostalAddress).Distinct().Count();
23+
uniqueAddresses.ShouldBeGreaterThan(3);
24+
uniqueAddresses.ShouldBeLessThan(15);
25+
}
26+
27+
[Fact]
28+
public void RepeatingSequenceFrom_should_add_items_from_list_sequentially_and_repeat_when_list_completes()
29+
{
30+
var addresses = Builder<Address>.CreateListOfSize(3).BuildList();
31+
var customers = Builder<Customer>
32+
.CreateListOfSize(9)
33+
.All()
34+
.Set(x => x.PostalAddress, Pick.RepeatingSequenceFrom(addresses).Next)
35+
.BuildList();
36+
37+
for (int i = 0; i < 2; i++)
38+
{
39+
var address = customers[i].PostalAddress;
40+
address.ShouldBeSameAs(customers[i + 3].PostalAddress);
41+
address.ShouldBeSameAs(customers[i + 6].PostalAddress);
42+
43+
address.ShouldNotBeSameAs(customers[i + 1].PostalAddress);
44+
address.ShouldNotBeSameAs(customers[i + 2].PostalAddress);
45+
}
46+
}
47+
}
48+
}

TestStack.Dossier.Tests/TestStack.Dossier.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<Compile Include="Builder_CreateListTests.cs" />
5959
<Compile Include="Builder_CreateNewTests.cs" />
6060
<Compile Include="Builder_SetUsingBuilderTests.cs" />
61+
<Compile Include="Picking\PickingTests.cs" />
6162
<Compile Include="TestHelpers\Builders\AddressViewModelBuilder.cs" />
6263
<Compile Include="TestHelpers\Builders\AutoConstructorCustomerBuilder.cs" />
6364
<Compile Include="ChildBuilderTests.cs" />
@@ -119,6 +120,7 @@
119120
<Name>TestStack.Dossier</Name>
120121
</ProjectReference>
121122
</ItemGroup>
123+
<ItemGroup />
122124
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
123125
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
124126
Other similar extension points exist, see Microsoft.Common.targets.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
using TestStack.Dossier.DataSources.Generators;
3+
4+
namespace TestStack.Dossier.Picking
5+
{
6+
/// <summary>
7+
/// The base class with all of the item picking functionality
8+
/// </summary>
9+
/// <typeparam name="T"></typeparam>
10+
public abstract class ItemPicker<T>
11+
{
12+
private readonly IList<T> _list;
13+
private readonly IGenerator _generator;
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="ItemPicker{T}"/> class.
17+
/// </summary>
18+
/// <param name="list">The list.</param>
19+
/// <param name="generator">The generator.</param>
20+
protected ItemPicker(IList<T> list, IGenerator generator)
21+
{
22+
_list = list;
23+
_generator = generator;
24+
_generator.StartIndex = 0;
25+
_generator.ListSize = _list.Count;
26+
}
27+
28+
/// <summary>
29+
/// Called to return the next item in the sequence.
30+
/// </summary>
31+
/// <returns></returns>
32+
public T Next()
33+
{
34+
var index = _generator.Generate();
35+
return _list[index];
36+
}
37+
}
38+
}

TestStack.Dossier/Picking/Pick.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Collections.Generic;
2+
3+
namespace TestStack.Dossier.Picking
4+
{
5+
/// <summary>
6+
/// Pick a sequence of items from a collection of items according to different selection strategies.
7+
/// </summary>
8+
public class Pick
9+
{
10+
/// <summary>
11+
/// Selects a random item from the list each time it is called.
12+
/// </summary>
13+
/// <typeparam name="T"></typeparam>
14+
/// <param name="list">The list.</param>
15+
/// <returns>The RandomItemPicker class.</returns>
16+
public static RandomItemPicker<T> RandomItemFrom<T>(IList<T> list)
17+
{
18+
return new RandomItemPicker<T>(list);
19+
}
20+
21+
/// <summary>
22+
/// Selects each item sequentially from the list and starts again from the beginning when the list is exhausted.
23+
/// </summary>
24+
/// <typeparam name="T"></typeparam>
25+
/// <param name="list">The list.</param>
26+
/// <returns></returns>
27+
public static RepeatingSequenceItemPicker<T> RepeatingSequenceFrom<T>(IList<T> list)
28+
{
29+
return new RepeatingSequenceItemPicker<T>(list);
30+
}
31+
}
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
using TestStack.Dossier.DataSources.Generators;
3+
4+
namespace TestStack.Dossier.Picking
5+
{
6+
/// <summary>
7+
/// Implements the random item strategy
8+
/// </summary>
9+
/// <typeparam name="T"></typeparam>
10+
/// <seealso cref="ItemPicker{T}" />
11+
public class RandomItemPicker<T> : ItemPicker<T>
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="RandomItemPicker{T}"/> class.
15+
/// </summary>
16+
/// <param name="list">The list.</param>
17+
public RandomItemPicker(IList<T> list)
18+
: base(list, new RandomGenerator()) { }
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
using TestStack.Dossier.DataSources.Generators;
3+
4+
namespace TestStack.Dossier.Picking
5+
{
6+
/// <summary>
7+
/// Implements the repeatable sequence strategy
8+
/// </summary>
9+
/// <typeparam name="T"></typeparam>
10+
/// <seealso cref="ItemPicker{T}" />
11+
public class RepeatingSequenceItemPicker<T> : ItemPicker<T>
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="RepeatingSequenceItemPicker{T}"/> class.
15+
/// </summary>
16+
/// <param name="list">The list.</param>
17+
public RepeatingSequenceItemPicker(IList<T> list)
18+
: base(list, new SequentialGenerator()) { }
19+
}
20+
}

TestStack.Dossier/TestStack.Dossier.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@
9494
<Compile Include="NullingExpandoObject.cs" />
9595
<Compile Include="Factories\IFactory.cs" />
9696
<Compile Include="PathExpressionVisitor.cs" />
97+
<Compile Include="Picking\ItemPicker.cs" />
98+
<Compile Include="Picking\Pick.cs" />
99+
<Compile Include="Picking\RandomItemPicker.cs" />
100+
<Compile Include="Picking\RepeatingSequenceItemPicker.cs" />
97101
<Compile Include="Reflector.cs" />
98102
<Compile Include="Suppliers\DefaultEmailValueSupplier.cs" />
99103
<Compile Include="Suppliers\DefaultLastNameValueSupplier.cs" />

0 commit comments

Comments
 (0)