Skip to content

Commit

Permalink
Implement more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Jul 13, 2024
1 parent 8a18e0f commit 2337266
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 9 deletions.
5 changes: 0 additions & 5 deletions Source/FeatureHelpers/FeatureToggle.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ class FeatureToggleBase {
}
}

[[nodiscard]] bool isEnabled() const noexcept
{
return static_cast<Feature&>(*this).enabledVariable(ToggleMethod{});
}

private:
void callOnEnable() noexcept
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Features/Hud/DefusingAlert/DefusingAlertPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct DefusingAlertPanel {
{
}

void showAndUpdate(const auto bomb) const noexcept
void showAndUpdate(auto&& bomb) const noexcept
{
context.defusingAlertContainerPanel().show();
context.defusingCountdownTextPanel()
Expand Down
2 changes: 2 additions & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ add_executable(Tests
FeatureTests/HudFeaturesTests/BombTimerTests/BombTimerPanelTests.cpp
FeatureTests/HudFeaturesTests/BombTimerTests/BombTimerTests.cpp
FeatureTests/HudFeaturesTests/BombTimerTests/BombTimerTextPanelTests.cpp
FeatureTests/HudFeaturesTests/BombTimerTests/BombTimerToggleTests.cpp
FeatureTests/HudFeaturesTests/BombTimerTests/GameBombStatusPanelTests.cpp
FeatureTests/HudFeaturesTests/DefusingAlertTests/DefusingAlertPanelTests.cpp
)

add_subdirectory(HelpersTests)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ TEST_P(BombSiteIconPanelTestWithParam, SetsCorrectImageUrl) {
bombSiteIconPanel.setIcon(GetParam());
}

INSTANTIATE_TEST_CASE_P(, BombSiteIconPanelTestWithParam,
INSTANTIATE_TEST_SUITE_P(, BombSiteIconPanelTestWithParam,
testing::Values(cs2::kBombSiteAIconUrl, cs2::kBombSiteBIconUrl));
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BombTimerPanelTest : public testing::Test {
BombTimerPanel<MockBombTimerContext&> bombTimerPanel{mockBombTimerContext};
};

TEST_F(BombTimerPanelTest, HideMethodHidesContainerPanel) {
TEST_F(BombTimerPanelTest, HidesContainerPanel) {
EXPECT_CALL(mockBombTimerContext, bombTimerContainerPanel()).WillOnce(testing::ReturnRef(mockBombTimerContainerPanel));
EXPECT_CALL(mockBombTimerContainerPanel, hide());

Expand All @@ -35,7 +35,7 @@ struct BombTimerPanelTestParam {
class BombTimerPanelTestWithParam : public BombTimerPanelTest, public testing::WithParamInterface<BombTimerPanelTestParam> {
};

TEST_P(BombTimerPanelTestWithParam, aaa) {
TEST_P(BombTimerPanelTestWithParam, ShowsContainerPanelAndSetsBombSiteIconAndTimeToExplosion) {
EXPECT_CALL(mockBombTimerContext, bombTimerContainerPanel()).WillOnce(testing::ReturnRef(mockBombTimerContainerPanel));
EXPECT_CALL(mockBombTimerContext, tickingC4()).WillOnce(testing::ReturnRef(mockPlantedC4));
EXPECT_CALL(mockBombTimerContext, bombSiteIconPanel()).WillOnce(testing::ReturnRef(mockBombSiteIconPanel));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <Fakes/FakeBombTimerState.h>
#include <Features/Hud/BombTimer/BombTimerToggle.h>
#include <Mocks/MockBombTimerContext.h>
#include <Mocks/MockBombTimerPanel.h>
#include <Mocks/MockGameBombStatusPanel.h>

class BombTimerToggleTest : public testing::Test {
protected:
BombTimerToggleTest()
{
EXPECT_CALL(mockBombTimerContext, state()).WillOnce(testing::ReturnRef(state));
}

testing::StrictMock<MockBombTimerContext> mockBombTimerContext;
testing::StrictMock<MockGameBombStatusPanel> mockGameBombStatusPanel;
testing::StrictMock<MockBombTimerPanel> mockBombTimerPanel;

BombTimerToggle<MockBombTimerContext&> bombTimerToggle{mockBombTimerContext};
FakeBombTimerState state;
};

TEST_F(BombTimerToggleTest, EnablingSetsEnabledVariableToTrue) {
state.enabled = false;
bombTimerToggle.enable();
EXPECT_EQ(state.enabled, true);
}

TEST_F(BombTimerToggleTest, EnablingDoesNothingIfAlreadyEnabled) {
state.enabled = true;
bombTimerToggle.enable();
EXPECT_EQ(state.enabled, true);
}

TEST_F(BombTimerToggleTest, DisablingDoesNothingIfAlreadyDisabled) {
state.enabled = false;
bombTimerToggle.disable();
EXPECT_EQ(state.enabled, false);
}

TEST_F(BombTimerToggleTest, DisablingRestoresBombStatusPanelAndHidesBombTimerPanel) {
state.enabled = true;

EXPECT_CALL(mockBombTimerContext, gameBombStatusPanel()).WillOnce(testing::ReturnRef(mockGameBombStatusPanel));
EXPECT_CALL(mockGameBombStatusPanel, restore());

EXPECT_CALL(mockBombTimerContext, bombTimerPanel()).WillOnce(testing::ReturnRef(mockBombTimerPanel));
EXPECT_CALL(mockBombTimerPanel, hide());

bombTimerToggle.disable();

EXPECT_EQ(state.enabled, false);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <optional>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <Features/Hud/DefusingAlert/DefusingAlertPanel.h>
#include <Mocks/MockDefusingAlertContext.h>
#include <Mocks/MockDefusingCountdownTextPanel.h>
#include <Mocks/MockPanel.h>
#include <Mocks/MockPlantedC4.h>

class DefusingAlertPanelTest : public testing::Test {
protected:
testing::StrictMock<MockDefusingAlertContext> mockDefusingAlertContext;
testing::StrictMock<MockPanel> mockDefusingAlertContainerPanel;
testing::StrictMock<MockDefusingCountdownTextPanel> mockDefusingCountdownTextPanel;
testing::StrictMock<MockPlantedC4> mockPlantedC4;

DefusingAlertPanel<MockDefusingAlertContext&> defusingAlertPanel{mockDefusingAlertContext};
};

TEST_F(DefusingAlertPanelTest, HidesDefusingAlertContainerPanel) {
EXPECT_CALL(mockDefusingAlertContext, defusingAlertContainerPanel()).WillOnce(testing::ReturnRef(mockDefusingAlertContainerPanel));
EXPECT_CALL(mockDefusingAlertContainerPanel, hide());

defusingAlertPanel.hide();
}

struct DefusingAlertPanelTestParam {
std::optional<float> timeToDefuseEnd{3.34f};
std::optional<bool> canBeDefused{true};
};

class DefusingAlertPanelTestWithParam : public DefusingAlertPanelTest, public testing::WithParamInterface<DefusingAlertPanelTestParam> {
};

TEST_P(DefusingAlertPanelTestWithParam, ShowsDefusingAlertContainerPanelAndUpdatesCountdownTextPanel) {
EXPECT_CALL(mockDefusingAlertContext, defusingAlertContainerPanel()).WillOnce(testing::ReturnRef(mockDefusingAlertContainerPanel));
EXPECT_CALL(mockDefusingAlertContainerPanel, show());

EXPECT_CALL(mockDefusingAlertContext, defusingCountdownTextPanel()).WillOnce(testing::ReturnRef(mockDefusingCountdownTextPanel));
EXPECT_CALL(mockDefusingCountdownTextPanel, setTimeToDefuseEnd(GetParam().timeToDefuseEnd)).WillOnce(testing::ReturnRef(mockDefusingCountdownTextPanel));
EXPECT_CALL(mockDefusingCountdownTextPanel, setCanBeDefused(GetParam().canBeDefused)).WillOnce(testing::ReturnRef(mockDefusingCountdownTextPanel));

EXPECT_CALL(mockPlantedC4, getTimeToDefuseEnd()).WillOnce(testing::Return(GetParam().timeToDefuseEnd));
EXPECT_CALL(mockPlantedC4, canBeDefused()).WillOnce(testing::Return(GetParam().canBeDefused));

defusingAlertPanel.showAndUpdate(mockPlantedC4);
}

INSTANTIATE_TEST_SUITE_P(, DefusingAlertPanelTestWithParam, testing::Values(
DefusingAlertPanelTestParam{.timeToDefuseEnd = 0.0f},
DefusingAlertPanelTestParam{.timeToDefuseEnd = 3.45f},
DefusingAlertPanelTestParam{.timeToDefuseEnd = 10.0f},
DefusingAlertPanelTestParam{.timeToDefuseEnd = std::nullopt},
DefusingAlertPanelTestParam{.canBeDefused = false},
DefusingAlertPanelTestParam{.canBeDefused = true},
DefusingAlertPanelTestParam{.canBeDefused = std::nullopt},
DefusingAlertPanelTestParam{.timeToDefuseEnd = std::nullopt, .canBeDefused = std::nullopt}
));
9 changes: 9 additions & 0 deletions Tests/Mocks/MockDefusingAlertContext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

struct MockDefusingCountdownTextPanel;
struct MockPanel;

struct MockDefusingAlertContext {
MOCK_METHOD(MockPanel&, defusingAlertContainerPanel, ());
MOCK_METHOD(MockDefusingCountdownTextPanel&, defusingCountdownTextPanel, ());
};
10 changes: 10 additions & 0 deletions Tests/Mocks/MockDefusingCountdownTextPanel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <optional>

#include <gmock/gmock.h>

struct MockDefusingCountdownTextPanel {
MOCK_METHOD(MockDefusingCountdownTextPanel&, setTimeToDefuseEnd, (std::optional<float> timeToDefuseEnd));
MOCK_METHOD(MockDefusingCountdownTextPanel&, setCanBeDefused, (std::optional<bool> canBeDefused));
};
4 changes: 4 additions & 0 deletions Tests/Mocks/MockPlantedC4.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#pragma once

#include <optional>

#include <gmock/gmock.h>

struct MockPlantedC4 {
MOCK_METHOD(const char*, getBombSiteIconUrl, ());
MOCK_METHOD(float, getTimeToExplosion, ());
MOCK_METHOD(std::optional<float>, getTimeToDefuseEnd, ());
MOCK_METHOD(std::optional<bool>, canBeDefused, ());
};

0 comments on commit 2337266

Please sign in to comment.