Skip to content

Commit c532fd9

Browse files
Add tests for lexical casts
1 parent 283df2e commit c532fd9

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

hardware_interface/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ if(BUILD_TESTING)
9090
ament_add_gmock(test_helpers test/test_helpers.cpp)
9191
target_link_libraries(test_helpers hardware_interface)
9292

93+
# Test lexical casts methods
94+
ament_add_gtest(test_lexical_casts test/test_lexical_casts.cpp)
95+
target_link_libraries(test_lexical_casts hardware_interface)
96+
9397
ament_add_gmock(test_component_interfaces test/test_component_interfaces.cpp)
9498
target_link_libraries(test_component_interfaces hardware_interface ros2_control_test_assets::ros2_control_test_assets)
9599

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2025 PAL Robotics S.L.
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 <stdexcept>
16+
17+
#include "gtest/gtest.h"
18+
19+
#include "hardware_interface/lexical_casts.hpp"
20+
21+
TEST(TestLexicalCasts, test_stod)
22+
{
23+
using hardware_interface::stod;
24+
25+
ASSERT_THROW(stod(""), std::invalid_argument);
26+
ASSERT_THROW(stod("abc"), std::invalid_argument);
27+
ASSERT_THROW(stod("1.2.3"), std::invalid_argument);
28+
ASSERT_EQ(stod("1.2"), 1.2);
29+
ASSERT_EQ(stod("-1.2"), -1.2);
30+
ASSERT_EQ(stod("1.0"), 1.0);
31+
}
32+
33+
TEST(TestLexicalCasts, test_parse_bool)
34+
{
35+
using hardware_interface::parse_bool;
36+
37+
ASSERT_TRUE(parse_bool("true"));
38+
ASSERT_TRUE(parse_bool("True"));
39+
40+
// Any other value should return false
41+
ASSERT_FALSE(parse_bool("false"));
42+
ASSERT_FALSE(parse_bool("False"));
43+
ASSERT_FALSE(parse_bool(""));
44+
ASSERT_FALSE(parse_bool("abc"));
45+
ASSERT_FALSE(parse_bool("1"));
46+
}
47+
48+
TEST(TestLexicalCasts, test_parse_string_array)
49+
{
50+
using hardware_interface::parse_string_array;
51+
52+
ASSERT_THROW(parse_string_array(""), std::invalid_argument);
53+
ASSERT_THROW(parse_string_array("abc"), std::invalid_argument);
54+
ASSERT_THROW(parse_string_array("[abc"), std::invalid_argument);
55+
ASSERT_THROW(parse_string_array("abc]"), std::invalid_argument);
56+
ASSERT_THROW(parse_string_array("[[abc, def], hij]"), std::invalid_argument);
57+
ASSERT_THROW(parse_string_array("[ ]"), std::invalid_argument);
58+
ASSERT_THROW(parse_string_array("[,]"), std::invalid_argument);
59+
ASSERT_THROW(parse_string_array("[abc,]"), std::invalid_argument);
60+
ASSERT_THROW(parse_string_array("[,abc]"), std::invalid_argument);
61+
ASSERT_THROW(parse_string_array("[abc,,def]"), std::invalid_argument);
62+
63+
ASSERT_EQ(parse_string_array("[]"), std::vector<std::string>());
64+
ASSERT_EQ(parse_string_array("[abc]"), std::vector<std::string>({"abc"}));
65+
ASSERT_EQ(parse_string_array("[abc,def]"), std::vector<std::string>({"abc", "def"}));
66+
ASSERT_EQ(parse_string_array("[abc, def]"), std::vector<std::string>({"abc", "def"}));
67+
ASSERT_EQ(parse_string_array("[ abc, def ]"), std::vector<std::string>({"abc", "def"}));
68+
}

0 commit comments

Comments
 (0)