-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented the tests for
GameEngine
, load_hitbox()
, `get_factori…
…es()`, and `MapGenerator::get_enemy_limit()`. Fixed and improved the Python tests so that they work with the new `GameEngine` class. This required moving to a more mock-based design as the Python tests cannot initialise the components any more. `PhysicsSystem::add_bullet()` now uses the factories to initialise a bullet allowing all of them to be defined in one place. `load_hitbox()` now returns whether the load was successful or not.
- Loading branch information
1 parent
accfb95
commit 7754439
Showing
15 changed files
with
374 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Local headers | ||
#include "factories.hpp" | ||
#include "macros.hpp" | ||
|
||
/// Implements the fixture for the factories.hpp tests. | ||
class FactoriesFixture : public testing::Test { // NOLINT | ||
protected: | ||
void TearDown() override { clear_hitboxes(); } | ||
}; | ||
|
||
/// Test that loading a hitbox for a game object type works. | ||
TEST_F(FactoriesFixture, TestLoadHitboxOnce) { ASSERT_TRUE(load_hitbox(GameObjectType::Player, {{0.0, 0.0}})); } | ||
|
||
/// Test that loading two hitboxes for the same game object type doesn't do anything. | ||
TEST_F(FactoriesFixture, TestLoadHitboxTwice) { | ||
load_hitbox(GameObjectType::Player, {{0.0, 0.0}}); | ||
ASSERT_FALSE(load_hitbox(GameObjectType::Player, {{1.0, 1.0}})); | ||
} | ||
|
||
/// Test that loading a factory that doesn't require a hitbox works. | ||
TEST_F(FactoriesFixture, TestGetFactoryNoHitboxRequired) { | ||
ASSERT_NO_THROW(get_factories().at(GameObjectType::Floor)()); | ||
} | ||
|
||
/// Test that loading a factory that requires a hitbox works when the hitbox is loaded. | ||
TEST_F(FactoriesFixture, TestGetFactoryHitboxLoaded) { | ||
load_hitbox(GameObjectType::Player, {{0.0, 0.0}}); | ||
ASSERT_NO_THROW(get_factories().at(GameObjectType::Player)()); | ||
} | ||
|
||
/// Test that loading a factory that requires a hitbox which isn't loaded throws an exception. | ||
TEST_F(FactoriesFixture, TestGetFactoriesHitboxNotLoaded) { | ||
ASSERT_THROW_MESSAGE(get_factories().at(GameObjectType::Player)(), std::out_of_range, | ||
"invalid unordered_map<K, T> key"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Local headers | ||
#include "factories.hpp" | ||
#include "game_engine.hpp" | ||
#include "macros.hpp" | ||
|
||
/// Implements the fixture for the game_engine.hpp tests. | ||
class GameEngineFixture : public testing::Test { // NOLINT | ||
protected: | ||
/// The game engine object. | ||
GameEngine game_engine{0, 10}; | ||
|
||
void SetUp() override { | ||
load_hitbox(GameObjectType::Player, {{0, 0}}); | ||
load_hitbox(GameObjectType::Enemy, {{0, 0}}); | ||
} | ||
}; | ||
|
||
/// Test that the game engine is created correctly. | ||
TEST_F(GameEngineFixture, TestGameEngineZeroLevel) { | ||
ASSERT_NE(game_engine.get_registry(), nullptr); | ||
ASSERT_EQ(game_engine.get_player_id(), -1); | ||
} | ||
|
||
/// Test that the game engine throws an exception when given a negative level. | ||
TEST_F(GameEngineFixture, TestGameEngineNegativeLevel) { | ||
ASSERT_THROW_MESSAGE(GameEngine{-1}, std::length_error, "Level must be bigger than or equal to 0."); | ||
} | ||
|
||
/// Test that the game engine creates game objects correctly. | ||
TEST_F(GameEngineFixture, TestGameEngineCreateGameObjects) { | ||
game_engine.create_game_objects(); | ||
ASSERT_NE(game_engine.get_player_id(), -1); | ||
} | ||
|
||
/// Test that the game engine creates game objects correctly given no seed. | ||
TEST_F(GameEngineFixture, TestGameEngineCreateGameObjectsNoSeed) { | ||
GameEngine game_engine_no_seed{0}; | ||
game_engine.create_game_objects(); | ||
game_engine_no_seed.create_game_objects(); | ||
ASSERT_NE(game_engine.get_player_id(), game_engine_no_seed.get_player_id()); | ||
} | ||
|
||
/// Test that the game engine generates an enemy correctly. | ||
TEST_F(GameEngineFixture, TestGameEngineGenerateEnemy) { | ||
auto enemy_created{-1}; | ||
auto enemy_creation{[&](const GameObjectID enemy_id) { enemy_created = enemy_id; }}; | ||
game_engine.create_game_objects(); | ||
game_engine.get_registry()->add_callback(EventType::GameObjectCreation, enemy_creation); | ||
game_engine.generate_enemy(); | ||
ASSERT_NE(enemy_created, -1); | ||
} | ||
|
||
/// Test that the game engine throws an exception if the game objects haven't been created yet. | ||
TEST_F(GameEngineFixture, TestGameEngineGenerateEnemyNoGameObjects) { | ||
ASSERT_THROW_MESSAGE( | ||
game_engine.generate_enemy(), RegistryError, | ||
"The component `KinematicComponent` for the game object ID `-1` is not registered with the registry."); | ||
} | ||
|
||
/// Test that the game engine throws an exception if the player is dead. | ||
TEST_F(GameEngineFixture, TestGameEngineGenerateEnemyPlayerDead) { | ||
game_engine.create_game_objects(); | ||
game_engine.get_registry()->delete_game_object(game_engine.get_player_id()); | ||
ASSERT_THROW_MESSAGE( | ||
game_engine.generate_enemy(), RegistryError, | ||
"The component `KinematicComponent` for the game object ID `291` is not registered with the registry."); | ||
} | ||
|
||
/// Test that the game engine doesn't generate an enemy correctly if the enemy limit has been reached. | ||
TEST_F(GameEngineFixture, TestGameEngineGenerateEnemyLimit) { | ||
game_engine.create_game_objects(); | ||
for (auto i{0}; i < 10; i++) { | ||
game_engine.generate_enemy(); | ||
} | ||
auto enemy_created{-1}; | ||
auto enemy_creation{[&](const GameObjectID enemy_id) { enemy_created = enemy_id; }}; | ||
game_engine.get_registry()->add_callback(EventType::GameObjectCreation, enemy_creation); | ||
game_engine.generate_enemy(); | ||
ASSERT_EQ(enemy_created, -1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.