43 TEST_CASE(
"SerialInterface's can be constructed.",
"[SerialInterface]")
45 fakeit::Mock<SerialPort> mock_port;
46 fakeit::Mock<ConnectionPool> mock_pool;
47 fakeit::Mock<Connection> mock_connection;
48 fakeit::Fake(Method(mock_pool, add));
52 SECTION(
"When all inputs are valid, registers connection with pool.")
55 fakeit::When(Method(mock_pool, add)).AlwaysDo([&](
auto a)
57 conn = a.lock().get();
61 fakeit::Verify(Method(mock_pool, add)).Once();
62 REQUIRE(conn == &mock_connection.get());
64 SECTION(
"Ensures the serial port pointer is not null.")
68 std::invalid_argument);
72 "Given serial port pointer is null.");
74 SECTION(
"Ensures the connection pool pointer is not null.")
78 std::invalid_argument);
83 "Given connection pool pointer is null.");
85 SECTION(
"Ensures the connection pointer is not null.")
89 std::invalid_argument);
93 "Given connection pointer is null.");
98 TEST_CASE(
"SerialInterface's 'receive_packet' method.",
"[SerialInterface]")
102 std::make_shared<packet_v2::Packet>(to_vector(HeartbeatV2()));
103 auto encapsulated_data =
104 std::make_shared<packet_v2::Packet>(to_vector(EncapsulatedDataV2()));
107 fakeit::Mock<SerialPort> mock_port(serial_port);
110 void(std::back_insert_iterator<std::vector<uint8_t>>,
111 const std::chrono::nanoseconds &);
113 fakeit::Mock<ConnectionPool> mock_pool;
115 fakeit::Fake(Method(mock_pool, add));
118 send_packets([](
const auto & a,
const auto & b)
120 return a.data() < b.data();
122 fakeit::When(Method(mock_pool, send)).AlwaysDo([&](
auto & a)
124 const Packet *packet = a.get();
127 *dynamic_cast<const packet_v2::Packet *>(packet)));
130 fakeit::Mock<Connection> mock_connection;
132 fakeit::Fake(Method(mock_connection, add_address));
135 std::chrono::nanoseconds timeout = 250ms;
136 SECTION(
"No packet received.")
139 fakeit::When(OverloadedMethod(mock_port, read, read_type)
140 ).AlwaysDo([](
auto a,
auto b)
148 fakeit::Verify(OverloadedMethod(mock_port, read, read_type).Matching(
154 fakeit::Verify(Method(mock_connection, add_address)).Exactly(0);
155 fakeit::Verify(Method(mock_pool, send)).Exactly(0);
157 SECTION(
"Partial packet received.")
160 fakeit::When(OverloadedMethod(mock_port, read, read_type)
161 ).AlwaysDo([](
auto a,
auto b)
164 auto vec = to_vector(HeartbeatV2());
165 std::copy(vec.begin(), vec.end() - 1, a);
170 fakeit::Verify(OverloadedMethod(mock_port, read, read_type).Matching(
176 fakeit::Verify(Method(mock_connection, add_address)).Exactly(0);
177 fakeit::Verify(Method(mock_pool, send)).Exactly(0);
179 SECTION(
"Full packet received.")
182 fakeit::When(OverloadedMethod(mock_port, read, read_type)
183 ).AlwaysDo([](
auto a,
auto b)
186 auto vec = to_vector(HeartbeatV2());
187 std::copy(vec.begin(), vec.end(), a);
192 fakeit::Verify(OverloadedMethod(mock_port, read, read_type).Matching(
198 fakeit::Verify(Method(mock_connection, add_address
200 fakeit::Verify(Method(mock_pool, send)).Exactly(1);
201 REQUIRE(send_packets.count(*
heartbeat) == 1);
203 REQUIRE(it != send_packets.end());
204 REQUIRE(it->connection() !=
nullptr);
206 SECTION(
"Multiple packets received (same MAVLink address).")
209 fakeit::When(OverloadedMethod(mock_port, read, read_type)
210 ).AlwaysDo([](
auto a,
auto b)
213 auto vec = to_vector(HeartbeatV2());
214 std::copy(vec.begin(), vec.end(), a);
220 fakeit::Verify(OverloadedMethod(mock_port, read, read_type).Matching(
226 fakeit::Verify(Method(mock_connection, add_address
228 fakeit::Verify(Method(mock_pool, send)).Exactly(2);
229 REQUIRE(send_packets.count(*
heartbeat) == 2);
231 REQUIRE(it != send_packets.end());
232 REQUIRE(it->connection() !=
nullptr);
234 REQUIRE(it != send_packets.end());
235 REQUIRE(it->connection() !=
nullptr);
237 SECTION(
"Multiple packets received (different MAVLink address).")
240 fakeit::When(OverloadedMethod(mock_port, read, read_type)
241 ).Do([](
auto a,
auto b)
244 auto vec = to_vector(HeartbeatV2());
245 std::copy(vec.begin(), vec.end(), a);
246 }).Do([](
auto a,
auto b)
249 auto vec = to_vector(EncapsulatedDataV2());
250 std::copy(vec.begin(), vec.end(), a);
256 fakeit::Verify(OverloadedMethod(mock_port, read, read_type).Matching(
262 fakeit::Verify(Method(mock_connection, add_address
264 fakeit::Verify(Method(mock_connection, add_address
266 fakeit::Verify(Method(mock_pool, send)).Exactly(2);
267 REQUIRE(send_packets.count(*
heartbeat) == 1);
268 REQUIRE(send_packets.count(*encapsulated_data) == 1);
270 REQUIRE(it != send_packets.end());
271 REQUIRE(it->connection() !=
nullptr);
272 it = send_packets.find(*encapsulated_data);
273 REQUIRE(it != send_packets.end());
274 REQUIRE(it->connection() !=
nullptr);
276 SECTION(
"Partial packets should be combined and parsed.")
279 fakeit::When(OverloadedMethod(mock_port, read, read_type)
280 ).Do([](
auto a,
auto b)
283 auto vec = to_vector(EncapsulatedDataV2());
284 std::copy(vec.begin(), vec.end() - 10, a);
285 }).Do([](
auto a,
auto b)
288 auto vec = to_vector(EncapsulatedDataV2());
289 std::copy(vec.end() - 10, vec.end(), a);
295 fakeit::Verify(OverloadedMethod(mock_port, read, read_type).Matching(
301 fakeit::Verify(Method(mock_connection, add_address
303 fakeit::Verify(Method(mock_pool, send)).Exactly(1);
304 REQUIRE(send_packets.count(*encapsulated_data) == 1);
305 auto it = send_packets.find(*encapsulated_data);
306 REQUIRE(it != send_packets.end());
307 REQUIRE(it->connection() !=
nullptr);
312 TEST_CASE(
"SerialInterface's 'send_packet' method.",
"[SerialInterface]")
316 std::make_shared<packet_v2::Packet>(to_vector(HeartbeatV2()));
317 auto encapsulated_data =
318 std::make_shared<packet_v2::Packet>(to_vector(EncapsulatedDataV2()));
321 fakeit::Mock<SerialPort> mock_port(serial_port);
324 void(std::vector<uint8_t>::const_iterator first,
325 std::vector<uint8_t>::const_iterator last);
326 std::multiset<std::vector<uint8_t>> write_bytes;
327 fakeit::When(OverloadedMethod(mock_port, write, write_type)).AlwaysDo(
330 std::vector<uint8_t> vec;
331 std::copy(a, b, std::back_inserter(vec));
332 write_bytes.insert(vec);
335 fakeit::Mock<ConnectionPool> mock_pool;
337 fakeit::Fake(Method(mock_pool, add));
339 fakeit::Mock<Connection> mock_connection;
343 std::chrono::nanoseconds timeout = 250ms;
344 SECTION(
"No packets, timeout.")
347 fakeit::When(Method(mock_connection, next_packet)).Return(
nullptr);
352 Method(mock_connection, next_packet).Using(250ms)).Once();
354 OverloadedMethod(mock_port, write, write_type)).Exactly(0);
356 SECTION(
"Single packet.")
359 fakeit::When(Method(mock_connection, next_packet)).Return(
heartbeat);
364 Method(mock_connection, next_packet).Using(250ms)).Once();
366 OverloadedMethod(mock_port, write, write_type)).Exactly(1);
367 REQUIRE(write_bytes.count(
heartbeat->data()) == 1);
369 SECTION(
"Multiple packets.")
372 fakeit::When(Method(mock_connection, next_packet)
373 ).Return(
heartbeat).Return(encapsulated_data);
378 Method(mock_connection, next_packet).Using(250ms)).Once();
380 OverloadedMethod(mock_port, write, write_type)).Exactly(1);
381 REQUIRE(write_bytes.count(
heartbeat->data()) == 1);
386 Method(mock_connection, next_packet).Using(250ms)).Exactly(2);
388 OverloadedMethod(mock_port, write, write_type)).Exactly(2);
389 REQUIRE(write_bytes.count(
heartbeat->data()) == 1);
390 REQUIRE(write_bytes.count(encapsulated_data->data()) == 1);
395 TEST_CASE(
"SerialInterface's are printable.",
"[SerialInterface]")
397 fakeit::Mock<ConnectionPool> mock_pool;
398 fakeit::Mock<Connection> mock_connection;
399 fakeit::Fake(Method(mock_pool, add));
402 auto port = std::make_unique<SerialPort>();
404 REQUIRE(
str(serial) ==
"unknown serial port");
void receive_packet(const std::chrono::nanoseconds &timeout) final
std::string str(const T &object)
TEST_CASE("SerialInterface's can be constructed.", "[SerialInterface]")
void send_packet(const std::chrono::nanoseconds &timeout) final
std::shared_ptr< T > mock_shared(fakeit::Mock< T > &mock)
std::unique_ptr< T > mock_unique(fakeit::Mock< T > &mock)