Skip to content

Commit

Permalink
Added first IGpio mock object to ShiftController tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crsz20 committed Jul 12, 2024
1 parent 2511e59 commit fcf52be
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void ShiftController::LowGear::Enter(ShiftController& context) {
}

void ShiftController::LowGear::Compute(ShiftController& context) {

}

void ShiftController::LowGear::Exit(ShiftController& context) {
Expand All @@ -50,7 +50,9 @@ void ShiftController::Neutral::Enter(ShiftController& context) {
}

void ShiftController::Neutral::Compute(ShiftController& context) {

if(context.neutral_switch_->Read()) {
context.SetState(&context.low_gear_state_);
}
}

void ShiftController::Neutral::Exit(ShiftController& context) {
Expand Down
48 changes: 40 additions & 8 deletions Firmware/Tests/Src/Application/shift_controller_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@
// DFR Custom Dependencies
#include "../../../Shifter_System/Program/Src/Application/ShiftController/shift_controller.hpp"

class MockGpio : public platform::IGpio {
public:
MOCK_METHOD(bool, Read, (), (override));
MOCK_METHOD(void, Write, (bool signal), (override));
MOCK_METHOD(bool, ToggleDetected, (), (override));
};


namespace application {

class ShiftControllerWrapper : public ShiftController {
public:

int16_t rpm_;
std::array<float, 2> wheel_speeds_;

ShiftControllerWrapper()
: ShiftController(rpm_,
wheel_speeds_) { }
ShiftControllerWrapper(int16_t &rpm,
std::array<float, 2> &wheel_speeds,
std::shared_ptr<platform::IGpio> neutral_switch)
: ShiftController(rpm,
wheel_speeds,
neutral_switch) { }

bool IsLowGearState() { return dynamic_cast<LowGear*>(current_state_) != nullptr; }
bool IsNeutralState() { return dynamic_cast<Neutral*>(current_state_) != nullptr; }
Expand All @@ -26,11 +33,36 @@ class ShiftControllerWrapper : public ShiftController {

class ShiftControllerFixture : public testing::Test {
protected:
ShiftControllerWrapper shift_controller_;
int16_t rpm;
std::array<float, 2> wheel_speeds_;
std::shared_ptr<MockGpio> neutral_switch_ = std::make_shared<MockGpio>();
ShiftControllerWrapper shift_controller_{rpm, wheel_speeds_, neutral_switch_};
};

TEST_F(ShiftControllerFixture, DefaultToNeutralState) {
EXPECT_TRUE(shift_controller_.IsNeutralState());
}

TEST_F(ShiftControllerFixture, TransitionToLowGearState) {
EXPECT_TRUE(shift_controller_.IsNeutralState());

EXPECT_CALL(*neutral_switch_, Read())
.Times(1)
.WillOnce(::testing::Return(true));

shift_controller_.Run();
EXPECT_TRUE(shift_controller_.IsLowGearState());
}

TEST_F(ShiftControllerFixture, RemainNeutralState) {
EXPECT_TRUE(shift_controller_.IsNeutralState());

EXPECT_CALL(*neutral_switch_, Read())
.Times(1)
.WillOnce(::testing::Return(false));

shift_controller_.Run();
EXPECT_TRUE(shift_controller_.IsNeutralState());
}

} // namespace application

0 comments on commit fcf52be

Please sign in to comment.