Skip to content

Commit

Permalink
Write UTs for LoadLevelDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
nerudaj committed Apr 27, 2024
1 parent 6aebe1a commit 0587b1a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/lib-editor/src/Dialogs/LoadLevelDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Resources;
import FormBuilder;
import WidgetBuilder;

constexpr const char* SELECT_PACK_ID = "SelectPackId";
constexpr const char* SELECT_LEVEL_ID = "SelectLevelId";

LoadLevelDialog::LoadLevelDialog(
Expand All @@ -24,17 +25,19 @@ void LoadLevelDialog::buildLayoutImpl(tgui::Panel::Ptr panel)
{
// must be initialized each time a modal is opened
mapPackNames = Filesystem::getLevelPackNames(levelsDir);
panel->setWidgetName(SELECT_LEVEL_ID + std::string("LayoutPanel"));
panel->add(
FormBuilder()
.addOption(
.addOptionWithWidgetId(
Strings::Dialog::Body::SELECT_PACK,
WidgetBuilder::createDropdown(
mapPackNames,
mapPackName,
std::bind(
&LoadLevelDialog::handleSelectedMapPack,
this,
std::placeholders::_1)))
std::placeholders::_1)),
SELECT_PACK_ID)
.addOptionWithWidgetId(
Strings::Dialog::Body::SELECT_LEVEL,
WidgetBuilder::createDropdown(
Expand Down
2 changes: 1 addition & 1 deletion src/lib-editor/src/Dialogs/MapPickerDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void MapPickerDialog::buildLayoutImpl(tgui::Panel::Ptr panel)

for (auto&& [idx, map] : std::views::enumerate(maps))
{
builder.addOption(
std::ignore = builder.addOption(
map.name,
WidgetBuilder::createCheckbox(
map.enabled,
Expand Down
2 changes: 2 additions & 0 deletions src/tests/include/TestHelpers/TestAssets.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Rect.hpp>

const std::filesystem::path BINARY_DIR = "@CMAKE_BINARY_DIR@";

const std::filesystem::path GRAPHICS_DIR = "@CMAKE_SOURCE_DIR@/resources/graphics";

namespace Mesh
Expand Down
59 changes: 59 additions & 0 deletions src/tests/src/LoadLevelDialogTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "TestHelpers/TestAssets.hpp"
#include <Dialogs/LoadLevelDialog.hpp>
#include <catch.hpp>
#include <fstream>

namespace fs = std::filesystem;

void mockFilesystem(const fs::path& rootDir, unsigned mappacks)
{
auto&& levelsDir = rootDir / "resources" / "levels";
fs::create_directories(levelsDir);

for (unsigned i = 0; i < mappacks; ++i)
{
auto&& packDir = levelsDir / ("pack" + std::to_string(i));
fs::create_directory(packDir);
auto&& save = std::ofstream(packDir / "map.lvd");
save << "data";
}
}

void clearFilesystem(const fs::path& rootDir)
{
fs::remove_all(rootDir / "resources");
}

TEST_CASE("[LoadLevelDialog]")
{
mockFilesystem(BINARY_DIR, 1);

auto&& gui = mem::Rc<Gui>();
auto&& dialog = LoadLevelDialog(gui, BINARY_DIR / "resources");

dialog.open([] {});
{
auto&& dropdown = gui->get<tgui::ComboBox>("SelectPackId");
auto&& items = dropdown->getItems();
REQUIRE(items.size() == 1u);
REQUIRE(items[0] == "pack0");
}
dialog.close();

// Update file structure
clearFilesystem(BINARY_DIR);
mockFilesystem(BINARY_DIR, 2);

// Reopen dialog, verify dialog noticed it
dialog.open([] {});
{
auto&& dropdown = gui->get<tgui::ComboBox>("SelectPackId");
auto&& items = dropdown->getItems();
REQUIRE(items.size() == 2u);
REQUIRE(items[0] == "pack0");
REQUIRE(items[1] == "pack1");
}
dialog.close();

clearFilesystem(BINARY_DIR);
}

0 comments on commit 0587b1a

Please sign in to comment.