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
+ void echoOn (int serial) {
9
+ serialPuts (serial, " echo on\n " );
10
+ }
11
+
12
+ bool initializeSerial (int * serial) {
13
+ if ((*serial = serialOpen (" /dev/serial/by-id/usb-MicroPython_Board_in_FS_mode_e6614864d3798738-if00" , 115200 )) < 0 ) {
14
+ return false ;
15
+ }
16
+ echoOn (*serial);
17
+ return true ;
18
+ }
19
+
20
+ int getSerialChar (int * serial) {
21
+ if (*serial == -1 ) {
22
+ if (!initializeSerial
23
+ (serial)) {
24
+ std::cerr << " Unable to open serial port! Exiting." << std::endl;
25
+ exit (42 );
26
+ }
27
+ }
28
+ return serialGetchar (*serial); // from wiringSerial
29
+ }
30
+
31
+ #else
32
+
33
+ bool initializeSerial (int * serial) {
34
+ return true ;
35
+ }
36
+
37
+ int getSerialChar (int * serial) {
38
+ return EOF;
39
+ }
40
+
41
+ #endif
42
+
4
43
TEST (CommandInterpreterTest, CreateCommandInterpreter) {
5
44
testing::internal::CaptureStdout ();
45
+ int serial = -1 ;
46
+ initializeSerial (&serial);
6
47
7
48
auto pinNumbers = std::vector<int >{4 , 5 , 2 , 3 , 9 , 7 , 8 , 6 };
8
49
@@ -28,13 +69,27 @@ TEST(CommandInterpreterTest, CreateCommandInterpreter) {
28
69
expectedOutput.append (" PWM 1500\n " );
29
70
}
30
71
72
+ int charRead = EOF;
73
+ std::string serialOutput;
74
+ while ((charRead = getSerialChar (&serial)) != EOF) {
75
+ serialOutput.push_back ((char )charRead);
76
+ }
77
+
31
78
ASSERT_EQ (pinStatus.size (), 8 );
32
79
ASSERT_EQ (pinStatus, (std::vector<int >{1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 }));
33
- ASSERT_EQ (output, expectedOutput);
80
+ if (serialOutput.empty ()) {
81
+ ASSERT_EQ (output, expectedOutput);
82
+ }
83
+ else {
84
+ expectedOutput.insert (0 , " echo on\n " );
85
+ ASSERT_EQ (serialOutput, expectedOutput);
86
+ }
34
87
}
35
88
36
89
TEST (CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
37
90
testing::internal::CaptureStdout ();
91
+ int serial = -1 ;
92
+ initializeSerial (&serial);
38
93
39
94
auto pinNumbers = std::vector<int >{4 , 5 , 2 , 3 , 9 , 7 , 8 , 6 };
40
95
@@ -66,15 +121,28 @@ TEST(CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
66
121
expectedOutput.append (" Configure 8 Digital\n Set 8 Digital High\n " );
67
122
expectedOutput.append (" Configure 9 Digital\n Set 9 Digital Low\n " );
68
123
124
+ int charRead = EOF;
125
+ std::string serialOutput;
126
+ while ((charRead = getSerialChar (&serial)) != EOF) {
127
+ serialOutput.push_back ((char )charRead);
128
+ }
69
129
ASSERT_EQ (pinStatus.size (), 10 );
70
130
ASSERT_EQ (pinStatus, (std::vector<int >{1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1 , 0 }));
71
- ASSERT_EQ (output, expectedOutput);
131
+ if (serialOutput.empty ()) {
132
+ ASSERT_EQ (output, expectedOutput);
133
+ }
134
+ else {
135
+ expectedOutput.insert (0 , " echo on\n " );
136
+ ASSERT_EQ (serialOutput, expectedOutput);
137
+ }
72
138
}
73
139
74
140
TEST (CommandInterpreterTest, BlindExecuteHardwarePwm) {
75
141
testing::internal::CaptureStdout ();
142
+ int serial = -1 ;
143
+ initializeSerial (&serial);
76
144
77
- const CommandComponent acceleration = {1500 , 1900 , 1100 ,
145
+ const CommandComponent acceleration = {1900 , 1900 , 1100 ,
78
146
1250 , 1300 , 1464 , 1535 ,
79
147
1536 , std::chrono::milliseconds (2000 )};
80
148
@@ -106,7 +174,7 @@ TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
106
174
expectedOutput.append (std::to_string (pinNumber));
107
175
expectedOutput.append (" PWM 1500\n " );
108
176
}
109
- expectedOutput.append (" Set 4 PWM 1500 \n " );
177
+ expectedOutput.append (" Set 4 PWM 1900 \n " );
110
178
expectedOutput.append (" Set 5 PWM 1900\n " );
111
179
expectedOutput.append (" Set 2 PWM 1100\n " );
112
180
expectedOutput.append (" Set 3 PWM 1250\n " );
@@ -115,16 +183,29 @@ TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
115
183
expectedOutput.append (" Set 8 PWM 1535\n " );
116
184
expectedOutput.append (" Set 6 PWM 1536\n " );
117
185
186
+ int charRead = EOF;
187
+ std::string serialOutput;
188
+ while ((charRead = getSerialChar (&serial)) != EOF) {
189
+ serialOutput.push_back ((char )charRead);
190
+ }
118
191
ASSERT_NEAR ((endTime - startTime) / std::chrono::milliseconds (1 ), std::chrono::milliseconds (2000 ) /
119
192
std::chrono::milliseconds (1 ), std::chrono::milliseconds (10 ) / std::chrono::milliseconds (1 ));
120
- ASSERT_EQ (pinStatus, (std::vector<int >{1500 , 1900 , 1100 , 1250 , 1300 , 1464 , 1535 , 1536 }));
121
- ASSERT_EQ (output, expectedOutput);
193
+ ASSERT_EQ (pinStatus, (std::vector<int >{1900 , 1900 , 1100 , 1250 , 1300 , 1464 , 1535 , 1536 }));
194
+ if (serialOutput.empty ()) {
195
+ ASSERT_EQ (output, expectedOutput);
196
+ }
197
+ else {
198
+ expectedOutput.insert (0 , " echo on\n " );
199
+ ASSERT_EQ (serialOutput, expectedOutput);
200
+ }
122
201
}
123
202
124
203
TEST (CommandInterpreterTest, BlindExecuteSoftwarePwm) {
125
204
testing::internal::CaptureStdout ();
205
+ int serial = -1 ;
206
+ initializeSerial (&serial);
126
207
127
- const CommandComponent acceleration = {1500 , 1900 , 1100 ,
208
+ const CommandComponent acceleration = {1100 , 1900 , 1100 ,
128
209
1250 , 1300 , 1464 , 1535 ,
129
210
1536 , std::chrono::milliseconds (2000 )};
130
211
@@ -156,7 +237,7 @@ TEST(CommandInterpreterTest, BlindExecuteSoftwarePwm) {
156
237
expectedOutput.append (std::to_string (pinNumber));
157
238
expectedOutput.append (" PWM 1500\n " );
158
239
}
159
- expectedOutput.append (" Set 4 PWM 1500 \n " );
240
+ expectedOutput.append (" Set 4 PWM 1100 \n " );
160
241
expectedOutput.append (" Set 5 PWM 1900\n " );
161
242
expectedOutput.append (" Set 2 PWM 1100\n " );
162
243
expectedOutput.append (" Set 3 PWM 1250\n " );
@@ -165,10 +246,21 @@ TEST(CommandInterpreterTest, BlindExecuteSoftwarePwm) {
165
246
expectedOutput.append (" Set 8 PWM 1535\n " );
166
247
expectedOutput.append (" Set 6 PWM 1536\n " );
167
248
249
+ int charRead = EOF;
250
+ std::string serialOutput;
251
+ while ((charRead = getSerialChar (&serial)) != EOF) {
252
+ serialOutput.push_back ((char )charRead);
253
+ }
168
254
ASSERT_NEAR ((endTime - startTime) / std::chrono::milliseconds (1 ), std::chrono::milliseconds (2000 ) /
169
255
std::chrono::milliseconds (1 ), std::chrono::milliseconds (10 ) / std::chrono::milliseconds (1 ));
170
256
171
- ASSERT_EQ (pinStatus, (std::vector<int >{1500 , 1900 , 1100 , 1250 , 1300 , 1464 , 1535 , 1536 }));
172
- ASSERT_EQ (output, expectedOutput);
257
+ ASSERT_EQ (pinStatus, (std::vector<int >{1100 , 1900 , 1100 , 1250 , 1300 , 1464 , 1535 , 1536 }));
258
+ if (serialOutput.empty ()) {
259
+ ASSERT_EQ (output, expectedOutput);
260
+ }
261
+ else {
262
+ expectedOutput.insert (0 , " echo on\n " );
263
+ ASSERT_EQ (serialOutput, expectedOutput);
264
+ }
173
265
}
174
266
0 commit comments