Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement all possible tests - Zappy GUI #218

Merged
merged 18 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gui/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ TEST_FILES = Network/TestNetwork.cpp \
Render/TestDecoration.cpp \
GUIUpdater/TestGUIUpdater.cpp \
Event/TestEvent.cpp \
Mocks/TestMocks.cpp \

OBJ = $(SRC:.cpp=.o)
MAIN_OBJ = $(MAIN:.cpp=.o)
Expand Down
14 changes: 14 additions & 0 deletions gui/include/Event/AEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ class Gui::AEvent : public Gui::IEvent {
*/
void setGameData(std::shared_ptr<GameData> gameData);

/**
* @brief Get the Render object.
*
* @return std::shared_ptr<Render> - Render class.
*/
std::shared_ptr<Render> getRender();

/**
* @brief Get the GameData object.
*
* @return std::shared_ptr<GameData> - GameData class.
*/
std::shared_ptr<GameData> getGameData();

protected:

std::shared_ptr<Render> _render; //!< Render class to draw scene.
Expand Down
10 changes: 10 additions & 0 deletions gui/src/Event/AEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ void Gui::AEvent::setGameData(std::shared_ptr<GameData> gameData)
{
_gameData = gameData;
}

std::shared_ptr<Gui::Render> Gui::AEvent::getRender()
{
return _render;
}

std::shared_ptr<Gui::GameData> Gui::AEvent::getGameData()
{
return _gameData;
}
17 changes: 17 additions & 0 deletions tests/gui/tests/Event/TestEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,20 @@ Test(Event, constructor, .timeout = 5)

cr_assert_not_null(&event);
}

Test(Event, setGameData, .timeout = 5)
{
Gui::Event event;
Gui::GameData gameData;

cr_assert_null(event.getGameData());
event.setGameData(std::make_shared<Gui::GameData>(gameData));
cr_assert_not_null(event.getGameData());
}

Test(Event, getRender, .timeout = 5)
{
Gui::Event event;

cr_assert_null(event.getRender());
}
12 changes: 10 additions & 2 deletions tests/gui/tests/GUIUpdater/TestGUIUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ Test(GUIUpdater, updateTeamMember, .timeout = 5)

gameData->addTeam("TEAM1", (Color){0, 0, 0, 0});

gameData.get()->getTeam("TEAM1").addEgg(Gui::Egg(1, "TEAM1", std::pair<std::size_t, std::size_t>(1, 1)));
guiUpdater.update("pnw", {"1", "1", "1", "1", "1", "TEAM1"});
cr_assert_eq(gameData->getTeam("TEAM1").getPlayer(1).get()->getTeam(), "TEAM1");
cr_assert_eq(gameData->getTeam("TEAM1").getPlayer(1).get()->getId(), 1);
Expand Down Expand Up @@ -218,9 +219,16 @@ Test(GUIUpdater, updatePlayerPosition, .timeout = 5)
std::shared_ptr<Gui::GameData> gameData = std::make_shared<Gui::GameData>();
std::shared_ptr<Gui::INetwork> network = std::make_shared<Gui::Network>(4242, "no_tested");
Gui::GUIUpdater guiUpdater(gameData, network);
Model model;
ModelAnimation *modelAnimation = nullptr;
Gui::Team team("TEAM1", model, model, modelAnimation, (Color){0, 0, 0, 0});
Gui::Player player(1, "TEAM1", std::pair<std::size_t, std::size_t>(1, 1), 1, 1);
player.setState(Gui::Player::PlayerState::IDLE);
gameData->addTeam(team);
gameData->addPlayerToTeam("TEAM1", player);

gameData->addTeam("TEAM1", (Color){0, 0, 0, 0});
gameData->addPlayerToTeam("TEAM1", Gui::Player(1, "TEAM1", std::make_pair(1, 1), 1, 1));
gameData->addTeam("TEAM2", (Color){0, 0, 0, 0});
gameData->addPlayerToTeam("TEAM2", Gui::Player(1, "TEAM2", std::make_pair(1, 1), 1, 1));

