Skip to content

Commit

Permalink
cleanup, added test cases for descriptors, fixed namespaced descriptors
Browse files Browse the repository at this point in the history
remove MAX_STRING_SIZE comparisons for rebase

add new lines

Signed-off-by: bpwilcox <bpwilcox@eng.ucsd.edu>

change to/from to min/max

Signed-off-by: bpwilcox <bpwilcox@eng.ucsd.edu>
  • Loading branch information
bpwilcox committed Oct 28, 2019
1 parent fd939c4 commit f28d0cc
Show file tree
Hide file tree
Showing 14 changed files with 446 additions and 194 deletions.
9 changes: 0 additions & 9 deletions rcl_yaml_param_parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ if(BUILD_TESTING)
PRIVATE ${osrf_testing_tools_cpp_INCLUDE_DIRS})
endif()

ament_add_gtest(test_parse_yaml_descriptors
test/test_parse_yaml_descriptors.cpp
)
if(TARGET test_parse_yaml_descriptors)
target_link_libraries(test_parse_yaml_descriptors ${PROJECT_NAME})
target_include_directories(test_parse_yaml_descriptors
PRIVATE ${osrf_testing_tools_cpp_INCLUDE_DIRS})
endif()

endif()

ament_export_dependencies(ament_cmake)
Expand Down
11 changes: 11 additions & 0 deletions rcl_yaml_param_parser/include/rcl_yaml_param_parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ rcl_variant_t * rcl_yaml_node_struct_get(
const char * param_name,
rcl_params_t * params_st);

/// \brief Get the descriptor struct for a given parameter
/// \param[in] node_name is the name of the node to which the parameter belongs
/// \param[in] param_name is the name of the parameter whose value is to be retrieved
/// \param[inout] params_st points to the populated (or to be populated) parameter struct
/// \return parameter descriptor struct on success and NULL on failure
RCL_YAML_PARAM_PARSER_PUBLIC
rcl_param_descriptor_t * rcl_yaml_node_struct_get_descriptor(
const char * node_name,
const char * param_name,
rcl_params_t * params_st);

/// \brief Print the parameter structure to stdout
/// \param[in] params_st points to the populated parameter struct
RCL_YAML_PARAM_PARSER_PUBLIC
Expand Down
10 changes: 5 additions & 5 deletions rcl_yaml_param_parser/include/rcl_yaml_param_parser/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ typedef struct rcl_param_descriptor_s
uint8_t * type;
char * description;
char * additional_constraints;
double * from_value_float;
double * to_value_float;
double * step_float;
int64_t * from_value_int;
int64_t * to_value_int;
double * min_value_double;
double * max_value_double;
double * step_double;
int64_t * min_value_int;
int64_t * max_value_int;
int64_t * step_int;
} rcl_param_descriptor_t;

Expand Down
280 changes: 181 additions & 99 deletions rcl_yaml_param_parser/src/parser.c

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions rcl_yaml_param_parser/test/assign_param_in_descriptors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node1:
ros__parameters:
parameter__descriptors:
bar: 42
foo:
read_only: true
2 changes: 0 additions & 2 deletions rcl_yaml_param_parser/test/correct_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ lidar_ns:
dy: 2.30
fr_sensor_specs: [12, 3, 0, 7]
bk_sensor_specs: [12.1, -2.3, 5.2, 9.0]
foo:
bar: 2
is_front: true
driver2:
dx: 1.23
Expand Down
32 changes: 32 additions & 0 deletions rcl_yaml_param_parser/test/correct_param_descriptors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# config/test_yaml
---

node_ns:
node1:
ros__parameters:
parameter__descriptors:
param1:
type: 2
min_value: 5
max_value: 100
step: 5
read_only: false
description: "int parameter"
additional_constraints: "only multiples of 5"
param2:
min_value: 1.0
max_value: 10.0
description: "double parameter"
node2:
ros__parameters:
foo:
bar: 10
baz: "hello"
parameter__descriptors:
foo:
bar:
description: "namespaced parameter"
read_only: false
baz:
description: "another namespaced parameter"
foobar: true
6 changes: 6 additions & 0 deletions rcl_yaml_param_parser/test/invalid_param_descriptor_key.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node1:
ros__parameters:
parameter__descriptors:
foo:
read_only: true
invalid_description: "This is an invalid descriptor key"
5 changes: 5 additions & 0 deletions rcl_yaml_param_parser/test/invalid_param_descriptor_type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node1:
ros__parameters:
parameter__descriptors:
foo:
min_value: "5"
5 changes: 5 additions & 0 deletions rcl_yaml_param_parser/test/no_value_descriptor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node1:
ros__parameters:
parameter__descriptors:
foo:
description:
16 changes: 16 additions & 0 deletions rcl_yaml_param_parser/test/overlay_descriptors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
node_ns:
node1:
ros__parameters:
parameter__descriptors:
param1:
max_value: 200
param2:
min_value: -2.0
node2:
ros__parameters:
parameter__descriptors:
foo:
bar:
read_only: true
baz:
description: "other namespaced parameter"
19 changes: 0 additions & 19 deletions rcl_yaml_param_parser/test/parameter_descriptors.yaml

