Skip to content

Commit

Permalink
Fix up test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Laguna1989 committed Oct 31, 2023
1 parent 0e47d46 commit 6e8d192
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 34 deletions.
8 changes: 6 additions & 2 deletions impl/jamtemplate/common/log/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void jt::Console::doUpdate(float const /*elapsed*/)
m_focus = m_showConsole;
}
}

void jt::Console::handleCommand() const
{
if (m_lastCommand.empty()) {
Expand All @@ -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);
Expand Down Expand Up @@ -92,6 +93,7 @@ void jt::Console::storeInputInCommand() const
ImGui::SetKeyboardFocusHere(-1);
}
}

void jt::Console::storeActionInCommand() const
{
std::string str = m_inputBufferAction.data();
Expand All @@ -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
Expand Down Expand Up @@ -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<int>(History.size()) - 1;
Expand Down
2 changes: 0 additions & 2 deletions impl/jamtemplate/common/pathfinder/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ void jt::pathfinder::Node::addNeighbour(std::weak_ptr<jt::pathfinder::NodeInterf
float jt::pathfinder::Node::getValue() const { return m_value; }

void jt::pathfinder::Node::setValue(float value) { m_value = value; }
int jt::pathfinder::Node::getTileID() const { return 0; }

void jt::pathfinder::Node::setTileID(int id) { m_tileId = id; }
bool jt::pathfinder::Node::getBlocked() const { return m_isBlocked; }
void jt::pathfinder::Node::setBlocked(bool blocked) { m_isBlocked = blocked; }
4 changes: 0 additions & 4 deletions impl/jamtemplate/common/pathfinder/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ class Node : public NodeInterface {
float getValue() const override;
void setValue(float value) override;

void setTileID(int id);
int getTileID() const override;

void setBlocked(bool blocked) override;
bool getBlocked() const override;

Expand All @@ -34,7 +31,6 @@ class Node : public NodeInterface {
float m_value { -1.0f };
jt::Vector2u m_position;
std::vector<std::weak_ptr<jt::pathfinder::NodeInterface>> m_neighbours;
int m_tileId { -1 };
int m_isBlocked { false };
};
} // namespace pathfinder
Expand Down
4 changes: 0 additions & 4 deletions impl/jamtemplate/common/pathfinder/node_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions impl/jamtemplate/common/state_manager/state_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ void jt::StateManager::draw(std::shared_ptr<jt::RenderTargetInterface> rt)
getTransition()->draw(rt);
}
}

void jt::StateManager::storeCurrentState(std::string const& identifier)
{
m_storedStates[identifier] = getCurrentState();
}

