1+ using System . Collections . Generic ;
2+ using System . Linq ;
3+ using Shouldly ;
4+ using TestStack . Dossier . DataSources . Generators ;
5+ using TestStack . Dossier . Lists ;
6+ using TestStack . Dossier . Tests . Builders ;
7+ using TestStack . Dossier . Tests . Entities ;
8+ using Xunit ;
9+
10+ namespace TestStack . Dossier . Tests
11+ {
12+ public class BuilderBuildListTests
13+ {
14+ [ Fact ]
15+ public void GivenANormalBuilderInstance_WhenCallingIsListBuilderProxy_ThenReturnFalse ( )
16+ {
17+ var builder = Builder < Customer > . CreateNew ( ) ;
18+
19+ builder . IsListBuilderProxy ( ) . ShouldBe ( false ) ;
20+ }
21+
22+ [ Fact ]
23+ public void GivenAListBuilderProxyInstance_WhenCallingIsListBuilderProxy_ThenReturnTrue ( )
24+ {
25+ var builder = Builder < Customer > . CreateListOfSize ( 1 ) . TheFirst ( 1 ) ;
26+
27+ builder . IsListBuilderProxy ( ) . ShouldBe ( true ) ;
28+ }
29+
30+ [ Fact ]
31+ public void GivenListOfBuilders_WhenCallingBuildListExplicitly_ThenAListOfEntitiesOfTheRightSizeShouldBeReturned ( )
32+ {
33+ var builders = Builder < Customer > . CreateListOfSize ( 5 ) ;
34+
35+ var entities = builders . BuildList ( ) ;
36+
37+ entities . Count . ShouldBe ( 5 ) ;
38+ }
39+
40+ [ Fact ]
41+ public void GivenListOfBuilders_WhenCallingBuildListImplicitly_ThenAListOfEntitiesOfTheRightSizeShouldBeReturned ( )
42+ {
43+ List < Customer > entities = Builder < Customer > . CreateListOfSize ( 5 ) ;
44+
45+ entities . Count . ShouldBe ( 5 ) ;
46+ }
47+
48+ [ Fact ]
49+ public void GivenListOfBuilders_WhenCallingBuildListExplicitly_ThenAListOfEntitiesOfTheRightTypeShouldBeReturned ( )
50+ {
51+ var builders = Builder < Customer > . CreateListOfSize ( 5 ) ;
52+
53+ var entities = builders . BuildList ( ) ;
54+
55+ entities . ShouldBeAssignableTo < IList < Customer > > ( ) ;
56+ }
57+
58+ [ Fact ]
59+ public void GivenListOfBuilders_WhenCallingBuildListImplicitly_ThenAListOfEntitiesOfTheRightTypeShouldBeReturned ( )
60+ {
61+ List < Customer > entities = Builder < Customer > . CreateListOfSize ( 5 ) ;
62+
63+ entities . ShouldBeAssignableTo < IList < Customer > > ( ) ;
64+ }
65+
66+ [ Fact ]
67+ public void GivenListOfBuilders_WhenCallingBuildListExplicitly_ThenAListOfUniqueEntitiesShouldBeReturned ( )
68+ {
69+ var builders = Builder < Customer > . CreateListOfSize ( 5 ) ;
70+
71+ var entities = builders . BuildList ( ) ;
72+
73+ entities [ 0 ] . ShouldNotBe ( entities [ 1 ] ) ;
74+ entities [ 0 ] . ShouldNotBe ( entities [ 2 ] ) ;
75+ entities [ 0 ] . ShouldNotBe ( entities [ 3 ] ) ;
76+ entities [ 0 ] . ShouldNotBe ( entities [ 4 ] ) ;
77+ entities [ 1 ] . ShouldNotBe ( entities [ 2 ] ) ;
78+ entities [ 1 ] . ShouldNotBe ( entities [ 3 ] ) ;
79+ entities [ 1 ] . ShouldNotBe ( entities [ 4 ] ) ;
80+ entities [ 2 ] . ShouldNotBe ( entities [ 3 ] ) ;
81+ entities [ 2 ] . ShouldNotBe ( entities [ 4 ] ) ;
82+ entities [ 3 ] . ShouldNotBe ( entities [ 4 ] ) ;
83+ }
84+
85+ [ Fact ]
86+ public void GivenListOfBuilders_WhenCallingBuildListImplicitly_ThenAListOfUniqueEntitiesShouldBeReturned ( )
87+ {
88+ List < Customer > entities = Builder < Customer > . CreateListOfSize ( 5 ) ;
89+
90+ entities [ 0 ] . ShouldNotBe ( entities [ 1 ] ) ;
91+ entities [ 0 ] . ShouldNotBe ( entities [ 2 ] ) ;
92+ entities [ 0 ] . ShouldNotBe ( entities [ 3 ] ) ;
93+ entities [ 0 ] . ShouldNotBe ( entities [ 4 ] ) ;
94+ entities [ 1 ] . ShouldNotBe ( entities [ 2 ] ) ;
95+ entities [ 1 ] . ShouldNotBe ( entities [ 3 ] ) ;
96+ entities [ 1 ] . ShouldNotBe ( entities [ 4 ] ) ;
97+ entities [ 2 ] . ShouldNotBe ( entities [ 3 ] ) ;
98+ entities [ 2 ] . ShouldNotBe ( entities [ 4 ] ) ;
99+ entities [ 3 ] . ShouldNotBe ( entities [ 4 ] ) ;
100+ }
101+
102+ [ Fact ]
103+ public void GivenListOfBuildersWithCustomisation_WhenBuildingEntitiesExplicitly_ThenTheCustomisationShouldTakeEffect ( )
104+ {
105+ var generator = new SequentialGenerator ( 0 , 100 ) ;
106+ var list = CustomerBuilder . CreateListOfSize ( 3 )
107+ . All ( ) . With ( b => b . WithFirstName ( generator . Generate ( ) . ToString ( ) ) ) ;
108+
109+ var data = list . BuildList ( ) ;
110+
111+ data . Select ( c => c . FirstName ) . ToArray ( )
112+ . ShouldBe ( new [ ] { "0" , "1" , "2" } ) ;
113+ }
114+
115+ [ Fact ]
116+ public void GivenListOfBuildersWithCustomisation_WhenBuildingEntitiesImplicitly_ThenTheCustomisationShouldTakeEffect ( )
117+ {
118+ var generator = new SequentialGenerator ( 0 , 100 ) ;
119+
120+ List < Customer > data = CustomerBuilder . CreateListOfSize ( 3 )
121+ . All ( ) . With ( b => b . WithFirstName ( generator . Generate ( ) . ToString ( ) ) ) ;
122+
123+ data . Select ( c => c . FirstName ) . ToArray ( )
124+ . ShouldBe ( new [ ] { "0" , "1" , "2" } ) ;
125+ }
126+
127+ [ Fact ]
128+ public void GivenListOfBuildersWithARangeOfCustomisationMethods_WhenBuildingEntitiesExplicitly_ThenThenTheListIsBuiltAndModifiedCorrectly ( )
129+ {
130+ var i = 0 ;
131+ var customers = CustomerBuilder . CreateListOfSize ( 5 )
132+ . TheFirst ( 1 ) . WithFirstName ( "First" )
133+ . TheNext ( 1 ) . WithLastName ( "Next Last" )
134+ . TheLast ( 1 ) . WithLastName ( "Last Last" )
135+ . ThePrevious ( 2 ) . With ( b => b . WithLastName ( "last" + ( ++ i ) . ToString ( ) ) )
136+ . All ( ) . WhoJoinedIn ( 1999 )
137+ . BuildList ( ) ;
138+
139+ customers . ShouldBeAssignableTo < IList < Customer > > ( ) ;
140+ customers . Count . ShouldBe ( 5 ) ;
141+ customers [ 0 ] . FirstName . ShouldBe ( "First" ) ;
142+ customers [ 1 ] . LastName . ShouldBe ( "Next Last" ) ;
143+ customers [ 2 ] . LastName . ShouldBe ( "last1" ) ;
144+ customers [ 3 ] . LastName . ShouldBe ( "last2" ) ;
145+ customers [ 4 ] . LastName . ShouldBe ( "Last Last" ) ;
146+ customers . ShouldAllBe ( c => c . YearJoined == 1999 ) ;
147+ }
148+
149+ [ Fact ]
150+ public void GivenListOfBuildersWithARangeOfCustomisationMethods_WhenBuildingEntitiesImplicitly_ThenThenTheListIsBuiltAndModifiedCorrectly ( )
151+ {
152+ var i = 0 ;
153+ List < Customer > customers = CustomerBuilder . CreateListOfSize ( 5 )
154+ . TheFirst ( 1 ) . WithFirstName ( "First" )
155+ . TheNext ( 1 ) . WithLastName ( "Next Last" )
156+ . TheLast ( 1 ) . WithLastName ( "Last Last" )
157+ . ThePrevious ( 2 ) . With ( b => b . WithLastName ( "last" + ( ++ i ) . ToString ( ) ) )
158+ . All ( ) . WhoJoinedIn ( 1999 ) ;
159+
160+ customers . ShouldBeAssignableTo < IList < Customer > > ( ) ;
161+ customers . Count . ShouldBe ( 5 ) ;
162+ customers [ 0 ] . FirstName . ShouldBe ( "First" ) ;
163+ customers [ 1 ] . LastName . ShouldBe ( "Next Last" ) ;
164+ customers [ 2 ] . LastName . ShouldBe ( "last1" ) ;
165+ customers [ 3 ] . LastName . ShouldBe ( "last2" ) ;
166+ customers [ 4 ] . LastName . ShouldBe ( "Last Last" ) ;
167+ customers . ShouldAllBe ( c => c . YearJoined == 1999 ) ;
168+ }
169+
170+ [ Fact ]
171+ public void WhenBuildingEntitiesExplicitly_ThenTheAnonymousValueFixtureIsSharedAcrossBuilders ( )
172+ {
173+ var customers = CustomerBuilder . CreateListOfSize ( 5 ) . BuildList ( ) ;
174+
175+ customers [ 0 ] . CustomerClass . ShouldBe ( CustomerClass . Normal ) ;
176+ customers [ 1 ] . CustomerClass . ShouldBe ( CustomerClass . Bronze ) ;
177+ customers [ 2 ] . CustomerClass . ShouldBe ( CustomerClass . Silver ) ;
178+ customers [ 3 ] . CustomerClass . ShouldBe ( CustomerClass . Gold ) ;
179+ customers [ 4 ] . CustomerClass . ShouldBe ( CustomerClass . Platinum ) ;
180+ }
181+
182+ [ Fact ]
183+ public void WhenBuildingEntitiesImplicitly_ThenTheAnonymousValueFixtureIsSharedAcrossBuilders ( )
184+ {
185+ List < Customer > customers = CustomerBuilder . CreateListOfSize ( 5 ) ;
186+
187+ customers [ 0 ] . CustomerClass . ShouldBe ( CustomerClass . Normal ) ;
188+ customers [ 1 ] . CustomerClass . ShouldBe ( CustomerClass . Bronze ) ;
189+ customers [ 2 ] . CustomerClass . ShouldBe ( CustomerClass . Silver ) ;
190+ customers [ 3 ] . CustomerClass . ShouldBe ( CustomerClass . Gold ) ;
191+ customers [ 4 ] . CustomerClass . ShouldBe ( CustomerClass . Platinum ) ;
192+ }
193+ }
194+ }
0 commit comments