mavtables  0.2.1
MAVLink router and firewall.
common.hpp
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 <iostream>
19 #include <memory>
20 #include <sstream>
21 #include <string>
22 #include <utility>
23 
24 #include "fakeit.hpp"
25 
26 
27 /** Construct a std::shared_ptr from a fakit::Mock object.
28  *
29  * \tparam T The type of object being mocked.
30  * \param mock The mock object itself.
31  */
32 template<typename T>
33 std::shared_ptr<T> mock_shared(fakeit::Mock<T> &mock){
34  fakeit::Fake(Dtor(mock));
35  std::shared_ptr<T> ptr(&mock.get());
36  return std::move(ptr);
37 }
38 
39 
40 /** Construct a std::unique_ptr from a fakit::Mock object.
41  *
42  * \tparam T The type of object being mocked.
43  * \param mock The mock object itself.
44  */
45 template<typename T>
46 std::unique_ptr<T> mock_unique(fakeit::Mock<T> &mock){
47  fakeit::Fake(Dtor(mock));
48  std::unique_ptr<T> ptr(&mock.get());
49  return std::move(ptr);
50 }
51 
52 
53 /** RAII class to replace std::cout with a mocked buffer.
54  */
55 class MockCOut
56 {
57  public:
58  /** Replace std::cout with this mock.
59  */
61  : sbuf_(std::cout.rdbuf())
62  {
63  std::cout.rdbuf(buffer_.rdbuf());
64  }
65  /** Restore std::cout.
66  */
68  {
69  std::cout.rdbuf(sbuf_);
70  }
71  /** Return the contents of the mocked std::cout buffer as a string.
72  *
73  * \returns The contents of the mocked buffer.
74  */
75  std::string buffer()
76  {
77  return buffer_.str();
78  }
79  /** Erase the mocked buffer.
80  */
81  void reset()
82  {
83  buffer_.str("");
84  }
85 
86  private:
87  std::stringstream buffer_;
88  std::streambuf *sbuf_;
89 };
90 
91 
92 /** RAII class to replace std::cerr with a mocked buffer.
93  */
94 class MockCErr
95 {
96  public:
97  /** Replace std::cerr with this mock.
98  */
100  : sbuf_(std::cerr.rdbuf())
101  {
102  std::cerr.rdbuf(buffer_.rdbuf());
103  }
104  /** Restore std::cerr.
105  */
107  {
108  std::cerr.rdbuf(sbuf_);
109  }
110  /** Return the contents of the mocked std::cerr buffer as a string.
111  *
112  * \returns The contents of the mocked buffer.
113  */
114  std::string buffer()
115  {
116  return buffer_.str();
117  }
118  /** Erase the mocked buffer.
119  */
120  void reset()
121  {
122  buffer_.str("");
123  }
124 
125  private:
126  std::stringstream buffer_;
127  std::streambuf *sbuf_;
128 };
void reset()
Definition: common.hpp:81
MockCOut()
Definition: common.hpp:60
STL namespace.
std::string buffer()
Definition: common.hpp:114
MockCErr()
Definition: common.hpp:99
void reset()
Definition: common.hpp:120
std::string buffer()
Definition: common.hpp:75
~MockCOut()
Definition: common.hpp:67
std::shared_ptr< T > mock_shared(fakeit::Mock< T > &mock)
Definition: common.hpp:33
~MockCErr()
Definition: common.hpp:106
std::unique_ptr< T > mock_unique(fakeit::Mock< T > &mock)
Definition: common.hpp:46