diff --git a/CMakeLists.txt b/CMakeLists.txt index 10fb2d9c..5529b25f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -165,6 +165,7 @@ set (LOOT_VALIDATOR_HEADERS ${LOOT_HEADERS}) set (LOOT_TESTS_SRC ${LOOT_SRC} "${CMAKE_SOURCE_DIR}/src/api/loot_db.cpp" "${CMAKE_SOURCE_DIR}/src/gui/loot_settings.cpp" + "${CMAKE_SOURCE_DIR}/src/gui/loot_state.cpp" "${CMAKE_SOURCE_DIR}/src/tests/main.cpp") set (LOOT_TESTS_HEADERS "${CMAKE_SOURCE_DIR}/src/tests/fixtures.h" @@ -193,7 +194,8 @@ set (LOOT_TESTS_HEADERS "${CMAKE_SOURCE_DIR}/src/tests/fixtures.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/test_metadata_list.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/test_masterlist.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/test_plugin_sorter.h" - "${CMAKE_SOURCE_DIR}/src/tests/gui/test_loot_settings.h") + "${CMAKE_SOURCE_DIR}/src/tests/gui/test_loot_settings.h" + "${CMAKE_SOURCE_DIR}/src/tests/gui/test_loot_state.h") source_group("Header Files" FILES ${LOOT_HEADERS} ${LOOT_GUI_HEADERS} ${LOOT_API_HEADERS} ${LOOT_TESTS_HEADERS}) diff --git a/src/gui/loot_state.cpp b/src/gui/loot_state.cpp index d2ac57fa..8f8ff875 100644 --- a/src/gui/loot_state.cpp +++ b/src/gui/loot_state.cpp @@ -239,7 +239,8 @@ namespace loot { } void LootState::decrementUnappliedChangeCounter() { - --unappliedChangeCounter; + if (unappliedChangeCounter > 0) + --unappliedChangeCounter; } void LootState::SelectGame(std::string preferredGame) { @@ -252,12 +253,12 @@ namespace loot { } // Get iterator to preferred game. - _currentGame = find_if(begin(_games), end(_games), [&](auto& game) { + _currentGame = find_if(begin(_games), end(_games), [&](Game& game) { return (preferredGame.empty() || preferredGame == game.FolderName()) && game.IsInstalled(); }); // If the preferred game cannot be found, get the first installed game. if (_currentGame == end(_games)) { - _currentGame = find_if(begin(_games), end(_games), [](auto& game) { + _currentGame = find_if(begin(_games), end(_games), [](Game& game) { return game.IsInstalled(); }); } diff --git a/src/tests/gui/test_loot_state.h b/src/tests/gui/test_loot_state.h new file mode 100644 index 00000000..6799cd56 --- /dev/null +++ b/src/tests/gui/test_loot_state.h @@ -0,0 +1,77 @@ +/* LOOT + +A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and +Fallout: New Vegas. + +Copyright (C) 2014-2015 WrinklyNinja + +This file is part of LOOT. + +LOOT is free software: you can redistribute +it and/or modify it under the terms of the GNU General Public License +as published by the Free Software Foundation, either version 3 of +the License, or (at your option) any later version. + +LOOT is distributed in the hope that it will +be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with LOOT. If not, see +. +*/ + +#ifndef LOOT_TEST_GUI_LOOT_STATE +#define LOOT_TEST_GUI_LOOT_STATE + +#include "gui/loot_state.h" + +#include "tests/fixtures.h" + +namespace loot { + namespace test { + class LootState : public ::testing::Test { + protected: + loot::LootState lootState; + }; + + TEST_F(LootState, hasUnappliedChangesShouldBeFalseByDefault) { + EXPECT_FALSE(lootState.hasUnappliedChanges()); + } + + TEST_F(LootState, shouldNotHaveUnappliedChangesIfCounterIsDeccremented) { + lootState.decrementUnappliedChangeCounter(); + EXPECT_FALSE(lootState.hasUnappliedChanges()); + } + + TEST_F(LootState, shouldHaveUnappliedChangesIfCounterIsIncremented) { + lootState.incrementUnappliedChangeCounter(); + EXPECT_TRUE(lootState.hasUnappliedChanges()); + } + + TEST_F(LootState, incrementingTheChangeCounterMoreThanItIsDecrementedShouldLeaveUnappliedChanges) { + lootState.incrementUnappliedChangeCounter(); + lootState.incrementUnappliedChangeCounter(); + lootState.decrementUnappliedChangeCounter(); + EXPECT_TRUE(lootState.hasUnappliedChanges()); + } + + TEST_F(LootState, incrementingTheChangeCounterLessThanItIsDecrementedShouldLeaveNoUnappliedChanges) { + lootState.incrementUnappliedChangeCounter(); + lootState.decrementUnappliedChangeCounter(); + lootState.decrementUnappliedChangeCounter(); + EXPECT_FALSE(lootState.hasUnappliedChanges()); + } + + TEST_F(LootState, incrementingTheChangeCounterThenDecrementingItEquallyShouldLeaveNoUnappliedChanges) { + lootState.incrementUnappliedChangeCounter(); + lootState.incrementUnappliedChangeCounter(); + lootState.decrementUnappliedChangeCounter(); + lootState.decrementUnappliedChangeCounter(); + EXPECT_FALSE(lootState.hasUnappliedChanges()); + } + } +} + +#endif diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 78023cc1..fca2ac5b 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -52,6 +52,7 @@ #include "backend/test_masterlist.h" #include "backend/test_plugin_sorter.h" #include "gui/test_loot_settings.h" +#include "gui/test_loot_state.h" #include