Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions rcl_yaml_param_parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ if(BUILD_TESTING)
ament_lint_auto_find_test_dependencies()

# Gtests
ament_add_gtest(test_namespace
test/test_namespace.cpp
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
if(TARGET test_namespace)
ament_target_dependencies(test_namespace
"rcutils"
"osrf_testing_tools_cpp"
)
target_link_libraries(test_namespace ${PROJECT_NAME})
endif()

ament_add_gtest(test_node_params
test/test_node_params.cpp
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
if(TARGET test_node_params)
ament_target_dependencies(test_node_params
"rcutils"
"osrf_testing_tools_cpp"
)
target_link_libraries(test_node_params ${PROJECT_NAME})
endif()

ament_add_gtest(test_parse_yaml
test/test_parse_yaml.cpp
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
Expand All @@ -68,6 +92,18 @@ if(BUILD_TESTING)
PRIVATE ${osrf_testing_tools_cpp_INCLUDE_DIRS})
endif()

ament_add_gtest(test_parse
test/test_parse.cpp
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
if(TARGET test_parse)
ament_target_dependencies(test_parse
"rcutils"
"osrf_testing_tools_cpp"
)
target_link_libraries(test_parse ${PROJECT_NAME})
endif()

ament_add_gtest(test_parser
test/test_parser.cpp
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
Expand All @@ -80,6 +116,18 @@ if(BUILD_TESTING)
target_link_libraries(test_parser ${PROJECT_NAME})
endif()

ament_add_gtest(test_yaml_variant
test/test_yaml_variant.cpp
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
if(TARGET test_yaml_variant)
ament_target_dependencies(test_yaml_variant
"rcutils"
"osrf_testing_tools_cpp"
)
target_link_libraries(test_yaml_variant ${PROJECT_NAME})
endif()

endif()

ament_export_dependencies(ament_cmake libyaml_vendor)
Expand Down
106 changes: 106 additions & 0 deletions rcl_yaml_param_parser/test/test_namespace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2020 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>

#include <yaml.h>

#include <string>
#include <vector>

#include "osrf_testing_tools_cpp/scope_exit.hpp"
#include "rcl_yaml_param_parser/parser.h"
#include "../src/impl/namespace.h"
#include "rcutils/allocator.h"
#include "rcutils/strdup.h"

TEST(TestNamespace, add_name_to_ns) {
rcutils_allocator_t allocator = rcutils_get_default_allocator();
namespace_tracker_t ns_tracker;
ns_tracker.node_ns = nullptr;
ns_tracker.parameter_ns = nullptr;
ns_tracker.num_node_ns = 0;
ns_tracker.num_parameter_ns = 0;

rcutils_ret_t ret = add_name_to_ns(&ns_tracker, nullptr, NS_TYPE_NODE, allocator);
EXPECT_EQ(RCUTILS_RET_INVALID_ARGUMENT, ret) << rcutils_get_error_string().str;
EXPECT_EQ(nullptr, ns_tracker.node_ns);

ret = add_name_to_ns(&ns_tracker, "node1", NS_TYPE_NODE, allocator);
EXPECT_EQ(RCUTILS_RET_OK, ret) << rcutils_get_error_string().str;
EXPECT_STREQ("node1", ns_tracker.node_ns);
EXPECT_EQ(1u, ns_tracker.num_node_ns);
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
allocator.deallocate(ns_tracker.node_ns, allocator.state);
});

ret = add_name_to_ns(&ns_tracker, "node2", NS_TYPE_NODE, allocator);
EXPECT_EQ(RCUTILS_RET_OK, ret) << rcutils_get_error_string().str;
EXPECT_STREQ("node1/node2", ns_tracker.node_ns);
EXPECT_EQ(2u, ns_tracker.num_node_ns);

ret = add_name_to_ns(&ns_tracker, "param1", NS_TYPE_PARAM, allocator);
EXPECT_EQ(RCUTILS_RET_OK, ret) << rcutils_get_error_string().str;
EXPECT_STREQ("param1", ns_tracker.parameter_ns);
EXPECT_EQ(1u, ns_tracker.num_parameter_ns);
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
allocator.deallocate(ns_tracker.parameter_ns, allocator.state);
});

ret = add_name_to_ns(&ns_tracker, "param2", NS_TYPE_PARAM, allocator);
EXPECT_EQ(RCUTILS_RET_OK, ret) << rcutils_get_error_string().str;
EXPECT_STREQ("param1.param2", ns_tracker.parameter_ns);
EXPECT_EQ(2u, ns_tracker.num_parameter_ns);
}

TEST(TestNamespace, replace_ns) {
rcutils_allocator_t allocator = rcutils_get_default_allocator();
namespace_tracker_t ns_tracker;
ns_tracker.node_ns = rcutils_strdup("initial_node1/initial_node2", allocator);
ASSERT_STREQ("initial_node1/initial_node2", ns_tracker.node_ns);
ns_tracker.parameter_ns = rcutils_strdup("initial_param1.initial_param2", allocator);
ASSERT_STREQ("initial_param1.initial_param2", ns_tracker.parameter_ns);
ns_tracker.num_node_ns = 2;
ns_tracker.num_parameter_ns = 2;

char * expected_ns = rcutils_strdup("new_ns1/new_ns2/new_ns3", allocator);
ASSERT_STREQ("new_ns1/new_ns2/new_ns3", expected_ns);
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
allocator.deallocate(ns_tracker.node_ns, allocator.state);
allocator.deallocate(ns_tracker.parameter_ns, allocator.state);
allocator.deallocate(expected_ns, allocator.state);
});

