diff --git a/test/desc/testing.proto b/test/desc/testing.proto new file mode 100644 index 00000000..e69de29b diff --git a/test/desc/testing.proto.bin b/test/desc/testing.proto.bin new file mode 100644 index 00000000..ea7cbe8c --- /dev/null +++ b/test/desc/testing.proto.bin @@ -0,0 +1,16 @@ + +C +testing/bytes.prototesting" +Bytes +data ( Rdatabproto3 +ˆ +testing/message.prototesting" + +FooMessage +foo ( Rfoo" + +BarMessage +foo ( Rfoo" + +BazMessage +foo ( Rfoobproto3 \ No newline at end of file diff --git a/test/desc/testing/bytes.proto b/test/desc/testing/bytes.proto new file mode 100644 index 00000000..b2524164 --- /dev/null +++ b/test/desc/testing/bytes.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; + +package testing; + +message Bytes +{ + bytes data = 1; +} diff --git a/test/desc/testing/message.proto b/test/desc/testing/message.proto new file mode 100644 index 00000000..751197d6 --- /dev/null +++ b/test/desc/testing/message.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; + +package testing; + +message FooMessage { + string data = 1; +} + +message BarMessage { + FooMessage foo = 1; +} + +message BazMessage { + BarMessage bar = 1; +} diff --git a/test/integration/CMakeLists.txt b/test/integration/CMakeLists.txt index d1b9e17f..56b1586f 100644 --- a/test/integration/CMakeLists.txt +++ b/test/integration/CMakeLists.txt @@ -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() diff --git a/test/integration/descriptors.cc b/test/integration/descriptors.cc new file mode 100644 index 00000000..6d38b969 --- /dev/null +++ b/test/integration/descriptors.cc @@ -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 + +#include + +#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")); + +}