Skip to content

Commit 9de8f15

Browse files
committed
Add serial read unit testing
Currently untested, so probably doesn't work. Only active when MOCK_RPI not set.
1 parent ccb7565 commit 9de8f15

File tree

1 file changed

+58
-4
lines changed

1 file changed

+58
-4
lines changed

testing/Command_Interpreter_Testing.cpp

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
#include "Command_Interpreter.h"
22
#include <gtest/gtest.h>
33

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+
429
TEST(CommandInterpreterTest, CreateCommandInterpreter) {
530
testing::internal::CaptureStdout();
631

@@ -28,9 +53,17 @@ TEST(CommandInterpreterTest, CreateCommandInterpreter) {
2853
expectedOutput.append(" PWM 1500\n");
2954
}
3055

56+
std::string serialOutput;
57+
while (getSerialChar() != EOF) {}
58+
3159
ASSERT_EQ(pinStatus.size(), 8);
3260
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+
}
3467
}
3568

3669
TEST(CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
@@ -66,9 +99,16 @@ TEST(CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
6699
expectedOutput.append("Configure 8 Digital\nSet 8 Digital High\n");
67100
expectedOutput.append("Configure 9 Digital\nSet 9 Digital Low\n");
68101

102+
std::string serialOutput;
103+
while (getSerialChar() != EOF) {}
69104
ASSERT_EQ(pinStatus.size(), 10);
70105
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+
}
72112
}
73113

74114
TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
@@ -115,10 +155,17 @@ TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
115155
expectedOutput.append("Set 8 PWM 1535\n");
116156
expectedOutput.append("Set 6 PWM 1536\n");
117157

158+
std::string serialOutput;
159+
while (getSerialChar() != EOF) {}
118160
ASSERT_NEAR((endTime - startTime) / std::chrono::milliseconds(1), std::chrono::milliseconds(2000) /
119161
std::chrono::milliseconds(1), std::chrono::milliseconds(10) / std::chrono::milliseconds(1));
120162
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+
}
122169
}
123170

124171
TEST(CommandInterpreterTest, BlindExecuteSoftwarePwm) {
@@ -165,10 +212,17 @@ TEST(CommandInterpreterTest, BlindExecuteSoftwarePwm) {
165212
expectedOutput.append("Set 8 PWM 1535\n");
166213
expectedOutput.append("Set 6 PWM 1536\n");
167214

215+
std::string serialOutput;
216+
while (getSerialChar() != EOF) {}
168217
ASSERT_NEAR((endTime - startTime) / std::chrono::milliseconds(1), std::chrono::milliseconds(2000) /
169218
std::chrono::milliseconds(1), std::chrono::milliseconds(10) / std::chrono::milliseconds(1));
170219

171220
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+
}
173227
}
174228

0 commit comments

Comments
 (0)