This file was deleted.

179 changes: 179 additions & 0 deletions rcl_yaml_param_parser/test/test_parse_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,185 @@ TEST(test_file_parser, maximum_number_parameters) {
EXPECT_FALSE(res);
}

TEST(test_parser, correct_syntax_descriptors) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024)) << rcutils_get_error_string().str;
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
ASSERT_TRUE(NULL != test_path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
allocator.deallocate(test_path, allocator.state);
});
char * path = rcutils_join_path(test_path, "correct_param_descriptors.yaml", allocator);
ASSERT_TRUE(NULL != path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
allocator.deallocate(path, allocator.state);
});
ASSERT_TRUE(rcutils_exists(path)) << "No test YAML file found at " << path;
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
ASSERT_TRUE(NULL != params_hdl) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
rcl_yaml_node_struct_fini(params_hdl);
});
bool res = rcl_parse_yaml_file(path, params_hdl);
ASSERT_TRUE(res) << rcutils_get_error_string().str;

char * another_path = rcutils_join_path(test_path, "overlay_descriptors.yaml", allocator);
ASSERT_TRUE(NULL != another_path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
allocator.deallocate(another_path, allocator.state);
});
ASSERT_TRUE(rcutils_exists(another_path)) << "No test YAML file found at " << another_path;
res = rcl_parse_yaml_file(another_path, params_hdl);
ASSERT_TRUE(res) << rcutils_get_error_string().str;

rcl_params_t * copy_of_params_hdl = rcl_yaml_node_struct_copy(params_hdl);
ASSERT_TRUE(NULL != copy_of_params_hdl) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
rcl_yaml_node_struct_fini(copy_of_params_hdl);
});

rcl_params_t * params_hdl_set[] = {params_hdl, copy_of_params_hdl};
for (rcl_params_t * params : params_hdl_set) {
rcl_param_descriptor_t * param_descriptor = NULL;
param_descriptor = rcl_yaml_node_struct_get_descriptor("node_ns/node1", "param1", params);
ASSERT_TRUE(NULL != param_descriptor);
ASSERT_TRUE(NULL != param_descriptor->name);
EXPECT_STREQ("param1", param_descriptor->name);
ASSERT_TRUE(NULL != param_descriptor->description);
EXPECT_STREQ("int parameter", param_descriptor->description);
ASSERT_TRUE(NULL != param_descriptor->additional_constraints);
EXPECT_STREQ("only multiples of 5", param_descriptor->additional_constraints);
ASSERT_TRUE(NULL != param_descriptor->type);
EXPECT_EQ(2U, *param_descriptor->type);
ASSERT_TRUE(NULL != param_descriptor->min_value_int);
EXPECT_EQ(5, *param_descriptor->min_value_int);
ASSERT_TRUE(NULL != param_descriptor->max_value_int);
EXPECT_EQ(200, *param_descriptor->max_value_int);
ASSERT_TRUE(NULL != param_descriptor->step_int);
EXPECT_EQ(5, *param_descriptor->step_int);
ASSERT_TRUE(NULL != param_descriptor->read_only);
EXPECT_FALSE(*param_descriptor->read_only);

param_descriptor = rcl_yaml_node_struct_get_descriptor("node_ns/node1", "param2", params);
ASSERT_TRUE(NULL != param_descriptor);
ASSERT_TRUE(NULL != param_descriptor->name);
EXPECT_STREQ("param2", param_descriptor->name);
ASSERT_TRUE(NULL != param_descriptor->description);
EXPECT_STREQ("double parameter", param_descriptor->description);
ASSERT_TRUE(NULL != param_descriptor->min_value_double);
EXPECT_DOUBLE_EQ(-2.0, *param_descriptor->min_value_double);
ASSERT_TRUE(NULL != param_descriptor->max_value_double);
EXPECT_DOUBLE_EQ(10.0, *param_descriptor->max_value_double);

param_descriptor = rcl_yaml_node_struct_get_descriptor("node_ns/node2", "foo.bar", params);
ASSERT_TRUE(NULL != param_descriptor);
ASSERT_TRUE(NULL != param_descriptor->name);
EXPECT_STREQ("foo.bar", param_descriptor->name);
ASSERT_TRUE(NULL != param_descriptor->description);
EXPECT_STREQ("namespaced parameter", param_descriptor->description);
ASSERT_TRUE(NULL != param_descriptor->read_only);
EXPECT_TRUE(*param_descriptor->read_only);

param_descriptor = rcl_yaml_node_struct_get_descriptor("node_ns/node2", "foo.baz", params);
ASSERT_TRUE(NULL != param_descriptor);
ASSERT_TRUE(NULL != param_descriptor->name);
EXPECT_STREQ("foo.baz", param_descriptor->name);
ASSERT_TRUE(NULL != param_descriptor->description);
EXPECT_STREQ("other namespaced parameter", param_descriptor->description);

rcl_yaml_node_struct_print(params);
}
}

