mavtables  0.2.1
MAVLink router and firewall.
test_SerialPort.cpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #include <algorithm>
19 #include <chrono>
20 #include <iterator>
21 #include <optional>
22 #include <utility>
23 
24 #include <catch.hpp>
25 #include <fakeit.hpp>
26 
27 #include "SerialPort.hpp"
28 #include "utility.hpp"
29 
30 
31 using namespace std::chrono_literals;
32 
33 
34 TEST_CASE("SerialPort's 'read' method takes a timeout and returns a vector "
35  "of bytes.", "[SerialPort]")
36 {
37  // This test ensures that one receive method calls the other.
38  SerialPort serial;
39  fakeit::Mock<SerialPort> mock_port(serial);
40  fakeit::When(
41  OverloadedMethod(
42  mock_port, read,
43  void(std::back_insert_iterator<std::vector<uint8_t>>,
44  const std::chrono::nanoseconds &))).AlwaysDo(
45  [](auto a, auto b)
46  {
47  (void)b;
48  std::vector<uint8_t> vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
49  std::copy(vec.begin(), vec.end(), a);
50  });
51  SerialPort &port = mock_port.get();
52  std::vector<uint8_t> vec_compare = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
53  std::chrono::nanoseconds timeout = 1ms;
54  auto data = port.read(timeout);
55  REQUIRE(data == vec_compare);
56  fakeit::Verify(
57  OverloadedMethod(
58  mock_port, read,
59  void(std::back_insert_iterator<std::vector<uint8_t>>,
60  const std::chrono::nanoseconds &)).Matching(
61  [](auto a, auto b)
62  {
63  (void)a;
64  return b == 1ms;
65  })).Once();
66 }
67 
68 
69 
70 TEST_CASE("SerialPort's 'read' method takes a back inserter and a timeout "
71  "writes the received bytes to the back inserter.", "[SerialPort]")
72 {
73  // This test ensures that one read method calls the other.
74  using read_type = std::vector<uint8_t>(const std::chrono::nanoseconds &);
75  SerialPort serial;
76  fakeit::Mock<SerialPort> mock_port(serial);
77  fakeit::When(
78  OverloadedMethod(
79  mock_port, read, read_type)).AlwaysDo([](auto a)
80  {
81  (void)a;
82  std::vector<uint8_t> vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
83  return vec;
84  });
85  SerialPort &port = mock_port.get();
86  std::vector<uint8_t> vec_compare = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
87  std::chrono::nanoseconds timeout = 1ms;
88  std::vector<uint8_t> data;
89  port.read(std::back_inserter(data), timeout);
90  REQUIRE(data == vec_compare);
91  fakeit::Verify(
92  OverloadedMethod(
93  mock_port, read, read_type).Matching([](auto a)
94  {
95  return a == 1ms;
96  })).Once();
97 }
98 
99 
100 TEST_CASE("SerialPort's 'write' method accepts a vector of bytes.",
101  "[SerialPort]")
102 {
103  // This test ensures that one write method calls the other.
104  SerialPort serial;
105  fakeit::Mock<SerialPort> mock_port(serial);
106  fakeit::Fake(
107  OverloadedMethod(
108  mock_port, write,
109  void(std::vector<uint8_t>::const_iterator,
110  std::vector<uint8_t>::const_iterator)));
111  SerialPort &port = mock_port.get();
112  std::vector<uint8_t> vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
113  port.write(vec);
114  fakeit::Verify(
115  OverloadedMethod(
116  mock_port, write,
117  void(std::vector<uint8_t>::const_iterator,
118  std::vector<uint8_t>::const_iterator)).Matching(
119  [&](auto a, auto b)
120  {
121  return a == vec.begin() && b == vec.end();
122  })).Once();
123 }
124 
125 
126 TEST_CASE("SerialPort's 'write' method accepts two vector iterators",
127  "[UDPSocket]")
128 {
129  // This test ensures that one write method calls the other.
130  SerialPort serial;
131  fakeit::Mock<SerialPort> mock_port(serial);
132  std::vector<uint8_t> write_vec;
133  fakeit::When(
134  OverloadedMethod(
135  mock_port, write,
136  void(const std::vector<uint8_t> &))).AlwaysDo(
137  [&](auto a)
138  {
139  write_vec = a;
140  });
141  SerialPort &port = mock_port.get();
142  std::vector<uint8_t> vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
143  port.write(vec.begin(), vec.end());
144  fakeit::Verify(
145  OverloadedMethod(
146  mock_port, write,
147  void(const std::vector<uint8_t> &))).Once();
148  REQUIRE(vec == write_vec);
149 }
150 
151 
152 TEST_CASE("SerialPort's are printable.", "SerialPort")
153 {
154  REQUIRE(str(SerialPort()) == "unknown serial port");
155 }
std::string str(const T &object)
Definition: utility.hpp:128
TEST_CASE("SerialPort's 'read' method takes a timeout and returns a vector " "of bytes.", "[SerialPort]")
virtual std::vector< uint8_t > read(const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds::zero())
Definition: SerialPort.cpp:47
virtual void write(const std::vector< uint8_t > &data)
Definition: SerialPort.cpp:78