guiUpdater.update("ppo", {"1", "1", "1", "1"});
cr_assert_eq(gameData->getTeam("TEAM1").getPlayer(1).get()->getPosition().first, 1);
Expand Down
39 changes: 39 additions & 0 deletions tests/gui/tests/GameDatas/TestGameData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "CriterionHeaders.hpp"
#include "GameDatas/GameData.hpp"

#include <unistd.h>

Test(GameData, getTeams, .timeout = 5)
{
Gui::GameData gameData;
Expand Down Expand Up @@ -233,3 +235,40 @@ Test(GameData, getTeamByIdFailed, .timeout = 5)

cr_assert_throw(gameData.getTeamById(0), Gui::Errors::GuiGameDataException);
}

Test(GameData, getTimeUnitFromServer, .timeout = 5)
{
Gui::GameData gameData;

cr_assert_eq(gameData.getTimeUnitFromServer(), Gui::GameData::TimeUnitState::NONE);
}

Test(GameData, addServerEggFailing, .timeout = 5)
{
Gui::GameData gameData;
Gui::Egg egg(0, "Team1", std::make_pair(1, 2));

gameData.addServerEgg(egg);
cr_assert_throw(gameData.addServerEgg(egg), Gui::Errors::GuiGameDataException);
}

Test(GameData, removeServerEgg, .timeout = 5)
{
Gui::GameData gameData;
Gui::Egg egg(0, "Team1", std::make_pair(1, 2));

gameData.addServerEgg(egg);
gameData.removeServerEgg(0);
cr_assert_eq(gameData.getServerEggs().size(), 0);
}

Test(GameData, restartLastTickMctCommand, .timeout = 5)
{
Gui::GameData gameData;
clock_t lastTick = gameData.getLastTickMctCommand();
usleep(1000);

gameData.restartLastTickMctCommand();

cr_assert_neq(gameData.getLastTickMctCommand(), lastTick);
}
52 changes: 52 additions & 0 deletions tests/gui/tests/GameDatas/TestInventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,55 @@ Test(Inventory, getRessources, .timeout = 5)
cr_assert_eq(ressources2[4], 5);
cr_assert_eq(ressources2[5], 6);
}

Test(Inventory, addRessource_food, .timeout = 5)
{
Gui::Inventory inventory;

inventory.addResource(0, 14);
cr_assert_eq(inventory.getFood(), 14);
}

Test(Inventory, addRessource, .timeout = 5)
{
Gui::Inventory inventory;

inventory.addResource(5, 14);
cr_assert_eq(inventory.getPhiras(), 14);
}

Test(Inventory, removeResource_food, .timeout = 5)
{
Gui::Inventory inventory;

inventory.setFood(15);
inventory.removeResource(0, 14);
cr_assert_eq(inventory.getFood(), 1);
}

Test(Inventory, removeResource_to_more_food, .timeout = 5)
{
Gui::Inventory inventory;

inventory.setFood(10);
inventory.removeResource(0, 14);
cr_assert_eq(inventory.getFood(), 0);
}

Test(Inventory, removeResource, .timeout = 5)
{
Gui::Inventory inventory;

inventory.setPhiras(15);
inventory.removeResource(5, 12);
cr_assert_eq(inventory.getPhiras(), 3);
}

Test(Inventory, removeResource_to_more, .timeout = 5)
{
Gui::Inventory inventory;

inventory.setPhiras(10);
inventory.removeResource(5, 14);
cr_assert_eq(inventory.getPhiras(), 0);
}
44 changes: 44 additions & 0 deletions tests/gui/tests/GameDatas/TestPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "CriterionHeaders.hpp"
#include "Error/Error.hpp"

#include <unistd.h>

Test(Player, position, .timeout = 5)
{
Gui::Player player(3, "TEAM1", std::pair<std::size_t, std::size_t>(1, 2), 1);
Expand Down Expand Up @@ -117,3 +119,45 @@ Test(Player, getCenterPosition, .timeout = 5)
cr_assert_eq(player.getCenterPosition().y, 0);
cr_assert_eq(player.getCenterPosition().z, (float)(SIZE_TILE / 2));
}

