@@ -98,63 +98,114 @@ class FactoryTest : public Test {
9898TEST_F (FactoryTest, ConstructionTest) {
9999 ASSERT_EQ (factory->GetState (), FactoryStateName::UNBUILT);
100100
101- factory->Update ();
102- factory->LateUpdate ();
103-
104101 // Build the factory
105102 factory->IncrementConstructionCompletion (
106103 factory->GetTotalConstructionCompletion ());
107104
105+ // Expecting calls to gold manager
106+ EXPECT_CALL (*gold_manager, GetBalance (factory->GetPlayerId ()))
107+ .Times (2 )
108+ .WillRepeatedly (Return (1000 ));
109+ EXPECT_CALL (*gold_manager, GetCreateUnitCost (factory->GetProductionState ()))
110+ .Times (2 )
111+ .WillRepeatedly (Return (1000 ));
112+
108113 factory->Update ();
109114 factory->LateUpdate ();
110115
116+ // Factory is now in PRODUCTION state
111117 ASSERT_EQ (factory->GetState (), FactoryStateName::PRODUCTION);
112118 ASSERT_EQ (factory->GetProductionState (), ActorType::VILLAGER);
113119}
114120
115121TEST_F (FactoryTest, UnitProductionTest) {
116122 ASSERT_EQ (factory->GetState (), FactoryStateName::UNBUILT);
117123
118- factory->Update ();
119- factory->LateUpdate ();
120-
121124 // Build the factory
122125 factory->IncrementConstructionCompletion (
123126 factory->GetTotalConstructionCompletion ());
124127
128+ // Expecting gold manager calls when factory transitions from UNBUILD state
129+ // to PRODUCTION state
130+ EXPECT_CALL (*gold_manager, GetCreateUnitCost)
131+ .Times (2 )
132+ .WillRepeatedly (Return (1000 ));
133+ EXPECT_CALL (*gold_manager, GetBalance (factory->GetPlayerId ()))
134+ .Times (2 )
135+ .WillRepeatedly (Return (1000 ));
136+
137+ // Updating the factory state
138+ factory->Update ();
139+ factory->LateUpdate ();
140+
141+ // Factory is now in PRODUCTION state
142+ ASSERT_EQ (factory->GetState (), FactoryStateName::PRODUCTION);
143+
144+ // Producing villagers now
125145 factory->SetProductionState (ActorType::VILLAGER);
126146
147+ // Expecting gold manager calls
148+ EXPECT_CALL (*gold_manager, GetCreateUnitCost)
149+ .Times (21 * villager_frequency)
150+ .WillRepeatedly (Return (1000 ));
151+ EXPECT_CALL (*gold_manager, GetBalance (factory->GetPlayerId ()))
152+ .Times (21 * villager_frequency)
153+ .WillRepeatedly (Return (1000 ));
154+
155+ // The factory creates a soldier in the first tick itself
156+ ASSERT_EQ (villager_list.size (), 1 );
157+
127158 // After frequency no. of updates, a villager should have been produced
128159 for (int i = 0 ; i < villager_frequency; ++i) {
129160 factory->Update ();
130161 factory->LateUpdate ();
131162 }
132163
133- ASSERT_EQ (villager_list.size (), 1 );
164+ ASSERT_EQ (villager_list.size (), 2 );
134165
135166 // After 20 x frequency updates, 20 more villagers should have been produced
136167 for (int i = 0 ; i < 20 * villager_frequency; ++i) {
137168 factory->Update ();
138169 factory->LateUpdate ();
139170 }
140171
141- ASSERT_EQ (villager_list.size (), 1 + 20 );
172+ ASSERT_EQ (villager_list.size (), 2 + 20 );
142173}
143174
144175TEST_F (FactoryTest, SwitchUnitProductionTest) {
145176 ASSERT_EQ (factory->GetState (), FactoryStateName::UNBUILT);
146177
147- factory->Update ();
148- factory->LateUpdate ();
149-
150178 // Build the factory
151179 factory->IncrementConstructionCompletion (
152180 factory->GetTotalConstructionCompletion ());
153181
154182 factory->SetProductionState (ActorType::VILLAGER);
155183
184+ // Gold manager expecting calls
185+ EXPECT_CALL (*gold_manager, GetBalance (factory->GetPlayerId ()))
186+ .Times (2 )
187+ .WillRepeatedly (Return (1000 ));
188+ EXPECT_CALL (*gold_manager, GetCreateUnitCost (factory->GetProductionState ()))
189+ .Times (2 )
190+ .WillRepeatedly (Return (1000 ));
191+
192+ // Transitioning the villager from the UNBUILT state to the PRODUCTION
193+ factory->Update ();
194+ factory->LateUpdate ();
195+
196+ ASSERT_EQ (factory->GetState (), FactoryStateName::PRODUCTION);
197+
198+ // Gold manager expecting calls
199+ // Need to check why
200+ EXPECT_CALL (*gold_manager, GetBalance (factory->GetPlayerId ()))
201+ .Times (villager_frequency - 1 )
202+ .WillRepeatedly (Return (1000 ));
203+ EXPECT_CALL (*gold_manager, GetCreateUnitCost (factory->GetProductionState ()))
204+ .Times (villager_frequency - 1 )
205+ .WillRepeatedly (Return (1000 ));
206+
156207 // After frequency no. of updates, a villager should have been produced
157- for (int i = 0 ; i < villager_frequency; ++i) {
208+ for (int i = 0 ; i < villager_frequency - 1 ; ++i) {
158209 factory->Update ();
159210 factory->LateUpdate ();
160211 }
@@ -165,6 +216,13 @@ TEST_F(FactoryTest, SwitchUnitProductionTest) {
165216 factory->SetProductionState (ActorType::SOLDIER);
166217
167218 // After 20 x frequency updates, 20 soldiers should have been produced
219+ EXPECT_CALL (*gold_manager, GetBalance (factory->GetPlayerId ()))
220+ .Times (20 * soldier_frequency)
221+ .WillRepeatedly (Return (1000 ));
222+ EXPECT_CALL (*gold_manager, GetCreateUnitCost (factory->GetProductionState ()))
223+ .Times (20 * soldier_frequency)
224+ .WillRepeatedly (Return (1000 ));
225+
168226 for (int i = 0 ; i < 20 * soldier_frequency; ++i) {
169227 factory->Update ();
170228 factory->LateUpdate ();
@@ -176,31 +234,68 @@ TEST_F(FactoryTest, SwitchUnitProductionTest) {
176234TEST_F (FactoryTest, StopStartTest) {
177235 ASSERT_EQ (factory->GetState (), FactoryStateName::UNBUILT);
178236
179- factory-> Update ();
180- factory->LateUpdate ( );
237+ // Setting the production state to villager
238+ factory->SetProductionState (ActorType::SOLDIER );
181239
182240 // Build the factory and produce a unit
183241 factory->IncrementConstructionCompletion (
184242 factory->GetTotalConstructionCompletion ());
185243
244+ // Expecting calls to gold manager
245+ EXPECT_CALL (*gold_manager, GetBalance (factory->GetPlayerId ()))
246+ .Times (2 )
247+ .WillRepeatedly (Return (1000 ));
248+ EXPECT_CALL (*gold_manager, GetCreateUnitCost (factory->GetProductionState ()))
249+ .Times (2 )
250+ .WillRepeatedly (Return (1000 ));
251+
186252 factory->Update ();
187253 factory->LateUpdate ();
188254
255+ // The factory has transitioned from unbuilt state to production state
189256 ASSERT_EQ (factory->GetState (), FactoryStateName::PRODUCTION);
190257
258+ // Expect calls to GoldManager for getting balance and unit creation cost
259+ EXPECT_CALL (*gold_manager, GetBalance (factory->GetPlayerId ()))
260+ .Times (soldier_frequency - 1 )
261+ .WillRepeatedly (Return (1000 ));
262+ EXPECT_CALL (*gold_manager, GetCreateUnitCost (factory->GetProductionState ()))
263+ .Times (soldier_frequency - 1 )
264+ .WillRepeatedly (Return (1000 ));
265+
191266 // Produce a unit (- 1 since we ran 1 update just now)
192- for (int i = 0 ; i < villager_frequency - 1 ; ++i) {
267+ for (int i = 0 ; i < soldier_frequency - 1 ; ++i) {
193268 factory->Update ();
194269 factory->LateUpdate ();
195270 }
196- ASSERT_EQ (villager_list.size (), 1 );
271+
272+ // Expecting 2 calls to gold manager for transisioning from the PRODUCTION
273+ // state to the idle state
274+ EXPECT_CALL (*gold_manager, GetBalance (factory->GetPlayerId ()))
275+ .Times (2 )
276+ .WillRepeatedly (Return (1000 ));
277+ EXPECT_CALL (*gold_manager, GetCreateUnitCost)
278+ .Times (2 )
279+ .WillRepeatedly (Return (1000 ));
197280
198281 // Stop Production
199282 factory->Stop ();
200283 factory->Update ();
201284 factory->LateUpdate ();
202285 ASSERT_EQ (factory->GetState (), FactoryStateName::IDLE);
203286
287+ // NOTE: Factory is now in idle state
288+ // Hence, it will call the GetBalance and the GetCreateUnitCost function
289+ // every turn
290+
291+ // Expecting 2 more calls to gold manager for resuming production
292+ EXPECT_CALL (*gold_manager, GetBalance (factory->GetPlayerId ()))
293+ .Times (2 )
294+ .WillRepeatedly (Return (1000 ));
295+ EXPECT_CALL (*gold_manager, GetCreateUnitCost)
296+ .Times (2 )
297+ .WillRepeatedly (Return (1000 ));
298+
204299 // Resume Production
205300 factory->Start ();
206301 factory->Update ();
@@ -214,19 +309,27 @@ TEST_F(FactoryTest, DeathTest) {
214309 factory->Update ();
215310 factory->LateUpdate ();
216311
312+ // The factory is now in the IDLE state
217313 // Build the factory
218314 factory->IncrementConstructionCompletion (
219315 factory->GetTotalConstructionCompletion ());
220316
317+ // Calls are made in the idle state to GetCreateUnitCost and GetBalance
318+ EXPECT_CALL (*gold_manager, GetBalance (factory->GetPlayerId ()))
319+ .Times (2 )
320+ .WillRepeatedly (Return (1000 ));
321+ EXPECT_CALL (*gold_manager, GetCreateUnitCost (factory->GetProductionState ()))
322+ .Times (2 )
323+ .WillRepeatedly (Return (1000 ));
221324 factory->Update ();
222325 factory->LateUpdate ();
223326
327+ // Factory is now in production state
224328 ASSERT_EQ (factory->GetState (), FactoryStateName::PRODUCTION);
225329
226330 // Kill it
227331 factory->SetHp (0 );
228332 factory->Update ();
229333 factory->LateUpdate ();
230-
231334 ASSERT_EQ (factory->GetState (), FactoryStateName::DEAD);
232335}
0 commit comments