Skip to content

Commit

Permalink
Add test for init of ManuItem.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Sep 14, 2024
1 parent 69fcea6 commit 1d73370
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
15 changes: 8 additions & 7 deletions src/MenuItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <algorithm>

#include "Display.h"
#include "ResourceType.h"
#include "Screen.h"

Expand Down Expand Up @@ -29,16 +30,16 @@ void MenuItem::setSelected(bool selected)
resourceType_ = ResourceType::MENU_ITEM;
}

void MenuItem::init(const Screen& screen, int position, int count)
void MenuItem::init(const Display& display, int position, int count)
{
width_ = std::max(screen.getWidth() / 3,
screen.getResourceWidth(ResourceType::MENU_ITEM));
height_ = std::max(screen.getHeight() / 10,
screen.getResourceHeight(ResourceType::MENU_ITEM));
width_ = std::max(display.getWidth() / 3,
display.getResourceWidth(ResourceType::MENU_ITEM));
height_ = std::max(display.getHeight() / 10,
display.getResourceHeight(ResourceType::MENU_ITEM));

setX(screen.getCenterX() - width_ / 2);
setX(display.getCenterX() - width_ / 2);

const int locationOfFirstItem{screen.getCenterY() - (count * height_ / 2)};
const int locationOfFirstItem{display.getCenterY() - (count * height_ / 2)};
setY(locationOfFirstItem + (height_ * position));
}

Expand Down
4 changes: 3 additions & 1 deletion src/MenuItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#include "Drawable.h"
#include "UserChoice.h"

class Display;

class MenuItem : public Drawable
{
public:
explicit MenuItem(UserChoice userChoice);

void init(const Screen& screen, int position, int count);
void init(const Display& display, int position, int count);

void draw(const Screen& screen) const override;
ResourceType getResourceType() const override;
Expand Down
15 changes: 12 additions & 3 deletions test/FakeDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ class FakeDisplay : public Display
int getResourceWidth(
[[maybe_unused]] ResourceType resourceType) const override
{
return 0;
return resourceWidth_;
}
int getResourceHeight(
[[maybe_unused]] ResourceType resourceType) const override
{
return 0;
return resourceHeight_;
}
};

void setResourceWidth(int width) { resourceWidth_ = width; }

void setResourceHeight(int height) { resourceHeight_ = height; }

private:
int resourceWidth_;
int resourceHeight_;
};
;
34 changes: 34 additions & 0 deletions test/MenuItemTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <src/MenuItem.h>
#include <src/Screen.h>

#include "FakeDisplay.h"

TEST_CASE("MenuItem initialization and properties", "[MenuItem]")
{
MenuItem menuItem(UserChoice::LEVEL_MENU);
Expand All @@ -27,4 +29,36 @@ TEST_CASE("MenuItem initialization and properties", "[MenuItem]")
menuItem.setSelected(false);
REQUIRE(menuItem.getResourceType() == ResourceType::MENU_ITEM);
}

SECTION("initialization")
{
const int itemsCount{3};
const int resourceHeight{70};
const int resourceWidth{340};

FakeDisplay display;
display.setResourceHeight(resourceHeight);
display.setResourceWidth(resourceWidth);

menuItem.init(display, 0, itemsCount);
REQUIRE(menuItem.getX() == 230);
REQUIRE(menuItem.getY() == 195);
REQUIRE(menuItem.getWidth() == resourceWidth);
REQUIRE(menuItem.getHeight() == resourceHeight);
REQUIRE(menuItem.getCenter() == Point{170, 35});

menuItem.init(display, 1, itemsCount);
REQUIRE(menuItem.getX() == 230);
REQUIRE(menuItem.getY() == 265);
REQUIRE(menuItem.getWidth() == resourceWidth);
REQUIRE(menuItem.getHeight() == resourceHeight);
REQUIRE(menuItem.getCenter() == Point{170, 35});

menuItem.init(display, 2, itemsCount);
REQUIRE(menuItem.getX() == 230);
REQUIRE(menuItem.getY() == 335);
REQUIRE(menuItem.getWidth() == resourceWidth);
REQUIRE(menuItem.getHeight() == resourceHeight);
REQUIRE(menuItem.getCenter() == Point{170, 35});
}
}

0 comments on commit 1d73370

Please sign in to comment.