Test(Player, setPosition3D, .timeout = 5)
{
Gui::Player player(0, "TEAM1", std::pair<std::size_t, std::size_t>(0, 0), 1);

player.setPosition3D((Vector3){1, 2, 3});
cr_assert_eq(player.getPosition3D().x, 1);
cr_assert_eq(player.getPosition3D().y, 2);
cr_assert_eq(player.getPosition3D().z, 3);
}

Test(Player, setState, .timeout = 5)
{
Gui::Player player(0, "TEAM1", std::pair<std::size_t, std::size_t>(0, 0), 1);

player.setState(Gui::Player::IDLE);
cr_assert_eq(player.getState(), Gui::Player::IDLE);

player.setState(Gui::Player::WALK);
cr_assert_eq(player.getState(), Gui::Player::WALK);
}

Test(Player, setCurrentFrame, .timeout = 5)
{
Gui::Player player(0, "TEAM1", std::pair<std::size_t, std::size_t>(0, 0), 1);

player.setCurrentFrame(3);
cr_assert_eq(player.getCurrentFrame(), 3);

player.setCurrentFrame(5);
cr_assert_eq(player.getCurrentFrame(), 5);
}

Test(Player, restartAnimationTimeEllapsed, .timeout = 5)
{
Gui::Player player(0, "TEAM1", std::pair<std::size_t, std::size_t>(0, 0), 1);
clock_t tmp = player.getAnimationTimeEllapsed();
usleep(1000);

player.restartAnimationTimeEllapsed();
cr_assert_neq(player.getAnimationTimeEllapsed(), tmp);
}
54 changes: 47 additions & 7 deletions tests/gui/tests/GameDatas/TestTeam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@
*/

#include "Config.hpp"
#include "Assets.hpp"
#include "GameDatas/Team.hpp"
#include "CriterionHeaders.hpp"

Model LoadModel(const char *modelPath)
{
Model model;
(void) modelPath;
return model;
}

Test(Team, name, .timeout = 5)
{
Model model;
Expand Down Expand Up @@ -257,3 +251,49 @@ Test(Team, removeEggFailling, .timeout = 5)
team.removeEgg(4);
cr_assert_eq(team.getEggs().size(), 1);
}

Test(Team, getPlayerModelAnimation, .timeout = 5)
{
Model model;
ModelAnimation *modelAnimation = nullptr;
Gui::Team team("TEAM1", model, model, modelAnimation, (Color){0, 0, 0, 0});

ModelAnimation *model2 = team.getPlayerModelAnimation();
cr_assert_null(model2);
}

Test(Team, getPlayerPositionIn3DSpace, .timeout = 5)
{
Map<Gui::Tile> map;
for (std::size_t i = 0; i < 10; i++) {
std::vector<Gui::Tile> row;
for (std::size_t j = 0; j < 10; j++) {
row.push_back(Gui::Tile(std::pair<std::size_t, std::size_t>(i, j)));
}
map.push_back(row);
}
Model model;
ModelAnimation *modelAnimation = nullptr;
Gui::Team team("TEAM1", model, model, modelAnimation, (Color){0, 0, 0, 0});
Gui::Player player(3, "TEAM1", std::pair<std::size_t, std::size_t>(0, 0), 1);
team.addPlayer(player);

Vector3 pos = team.getPlayerPositionIn3DSpace(3, map);
Vector3 tmp = POS_PLAYER;
cr_assert_eq(pos.x, tmp.x);
cr_assert_eq(pos.y, tmp.y);
cr_assert_eq(pos.z, tmp.z);
}

Test(Team, getPlayerColor, .timeout = 5)
{
Model model;
ModelAnimation *modelAnimation = nullptr;
Gui::Team team("TEAM1", model, model, modelAnimation, (Color){255, 255, 255, 255});

Color color = team.getPlayerColor();
cr_assert_eq(color.r, 255);
cr_assert_eq(color.g, 255);
cr_assert_eq(color.b, 255);
cr_assert_eq(color.a, 255);
}
Loading