TEST(test_parser, param_assign_in_descriptors) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024)) << rcutils_get_error_string().str;
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
ASSERT_TRUE(NULL != test_path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
allocator.deallocate(test_path, allocator.state);
});
char * path = rcutils_join_path(test_path, "assign_param_in_descriptors.yaml", allocator);
ASSERT_TRUE(NULL != path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
allocator.deallocate(path, allocator.state);
});
ASSERT_TRUE(rcutils_exists(path)) << "No test YAML file found at " << path;
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
ASSERT_TRUE(NULL != params_hdl) << rcutils_get_error_string().str;
bool res = rcl_parse_yaml_file(path, params_hdl);
fprintf(stderr, "%s\n", rcutils_get_error_string().str);
EXPECT_FALSE(res);
}

TEST(test_parser, invalid_param_descriptor_key) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024)) << rcutils_get_error_string().str;
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
ASSERT_TRUE(NULL != test_path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
allocator.deallocate(test_path, allocator.state);
});
char * path = rcutils_join_path(test_path, "invalid_param_descriptor_key.yaml", allocator);
ASSERT_TRUE(NULL != path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
allocator.deallocate(path, allocator.state);
});
ASSERT_TRUE(rcutils_exists(path)) << "No test YAML file found at " << path;
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
ASSERT_TRUE(NULL != params_hdl) << rcutils_get_error_string().str;
bool res = rcl_parse_yaml_file(path, params_hdl);
fprintf(stderr, "%s\n", rcutils_get_error_string().str);
EXPECT_FALSE(res);
}

TEST(test_parser, invalid_param_descriptor_type) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024)) << rcutils_get_error_string().str;
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
ASSERT_TRUE(NULL != test_path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
allocator.deallocate(test_path, allocator.state);
});
char * path = rcutils_join_path(test_path, "invalid_param_descriptor_type.yaml", allocator);
ASSERT_TRUE(NULL != path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
allocator.deallocate(path, allocator.state);
});
ASSERT_TRUE(rcutils_exists(path)) << "No test YAML file found at " << path;
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
ASSERT_TRUE(NULL != params_hdl) << rcutils_get_error_string().str;
bool res = rcl_parse_yaml_file(path, params_hdl);
fprintf(stderr, "%s\n", rcutils_get_error_string().str);
EXPECT_FALSE(res);
}

TEST(test_parser, no_value_descriptor) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024)) << rcutils_get_error_string().str;
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
ASSERT_TRUE(NULL != test_path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
allocator.deallocate(test_path, allocator.state);
});
char * path = rcutils_join_path(test_path, "no_value_descriptor.yaml", allocator);
ASSERT_TRUE(NULL != path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
allocator.deallocate(path, allocator.state);
});
ASSERT_TRUE(rcutils_exists(path)) << "No test YAML file found at " << path;
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
ASSERT_TRUE(NULL != params_hdl) << rcutils_get_error_string().str;
bool res = rcl_parse_yaml_file(path, params_hdl);
fprintf(stderr, "%s\n", rcutils_get_error_string().str);
EXPECT_FALSE(res);
}

int32_t main(int32_t argc, char ** argv)
{
::testing::InitGoogleTest(&argc, argv);
Expand Down
Loading

0 comments on commit f28d0cc

Please sign in to comment.