1+ #include < gmock/gmock.h>
2+ #include < gtest/gtest.h>
3+
4+ #include < iostream>
5+ #include < vector>
6+
7+ #include " motor.h"
8+ #include " motorConfig.h"
9+ #include " motorsController.h"
10+
11+ // Include librobotcontrol mocks
12+ #include " mocks/librobotcontrol/include/mock_rc_encoder.h"
13+ #include " mocks/librobotcontrol/include/mock_rc_motor.h"
14+ #include " mocks/librobotcontrol/include/mock_rc_start_stop.h"
15+ #include " mocks/librobotcontrol/include/mock_rc_time.h"
16+
17+ class MotorControllerTest : public ::testing::Test {
18+ protected:
19+ MotorController motor_controller;
20+ const uint8_t kMotorNumber = 4 ;
21+
22+ void SetUp () override {
23+ // Reset all mock objects
24+ ResetMockRCMotor ();
25+ ResetMockRCEncoder ();
26+ setupDefaultMockActions (); // Ensure the mock is initialized
27+ ResetMockRCTime ();
28+
29+ // Set up default behaviors for mocks instead of expectations
30+ ON_CALL (getStartStopMock (), kill_existing_process (::testing::_)).WillByDefault (::testing::Return (0 ));
31+ ON_CALL (getStartStopMock (), enable_signal_handler ()).WillByDefault (::testing::Return (0 ));
32+ ON_CALL (GetMockRCMotor (), init_freq (::testing::_)).WillByDefault (::testing::Return (0 ));
33+ ON_CALL (GetMockRCEncoder (), init ()).WillByDefault (::testing::Return (0 ));
34+
35+ // Initialize with the same configuration as in the provided example
36+ std::vector<MotorConfig> motor_configs = {
37+ MotorConfig (1 , false , {1 .5f , 0 .056f , 1 .5f }),
38+ MotorConfig (2 , false , {1 .5f , 0 .056f , 1 .5f }),
39+ MotorConfig (3 , true , {1 .5f , 0 .056f , 1 .5f }),
40+ MotorConfig (4 , true , {1 .5f , 0 .056f , 1 .5f })
41+ };
42+
43+ motor_controller.Init (motor_configs, kMotorNumber );
44+ }
45+
46+ void TearDown () override {
47+ // Clean up expectations for Destroy
48+ EXPECT_CALL (GetMockRCMotor (), cleanup ()).WillOnce (::testing::Return (0 ));
49+ EXPECT_CALL (GetMockRCEncoder (), cleanup ()).WillOnce (::testing::Return (0 ));
50+
51+ motor_controller.Destroy ();
52+
53+ // Clean up mock objects to prevent memory leaks
54+ DestroyMockRCMotor ();
55+ DestroyMockRCEncoder ();
56+ DestroyStartStopMock ();
57+ DestroyMockRCTime ();
58+ }
59+ };
60+
61+ // Test MotorController::Init
62+ TEST_F (MotorControllerTest, Init) {
63+ // Expectations are already set in SetUp, no additional checks needed here
64+ ASSERT_EQ (motor_controller.GetMotorsNumber (), kMotorNumber );
65+ }
66+
67+ // Test MotorController::SetMotorRPM
68+ TEST_F (MotorControllerTest, SetMotorRPM) {
69+ int channel = 1 ;
70+ int rpm = 1000 ;
71+
72+ EXPECT_CALL (GetMockRCMotor (), set (channel, ::testing::_)).WillOnce (::testing::Return (0 ));
73+
74+ int result = motor_controller.SetMotorRPM (channel - 1 , rpm); // Convert to 0-based index
75+ ASSERT_EQ (result, 0 );
76+ }
77+
78+ // Test MotorController::SetMotorRPM with invalid channel
79+ TEST_F (MotorControllerTest, SetMotorRPMInvalidChannel) {
80+ int invalid_channel = kMotorNumber + 1 ; // Out of range
81+ int rpm = 1000 ;
82+
83+ int result = motor_controller.SetMotorRPM (invalid_channel, rpm);
84+ ASSERT_EQ (result, -1 );
85+ }
86+
87+ // Test MotorController::StopMotor
88+ TEST_F (MotorControllerTest, StopMotor) {
89+ int channel = 1 ;
90+
91+ EXPECT_CALL (GetMockRCMotor (), brake (channel)).WillOnce (::testing::Return (0 ));
92+
93+ int result = motor_controller.StopMotor (channel - 1 ); // Convert to 0-based index
94+ ASSERT_EQ (result, 0 );
95+ }
96+
97+ // Test MotorController::GetEncoderCounter
98+ TEST_F (MotorControllerTest, GetEncoderCounter) {
99+ int channel = 1 ;
100+ int encoder_value = 42 ;
101+
102+ EXPECT_CALL (GetMockRCEncoder (), read (channel)).WillOnce (::testing::Return (encoder_value));
103+
104+ int result = motor_controller.GetEncoderCounter (channel - 1 ); // Convert to 0-based index
105+ ASSERT_EQ (result, encoder_value);
106+ }
107+
108+ // Test MotorController::Destroy
109+ TEST_F (MotorControllerTest, Destroy) {
110+ // Expectations are already set in TearDown, no additional checks needed here
111+ SUCCEED ();
112+ }
0 commit comments