Skip to content

Commit 366b22b

Browse files
author
Shota Aoki
authored
Add an unittest environment for hardware class. (#32)
* Add write_data function * Updating to use mock * Test load_config_file successfuly * Make all public method to virtual * Remove unused heade file * Refactoring to pass cpplint check * Refactoring method name
1 parent f414296 commit 366b22b

File tree

5 files changed

+134
-22
lines changed

5 files changed

+134
-22
lines changed

rt_manipulators_lib/include/hardware.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ using JointName = std::string;
3434
class Hardware {
3535
public:
3636
explicit Hardware(const std::string device_name);
37+
explicit Hardware(std::unique_ptr<hardware_communicator::Communicator> comm);
3738
~Hardware();
3839
bool load_config_file(const std::string& config_yaml);
3940
bool connect(const int baudrate = 3000000);

rt_manipulators_lib/include/hardware_communicator.hpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,38 @@ using GroupSyncWrite = dynamixel::GroupSyncWrite;
3939
class Communicator{
4040
public:
4141
explicit Communicator(const std::string device_name);
42-
~Communicator();
43-
bool is_connected();
44-
bool connect(const int baudrate = 3000000);
45-
void disconnect();
46-
void make_sync_read_group(const group_name_t & group_name, const dxl_address_t & start_address,
47-
const dxl_data_length_t & data_length);
48-
void make_sync_write_group(const group_name_t & group_name, const dxl_address_t & start_address,
49-
const dxl_data_length_t & data_length);
50-
bool append_id_to_sync_read_group(const group_name_t & group_name, const dxl_id_t & id);
51-
bool append_id_to_sync_write_group(const group_name_t & group_name, const dxl_id_t & id,
42+
virtual ~Communicator();
43+
virtual bool is_connected();
44+
virtual bool connect(const int baudrate = 3000000);
45+
virtual void disconnect();
46+
virtual void make_sync_read_group(
47+
const group_name_t & group_name, const dxl_address_t & start_address,
48+
const dxl_data_length_t & data_length);
49+
virtual void make_sync_write_group(
50+
const group_name_t & group_name, const dxl_address_t & start_address,
51+
const dxl_data_length_t & data_length);
52+
virtual bool append_id_to_sync_read_group(const group_name_t & group_name, const dxl_id_t & id);
53+
virtual bool append_id_to_sync_write_group(const group_name_t & group_name, const dxl_id_t & id,
5254
std::vector<dxl_byte_t> & init_data);
53-
bool send_sync_read_packet(const group_name_t & group_name);
54-
bool send_sync_write_packet(const group_name_t & group_name);
55-
bool get_sync_read_data(const group_name_t & group_name, const dxl_id_t id,
55+
virtual bool send_sync_read_packet(const group_name_t & group_name);
56+
virtual bool send_sync_write_packet(const group_name_t & group_name);
57+
virtual bool get_sync_read_data(const group_name_t & group_name, const dxl_id_t id,
5658
const dxl_address_t & address, const dxl_data_length_t & length,
5759
dxl_double_word_t & read_data);
58-
bool set_sync_write_data(const group_name_t & group_name, const dxl_id_t id,
60+
virtual bool set_sync_write_data(const group_name_t & group_name, const dxl_id_t id,
5961
std::vector<dxl_byte_t> & write_data);
60-
bool write_byte_data(const dxl_id_t & id, const dxl_address_t & address,
62+
virtual bool write_byte_data(const dxl_id_t & id, const dxl_address_t & address,
6163
const dxl_byte_t & write_data);
62-
bool write_word_data(const dxl_id_t & id, const dxl_address_t & address,
64+
virtual bool write_word_data(const dxl_id_t & id, const dxl_address_t & address,
6365
const dxl_word_t & write_data);
64-
bool write_double_word_data(const dxl_id_t & id, const dxl_address_t & address,
65-
const dxl_double_word_t & write_data);
66-
bool read_byte_data(const dxl_id_t & id, const dxl_address_t & address, dxl_byte_t & read_data);
67-
bool read_word_data(const dxl_id_t & id, const dxl_address_t & address, dxl_word_t & read_data);
68-
bool read_double_word_data(const dxl_id_t & id, const dxl_address_t & address,
66+
virtual bool write_double_word_data(
67+
const dxl_id_t & id, const dxl_address_t & address,
68+
const dxl_double_word_t & write_data);
69+
virtual bool read_byte_data(
70+
const dxl_id_t & id, const dxl_address_t & address, dxl_byte_t & read_data);
71+
virtual bool read_word_data(
72+
const dxl_id_t & id, const dxl_address_t & address, dxl_word_t & read_data);
73+
virtual bool read_double_word_data(const dxl_id_t & id, const dxl_address_t & address,
6974
dxl_double_word_t & read_data);
7075

7176
private:

rt_manipulators_lib/src/hardware.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ namespace rt_manipulators_cpp {
2323

2424
Hardware::Hardware(const std::string device_name) :
2525
thread_enable_(false) {
26-
comm_ = std::make_shared<hardware_communicator::Communicator>(device_name);
26+
comm_ = std::make_unique<hardware_communicator::Communicator>(device_name);
27+
}
28+
29+
Hardware::Hardware(std::unique_ptr<hardware_communicator::Communicator> comm) :
30+
thread_enable_(false) {
31+
comm_ = std::move(comm);
2732
}
2833

2934
Hardware::~Hardware() {

rt_manipulators_lib/test/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,19 @@ set(list_tests
2222
test_dynamixel_x
2323
test_dynamixel_xh
2424
test_dynamixel_p
25+
test_hardware
2526
)
27+
28+
# Download FakeIt
29+
Set(FETCHCONTENT_QUIET FALSE)
30+
include(FetchContent)
31+
FetchContent_Declare(
32+
fakeit
33+
GIT_REPOSITORY https://github.com/eranpeer/FakeIt
34+
GIT_TAG 2.4.0
35+
GIT_PROGRESS TRUE)
36+
FetchContent_MakeAvailable(fakeit)
37+
2638
foreach(test_executable IN LISTS list_tests)
2739
message("${test_executable}")
2840
add_executable(${test_executable}
@@ -33,5 +45,8 @@ foreach(test_executable IN LISTS list_tests)
3345
GTest::Main
3446
rt_manipulators_cpp
3547
)
48+
target_include_directories(${test_executable} PRIVATE
49+
${fakeit_SOURCE_DIR}/single_header/gtest
50+
)
3651
gtest_discover_tests(${test_executable})
3752
endforeach()
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2023 RT Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <memory>
16+
17+
#include "fakeit.hpp"
18+
#include "gtest/gtest.h"
19+
#include "rt_manipulators_cpp/hardware.hpp"
20+
#include "rt_manipulators_cpp/hardware_communicator.hpp"
21+
22+
using fakeit::Mock;
23+
using fakeit::Verify;
24+
using fakeit::When;
25+
26+
Mock<hardware_communicator::Communicator> create_comm_mock(void) {
27+
Mock<hardware_communicator::Communicator> mock;
28+
When(Method(mock, is_connected)).AlwaysReturn(true);
29+
When(Method(mock, connect)).AlwaysReturn(true);
30+
When(Method(mock, disconnect)).AlwaysReturn();
31+
When(Method(mock, make_sync_write_group)).AlwaysReturn();
32+
When(Method(mock, make_sync_read_group)).AlwaysReturn();
33+
When(Method(mock, append_id_to_sync_write_group)).AlwaysReturn(true);
34+
When(Method(mock, append_id_to_sync_read_group)).AlwaysReturn(true);
35+
When(Method(mock, send_sync_read_packet)).AlwaysReturn(true);
36+
When(Method(mock, send_sync_write_packet)).AlwaysReturn(true);
37+
When(Method(mock, get_sync_read_data)).AlwaysReturn(true);
38+
When(Method(mock, set_sync_write_data)).AlwaysReturn(true);
39+
When(Method(mock, write_byte_data)).AlwaysReturn(true);
40+
When(Method(mock, write_word_data)).AlwaysReturn(true);
41+
When(Method(mock, write_double_word_data)).AlwaysReturn(true);
42+
When(Method(mock, read_byte_data)).AlwaysReturn(true);
43+
When(Method(mock, read_word_data)).AlwaysReturn(true);
44+
When(Method(mock, read_double_word_data)).AlwaysReturn(true);
45+
46+
return mock;
47+
}
48+
49+
TEST(HardwareTest, load_config_file) {
50+
// Expect the load_config_file method to be called twice and return true and false respectively.
51+
auto mock = create_comm_mock();
52+
53+
rt_manipulators_cpp::Hardware hardware(
54+
std::unique_ptr<hardware_communicator::Communicator>(&mock.get()));
55+
56+
EXPECT_TRUE(hardware.load_config_file("../config/ok_has_dynamixel_name.yaml"));
57+
EXPECT_FALSE(hardware.load_config_file("../config/ng_has_same_joints.yaml"));
58+
}
59+
60+
TEST(HardwareTest, connect) {
61+
// Expect the connect method to be called twice and return true and false respectively.
62+
auto mock = create_comm_mock();
63+
When(Method(mock, connect)).Return(true, false); // Return true then false.
64+
65+
rt_manipulators_cpp::Hardware hardware(
66+
std::unique_ptr<hardware_communicator::Communicator>(&mock.get()));
67+
68+
EXPECT_TRUE(hardware.connect());
69+
EXPECT_FALSE(hardware.connect());
70+
}
71+
72+
TEST(HardwareTest, disconnect) {
73+
// Expect the disconnect method to be called once and never.
74+
auto mock = create_comm_mock();
75+
When(Method(mock, is_connected)).Return(false).AlwaysReturn(true); // Return false then true.
76+
When(Method(mock, disconnect)).AlwaysReturn();
77+
78+
rt_manipulators_cpp::Hardware hardware(
79+
std::unique_ptr<hardware_communicator::Communicator>(&mock.get()));
80+
81+
hardware.disconnect();
82+
Verify(Method(mock, disconnect)).Never();
83+
84+
hardware.disconnect();
85+
Verify(Method(mock, disconnect)).Once();
86+
}

0 commit comments

Comments
 (0)