diff --git a/TESTING.md b/TESTING.md new file mode 100644 index 000000000..f6ba0daaf --- /dev/null +++ b/TESTING.md @@ -0,0 +1,11 @@ +testing strategy: +- using GTest for mocking systems +- custom re-definitions of interfaces (for now) + - cant use GTest mocking easily for this I dont think + +- the mock interfaces will just be empty classes p much to have something that the systems classes can have pointers to if they own a pointer to an interface + +- the mock systems will use the gmock lib for better mocking of systems for system to system interaction in the test env + + + diff --git a/TODO.md b/TODO.md new file mode 100644 index 000000000..e69de29bb diff --git a/lib/mock_interfaces/DashboardInterface.h b/lib/mock_interfaces/DashboardInterface.h index 9500b42e9..ab5523031 100644 --- a/lib/mock_interfaces/DashboardInterface.h +++ b/lib/mock_interfaces/DashboardInterface.h @@ -4,8 +4,8 @@ class DashboardInterface { private: public: - bool start_button; - bool start_button_pressed() { return start_button; }; + bool start_button_status_; + bool start_button_pressed() { return start_button_status_; }; }; #endif /* DASHBOARDINTERFACE */ diff --git a/lib/state_machine/include/MCUStateMachine.h b/lib/state_machine/include/MCUStateMachine.h index 2c70a782b..2abdc38a1 100644 --- a/lib/state_machine/include/MCUStateMachine.h +++ b/lib/state_machine/include/MCUStateMachine.h @@ -2,6 +2,7 @@ #define __MCU_STATE_MACHINE__ #include "Logger.h" + #include "PedalsSystem.h" #include "DrivetrainSystem.h" #include "Buzzer.h" @@ -27,11 +28,12 @@ template class MCUStateMachine { public: - MCUStateMachine(BuzzerController *buzzer, DrivetrainSysType *drivetrain, DashboardInterface *dashboard) + MCUStateMachine(BuzzerController *buzzer, DrivetrainSysType *drivetrain, DashboardInterface *dashboard, PedalsSystem *pedals) { buzzer_ = buzzer; drivetrain_ = drivetrain; dashboard_ = dashboard; + pedals_ = pedals; } /// @brief our components can use this time to tell when to do things. We can set this ourselves for testing purposes instead of using metro timers diff --git a/platformio.ini b/platformio.ini index bbadaaf6c..c50157d91 100644 --- a/platformio.ini +++ b/platformio.ini @@ -1,6 +1,6 @@ [env:test_env] platform = native - +test_framework = googletest build_src_filter = -<**/*.c> -<**/*.cpp> @@ -22,6 +22,7 @@ upload_protocol = teensy-cli extra_scripts = pre:extra_script.py lib_ignore = mock_interfaces + mock_systems lib_deps = SPI https://github.com/tonton81/FlexCAN_T4 diff --git a/test/DashboardInterface.h b/test/DashboardInterface.h deleted file mode 100644 index 2a0d21278..000000000 --- a/test/DashboardInterface.h +++ /dev/null @@ -1,5 +0,0 @@ -#ifndef DASHBOARDINTERFACE -#define DASHBOARDINTERFACE - - -#endif /* DASHBOARDINTERFACE */ diff --git a/test/main.cpp b/test/main.cpp index 0d7092853..975ce0cc2 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,24 +1,17 @@ +#include + #include "state_machine_test.h" -#include "pedals_system_test.h" -void setUp(void) -{ - // declare a component obj - // possibly make temp class that is a freind class of PedalsSystem to access private members - // possibly modify the -} +// #include "pedals_system_test.h" -void tearDown(void) -{ -} -int main() -{ - UNITY_BEGIN(); +int main(int argc, char **argv) +{ - RUN_TEST(test_pedal_is_active); - RUN_TEST(test_state_machine_init_tick); - - UNITY_END(); // stop unit testing + testing::InitGoogleMock(&argc, argv); + if (RUN_ALL_TESTS()) + ; + // Always return zero-code and allow PlatformIO to parse results + return 0; } diff --git a/test/pedals_system_test.h b/test/pedals_system_test.h index 10610c334..960345a07 100644 --- a/test/pedals_system_test.h +++ b/test/pedals_system_test.h @@ -7,7 +7,6 @@ author: Lucas Plant #ifndef PEDALS_SYSTEM_TEST #define PEDALS_SYSTEM_TEST -#include #include #include "PedalsSystem.h" #include diff --git a/test/state_machine_test.h b/test/state_machine_test.h index b9c5a6e86..a7d87f5c8 100644 --- a/test/state_machine_test.h +++ b/test/state_machine_test.h @@ -1,55 +1,116 @@ #ifndef STATE_MACHINE_TEST #define STATE_MACHINE_TEST -#include +#include +#include #include "MCUStateMachine.h" -#include "PedalsSystem.h" + + + + class DrivetrainMock { public: - - bool drivetrain_ready_; - bool drivetrain_ready() {}; + bool hv_thresh_; + bool drivetrain_ready(){}; /// @param curr_time current system tick time (millis()) that sets the init phase start time void enable_drivetrain_hv(unsigned long curr_time){}; // startup phase 2 - bool check_drivetrain_quit_dc_on() {}; + bool check_drivetrain_quit_dc_on(){}; // on entry logic - void request_enable() {}; - void command_drivetrain_no_torque() {}; + void request_enable(){}; + void command_drivetrain_no_torque(){}; // final check for drivetrain initialization to check if quit inverter on bool drivetrain_enabled(){}; // check to see if init time limit has passed - bool inverter_init_timeout(unsigned long curr_time) {}; + bool inverter_init_timeout(unsigned long curr_time){}; - bool hv_over_threshold_on_drivetrain() {}; - void disable() {}; - bool drivetrain_error_occured() {}; + bool hv_over_threshold_on_drivetrain() { return hv_thresh_; }; + void disable(){}; + bool drivetrain_error_occured(){}; - void command_drivetrain(const DrivetrainCommand &data) {}; + void command_drivetrain(const DrivetrainCommand &data){}; }; - BuzzerController buzzer(500); DrivetrainMock drivetrain; + +PedalsParams params_for_test = {1, 1, 10, 10, 1, 1, 9, 9}; +PedalsSystem pedals(params_for_test, params_for_test); + DashboardInterface dash_interface; -MCUStateMachine state_machine(&buzzer, &drivetrain, &dash_interface); +MCUStateMachine state_machine(&buzzer, &drivetrain, &dash_interface, &pedals); -void test_state_machine_init_tick(void) +TEST(MCUStateMachineTesting, test_state_machine_init_tick) { unsigned long sys_time = 1000; - TEST_ASSERT_TRUE(state_machine.get_state() == CAR_STATE::STARTUP); + EXPECT_EQ(state_machine.get_state(), CAR_STATE::STARTUP); state_machine.tick_state_machine(sys_time); - TEST_ASSERT_TRUE(state_machine.get_state() == CAR_STATE::TRACTIVE_SYSTEM_NOT_ACTIVE); + EXPECT_EQ(state_machine.get_state(), CAR_STATE::TRACTIVE_SYSTEM_NOT_ACTIVE); } +TEST(MCUStateMachineTesting, test_state_machine_tractive_system_activation) +{ + unsigned long sys_time = 1000; + + // ticking without hv over threshold testing and ensuring the tractive system not active still + state_machine.tick_state_machine(sys_time); + sys_time += 1; + drivetrain.hv_thresh_ = false; + state_machine.tick_state_machine(sys_time); + sys_time += 1; + EXPECT_EQ(state_machine.get_state(), CAR_STATE::TRACTIVE_SYSTEM_NOT_ACTIVE); + + // hv going over threshold -> tractive system + drivetrain.hv_thresh_ = true; + sys_time +=1; + state_machine.tick_state_machine(sys_time); + EXPECT_EQ(state_machine.get_state(), CAR_STATE::TRACTIVE_SYSTEM_ACTIVE); + + + // hv going under thresh -> tractive system not active + drivetrain.hv_thresh_ =false; + sys_time +=1; + state_machine.tick_state_machine(sys_time); + EXPECT_EQ(state_machine.get_state(), CAR_STATE::TRACTIVE_SYSTEM_NOT_ACTIVE); +} + +TEST(MCUStateMachineTesting, test_state_machine_tractive_system_enabling) +{ + unsigned long sys_time = 1000; + + // ticking without hv over threshold testing and ensuring the tractive system not active still + state_machine.tick_state_machine(sys_time); + sys_time += 1; + drivetrain.hv_thresh_ = false; + state_machine.tick_state_machine(sys_time); + sys_time += 1; + EXPECT_EQ(state_machine.get_state(), CAR_STATE::TRACTIVE_SYSTEM_NOT_ACTIVE); + + // hv going over threshold -> tractive system + drivetrain.hv_thresh_ = true; + sys_time +=1; + state_machine.tick_state_machine(sys_time); + EXPECT_EQ(state_machine.get_state(), CAR_STATE::TRACTIVE_SYSTEM_ACTIVE); + + + sys_time +=1; + dash_interface.start_button_status_ = true; + auto pedals_brake_active_res = pedals.evaluate_pedals({0,0,3,3}, sys_time); + state_machine.tick_state_machine(sys_time); + EXPECT_EQ(state_machine.get_state(), CAR_STATE::ENABLING_INVERTERS); + + +} + + #endif /* STATE_MACHINE_TEST */