Skip to content

Commit

Permalink
Add test for descriptor loading
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <mjcarroll@intrinsic.ai>
  • Loading branch information
mjcarroll committed Mar 8, 2024
1 parent 19ee71f commit 4f257e3
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
Empty file added test/desc/testing.proto
Empty file.
16 changes: 16 additions & 0 deletions test/desc/testing.proto.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

C
testing/bytes.prototesting"
Bytes
data ( Rdatabproto3
�
testing/message.prototesting"

FooMessage
foo ( Rfoo"

BarMessage
foo ( Rfoo"

BazMessage
foo ( Rfoobproto3
8 changes: 8 additions & 0 deletions test/desc/testing/bytes.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

package testing;

message Bytes
{
bytes data = 1;
}
15 changes: 15 additions & 0 deletions test/desc/testing/message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

package testing;

message FooMessage {
string data = 1;
}

message BarMessage {
FooMessage foo = 1;
}

message BazMessage {
BarMessage bar = 1;
}
5 changes: 5 additions & 0 deletions test/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ if(MSVC)
endif()

gz_build_tests(TYPE INTEGRATION SOURCES ${tests})

if (TARGET INTEGRATION_descriptors)
target_compile_definitions(INTEGRATION_descriptors
PRIVATE GZ_MSGS_TEST_PATH="${PROJECT_SOURCE_DIR}/test")
endif()
84 changes: 84 additions & 0 deletions test/integration/descriptors.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2024 Open Source Robotics Foundation
*
* 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 <filesystem>

#include "gz/msgs/Factory.hh"

static constexpr const char * kMsgsTestPath = GZ_MSGS_TEST_PATH;

TEST(FactoryTest, DynamicFactory)
{
EXPECT_EQ(nullptr, gz::msgs::Factory::New("example.msgs.StringMsg"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.Bytes"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.FooMessage"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.BarMessage"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.BazMessage"));

// Test loading an invalid path
{
std::filesystem::path test_path(kMsgsTestPath);
std::string paths = (test_path / "desc" / "does_not_exist.desc").string();
gz::msgs::Factory::LoadDescriptors(paths);
}
EXPECT_EQ(nullptr, gz::msgs::Factory::New("example.msgs.StringMsg"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.Bytes"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.FooMessage"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.BarMessage"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.BazMessage"));

// Test loading an empty file
{
std::filesystem::path test_path(kMsgsTestPath);
std::string paths = (test_path / "desc" / "testing.proto").string();
gz::msgs::Factory::LoadDescriptors(paths);
}
EXPECT_EQ(nullptr, gz::msgs::Factory::New("example.msgs.StringMsg"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.Bytes"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.FooMessage"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.BarMessage"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.BazMessage"));

// Load a valid descriptor file with one message type
{
std::filesystem::path test_path(kMsgsTestPath);
std::string paths = (test_path / "desc" / "stringmsg.desc").string();
gz::msgs::Factory::LoadDescriptors(paths);
}

EXPECT_NE(nullptr, gz::msgs::Factory::New("example.msgs.StringMsg"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.Bytes"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.FooMessage"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.BarMessage"));
EXPECT_EQ(nullptr, gz::msgs::Factory::New("testing.BazMessage"));

// Load a directory
{
std::filesystem::path test_path(kMsgsTestPath);
std::string paths = (test_path / "desc").string();
gz::msgs::Factory::LoadDescriptors(paths);
}

EXPECT_NE(nullptr, gz::msgs::Factory::New("example.msgs.StringMsg"));
EXPECT_NE(nullptr, gz::msgs::Factory::New("testing.Bytes"));
EXPECT_NE(nullptr, gz::msgs::Factory::New("testing.FooMessage"));
EXPECT_NE(nullptr, gz::msgs::Factory::New("testing.BarMessage"));
EXPECT_NE(nullptr, gz::msgs::Factory::New("testing.BazMessage"));

}

0 comments on commit 4f257e3

Please sign in to comment.