Skip to content

Commit c94ffa7

Browse files
MarcoMatteoBassaSteveMacenski
authored andcommitted
Adding parameter warn_when_defaulting_parameters to control default parameter warnings (#5189)
* Adding a parameter warn_when_defaulting_parameters to control default parameter warnings instead of using a flag Signed-off-by: Marco Bassa <[email protected]> * Adding parameter strict_param_loading for optionally throwing an exception if parameter overrides are missing Signed-off-by: Marco Bassa <[email protected]> * Using default false declaration instead of declare_or_get in param util Signed-off-by: Marco Bassa <[email protected]> --------- Signed-off-by: Marco Bassa <[email protected]>
1 parent f50c698 commit c94ffa7

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

nav2_util/test/test_node_utils.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,50 @@ TEST(DeclareParameterIfNotDeclared, DeclareParameterIfNotDeclared)
7878
ASSERT_EQ(param, "fred");
7979
}
8080

81+
TEST(DeclareOrGetParam, DeclareOrGetParam)
82+
{
83+
auto node = std::make_shared<rclcpp::Node>("test_node");
84+
85+
// test declared parameter
86+
node->declare_parameter("foobar", "foo");
87+
std::string param = declare_or_get_parameter(node, "foobar", std::string{"bar"});
88+
EXPECT_EQ(param, "foo");
89+
node->get_parameter("foobar", param);
90+
EXPECT_EQ(param, "foo");
91+
92+
// test undeclared parameter
93+
node->set_parameter(rclcpp::Parameter("warn_on_missing_params", true));
94+
int int_param = declare_or_get_parameter(node, "waldo", 3);
95+
EXPECT_EQ(int_param, 3);
96+
97+
// test unknown parameter with strict_param_loading enabled
98+
bool got_exception{false};
99+
node->set_parameter(rclcpp::Parameter("strict_param_loading", true));
100+
try {
101+
declare_or_get_parameter(node, "burpy", true);
102+
} catch (const rclcpp::exceptions::InvalidParameterValueException & exc) {
103+
got_exception = true;
104+
}
105+
EXPECT_TRUE(got_exception);
106+
// The parameter is anyway declared with the default val and subsequent calls won't fail
107+
EXPECT_TRUE(declare_or_get_parameter(node, "burpy", true));
108+
109+
// test declaration by type of existing param
110+
int_param = declare_or_get_parameter<int>(node, "waldo",
111+
rclcpp::ParameterType::PARAMETER_INTEGER);
112+
EXPECT_EQ(int_param, 3);
113+
114+
// test declaration by type of non existing param
115+
got_exception = false;
116+
try {
117+
int_param = declare_or_get_parameter<int>(node, "wololo",
118+
rclcpp::ParameterType::PARAMETER_INTEGER);
119+
} catch (const rclcpp::exceptions::InvalidParameterValueException & exc) {
120+
got_exception = true;
121+
}
122+
EXPECT_TRUE(got_exception);
123+
}
124+
81125
TEST(GetPluginTypeParam, GetPluginTypeParam)
82126
{
83127
::testing::FLAGS_gtest_death_test_style = "threadsafe";

0 commit comments

Comments
 (0)