diff options
Diffstat (limited to 'Tests/FindProtobuf')
-rw-r--r-- | Tests/FindProtobuf/Test/CMakeLists.txt | 18 | ||||
-rw-r--r-- | Tests/FindProtobuf/Test/main-desc.cxx | 57 | ||||
-rw-r--r-- | Tests/FindProtobuf/Test/main-generate.cxx | 8 | ||||
-rw-r--r-- | Tests/FindProtobuf/Test/msgs/example.proto | 6 | ||||
-rw-r--r-- | Tests/FindProtobuf/Test/msgs/example_desc.proto | 10 |
5 files changed, 98 insertions, 1 deletions
diff --git a/Tests/FindProtobuf/Test/CMakeLists.txt b/Tests/FindProtobuf/Test/CMakeLists.txt index 10ce976..bc89190 100644 --- a/Tests/FindProtobuf/Test/CMakeLists.txt +++ b/Tests/FindProtobuf/Test/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.4) +cmake_minimum_required(VERSION 3.8) project(TestFindProtobuf CXX) include(CTest) @@ -32,3 +32,19 @@ target_link_libraries(test_var_protoc PRIVATE ${Protobuf_PROTOC_LIBRARIES}) add_test(NAME test_var_protoc COMMAND test_var_protoc) add_test(NAME test_tgt_protoc_version COMMAND protobuf::protoc --version) + +set(Protobuf_IMPORT_DIRS ${Protobuf_INCLUDE_DIRS}) +PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_HEADER msgs/example.proto) +PROTOBUF_GENERATE_CPP(DESC_PROTO_SRC DESC_PROTO_HEADER DESCRIPTORS DESC_PROTO_DESC msgs/example_desc.proto) +add_library(msgs ${PROTO_SRC} ${PROTO_HEADER}) + +add_executable(test_generate main-generate.cxx ${PROTO_SRC}) +target_include_directories(test_generate PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(test_generate msgs ${Protobuf_LIBRARIES}) +add_test(NAME test_generate COMMAND test_generate) + +add_executable(test_desc main-desc.cxx ${DESC_PROTO_SRC}) +target_compile_features(test_desc PRIVATE cxx_std_11) +target_include_directories(test_desc PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(test_desc msgs ${Protobuf_LIBRARIES}) +add_test(NAME test_desc COMMAND test_desc ${DESC_PROTO_DESC}) diff --git a/Tests/FindProtobuf/Test/main-desc.cxx b/Tests/FindProtobuf/Test/main-desc.cxx new file mode 100644 index 0000000..a26e562 --- /dev/null +++ b/Tests/FindProtobuf/Test/main-desc.cxx @@ -0,0 +1,57 @@ +#include <fstream> +#include <google/protobuf/descriptor.h> +#include <google/protobuf/descriptor.pb.h> +#include <google/protobuf/dynamic_message.h> +#include <google/protobuf/text_format.h> +#include <iostream> +#include <string> + +int main(int argc, char* argv[]) +{ + std::ifstream fs; + fs.open(argv[1], std::ifstream::in); + google::protobuf::FileDescriptorSet file_descriptor_set; + file_descriptor_set.ParseFromIstream(&fs); + + const google::protobuf::DescriptorPool* compiled_pool = + google::protobuf::DescriptorPool::generated_pool(); + + if (compiled_pool == NULL) { + std::cerr << "compiled pool is NULL." << std::endl; + return 1; + } + + google::protobuf::DescriptorPool pool(compiled_pool); + google::protobuf::DynamicMessageFactory dynamic_message_factory(&pool); + + for (const google::protobuf::FileDescriptorProto& file_descriptor_proto : + file_descriptor_set.file()) { + const google::protobuf::FileDescriptor* file_descriptor = + pool.BuildFile(file_descriptor_proto); + if (file_descriptor == NULL) { + continue; + } + + const google::protobuf::Descriptor* descriptor = + pool.FindMessageTypeByName("example.msgs.ExampleDesc"); + + if (descriptor == NULL) { + continue; + } + + google::protobuf::Message* msg = + dynamic_message_factory.GetPrototype(descriptor)->New(); + std::string data = "data: 1"; + bool success = google::protobuf::TextFormat::ParseFromString(data, msg); + + if (success) { + return 0; + } else { + std::cerr << "Failed to parse message." << std::endl; + return 2; + } + } + + std::cerr << "No matching message found." << std::endl; + return 3; +} diff --git a/Tests/FindProtobuf/Test/main-generate.cxx b/Tests/FindProtobuf/Test/main-generate.cxx new file mode 100644 index 0000000..ca33a68 --- /dev/null +++ b/Tests/FindProtobuf/Test/main-generate.cxx @@ -0,0 +1,8 @@ +#include <example.pb.h> + +int main() +{ + example::msgs::Example msg; + + return 0; +} diff --git a/Tests/FindProtobuf/Test/msgs/example.proto b/Tests/FindProtobuf/Test/msgs/example.proto new file mode 100644 index 0000000..d27262e --- /dev/null +++ b/Tests/FindProtobuf/Test/msgs/example.proto @@ -0,0 +1,6 @@ +syntax = "proto2"; +package example.msgs; + +message Example { + required int32 data = 1; +} diff --git a/Tests/FindProtobuf/Test/msgs/example_desc.proto b/Tests/FindProtobuf/Test/msgs/example_desc.proto new file mode 100644 index 0000000..4454473 --- /dev/null +++ b/Tests/FindProtobuf/Test/msgs/example_desc.proto @@ -0,0 +1,10 @@ +syntax = "proto2"; +package example.msgs; + +import "google/protobuf/descriptor.proto"; + +message ExampleDesc { + required int32 data = 1; + + optional google.protobuf.FileDescriptorSet desc = 2; +} |