Skip to content

Commit e44f847

Browse files
Add expectations for GoldManager mock
- Add GoldManager expectations for villager_test.cpp - Add GoldManager expectations for soldier_test.cpp - Add GoldManager expectations for logger_test.cpp - Add GoldManager expectations for factory_test.cpp - Add GoldManager expectations for state_test.cpp
1 parent d9ca328 commit e44f847

File tree

10 files changed

+229
-76
lines changed

10 files changed

+229
-76
lines changed

src/state/include/state/gold_manager/gold_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class STATE_EXPORT GoldManager : public IGoldManager {
189189
/**
190190
* Function to add build request to current requests
191191
*/
192-
void AddBuildRequest(PlayerId player_id, Vec2D offset) override;
192+
void AddMineRequest(PlayerId player_id, Vec2D offset) override;
193193

194194
/**
195195
* Function to assign amount of gold to be given to each player

src/state/include/state/interfaces/i_gold_manager.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ class STATE_EXPORT IGoldManager {
8080
/**
8181
* Function to add build request to current requests
8282
*/
83-
84-
virtual void AddBuildRequest(PlayerId player_id, Vec2D offset) = 0;
83+
virtual void AddMineRequest(PlayerId player_id, Vec2D offset) = 0;
8584

8685
/**
8786
* Function to assign amount of gold to be given to each player

src/state/src/actor/villager_states/villager_mine_state.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ std::unique_ptr<IActorState> VillagerMineState::Update() {
5353
}
5454

5555
// Mine gold
56-
villager->GetGoldManager()->AddBuildRequest(villager->GetPlayerId(),
57-
villager->GetMineTarget());
56+
villager->GetGoldManager()->AddMineRequest(villager->GetPlayerId(),
57+
villager->GetMineTarget());
5858

5959
return nullptr;
6060
}

src/state/src/gold_manager/gold_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void GoldManager::RewardMineGold(PlayerId player_id, GoldMine *gold_mine,
128128
Increase(player_id, extracted_amount);
129129
}
130130

131-
void GoldManager::AddBuildRequest(PlayerId player_id, Vec2D offset) {
131+
void GoldManager::AddMineRequest(PlayerId player_id, Vec2D offset) {
132132
int64_t id = static_cast<int64_t>(player_id);
133133
auto player_requests = this->mine_requests[id];
134134
auto gold_mine = GetGoldMine(offset);

test/drivers/main_driver_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MainDriverTest : public testing::Test {
5757
const vector<string> MainDriverTest::shared_memory_names = {"ShmTest1",
5858
"ShmTest2"};
5959
const int MainDriverTest::num_turns = pow(10, 4);
60-
const int MainDriverTest::time_limit_ms = 1000;
60+
const int MainDriverTest::time_limit_ms = 2000;
6161
const int MainDriverTest::turn_instruction_limit = 5;
6262
const int MainDriverTest::game_instruction_limit = 10;
6363

test/state/command_giver_test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,10 @@ TEST_F(CommandGiverTest, CommandExecutionTest) {
515515
this->player_states, ActorType::VILLAGER);
516516

517517
// Making villager create a factory on water
518-
EXPECT_CALL(*this->logger, LogError(PlayerId::PLAYER1,
519-
ErrorType::INVALID_BUILD_POSITION, _));
518+
EXPECT_CALL(
519+
*this->logger,
520+
LogError(PlayerId::PLAYER1, ErrorType::NO_BUILD_FACTORY_ON_WATER, _));
521+
520522
// (0, 0) is a water tile
521523
this->player_states[0].villagers[0].build_position = Vec2D(0, 0);
522524
ManageActorExpectations(state_soldiers, state_villagers, state_factories,

test/state/factory_test.cpp

Lines changed: 120 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,63 +98,114 @@ class FactoryTest : public Test {
9898
TEST_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

115121
TEST_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

144175
TEST_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) {
176234
TEST_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
}

test/state/mocks/gold_manager_mock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ class GoldManagerMock : public IGoldManager {
1616
MOCK_METHOD0(GetMaxGold, int64_t());
1717
MOCK_METHOD1(DeductFactorySuicidePenalty, void(PlayerId));
1818
MOCK_METHOD3(RewardMineGold, void(PlayerId, GoldMine *, int64_t));
19-
MOCK_METHOD2(AddBuildRequest, void(PlayerId, Vec2D));
19+
MOCK_METHOD2(AddMineRequest, void(PlayerId, Vec2D));
2020
MOCK_METHOD0(AssignGold, void());
2121
};

0 commit comments

Comments
 (0)