mocks.h
Go to the documentation of this file.
1 /***
2  * Software License Agreement: BSD 3-Clause License
3  *
4  * Copyright (c) 2016, qbrobotics®
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
8  * following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this list of conditions and the
11  * following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
14  * following disclaimer in the documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
17  * products derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 // Google Testing Framework
29 #include <gtest/gtest.h>
30 #include <gmock/gmock.h>
31 // qb robotics libraries
33 
34 using ::testing::Invoke;
35 using ::testing::A;
36 using ::testing::_;
37 
38 namespace qb_device_driver {
39 class qbDeviceAPIMock : public qbDeviceAPI {
40  public:
42  // delegate all calls to the base class (current real implementation)
43  ON_CALL(*this, activate(_,_,_))
44  .WillByDefault(Invoke(this, &qbDeviceAPIMock::realActivate));
45  ON_CALL(*this, close(_))
46  .WillByDefault(Invoke(this, &qbDeviceAPIMock::realClose));
47  ON_CALL(*this, getCurrents(_,_,_))
48  .WillByDefault(Invoke(this, &qbDeviceAPIMock::realGetCurrents));
49  ON_CALL(*this, getDeviceIds(_,_))
50  .WillByDefault(Invoke(this, &qbDeviceAPIMock::realGetDeviceIds));
51  ON_CALL(*this, getInfo(_,_,_,_))
52  .WillByDefault(Invoke(this, &qbDeviceAPIMock::realGetInfo));
53  ON_CALL(*this, getMeasurements(_,_,_))
54  .WillByDefault(Invoke(this, &qbDeviceAPIMock::realGetMeasurements));
55  ON_CALL(*this, getParameters(_,_,_,_,_,_,_))
56  .WillByDefault(Invoke(this, &qbDeviceAPIMock::realGetParameters));
57  ON_CALL(*this, getSerialPorts(_))
58  .WillByDefault(Invoke(this, &qbDeviceAPIMock::realGetSerialPorts));
59  ON_CALL(*this, getStatus(_,_,_))
60  .WillByDefault(Invoke(this, &qbDeviceAPIMock::realGetStatus));
61  ON_CALL(*this, open(_,_))
62  .WillByDefault(Invoke(this, &qbDeviceAPIMock::realOpen));
63  ON_CALL(*this, setInputs(_,_,_))
64  .WillByDefault(Invoke(this, &qbDeviceAPIMock::realSetInputs));
65  }
66 
67  MOCK_METHOD3(activate, void(comm_settings *file_descriptor, int id, char activate_command));
68  MOCK_METHOD1(close, void(comm_settings *file_descriptor));
69  MOCK_METHOD3(getCurrents, int(comm_settings *file_descriptor, int id, short int currents[2]));
70  MOCK_METHOD2(getDeviceIds, int(comm_settings *file_descriptor, char device_ids[255]));
71  MOCK_METHOD4(getInfo, int(comm_settings *file_descriptor, int id, short int info_type, char *info));
72  MOCK_METHOD3(getMeasurements, int(comm_settings *file_descriptor, int id, short int measurements[3]));
73  MOCK_METHOD7(getParameters, int(comm_settings *file_descriptor, int id, unsigned short index, void *values,
74  unsigned short value_size, unsigned short num_of_values, uint8_t *buffer));
75  MOCK_METHOD1(getSerialPorts, int(char serial_ports[10][255]));
76  MOCK_METHOD3(getStatus, int(comm_settings *file_descriptor, int id, char *activate_status));
77  MOCK_METHOD2(open, void(comm_settings *file_descriptor, const char *serial_port));
78  MOCK_METHOD3(setInputs, void(comm_settings *file_descriptor, int id, short int inputs[]));
79 
80  void realActivate(comm_settings *file_descriptor, int id, char activate_command) {
81  qbDeviceAPI::activate(file_descriptor, id, activate_command);
82  }
83  void realClose(comm_settings *file_descriptor) {
84  qbDeviceAPI::close(file_descriptor);
85  }
86  int realGetCurrents(comm_settings *file_descriptor, int id, short int currents[2]) {
87  return qbDeviceAPI::getCurrents(file_descriptor, id, currents);
88  }
89  int realGetDeviceIds(comm_settings *file_descriptor, char device_ids[255]) {
90  return qbDeviceAPI::getDeviceIds(file_descriptor, device_ids);
91  }
92  int realGetInfo(comm_settings *file_descriptor, int id, short int info_type, char *info) {
93  return qbDeviceAPI::getInfo(file_descriptor, id, info_type, info);
94  }
95  int realGetMeasurements(comm_settings *file_descriptor, int id, short int measurements[3]) {
96  return qbDeviceAPI::getMeasurements(file_descriptor, id, measurements);
97  }
98  int realGetParameters(comm_settings *file_descriptor, int id, unsigned short index, void *values,
99  unsigned short value_size, unsigned short num_of_values, uint8_t *buffer) {
100  return qbDeviceAPI::getParameters(file_descriptor, id, index, values, value_size, num_of_values, buffer);
101  }
102  int realGetSerialPorts(char serial_ports[10][255]) {
103  return qbDeviceAPI::getSerialPorts(serial_ports);
104  }
105  int realGetStatus(comm_settings *file_descriptor, int id, char *activate_status) {
106  return qbDeviceAPI::getStatus(file_descriptor, id, activate_status);
107  }
108  void realOpen(comm_settings *file_descriptor, const char *serial_port) {
109  qbDeviceAPI::open(file_descriptor, serial_port);
110  }
111  void realSetInputs(comm_settings *file_descriptor, int id, short int inputs[]) {
112  qbDeviceAPI::setInputs(file_descriptor, id, inputs);
113  }
114 
115 };
116 typedef std::shared_ptr<qbDeviceAPIMock> qbDeviceAPIMockPtr;
117 } // namespace qb_device_driver
118 
120 class qbDeviceHWMock : public qbDeviceHW {
121  public:
122  qbDeviceHWMock(qb_device_driver::qbDeviceAPIPtr api_mock) : qbDeviceHW(api_mock) {
123  // delegate all calls to the base class (current real implementation)
124  ON_CALL(*this, activate())
125  .WillByDefault(Invoke(this, &qbDeviceHWMock::realActivate));
126  ON_CALL(*this, close())
127  .WillByDefault(Invoke(this, &qbDeviceHWMock::realClose));
128  ON_CALL(*this, deactivate())
129  .WillByDefault(Invoke(this, &qbDeviceHWMock::realDeactivate));
130  ON_CALL(*this, get(_,_))
131  .WillByDefault(Invoke(this, &qbDeviceHWMock::realGet));
132  ON_CALL(*this, getInfo())
133  .WillByDefault(Invoke(this, &qbDeviceHWMock::realGetInfo));
134  ON_CALL(*this, isActive())
135  .WillByDefault(Invoke(this, &qbDeviceHWMock::realIsActive));
136  ON_CALL(*this, isOpen())
137  .WillByDefault(Invoke(this, &qbDeviceHWMock::realIsOpen));
138  ON_CALL(*this, open())
139  .WillByDefault(Invoke(this, static_cast<int (qbDeviceHWMock::*)()>(&qbDeviceHWMock::realOpen)));
140  ON_CALL(*this, open(A<const std::string&>()))
141  .WillByDefault(Invoke(this, static_cast<int (qbDeviceHWMock::*)(const std::string&)>(&qbDeviceHWMock::realOpen)));
142  ON_CALL(*this, retrieveDeviceParameters(_,_))
143  .WillByDefault(Invoke(this, &qbDeviceHWMock::realRetrieveDeviceParameters));
144  ON_CALL(*this, retrieveSerialPort())
145  .WillByDefault(Invoke(this, &qbDeviceHWMock::realRetrieveSerialPort));
146  ON_CALL(*this, set(_))
147  .WillByDefault(Invoke(this, &qbDeviceHWMock::realSet));
148  }
149 
150  MOCK_METHOD0(activate, int());
151  MOCK_METHOD0(close, int());
152  MOCK_METHOD0(deactivate, int());
153  MOCK_METHOD2(get, int(std::vector<double> &positions, std::vector<double> &currents));
154  MOCK_METHOD0(getInfo, std::string());
155  MOCK_METHOD0(isActive, bool());
156  MOCK_METHOD0(isOpen, bool());
157  MOCK_METHOD0(open, int());
158  MOCK_METHOD1(open, int(const std::string &serial_port));
159  MOCK_METHOD2(retrieveDeviceParameters, int(std::vector<int> &limits, std::vector<int> &resolutions));
160  MOCK_METHOD0(retrieveSerialPort, int());
161  MOCK_METHOD1(set, int(const std::vector<double> &commands));
162 
163  int realActivate() {
164  return qbDeviceHW::activate();
165  }
166  int realClose() {
167  return qbDeviceHW::close();
168  }
170  return qbDeviceHW::deactivate();
171  }
172  int realGet(std::vector<double> &positions, std::vector<double> &currents) {
173  return qbDeviceHW::get(positions, currents);
174  }
175  std::string realGetInfo() {
176  return qbDeviceHW::getInfo();
177  }
178  bool realIsActive() {
179  return qbDeviceHW::isActive();
180  }
181  bool realIsOpen() {
182  return qbDeviceHW::isOpen();
183  }
184  int realOpen() {
185  return qbDeviceHW::open();
186  }
187  int realOpen(const std::string &serial_port) {
188  return qbDeviceHW::open(serial_port);
189  }
190  int realRetrieveDeviceParameters(std::vector<int> &limits, std::vector<int> &resolutions) {
191  return qbDeviceHW::retrieveDeviceParameters(limits, resolutions);
192  }
194  return qbDeviceHW::retrieveSerialPort();
195  }
196  int realSet(const std::vector<double> &commands) {
197  return qbDeviceHW::set(commands);
198  }
199 };
200 } // namespace qb_device_hardware_interface
int realGetParameters(comm_settings *file_descriptor, int id, unsigned short index, void *values, unsigned short value_size, unsigned short num_of_values, uint8_t *buffer)
Definition: mocks.h:98
MOCK_METHOD3(activate, void(comm_settings *file_descriptor, int id, char activate_command))
void realActivate(comm_settings *file_descriptor, int id, char activate_command)
Definition: mocks.h:80
int realGetMeasurements(comm_settings *file_descriptor, int id, short int measurements[3])
Definition: mocks.h:95
int realGetInfo(comm_settings *file_descriptor, int id, short int info_type, char *info)
Definition: mocks.h:92
MOCK_METHOD2(getDeviceIds, int(comm_settings *file_descriptor, char device_ids[255]))
qbDeviceHWMock(qb_device_driver::qbDeviceAPIPtr api_mock)
Definition: mocks.h:122
int realGetDeviceIds(comm_settings *file_descriptor, char device_ids[255])
Definition: mocks.h:89
int realSet(const std::vector< double > &commands)
Definition: mocks.h:196
void realOpen(comm_settings *file_descriptor, const char *serial_port)
Definition: mocks.h:108
int realRetrieveDeviceParameters(std::vector< int > &limits, std::vector< int > &resolutions)
Definition: mocks.h:190
MOCK_METHOD7(getParameters, int(comm_settings *file_descriptor, int id, unsigned short index, void *values, unsigned short value_size, unsigned short num_of_values, uint8_t *buffer))
int realOpen(const std::string &serial_port)
Definition: mocks.h:187
void realSetInputs(comm_settings *file_descriptor, int id, short int inputs[])
Definition: mocks.h:111
The qbrobotics Device HardWare interface extends the hardware_interface::RobotHW by providing all the...
MOCK_METHOD4(getInfo, int(comm_settings *file_descriptor, int id, short int info_type, char *info))
std::shared_ptr< qbDeviceAPIMock > qbDeviceAPIMockPtr
Definition: mocks.h:116
MOCK_METHOD1(close, void(comm_settings *file_descriptor))
int realGetStatus(comm_settings *file_descriptor, int id, char *activate_status)
Definition: mocks.h:105
int realGet(std::vector< double > &positions, std::vector< double > &currents)
Definition: mocks.h:172
int realGetCurrents(comm_settings *file_descriptor, int id, short int currents[2])
Definition: mocks.h:86
int realGetSerialPorts(char serial_ports[10][255])
Definition: mocks.h:102
void realClose(comm_settings *file_descriptor)
Definition: mocks.h:83


qb_device_hardware_interface
Author(s): qbrobotics®
autogenerated on Wed Oct 9 2019 03:45:36