Skip to content

Commit 365cd06

Browse files
committed
Merge pull request #49 from mwhelan/picking
Added Pick functionality to builders
2 parents 6b04558 + 42af077 commit 365cd06

File tree

7 files changed

+143
-0
lines changed

7 files changed

+143
-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.DataSources.Picking;
4+
using TestStack.Dossier.Lists;
5+
using TestStack.Dossier.Tests.TestHelpers.Objects.Entities;
6+
using Xunit;
7+
8+
namespace TestStack.Dossier.Tests.DataSources.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="DataSources\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.

TestStack.Dossier/DataSources/DataSource.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public IList<T> Data
4040
}
4141
return _list;
4242
}
43+
internal set { _list = value; }
4344
}
4445

4546
/// <summary>
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.DataSources.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 RandomItemSource class.</returns>
16+
public static RandomItemSource<T> RandomItemFrom<T>(IList<T> list)
17+
{
18+
return new RandomItemSource<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 RepeatingSequenceSource<T> RepeatingSequenceFrom<T>(IList<T> list)
28+
{
29+
return new RepeatingSequenceSource<T>(list);
30+
}
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace TestStack.Dossier.DataSources.Picking
5+
{
6+
/// <summary>
7+
/// Implements the random item strategy
8+
/// </summary>
9+
public class RandomItemSource<T> : DataSource<T>
10+
{
11+
/// <inheritdoc />
12+
public RandomItemSource(IList<T> list)
13+
{
14+
Data = list;
15+
Generator.StartIndex = 0;
16+
Generator.ListSize = Data.Count;
17+
}
18+
19+
/// <inheritdoc />
20+
protected override IList<T> InitializeDataSource()
21+
{
22+
// This method will never be called as the list is set in the constructor.
23+
throw new NotImplementedException();
24+
}
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using TestStack.Dossier.DataSources.Generators;
4+
5+
namespace TestStack.Dossier.DataSources.Picking
6+
{
7+
/// <summary>
8+
/// Implements the repeatable sequence strategy
9+
/// </summary>
10+
public class RepeatingSequenceSource<T> : DataSource<T>
11+
{
12+
/// <inheritdoc />
13+
public RepeatingSequenceSource(IList<T> list)
14+
: base(new SequentialGenerator())
15+
{
16+
Data = list;
17+
Generator.StartIndex = 0;
18+
Generator.ListSize = Data.Count;
19+
}
20+
21+
/// <inheritdoc />
22+
protected override IList<T> InitializeDataSource()
23+
{
24+
// This method will never be called as the list is set in the constructor.
25+
throw new NotImplementedException();
26+
}
27+
}
28+
}

TestStack.Dossier/TestStack.Dossier.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
<ItemGroup>
5353
<Compile Include="AnonymousValueFixture.cs" />
5454
<Compile Include="Builder.cs" />
55+
<Compile Include="DataSources\Picking\Pick.cs" />
56+
<Compile Include="DataSources\Picking\RandomItemSource.cs" />
57+
<Compile Include="DataSources\Picking\RepeatingSequenceSource.cs" />
5558
<Compile Include="ITestDataBuilder.cs" />
5659
<Compile Include="DataSources\Dictionaries\Cache.cs" />
5760
<Compile Include="DataSources\Dictionaries\IDictionaryRepository.cs" />
@@ -179,6 +182,9 @@
179182
<EmbeddedResource Include="DataSources\Dictionaries\Resources\PersonUsername.txt" />
180183
<EmbeddedResource Include="DataSources\Dictionaries\Resources\ShirtSize.txt" />
181184
</ItemGroup>
185+
<ItemGroup>
186+
<Folder Include="Picking\" />
187+
</ItemGroup>
182188
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
183189
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
184190
Other similar extension points exist, see Microsoft.Common.targets.

0 commit comments

Comments
 (0)