Skip to content

Commit

Permalink
Merge branch 'gz-msgs10' into mjcarroll/messages_tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
mjcarroll authored Sep 25, 2023
2 parents 818fe5b + 9b6a7d8 commit 0a2e92c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions core/include/gz/msgs/PointCloudPackedUtils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <gz/msgs/pointcloud_packed.pb.h>

#include <cstdarg>
#include <functional>
#include <sstream>
#include <string>
#include <utility>
Expand Down
1 change: 1 addition & 0 deletions core/src/DynamicFactory.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#pragma warning(pop)
#endif

#include <functional>
#include <map>
#include <memory>
#include <string>
Expand Down
7 changes: 6 additions & 1 deletion core/src/MessageFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ MessageFactory::MessagePtr MessageFactory::New(
std::unique_ptr<google::protobuf::Message> msg = New(_msgType);
if (msg)
{
google::protobuf::TextFormat::ParseFromString(_args, msg.get());
if (!google::protobuf::TextFormat::ParseFromString(_args, msg.get()))
{
// The user-provided string was invalid,
// return nullptr rather than an empty message.
msg.reset();
}
}
return msg;
}
Expand Down
42 changes: 28 additions & 14 deletions test/integration/Factory_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,10 @@ TEST(FactoryTest, Type)
/////////////////////////////////////////////////
TEST(FactoryTest, New)
{
// Correct call, using periods, fully qualified
// Preferred call, using periods, fully qualified
auto msg = Factory::New<Vector3d>("gz.msgs.Vector3d");

ASSERT_TRUE(msg.get() != nullptr);

msg->set_x(1.0);
msg->set_y(2.0);
msg->set_z(3.0);

auto msgFilled = Factory::New<Vector3d>(
"gz_msgs.Vector3d", "x: 1.0, y: 2.0, z: 3.0");
ASSERT_TRUE(msgFilled.get() != nullptr);

EXPECT_DOUBLE_EQ(msg->x(), msgFilled->x());
EXPECT_DOUBLE_EQ(msg->y(), msgFilled->y());
EXPECT_DOUBLE_EQ(msg->z(), msgFilled->z());

// Various other supported ways of specifying gz.msgs
msg = Factory::New<Vector3d>("gz_msgs.Vector3d");
EXPECT_TRUE(msg.get() != nullptr);
Expand All @@ -81,6 +68,33 @@ TEST(FactoryTest, New)
EXPECT_TRUE(msg.get() != nullptr);
}

/////////////////////////////////////////////////
TEST(FactoryTest, NewWithWellFormedData)
{
gz::msgs::Vector3d msg;
msg.set_x(1.0);
msg.set_y(2.0);
msg.set_z(3.0);

auto msgFilled = Factory::New<gz::msgs::Vector3d>(
"gz.msgs.Vector3d", "x: 1.0, y: 2.0, z: 3.0");
ASSERT_TRUE(nullptr != msgFilled);

EXPECT_DOUBLE_EQ(msg.x(), msgFilled->x());
EXPECT_DOUBLE_EQ(msg.y(), msgFilled->y());
EXPECT_DOUBLE_EQ(msg.z(), msgFilled->z());
}


/////////////////////////////////////////////////
TEST(FactoryTest, NewWithMalformedData)
{
/// Passing bad data to New results in a nullptr
auto msgFilled = Factory::New<gz::msgs::Vector3d>(
"gz.msgs.Vector3d", "x: 1.0, y: asdf, z: 3.0");
ASSERT_TRUE(nullptr == msgFilled);
}

/////////////////////////////////////////////////
TEST(FactoryTest, DeprecatedNonFullyQualified)
{
Expand Down

0 comments on commit 0a2e92c

Please sign in to comment.