mavtables  0.2.1
MAVLink router and firewall.
test_Interface.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 <chrono>
19 #include <memory>
20 
21 #include <catch.hpp>
22 #include <fakeit.hpp>
23 
24 #include "ConnectionPool.hpp"
25 #include "Interface.hpp"
26 #include "Packet.hpp"
27 #include "PacketVersion2.hpp"
28 #include "utility.hpp"
29 
30 #include "common.hpp"
31 #include "common_Packet.hpp"
32 
33 
34 namespace
35 {
36 
37 #ifdef __clang__
38  #pragma clang diagnostic push
39  #pragma clang diagnostic ignored "-Wweak-vtables"
40 #endif
41 
42  // Subclass of Interface used for testing the abstract class Interface.
43  class InterfaceTestClass : public Interface
44  {
45  public:
46  InterfaceTestClass(std::shared_ptr<ConnectionPool> connection_pool)
47  : connection_pool_(std::move(connection_pool))
48  {
49  }
50  // LCOV_EXCL_START
51  ~InterfaceTestClass() = default;
52  // LCOV_EXCL_STOP
53  void send_packet(
54  const std::chrono::nanoseconds &timeout =
55  std::chrono::nanoseconds(100000)) final
56  {
57  (void)timeout;
58  }
59  void receive_packet(
60  const std::chrono::nanoseconds &timeout =
61  std::chrono::nanoseconds(100000)) final
62  {
63  (void)timeout;
64  connection_pool_->send(
65  std::make_unique<packet_v2::Packet>(to_vector(PingV2())));
66  }
67 
68  protected:
69  std::ostream &print_(std::ostream &os) const final
70  {
71  os << "interface test class";
72  return os;
73  }
74 
75  private:
76  std::shared_ptr<ConnectionPool> connection_pool_;
77  };
78 
79 #ifdef __clang__
80  #pragma clang diagnostic pop
81 #endif
82 
83 }
84 
85 
86 TEST_CASE("Interface's can be constructed.", "[Interface]")
87 {
88  fakeit::Mock<ConnectionPool> mock_pool;
89  std::shared_ptr<ConnectionPool> pool = mock_shared(mock_pool);
90  REQUIRE_NOTHROW(InterfaceTestClass(pool));
91 }
92 
93 
94 TEST_CASE("Interface's 'send_packet' method (included just for coverage)."
95  "[Interface]")
96 {
97  fakeit::Mock<ConnectionPool> mock_pool;
98  std::shared_ptr<ConnectionPool> pool = mock_shared(mock_pool);
99  InterfaceTestClass interface(pool);
100  REQUIRE_NOTHROW(interface.send_packet());
101 }
102 
103 
104 TEST_CASE("Interface's 'receive_packet' method sends the packet using the "
105  "contained ConnectionPool.", "[Interface]")
106 {
107  fakeit::Mock<ConnectionPool> mock_pool;
108  fakeit::Fake(Method(mock_pool, send));
109  std::shared_ptr<ConnectionPool> pool = mock_shared(mock_pool);
110  InterfaceTestClass interface(pool);
111  interface.receive_packet();
112  fakeit::Verify(Method(mock_pool, send).Matching([](auto &&a)
113  {
114  return a != nullptr && *a == packet_v2::Packet(to_vector(PingV2()));
115  })).Once();
116 }
117 
118 
119 TEST_CASE("Interface's are printable.", "[Interface]")
120 {
121  fakeit::Mock<ConnectionPool> mock_pool;
122  std::shared_ptr<ConnectionPool> pool = mock_shared(mock_pool);
123  REQUIRE(str(InterfaceTestClass(pool)) == "interface test class");
124 }
std::string str(const T &object)
Definition: utility.hpp:128
virtual std::ostream & print_(std::ostream &os) const =0
virtual void receive_packet(const std::chrono::nanoseconds &timeout)=0
STL namespace.
virtual void send_packet(const std::chrono::nanoseconds &timeout)=0
TEST_CASE("Interface's can be constructed.", "[Interface]")
std::shared_ptr< T > mock_shared(fakeit::Mock< T > &mock)
Definition: common.hpp:33