Skip to content

Commit

Permalink
add support for recursive XmlRpcSettings lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
mathias-luedtke committed Jul 7, 2020
1 parent b7d2b4e commit d2433ba
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions socketcan_interface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ install(
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)
if(CATKIN_ENABLE_TESTING)
find_package(xmlrpcpp REQUIRED)

catkin_add_gtest(${PROJECT_NAME}-test_dummy_interface
test/test_dummy_interface.cpp
Expand All @@ -125,9 +126,13 @@ if(CATKIN_ENABLE_TESTING)
catkin_add_gtest(${PROJECT_NAME}-test_settings
test/test_settings.cpp
)
target_include_directories(${PROJECT_NAME}-test_settings PRIVATE
${xmlrpcpp_INCLUDE_DIRS}
)
target_link_libraries(${PROJECT_NAME}-test_settings
${PROJECT_NAME}_string
${catkin_LIBRARIES}
${xmlrpcpp_LIBRARIES}
)

catkin_add_gtest(${PROJECT_NAME}-test_string
Expand Down
17 changes: 14 additions & 3 deletions socketcan_interface/include/socketcan_interface/xmlrpc_settings.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef SOCKETCAN_INTERFACE_XMLRPC_SETTINGS_H
#define SOCKETCAN_INTERFACE_XMLRPC_SETTINGS_H
#include <socketcan_interface/logging.h>

#include <socketcan_interface/settings.h>
#include "xmlrpcpp/XmlRpcValue.h"
Expand All @@ -12,10 +13,20 @@ class XmlRpcSettings : public can::Settings {
XmlRpcSettings(const XmlRpc::XmlRpcValue &v) : value_(v) {}
XmlRpcSettings& operator=(const XmlRpc::XmlRpcValue &v) { value_ = v; return *this; }
private:
virtual bool getRepr(const std::string &n, std::string & repr) const {
if(value_.hasMember(n)){
virtual bool getRepr(const std::string &name, std::string & repr) const {
const XmlRpc::XmlRpcValue *value = &value_;

std::string n = name;
size_t delim_pos;
while (value->getType() == XmlRpc::XmlRpcValue::TypeStruct && (delim_pos = n.find('/')) != std::string::npos){
std::string segment = n.substr(0, delim_pos);
if (!value->hasMember(segment)) return false;
value = &((*value)[segment]);
n.erase(0, delim_pos+1);
}
if(value->hasMember(n)){
std::stringstream sstr;
sstr << const_cast< XmlRpc::XmlRpcValue &>(value_)[n]; // does not write since already existing
sstr << (*value)[n];
repr = sstr.str();
return true;
}
Expand Down
1 change: 1 addition & 0 deletions socketcan_interface/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<depend>linux-kernel-headers</depend>

<test_depend>rosunit</test_depend>
<test_depend>xmlrpcpp</test_depend>

<export>
<socketcan_interface plugin="${prefix}/socketcan_interface_plugin.xml" />
Expand Down
29 changes: 29 additions & 0 deletions socketcan_interface/test/test_settings.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Bring in my package's API, which is what I'm testing
#include <socketcan_interface/settings.h>
#include <socketcan_interface/xmlrpc_settings.h>
#include <socketcan_interface/socketcan.h>

// Bring in gtest
Expand Down Expand Up @@ -55,6 +56,34 @@ TEST(SettingTest, socketcan_masks)
EXPECT_EQ(sci.getFatalErrorMask(), fatal_errors & (~CAN_ERR_TX_TIMEOUT));
}

TEST(SettingTest, xmlrpc)
{
XmlRpc::XmlRpcValue value;
value["param"] = 1;
XmlRpc::XmlRpcValue segment;
segment["param"] = 2;
value["segment"] = segment;
XmlRpcSettings settings(value);

ASSERT_TRUE(value["segment"].hasMember(std::string("param")));

int res;
EXPECT_TRUE(settings.get<int>("param", res));
EXPECT_EQ(res, 1);
EXPECT_EQ(settings.get_optional("param", 0), 1);
EXPECT_FALSE(settings.get<int>("param2", res));
EXPECT_EQ(settings.get_optional("param2", 0), 0);

EXPECT_TRUE(settings.get<int>("segment/param", res));
EXPECT_EQ(res, 2);
EXPECT_EQ(settings.get_optional("segment/param", 0), 2);

EXPECT_FALSE(settings.get<int>("segment/param2", res));
EXPECT_EQ(settings.get_optional("segment/param2", 0), 0);
EXPECT_FALSE(settings.get<int>("segment2/param", res));
EXPECT_EQ(settings.get_optional("segment2/param", 0), 0);
}

// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
Expand Down

0 comments on commit d2433ba

Please sign in to comment.