|
1 | 1 | #include "Command_Interpreter.h"
|
2 | 2 | #include <gtest/gtest.h>
|
3 | 3 |
|
| 4 | +#ifndef MOCK_RPI |
| 5 | + |
| 6 | +#include <wiringSerial.h> |
| 7 | + |
| 8 | +serial; |
| 9 | + |
| 10 | +bool WiringControl::initializeGPIO() { |
| 11 | + if ((serial = serialOpen("/dev/serial/by-id/usb-MicroPython_Board_in_FS_mode_e6614864d3798738-if00", 115200)) < 0) { |
| 12 | + return false; |
| 13 | + } |
| 14 | + return true; |
| 15 | +} |
| 16 | + |
| 17 | +int getSerialChar() { |
| 18 | + return serialGetchar(serial); // from wiringSerial |
| 19 | +} |
| 20 | + |
| 21 | +#else |
| 22 | + |
| 23 | +int getSerialChar() { |
| 24 | + return EOF; |
| 25 | +} |
| 26 | + |
| 27 | +#endif |
| 28 | + |
4 | 29 | TEST(CommandInterpreterTest, CreateCommandInterpreter) {
|
5 | 30 | testing::internal::CaptureStdout();
|
6 | 31 |
|
@@ -28,9 +53,17 @@ TEST(CommandInterpreterTest, CreateCommandInterpreter) {
|
28 | 53 | expectedOutput.append(" PWM 1500\n");
|
29 | 54 | }
|
30 | 55 |
|
| 56 | + std::string serialOutput; |
| 57 | + while (getSerialChar() != EOF) {} |
| 58 | + |
31 | 59 | ASSERT_EQ(pinStatus.size(), 8);
|
32 | 60 | ASSERT_EQ(pinStatus, (std::vector<int>{1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500}));
|
33 |
| - ASSERT_EQ(output, expectedOutput); |
| 61 | + if (serialOutput.empty()) { |
| 62 | + ASSERT_EQ(output, expectedOutput); |
| 63 | + } |
| 64 | + else { |
| 65 | + ASSERT_EQ(serialOutput, expectedOutput); |
| 66 | + } |
34 | 67 | }
|
35 | 68 |
|
36 | 69 | TEST(CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
|
@@ -66,9 +99,16 @@ TEST(CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
|
66 | 99 | expectedOutput.append("Configure 8 Digital\nSet 8 Digital High\n");
|
67 | 100 | expectedOutput.append("Configure 9 Digital\nSet 9 Digital Low\n");
|
68 | 101 |
|
| 102 | + std::string serialOutput; |
| 103 | + while (getSerialChar() != EOF) {} |
69 | 104 | ASSERT_EQ(pinStatus.size(), 10);
|
70 | 105 | ASSERT_EQ(pinStatus, (std::vector<int>{1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1, 0}));
|
71 |
| - ASSERT_EQ(output, expectedOutput); |
| 106 | + if (serialOutput.empty()) { |
| 107 | + ASSERT_EQ(output, expectedOutput); |
| 108 | + } |
| 109 | + else { |
| 110 | + ASSERT_EQ(serialOutput, expectedOutput); |
| 111 | + } |
72 | 112 | }
|
73 | 113 |
|
74 | 114 | TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
|
@@ -115,10 +155,17 @@ TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
|
115 | 155 | expectedOutput.append("Set 8 PWM 1535\n");
|
116 | 156 | expectedOutput.append("Set 6 PWM 1536\n");
|
117 | 157 |
|
| 158 | + std::string serialOutput; |
| 159 | + while (getSerialChar() != EOF) {} |
118 | 160 | ASSERT_NEAR((endTime - startTime) / std::chrono::milliseconds(1), std::chrono::milliseconds(2000) /
|
119 | 161 | std::chrono::milliseconds(1), std::chrono::milliseconds(10) / std::chrono::milliseconds(1));
|
120 | 162 | ASSERT_EQ(pinStatus, (std::vector<int>{1900, 1900, 1100, 1250, 1300, 1464, 1535, 1536}));
|
121 |
| - ASSERT_EQ(output, expectedOutput); |
| 163 | + if (serialOutput.empty()) { |
| 164 | + ASSERT_EQ(output, expectedOutput); |
| 165 | + } |
| 166 | + else { |
| 167 | + ASSERT_EQ(serialOutput, expectedOutput); |
| 168 | + } |
122 | 169 | }
|
123 | 170 |
|
124 | 171 | TEST(CommandInterpreterTest, BlindExecuteSoftwarePwm) {
|
@@ -165,10 +212,17 @@ TEST(CommandInterpreterTest, BlindExecuteSoftwarePwm) {
|
165 | 212 | expectedOutput.append("Set 8 PWM 1535\n");
|
166 | 213 | expectedOutput.append("Set 6 PWM 1536\n");
|
167 | 214 |
|
| 215 | + std::string serialOutput; |
| 216 | + while (getSerialChar() != EOF) {} |
168 | 217 | ASSERT_NEAR((endTime - startTime) / std::chrono::milliseconds(1), std::chrono::milliseconds(2000) /
|
169 | 218 | std::chrono::milliseconds(1), std::chrono::milliseconds(10) / std::chrono::milliseconds(1));
|
170 | 219 |
|
171 | 220 | ASSERT_EQ(pinStatus, (std::vector<int>{1100, 1900, 1100, 1250, 1300, 1464, 1535, 1536}));
|
172 |
| - ASSERT_EQ(output, expectedOutput); |
| 221 | + if (serialOutput.empty()) { |
| 222 | + ASSERT_EQ(output, expectedOutput); |
| 223 | + } |
| 224 | + else { |
| 225 | + ASSERT_EQ(serialOutput, expectedOutput); |
| 226 | + } |
173 | 227 | }
|
174 | 228 |
|
0 commit comments