File tree Expand file tree Collapse file tree 7 files changed +164
-0
lines changed Expand file tree Collapse file tree 7 files changed +164
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 58
58
<Compile Include =" Builder_CreateListTests.cs" />
59
59
<Compile Include =" Builder_CreateNewTests.cs" />
60
60
<Compile Include =" Builder_SetUsingBuilderTests.cs" />
61
+ <Compile Include =" Picking\PickingTests.cs" />
61
62
<Compile Include =" TestHelpers\Builders\AddressViewModelBuilder.cs" />
62
63
<Compile Include =" TestHelpers\Builders\AutoConstructorCustomerBuilder.cs" />
63
64
<Compile Include =" ChildBuilderTests.cs" />
119
120
<Name >TestStack.Dossier</Name >
120
121
</ProjectReference >
121
122
</ItemGroup >
123
+ <ItemGroup />
122
124
<Import Project =" $(MSBuildToolsPath)\Microsoft.CSharp.targets" />
123
125
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
124
126
Other similar extension points exist, see Microsoft.Common.targets.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 94
94
<Compile Include =" NullingExpandoObject.cs" />
95
95
<Compile Include =" Factories\IFactory.cs" />
96
96
<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" />
97
101
<Compile Include =" Reflector.cs" />
98
102
<Compile Include =" Suppliers\DefaultEmailValueSupplier.cs" />
99
103
<Compile Include =" Suppliers\DefaultLastNameValueSupplier.cs" />
You can’t perform that action at this time.
0 commit comments