service_client_mock.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef PILZ_TESTUTILS_SERVICE_CLIENT_MOCK_H
19 #define PILZ_TESTUTILS_SERVICE_CLIENT_MOCK_H
20 
21 #include <functional>
22 #include <string>
23 
24 #include <gmock/gmock.h>
25 
26 #include <ros/console.h>
27 
28 namespace pilz_testutils
29 {
36 template <typename S>
38 {
39 public:
40  typedef std::function<bool(const std::string&, S&)> CallFunction;
41  typedef std::function<bool(const std::string&)> LogicalOperatorFunction;
42 
43  ServiceClientMock(const std::string& name,
44  const CallFunction& call_callback,
45  const LogicalOperatorFunction& negation_operator_callback)
46  : name_(name), call_callback_(call_callback), negation_operator_callback_(negation_operator_callback)
47  {
48  }
49 
50  bool operator!() const
51  {
53  }
54 
55  bool call(S& s)
56  {
57  ROS_DEBUG_NAMED("Mock", "Received call to %s", name_.c_str());
58  return call_callback_(name_, s);
59  }
60 
61  std::string getService() const
62  {
63  return name_;
64  }
65 
66 private:
67  std::string name_;
70 };
71 
77 template <typename S>
79 {
80 public:
93  ServiceClientMock<S> create(const std::string& name, bool /*persistent*/)
94  {
95  using std::placeholders::_1;
96  using std::placeholders::_2;
97  return ServiceClientMock<S>(name,
98  std::bind(&ServiceClientMockFactory::call_named, this, _1, _2),
99  std::bind(&ServiceClientMockFactory::handle_invalid_named, this, _1));
100  }
101 
102  // service call mock method
103  MOCK_CONST_METHOD2_T(call_named, bool(const std::string& name, S& s));
104  MOCK_CONST_METHOD1_T(handle_invalid_named, bool(const std::string& name));
105 };
106 
107 } // namespace pilz_testutils
108 
109 #endif // PILZ_TESTUTILS_SERVICE_CLIENT_MOCK_H
pilz_testutils::ServiceClientMockFactory::create
ServiceClientMock< S > create(const std::string &name, bool)
Returns a ServiceClientMock, which passes service calls to a (named) mock method.
Definition: service_client_mock.h:93
pilz_testutils::ServiceClientMock::LogicalOperatorFunction
std::function< bool(const std::string &)> LogicalOperatorFunction
Definition: service_client_mock.h:41
pilz_testutils::ServiceClientMock::getService
std::string getService() const
Definition: service_client_mock.h:61
s
XmlRpcServer s
pilz_testutils::ServiceClientMock::negation_operator_callback_
LogicalOperatorFunction negation_operator_callback_
Definition: service_client_mock.h:69
pilz_testutils::ServiceClientMock::name_
std::string name_
Definition: service_client_mock.h:67
console.h
pilz_testutils::ServiceClientMock
Mock for a ros::ServiceClient, calls are passed through to a given callback (which can be a mock meth...
Definition: service_client_mock.h:37
ROS_DEBUG_NAMED
#define ROS_DEBUG_NAMED(name,...)
pilz_testutils::ServiceClientMock::operator!
bool operator!() const
Definition: service_client_mock.h:50
pilz_testutils
Definition: joint_state_publisher_mock.h:31
pilz_testutils::ServiceClientMock::call_callback_
CallFunction call_callback_
Definition: service_client_mock.h:68
pilz_testutils::ServiceClientMock::ServiceClientMock
ServiceClientMock(const std::string &name, const CallFunction &call_callback, const LogicalOperatorFunction &negation_operator_callback)
Definition: service_client_mock.h:43
pilz_testutils::ServiceClientMockFactory::MOCK_CONST_METHOD1_T
MOCK_CONST_METHOD1_T(handle_invalid_named, bool(const std::string &name))
pilz_testutils::ServiceClientMockFactory
Provides a mock method for service calls for a specific service type.
Definition: service_client_mock.h:78
pilz_testutils::ServiceClientMock::call
bool call(S &s)
Definition: service_client_mock.h:55
pilz_testutils::ServiceClientMock::CallFunction
std::function< bool(const std::string &, S &)> CallFunction
Definition: service_client_mock.h:40
pilz_testutils::ServiceClientMockFactory::MOCK_CONST_METHOD2_T
MOCK_CONST_METHOD2_T(call_named, bool(const std::string &name, S &s))


pilz_testutils
Author(s):
autogenerated on Sat Nov 25 2023 03:16:57