|
| 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