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/", _)).WillOnce(DoAll(SetArgReferee<1>(rpc_value), Return(true)));
85 
87 
88  ASSERT_TRUE(api_spec.hasRegisterDefinition("A"));
89  EXPECT_EQ(123u, api_spec.getRegisterDefinition("A"));
90 }
91 
92 TEST(ModbusApiSpecTest, NodeHandleConstructionMissingApiSpec)
93 {
94  XmlRpc::XmlRpcValue rpc_value;
95  rpc_value["A"] = 123;
96 
97  NodeHandleMock nh;
98  EXPECT_CALL(nh, getParam("read_api_spec/", _)).Times(1).WillOnce(Return(false));
99 
100  // Just for the error message in the exception
101  EXPECT_CALL(nh, getNamespace()).Times(1).WillOnce(Return("testnamespace"));
102 
104 }
105 
106 int main(int argc, char** argv)
107 {
108  testing::InitGoogleTest(&argc, argv);
109  return RUN_ALL_TESTS();
110 }
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 Wed Nov 25 2020 03:10:38