std::shared_ptr<jt::GameState> jt::StateManager::getStoredState(std::string const& identifier)
{
if (m_storedStates.count(identifier) == 0) {
Expand All @@ -100,6 +102,7 @@ void jt::StateManager::clearStoredState(std::string const& identifier)
{
m_storedStates.erase(identifier);
}

std::vector<std::string> jt::StateManager::getStoredStateIdentifiers() const
{
std::vector<std::string> identifiers;
Expand Down
13 changes: 7 additions & 6 deletions test/unit/jt_test/common/button_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ButtonTest : public ::testing::Test {
jt::TextureManagerInterface& tm { getTextureManager() };

std::shared_ptr<jt::Button> b { nullptr };

void SetUp() override
{
tm = getTextureManager();
Expand Down Expand Up @@ -117,7 +118,7 @@ TEST_F(ButtonTest, UpdateWithInput)

TEST_F(ButtonTest, Draw)
{
auto game = std::make_shared<MockGame>();
auto game = std::make_shared<::testing::NiceMock<MockGame>>();
::testing::NiceMock<MockInput> input;
EXPECT_CALL(*game, input()).WillRepeatedly(::testing::ReturnRef(input));
b->setGameInstance(game);
Expand All @@ -128,7 +129,7 @@ TEST_F(ButtonTest, Draw)

TEST_F(ButtonTest, DrawInvisibleButton)
{
auto game = std::make_shared<MockGame>();
auto game = std::make_shared<::testing::NiceMock<MockGame>>();
::testing::NiceMock<MockInput> input;
EXPECT_CALL(*game, input()).WillRepeatedly(::testing::ReturnRef(input));
b->setVisible(false);
Expand All @@ -138,9 +139,9 @@ TEST_F(ButtonTest, DrawInvisibleButton)
b->draw();
}

TEST_F(ButtonTest, DrawInActiveButton)
TEST_F(ButtonTest, DrawInactiveButton)
{
auto game = std::make_shared<MockGame>();
auto game = std::make_shared<::testing::NiceMock<MockGame>>();
::testing::NiceMock<MockInput> input;
EXPECT_CALL(*game, input()).WillRepeatedly(::testing::ReturnRef(input));
b->setActive(false);
Expand All @@ -152,13 +153,13 @@ TEST_F(ButtonTest, DrawInActiveButton)

TEST_F(ButtonTest, CustomDrawable)
{
auto game = std::make_shared<MockGame>();
auto game = std::make_shared<::testing::NiceMock<MockGame>>();
::testing::NiceMock<MockInput> input;
EXPECT_CALL(*game, input()).Times(2).WillRepeatedly(::testing::ReturnRef(input));
b->setGameInstance(game);
b->update(0.1f);

auto d = std::make_shared<MockDrawable>();
auto d = std::make_shared<::testing::NiceMock<MockDrawable>>();
b->setDrawable(d);
std::shared_ptr<jt::RenderTargetInterface> renderTarget { nullptr };
EXPECT_CALL(game->m_gfx, target()).Times(2);
Expand Down
12 changes: 10 additions & 2 deletions test/unit/jt_test/common/game_state_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
};

Expand Down Expand Up @@ -57,7 +60,7 @@ class GameStateTest : public ::testing::Test {
std::shared_ptr<MockObject> mockObject;
MockGfx gfx;
MockWindow window;
MockInput input;
::testing::NiceMock<MockInput> input;

void SetUp() override
{
Expand All @@ -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<MockObject>();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -132,7 +137,9 @@ TEST_F(GameStateTest, GameObjectList)

bool designate_object_for_kill = static_cast<bool>(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(_));
}
Expand Down Expand Up @@ -196,7 +203,7 @@ TEST_F(GameStateTest, AddGameObjectToTwoStatesWillRaiseException)
GameStateImpl gamestate2;
gamestate2.setGameInstance(g);

auto obj = std::make_shared<MockObject>();
auto obj = std::make_shared<::testing::NiceMock<MockObject>>();

EXPECT_CALL(*obj, doCreate());

Expand All @@ -208,6 +215,7 @@ TEST_F(GameStateTest, RemovalOfGameObjectWillCallDoDestroyOnGameObject)
{
AddGameObject();

EXPECT_CALL(*mockObject, doKill());
mockObject->kill();
EXPECT_CALL(*mockObject, doDestroy());
gamestate.update(0.1f);
Expand Down
2 changes: 0 additions & 2 deletions test/unit/jt_test/common/game_test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "game_test.hpp"
#include <action_commands/basic_action_commands.hpp>
#include <gfx_impl.hpp>
#include <mocks/mock_state.hpp>
#include <mocks/mock_state_manager.hpp>

using ::testing::NiceMock;

Expand Down
8 changes: 4 additions & 4 deletions test/unit/jt_test/common/game_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class GameTest : public ::testing::Test {
public:
float const zoom { 1.0f };
std::shared_ptr<jt::Game> g { nullptr };
MockWindow window;
MockCamera camera;
::testing::NiceMock<MockWindow> window;
::testing::NiceMock<MockCamera> camera;
jt::TextureManagerImpl textureManager { nullptr };
MockGfx gfx;
::testing::NiceMock<MockGfx> gfx;
jt::ActionCommandManager actionCommandManager { logger };
jt::AudioNull audio;

Expand All @@ -44,7 +44,7 @@ class GameTest : public ::testing::Test {

ON_CALL(camera, getZoom).WillByDefault([this]() { return zoom; });

state = std::make_shared<MockState>();
state = std::make_shared<::testing::NiceMock<MockState>>();
ON_CALL(stateManager, getCurrentState).WillByDefault(::testing::Return(state));
ON_CALL(stateManager, update)
.WillByDefault(
Expand Down
2 changes: 1 addition & 1 deletion test/unit/jt_test/common/graphics/gfx_impl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TEST(GfxImplTest, TargetReturnsNoNullptr)
TEST(GfxImplTest, UpdateCallUpdateOnCamera)
{
jt::RenderWindow window { 800, 600, "jt_tests" };
MockCamera cam {};
::testing::NiceMock<MockCamera> cam {};
jt::GfxImpl gfx { window, cam };
EXPECT_CALL(cam, update(1.0f));

Expand Down
4 changes: 2 additions & 2 deletions test/unit/jt_test/common/input/input_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ TEST(InputManagerTest, ResetWithMocks)

TEST(InputManagerTest, UpdateWithMocks)
{
auto keyboard = std::make_shared<MockKeyboardInput>();
auto mouse = std::make_shared<MockMouseInput>();
auto keyboard = std::make_shared<::testing::NiceMock<MockKeyboardInput>>();
auto mouse = std::make_shared<::testing::NiceMock<MockMouseInput>>();
std::shared_ptr<jt::GamepadInterface> gamepad = std::make_shared<jt::GamepadInput>(0);
jt::InputManager im { mouse, keyboard, { gamepad } };

Expand Down
15 changes: 13 additions & 2 deletions test/unit/jt_test/common/random/random_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <color/color_conversions.hpp>
#include <math_helper.hpp>
#include <random/random.hpp>
#include <random/random_sample_and_hold.hpp>
#include <vector.hpp>
Expand Down Expand Up @@ -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;
Expand All @@ -118,7 +119,7 @@ TEST(RandomPointIn, ValidWithRect)
}
}

TEST(RandomPointIn, ValidWithVector)
TEST(RandomPointIn, ResultIsInRectCreatedByVector)
{
auto const lower = 0.0f;
auto const upperX = 13.0f;
Expand All @@ -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<unsigned int> { };

INSTANTIATE_TEST_SUITE_P(RandomSetSeedTest, RandomSetSeedTestFixture,
Expand Down
Loading

0 comments on commit 6e8d192

Please sign in to comment.