From 0587b1a9973eaa211dd53710c51d8d247fd729c0 Mon Sep 17 00:00:00 2001 From: Jakub Neruda Date: Sat, 27 Apr 2024 11:52:31 +0200 Subject: [PATCH] Write UTs for LoadLevelDialog --- .../src/Dialogs/LoadLevelDialog.cpp | 7 ++- .../src/Dialogs/MapPickerDialog.cpp | 2 +- .../include/TestHelpers/TestAssets.hpp.in | 2 + src/tests/src/LoadLevelDialogTest.cpp | 59 +++++++++++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 src/tests/src/LoadLevelDialogTest.cpp diff --git a/src/lib-editor/src/Dialogs/LoadLevelDialog.cpp b/src/lib-editor/src/Dialogs/LoadLevelDialog.cpp index 048ceb95..04e19b76 100644 --- a/src/lib-editor/src/Dialogs/LoadLevelDialog.cpp +++ b/src/lib-editor/src/Dialogs/LoadLevelDialog.cpp @@ -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( @@ -24,9 +25,10 @@ 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, @@ -34,7 +36,8 @@ void LoadLevelDialog::buildLayoutImpl(tgui::Panel::Ptr panel) std::bind( &LoadLevelDialog::handleSelectedMapPack, this, - std::placeholders::_1))) + std::placeholders::_1)), + SELECT_PACK_ID) .addOptionWithWidgetId( Strings::Dialog::Body::SELECT_LEVEL, WidgetBuilder::createDropdown( diff --git a/src/lib-editor/src/Dialogs/MapPickerDialog.cpp b/src/lib-editor/src/Dialogs/MapPickerDialog.cpp index 3b6e3234..e7d82d85 100644 --- a/src/lib-editor/src/Dialogs/MapPickerDialog.cpp +++ b/src/lib-editor/src/Dialogs/MapPickerDialog.cpp @@ -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, diff --git a/src/tests/include/TestHelpers/TestAssets.hpp.in b/src/tests/include/TestHelpers/TestAssets.hpp.in index ac6804a0..d25b8175 100644 --- a/src/tests/include/TestHelpers/TestAssets.hpp.in +++ b/src/tests/include/TestHelpers/TestAssets.hpp.in @@ -5,6 +5,8 @@ #include #include +const std::filesystem::path BINARY_DIR = "@CMAKE_BINARY_DIR@"; + const std::filesystem::path GRAPHICS_DIR = "@CMAKE_SOURCE_DIR@/resources/graphics"; namespace Mesh diff --git a/src/tests/src/LoadLevelDialogTest.cpp b/src/tests/src/LoadLevelDialogTest.cpp new file mode 100644 index 00000000..a4289aba --- /dev/null +++ b/src/tests/src/LoadLevelDialogTest.cpp @@ -0,0 +1,59 @@ +#include "TestHelpers/TestAssets.hpp" +#include +#include +#include + +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(); + auto&& dialog = LoadLevelDialog(gui, BINARY_DIR / "resources"); + + dialog.open([] {}); + { + auto&& dropdown = gui->get("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("SelectPackId"); + auto&& items = dropdown->getItems(); + REQUIRE(items.size() == 2u); + REQUIRE(items[0] == "pack0"); + REQUIRE(items[1] == "pack1"); + } + dialog.close(); + + clearFilesystem(BINARY_DIR); +} \ No newline at end of file