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_;
68  CallFunction call_callback_;
69  LogicalOperatorFunction negation_operator_callback_;
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
Mock for a ros::ServiceClient, calls are passed through to a given callback (which can be a mock meth...
XmlRpcServer s
ServiceClientMock(const std::string &name, const CallFunction &call_callback, const LogicalOperatorFunction &negation_operator_callback)
ServiceClientMock< S > create(const std::string &name, bool)
Returns a ServiceClientMock, which passes service calls to a (named) mock method. ...
#define ROS_DEBUG_NAMED(name,...)
std::function< bool(const std::string &)> LogicalOperatorFunction
LogicalOperatorFunction negation_operator_callback_
std::function< bool(const std::string &, S &)> CallFunction
Provides a mock method for service calls for a specific service type.


pilz_testutils
Author(s):
autogenerated on Mon Feb 28 2022 23:13:41