unittest_modbus_api_spec.cpp
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 #include <gtest/gtest.h>
19 #include <gmock/gmock.h>
20 #include <string>
21 #include <memory>
22 
24 
25 using namespace prbt_hardware_support;
27 using namespace ::testing;
28 
30 {
31  public:
32  MOCK_CONST_METHOD0(getNamespace, std::string(void));
33  MOCK_CONST_METHOD2(getParam, bool(const std::string& key, XmlRpc::XmlRpcValue& v));
34 };
35 
40 TEST(ModbusApiSpecTest, testModbusApiSpecExceptionDtor)
41 {
42  std::shared_ptr<ModbusApiSpecException> ex {new ModbusApiSpecException("Test msg")};
43 }
44 
45 TEST(ModbusApiSpecTest, ConstructionViaInitilizerListRead)
46 {
47  ModbusApiSpec api_spec{{"test", 123}};
48  ASSERT_TRUE(api_spec.hasRegisterDefinition("test"));
49  EXPECT_EQ(123u, api_spec.getRegisterDefinition("test"));
50 
51  EXPECT_EQ(123u, api_spec.getMinRegisterDefinition());
52  EXPECT_EQ(123u, api_spec.getMaxRegisterDefinition());
53 }
54 
55 TEST(ModbusApiSpecTest, ConstructionViaInitilizerListReadNonExistent)
56 {
57  ModbusApiSpec api_spec{};
58  EXPECT_THROW(api_spec.getRegisterDefinition("test"), ModbusApiSpecException);
59  EXPECT_THROW(api_spec.getMinRegisterDefinition(), ModbusApiSpecException);
60  EXPECT_THROW(api_spec.getMaxRegisterDefinition(), ModbusApiSpecException);
61 }
62 
63 TEST(ModbusApiSpecTest, ConstructionViaInitilizerListOverwrite)
64 {
65  ModbusApiSpec api_spec{{"test", 123}, {"dummy", 234}};
66 
67  EXPECT_EQ(123u, api_spec.getMinRegisterDefinition());
68  EXPECT_EQ(234u, api_spec.getMaxRegisterDefinition());
69 
70  api_spec.setRegisterDefinition("test", 555);
71  ASSERT_TRUE(api_spec.hasRegisterDefinition("test"));
72  EXPECT_EQ(555u, api_spec.getRegisterDefinition("test"));
73 
74  EXPECT_EQ(234u, api_spec.getMinRegisterDefinition());
75  EXPECT_EQ(555u, api_spec.getMaxRegisterDefinition());
76 }
77 
78 TEST(ModbusApiSpecTest, NodeHandleConstructionSimpleRead)
79 {
80  XmlRpc::XmlRpcValue rpc_value;
81  rpc_value["A"] = 123;
82 
83  NodeHandleMock nh;
84  EXPECT_CALL(nh, getParam("read_api_spec/",_))
85  .WillOnce(DoAll(SetArgReferee<1>(rpc_value), Return(true)));
86 
88 
89  ASSERT_TRUE(api_spec.hasRegisterDefinition("A"));
90  EXPECT_EQ(123u, api_spec.getRegisterDefinition("A"));
91 }
92 
93 
94 TEST(ModbusApiSpecTest, NodeHandleConstructionMissingApiSpec)
95 {
96  XmlRpc::XmlRpcValue rpc_value;
97  rpc_value["A"] = 123;
98 
99  NodeHandleMock nh;
100  EXPECT_CALL(nh, getParam("read_api_spec/",_))
101  .Times(1)
102  .WillOnce(Return(false));
103 
104  // Just for the error message in the exception
105  EXPECT_CALL(nh, getNamespace())
106  .Times(1)
107  .WillOnce(Return("testnamespace"));
108 
110 }
111 
112 int main(int argc, char **argv){
113  testing::InitGoogleTest(&argc, argv);
114  return RUN_ALL_TESTS();
115 }
Expection thrown by prbt_hardware_support::ModbusApiSpec.
ROSCPP_DECL const std::string & getNamespace()
void setRegisterDefinition(const std::string &key, unsigned short value)
int main(int argc, char **argv)
T getParam(const ros::NodeHandle &nh, const std::string &param_name)
TEST(IntegrationtestExecuteBrakeTest, testBrakeTestService)
Specifies the meaning of the holding registers.


prbt_hardware_support
Author(s):
autogenerated on Tue Feb 2 2021 03:50:17