rcutils_ret_t ret =
replace_ns(&ns_tracker, expected_ns, 3, NS_TYPE_NODE, allocator);
EXPECT_EQ(RCUTILS_RET_OK, ret) << rcutils_get_error_string().str;
EXPECT_STREQ(expected_ns, ns_tracker.node_ns);
EXPECT_EQ(3u, ns_tracker.num_node_ns);

char * expected_param_ns =
rcutils_strdup("new_param1.new_param2.new_param3", allocator);
ASSERT_STREQ("new_param1.new_param2.new_param3", expected_param_ns);
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
allocator.deallocate(expected_param_ns, allocator.state);
});

ret = replace_ns(&ns_tracker, expected_param_ns, 3, NS_TYPE_PARAM, allocator);
EXPECT_EQ(RCUTILS_RET_OK, ret) << rcutils_get_error_string().str;
EXPECT_STREQ(expected_param_ns, ns_tracker.parameter_ns);
EXPECT_EQ(3u, ns_tracker.num_parameter_ns);
}
42 changes: 42 additions & 0 deletions rcl_yaml_param_parser/test/test_node_params.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2020 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>

#include <yaml.h>

#include <string>
#include <vector>

#include "osrf_testing_tools_cpp/scope_exit.hpp"
#include "rcl_yaml_param_parser/parser.h"
#include "../src/impl/node_params.h"
#include "rcutils/allocator.h"

TEST(TestNodeParams, init_fini) {
rcutils_allocator_t allocator = rcutils_get_default_allocator();
rcl_node_params_t node_params = {NULL, NULL, 0u};
EXPECT_EQ(RCUTILS_RET_OK, node_params_init(&node_params, allocator));
EXPECT_NE(nullptr, node_params.parameter_names);
EXPECT_NE(nullptr, node_params.parameter_values);
EXPECT_EQ(0u, node_params.num_params);
rcl_yaml_node_params_fini(&node_params, allocator);
EXPECT_EQ(nullptr, node_params.parameter_names);
EXPECT_EQ(nullptr, node_params.parameter_values);
EXPECT_EQ(0u, node_params.num_params);

// This function doesn't return anything, so just check it doesn't segfault on the second try
rcl_yaml_node_params_fini(&node_params, allocator);
rcl_yaml_node_params_fini(nullptr, allocator);
}
Loading