diff --git a/impl/jamtemplate/common/log/console.cpp b/impl/jamtemplate/common/log/console.cpp index 052b0925..20839e66 100644 --- a/impl/jamtemplate/common/log/console.cpp +++ b/impl/jamtemplate/common/log/console.cpp @@ -21,6 +21,7 @@ void jt::Console::doUpdate(float const /*elapsed*/) m_focus = m_showConsole; } } + void jt::Console::handleCommand() const { if (m_lastCommand.empty()) { @@ -45,7 +46,7 @@ void jt::Console::doDraw() const // Display contents in a scrolling region ImGui::TextColored(ImVec4(1, 1, 0, 1), ""); - const float footer_height_to_reserve + float const footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), false, ImGuiWindowFlags_HorizontalScrollbar); @@ -92,6 +93,7 @@ void jt::Console::storeInputInCommand() const ImGui::SetKeyboardFocusHere(-1); } } + void jt::Console::storeActionInCommand() const { std::string str = m_inputBufferAction.data(); @@ -104,6 +106,7 @@ void jt::Console::storeActionInCommand() const History.push_back(m_lastCommand); getGame()->logger().action(str); } + void jt::Console::clearInput() const { strcpy(m_inputBufferAction.data(), ""); } void jt::Console::renderOneLogEntry(jt::LogEntry const& entry) const @@ -159,12 +162,13 @@ void jt::Console::renderOneLogEntry(jt::LogEntry const& entry) const ImGui::Text("%s", text.c_str()); ImGui::PopStyleColor(); } + int jt::Console::inputUserCallback(ImGuiInputTextCallbackData* data) { switch (data->EventFlag) { case ImGuiInputTextFlags_CallbackHistory: { // Example of HISTORY - const int prev_history_pos = m_historyPos; + int const prev_history_pos = m_historyPos; if (data->EventKey == ImGuiKey_UpArrow) { if (m_historyPos == -1) m_historyPos = static_cast(History.size()) - 1; diff --git a/impl/jamtemplate/common/pathfinder/node.cpp b/impl/jamtemplate/common/pathfinder/node.cpp index 55e886a8..7b8ea55f 100644 --- a/impl/jamtemplate/common/pathfinder/node.cpp +++ b/impl/jamtemplate/common/pathfinder/node.cpp @@ -31,8 +31,6 @@ void jt::pathfinder::Node::addNeighbour(std::weak_ptr> m_neighbours; - int m_tileId { -1 }; int m_isBlocked { false }; }; } // namespace pathfinder diff --git a/impl/jamtemplate/common/pathfinder/node_interface.hpp b/impl/jamtemplate/common/pathfinder/node_interface.hpp index 20bc7a31..293e557c 100644 --- a/impl/jamtemplate/common/pathfinder/node_interface.hpp +++ b/impl/jamtemplate/common/pathfinder/node_interface.hpp @@ -38,10 +38,6 @@ class NodeInterface { /// \param value value to be set virtual void setValue(float value) = 0; - /// get tile id - /// \return - virtual int getTileID() const = 0; - /// set tile blocked /// \param blocked true if blocked, false otherwise virtual void setBlocked(bool blocked) = 0; diff --git a/impl/jamtemplate/common/state_manager/state_manager.cpp b/impl/jamtemplate/common/state_manager/state_manager.cpp index 54874caa..f576ecee 100644 --- a/impl/jamtemplate/common/state_manager/state_manager.cpp +++ b/impl/jamtemplate/common/state_manager/state_manager.cpp @@ -84,10 +84,12 @@ void jt::StateManager::draw(std::shared_ptr rt) getTransition()->draw(rt); } } + void jt::StateManager::storeCurrentState(std::string const& identifier) { m_storedStates[identifier] = getCurrentState(); } + std::shared_ptr jt::StateManager::getStoredState(std::string const& identifier) { if (m_storedStates.count(identifier) == 0) { @@ -100,6 +102,7 @@ void jt::StateManager::clearStoredState(std::string const& identifier) { m_storedStates.erase(identifier); } + std::vector jt::StateManager::getStoredStateIdentifiers() const { std::vector identifiers; diff --git a/test/unit/jt_test/common/button_test.cpp b/test/unit/jt_test/common/button_test.cpp index 7624e155..c518cc70 100644 --- a/test/unit/jt_test/common/button_test.cpp +++ b/test/unit/jt_test/common/button_test.cpp @@ -11,6 +11,7 @@ class ButtonTest : public ::testing::Test { jt::TextureManagerInterface& tm { getTextureManager() }; std::shared_ptr b { nullptr }; + void SetUp() override { tm = getTextureManager(); @@ -117,7 +118,7 @@ TEST_F(ButtonTest, UpdateWithInput) TEST_F(ButtonTest, Draw) { - auto game = std::make_shared(); + auto game = std::make_shared<::testing::NiceMock>(); ::testing::NiceMock input; EXPECT_CALL(*game, input()).WillRepeatedly(::testing::ReturnRef(input)); b->setGameInstance(game); @@ -128,7 +129,7 @@ TEST_F(ButtonTest, Draw) TEST_F(ButtonTest, DrawInvisibleButton) { - auto game = std::make_shared(); + auto game = std::make_shared<::testing::NiceMock>(); ::testing::NiceMock input; EXPECT_CALL(*game, input()).WillRepeatedly(::testing::ReturnRef(input)); b->setVisible(false); @@ -138,9 +139,9 @@ TEST_F(ButtonTest, DrawInvisibleButton) b->draw(); } -TEST_F(ButtonTest, DrawInActiveButton) +TEST_F(ButtonTest, DrawInactiveButton) { - auto game = std::make_shared(); + auto game = std::make_shared<::testing::NiceMock>(); ::testing::NiceMock input; EXPECT_CALL(*game, input()).WillRepeatedly(::testing::ReturnRef(input)); b->setActive(false); @@ -152,13 +153,13 @@ TEST_F(ButtonTest, DrawInActiveButton) TEST_F(ButtonTest, CustomDrawable) { - auto game = std::make_shared(); + auto game = std::make_shared<::testing::NiceMock>(); ::testing::NiceMock input; EXPECT_CALL(*game, input()).Times(2).WillRepeatedly(::testing::ReturnRef(input)); b->setGameInstance(game); b->update(0.1f); - auto d = std::make_shared(); + auto d = std::make_shared<::testing::NiceMock>(); b->setDrawable(d); std::shared_ptr renderTarget { nullptr }; EXPECT_CALL(game->m_gfx, target()).Times(2); diff --git a/test/unit/jt_test/common/game_state_test.cpp b/test/unit/jt_test/common/game_state_test.cpp index 49728a4e..c407a6e8 100644 --- a/test/unit/jt_test/common/game_state_test.cpp +++ b/test/unit/jt_test/common/game_state_test.cpp @@ -18,8 +18,11 @@ class GameStateImpl : public GameState { private: virtual void onCreate() override { } + void onEnter() override { } + virtual void onUpdate(float /*elapsed*/) override { } + virtual void onDraw() const override { } }; @@ -57,7 +60,7 @@ class GameStateTest : public ::testing::Test { std::shared_ptr mockObject; MockGfx gfx; MockWindow window; - MockInput input; + ::testing::NiceMock input; void SetUp() override { @@ -67,6 +70,7 @@ class GameStateTest : public ::testing::Test { ON_CALL(*game, input).WillByDefault(::testing::ReturnRef(input)); gamestate.setGameInstance(game); } + void AddGameObject() { mockObject = std::make_shared(); @@ -97,6 +101,7 @@ TEST_F(GameStateTest, AddTweenObject) TEST_F(GameStateTest, DrawCallsDrawOnAddedGameObjects) { AddGameObject(); + EXPECT_CALL(*mockObject, doUpdate(0.1f)); gamestate.update(0.1f); EXPECT_CALL(*mockObject, doDraw()); gamestate.draw(); @@ -132,7 +137,9 @@ TEST_F(GameStateTest, GameObjectList) bool designate_object_for_kill = static_cast(i % 2 == 1); if (designate_object_for_kill) { + EXPECT_CALL(*mo.lock(), doKill()); mo.lock()->kill(); + EXPECT_CALL(*mo.lock(), doDestroy()); } else { EXPECT_CALL(*mo.lock(), doUpdate(_)); } @@ -196,7 +203,7 @@ TEST_F(GameStateTest, AddGameObjectToTwoStatesWillRaiseException) GameStateImpl gamestate2; gamestate2.setGameInstance(g); - auto obj = std::make_shared(); + auto obj = std::make_shared<::testing::NiceMock>(); EXPECT_CALL(*obj, doCreate()); @@ -208,6 +215,7 @@ TEST_F(GameStateTest, RemovalOfGameObjectWillCallDoDestroyOnGameObject) { AddGameObject(); + EXPECT_CALL(*mockObject, doKill()); mockObject->kill(); EXPECT_CALL(*mockObject, doDestroy()); gamestate.update(0.1f); diff --git a/test/unit/jt_test/common/game_test.cpp b/test/unit/jt_test/common/game_test.cpp index faadc844..c87ba7bb 100644 --- a/test/unit/jt_test/common/game_test.cpp +++ b/test/unit/jt_test/common/game_test.cpp @@ -1,8 +1,6 @@ #include "game_test.hpp" #include -#include #include -#include using ::testing::NiceMock; diff --git a/test/unit/jt_test/common/game_test.hpp b/test/unit/jt_test/common/game_test.hpp index 5868219a..9f8034f1 100644 --- a/test/unit/jt_test/common/game_test.hpp +++ b/test/unit/jt_test/common/game_test.hpp @@ -19,10 +19,10 @@ class GameTest : public ::testing::Test { public: float const zoom { 1.0f }; std::shared_ptr g { nullptr }; - MockWindow window; - MockCamera camera; + ::testing::NiceMock window; + ::testing::NiceMock camera; jt::TextureManagerImpl textureManager { nullptr }; - MockGfx gfx; + ::testing::NiceMock gfx; jt::ActionCommandManager actionCommandManager { logger }; jt::AudioNull audio; @@ -44,7 +44,7 @@ class GameTest : public ::testing::Test { ON_CALL(camera, getZoom).WillByDefault([this]() { return zoom; }); - state = std::make_shared(); + state = std::make_shared<::testing::NiceMock>(); ON_CALL(stateManager, getCurrentState).WillByDefault(::testing::Return(state)); ON_CALL(stateManager, update) .WillByDefault( diff --git a/test/unit/jt_test/common/graphics/gfx_impl_test.cpp b/test/unit/jt_test/common/graphics/gfx_impl_test.cpp index 8e8d7b57..9c37b8ae 100644 --- a/test/unit/jt_test/common/graphics/gfx_impl_test.cpp +++ b/test/unit/jt_test/common/graphics/gfx_impl_test.cpp @@ -47,7 +47,7 @@ TEST(GfxImplTest, TargetReturnsNoNullptr) TEST(GfxImplTest, UpdateCallUpdateOnCamera) { jt::RenderWindow window { 800, 600, "jt_tests" }; - MockCamera cam {}; + ::testing::NiceMock cam {}; jt::GfxImpl gfx { window, cam }; EXPECT_CALL(cam, update(1.0f)); diff --git a/test/unit/jt_test/common/input/input_manager_test.cpp b/test/unit/jt_test/common/input/input_manager_test.cpp index 0c1039a3..33d7d7f9 100644 --- a/test/unit/jt_test/common/input/input_manager_test.cpp +++ b/test/unit/jt_test/common/input/input_manager_test.cpp @@ -57,8 +57,8 @@ TEST(InputManagerTest, ResetWithMocks) TEST(InputManagerTest, UpdateWithMocks) { - auto keyboard = std::make_shared(); - auto mouse = std::make_shared(); + auto keyboard = std::make_shared<::testing::NiceMock>(); + auto mouse = std::make_shared<::testing::NiceMock>(); std::shared_ptr gamepad = std::make_shared(0); jt::InputManager im { mouse, keyboard, { gamepad } }; diff --git a/test/unit/jt_test/common/random/random_test.cpp b/test/unit/jt_test/common/random/random_test.cpp index 884e911e..25ac099b 100644 --- a/test/unit/jt_test/common/random/random_test.cpp +++ b/test/unit/jt_test/common/random/random_test.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -103,7 +104,7 @@ TEST(RandomColor, HSV) ASSERT_FLOAT_EQ(h, 0.0f); } -TEST(RandomPointIn, ValidWithRect) +TEST(RandomPointIn, ResultIsInRectCreatedByRect) { auto const lowerX = 10.0f; auto const lowerY = 10.0f; @@ -118,7 +119,7 @@ TEST(RandomPointIn, ValidWithRect) } } -TEST(RandomPointIn, ValidWithVector) +TEST(RandomPointIn, ResultIsInRectCreatedByVector) { auto const lower = 0.0f; auto const upperX = 13.0f; @@ -132,6 +133,16 @@ TEST(RandomPointIn, ValidWithVector) } } +TEST(RandomPointInCircle, ResultIsInsideCircle) +{ + auto const radius = 30.0f; + + for (auto i = 0u; i != 1000; ++i) { + auto const v = Random::getRandomPointInCircle(radius); + ASSERT_LE(jt::MathHelper::distanceBetween(v, { 0.0f, 0.0f }), radius); + } +} + class RandomSetSeedTestFixture : public ::testing::TestWithParam { }; INSTANTIATE_TEST_SUITE_P(RandomSetSeedTest, RandomSetSeedTestFixture, diff --git a/test/unit/jt_test/common/state_manager/state_manager_test.cpp b/test/unit/jt_test/common/state_manager/state_manager_test.cpp index 938c4e8a..5b4d1234 100644 --- a/test/unit/jt_test/common/state_manager/state_manager_test.cpp +++ b/test/unit/jt_test/common/state_manager/state_manager_test.cpp @@ -1,3 +1,4 @@ +#include "mocks/mock_input.hpp" #include "state_manager/state_manager_transition_none.hpp" #include #include @@ -23,10 +24,9 @@ TEST(StateManagerTest, InitialNextStateReturnsCorrectImpl) TEST(StateManagerTest, CheckForGameStateSwitchReturnsTrue) { - auto initialState = std::make_shared(); - jt::StateManager stateManager { initialState }; + jt::StateManager stateManager { std::make_shared<::testing::NiceMock>() }; - auto game = std::make_shared(); + auto const game = std::make_shared<::testing::NiceMock>(); // Note: Throw because of window being a default constructed object. ASSERT_ANY_THROW(stateManager.update(game, 0.0f)); } @@ -51,3 +51,111 @@ TEST(StateManagerTest, SetGetTransition) stateManager.setTransition(transition); ASSERT_EQ(stateManager.getTransition(), transition); } + +TEST(StateManagerTest, getStoredStateIdentifiersIsInitiallyEmpty) +{ + jt::StateManager stateManager { std::make_shared() }; + ASSERT_TRUE(stateManager.getStoredStateIdentifiers().empty()); +} + +TEST(StateManagerTest, getStoredStateIdentifiersAfterStoreState) +{ + jt::StateManager stateManager { std::make_shared() }; + + stateManager.storeCurrentState("testState"); + auto const stateIdentifiers = stateManager.getStoredStateIdentifiers(); + ASSERT_FALSE(stateIdentifiers.empty()); + ASSERT_EQ(stateIdentifiers.size(), 1u); + ASSERT_EQ(stateIdentifiers.at(0), "testState"); +} + +TEST(StateManagerTest, getStoredStateIdentifiersAfterClear) +{ + jt::StateManager stateManager { std::make_shared() }; + + stateManager.storeCurrentState("testState"); + stateManager.clearStoredState("testState"); + ASSERT_TRUE(stateManager.getStoredStateIdentifiers().empty()); +} + +TEST(StateManagerTest, getStoredStateReturnsNullptrByDefault) +{ + jt::StateManager stateManager { std::make_shared() }; + ASSERT_EQ(stateManager.getStoredState("abcd"), nullptr); +} + +TEST(StateManagerTest, getStoredStateReturnsValidPointer) +{ + jt::StateManager stateManager { std::make_shared<::testing::NiceMock>() }; + auto game = std::make_shared<::testing::NiceMock>(); + ::testing::NiceMock input; + ON_CALL(*game, input()).WillByDefault(::testing::ReturnRef(input)); + stateManager.update(game, 0.1f); + stateManager.storeCurrentState("test state"); + ASSERT_NE(stateManager.getStoredState("test state"), nullptr); +} + +TEST(StateManagerTest, UpdateAndDraw) +{ + jt::StateManager stateManager { std::make_shared<::testing::NiceMock>() }; + auto game = std::make_shared<::testing::NiceMock>(); + ::testing::NiceMock input; + ON_CALL(*game, input()).WillByDefault(::testing::ReturnRef(input)); + stateManager.update(game, 0.1f); + ASSERT_NO_THROW(stateManager.draw(game->gfx().target())); +} + +class MockTransition : public jt::StateManagerTransitionInterface { +public: + MOCK_METHOD(void, start, (), (override)); + MOCK_METHOD(bool, isInProgress, (), (override)); + MOCK_METHOD(bool, triggerStateChange, (), (override)); + MOCK_METHOD(void, update, (float elapsed), (override)); + MOCK_METHOD(void, draw, (std::shared_ptr), (override)); +}; + +TEST(StateManagerTest, UpdateAndDrawWithTransition) +{ + jt::StateManager stateManager { std::make_shared<::testing::NiceMock>() }; + auto const transition = std::make_shared(); + stateManager.setTransition(transition); + + auto game = std::make_shared<::testing::NiceMock>(); + + ::testing::NiceMock input; + ON_CALL(*game, input()).WillByDefault(::testing::ReturnRef(input)); + EXPECT_CALL(*transition, update(::testing::_)); + EXPECT_CALL(*transition, triggerStateChange).WillOnce(::testing::Return(true)); + EXPECT_CALL(*transition, isInProgress).WillOnce(::testing::Return(true)); + EXPECT_CALL(*transition, draw(::testing::_)); + stateManager.update(game, 0.1f); + ASSERT_NO_THROW(stateManager.draw(game->gfx().target())); +} + +TEST(StateManagerTest, switchToStoredStateWithInvalidState) +{ + auto const firstState = std::make_shared<::testing::NiceMock>(); + + jt::StateManager stateManager { firstState }; + ASSERT_THROW(stateManager.switchToStoredState("non existing"), std::invalid_argument); +} + +TEST(StateManagerTest, switchToStoredState) +{ + auto const firstState = std::make_shared<::testing::NiceMock>(); + auto const secondState = std::make_shared<::testing::NiceMock>(); + + jt::StateManager stateManager { firstState }; + auto game = std::make_shared<::testing::NiceMock>(); + ::testing::NiceMock input; + ON_CALL(*game, input()).WillByDefault(::testing::ReturnRef(input)); + stateManager.update(game, 0.1f); + stateManager.storeCurrentState("first state"); + stateManager.switchState(secondState); + stateManager.update(game, 0.1f); + + stateManager.switchToStoredState("first state"); + stateManager.update(game, 0.1f); + + ASSERT_EQ(stateManager.getCurrentState(), firstState); +} diff --git a/test/unit/jt_test/mocks/mock_object.hpp b/test/unit/jt_test/mocks/mock_object.hpp index c8f215db..fab9f579 100644 --- a/test/unit/jt_test/mocks/mock_object.hpp +++ b/test/unit/jt_test/mocks/mock_object.hpp @@ -9,6 +9,7 @@ class MockObject : public jt::GameObject { MOCK_METHOD(void, doUpdate, (float const), (override)); MOCK_METHOD(void, doDraw, (), (const)); MOCK_METHOD(void, doCreate, (), (override)); + MOCK_METHOD(void, doKill, (), (override)); MOCK_METHOD(void, doDestroy, (), (override)); };