Skip to content

Commit

Permalink
Merge pull request #10 from PragmaTwice/compatibility-test
Browse files Browse the repository at this point in the history
Add compatibility test between protopuf and protobuf
  • Loading branch information
PragmaTwice authored Jan 25, 2021
2 parents 201c77f + e379f43 commit 5dba0f9
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ jobs:
echo "CC=`which clang-12`" >> $GITHUB_ENV
echo "CXX=`which clang++-12`" >> $GITHUB_ENV
if: ${{ startsWith(matrix.os, 'ubuntu') && matrix.cxx == 'clang' }}
- name: install protobuf for compatibility test
run: sudo apt install protobuf-compiler libprotobuf-dev
- name: install coverage tools
run: sudo apt install lcov
if: ${{ matrix.coverage }}
- name: install tools
run: brew install gcc
if: ${{ startsWith(matrix.os, 'macos') }}
- name: generate build script
run: cmake -DCMAKE_BUILD_TYPE=${{ matrix.type }} ${{ matrix.gtest }} ${{ matrix.coverage }} .
run: cmake -DENABLE_COMPATIBILITY_TEST=ON -DCMAKE_BUILD_TYPE=${{ matrix.type }} ${{ matrix.gtest }} ${{ matrix.coverage }} .
- name: build project
run: cmake --build . --config ${{ matrix.type }}
- name: run unit testing
Expand Down
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ target_compile_features(protopuf INTERFACE cxx_std_20)

option(DOWNLOAD_GTEST "Download the git repo of GoogleTest, build and use the master branch version" ON)
option(BUILD_TESTS "Build unit testings" ON)
option(ENABLE_COMPATIBILITY_TEST "Build compatibility testing between protopuf and protobuf" OFF)

if(BUILD_TESTS)
if(DOWNLOAD_GTEST)
Expand Down Expand Up @@ -76,6 +77,10 @@ if(BUILD_TESTS)

include(GoogleTest)
gtest_discover_tests(protopuf_test)

if(ENABLE_COMPATIBILITY_TEST)
add_subdirectory(test/compatibility)
endif()
endif()

install(DIRECTORY include DESTINATION "${CMAKE_INSTALL_PREFIX}")
Expand Down
24 changes: 24 additions & 0 deletions test/compatibility/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2020-2021 PragmaTwice
#
# 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.

find_package(Protobuf REQUIRED)

protobuf_generate_cpp(PROTO_SRC PROTO_HEADER message.proto)

add_executable(protopuf_compatibility_test main.cpp ${PROTO_SRC})

target_include_directories(protopuf_compatibility_test PRIVATE ${Protobuf_INCLUDE_DIR} ${GTEST_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(protopuf_compatibility_test protopuf ${CMAKE_THREAD_LIBS_INIT} ${GTEST_LIBS} ${Protobuf_LIBRARIES})

gtest_discover_tests(protopuf_compatibility_test)
67 changes: 67 additions & 0 deletions test/compatibility/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2020-2021 PragmaTwice
//
// 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 <protopuf/message.h>
#include <message.pb.h>

using namespace pp;
using namespace std;

using Student = message<uint32_field<"id", 1>, string_field<"name", 3>>;
using Class = message<string_field<"name", 8>, message_field<"students", 3, Student, repeated>>;

GTEST_TEST(compatibility, encode) {
Student twice {123, "twice"}, tom{456, "tom"}, jerry{123456, "jerry"};
Class myClass {"class 101", {tom, jerry, twice}};

array<byte, 64> buffer{};
message_coder<Class>::encode(myClass, buffer);

pb::Class yourClass;
yourClass.ParseFromArray(buffer.data(), buffer.size());

EXPECT_EQ(yourClass.name(), "class 101");
EXPECT_EQ(yourClass.students_size(), 3);
EXPECT_EQ(yourClass.students(0).id(), 456);
EXPECT_EQ(yourClass.students(0).name(), "tom");
EXPECT_EQ(yourClass.students(1).id(), 123456);
EXPECT_EQ(yourClass.students(1).name(), "jerry");
EXPECT_EQ(yourClass.students(2).id(), 123);
EXPECT_EQ(yourClass.students(2).name(), "twice");
}

GTEST_TEST(compatibility, decode) {
pb::Class yourClass;
yourClass.set_name("class 101");
pb::Student* tom = yourClass.add_students();
tom->set_id(456);
tom->set_name("tom");
pb::Student* jerry = yourClass.add_students();
jerry->set_id(123456);
jerry->set_name("jerry");
pb::Student* twice = yourClass.add_students();
twice->set_id(123);
twice->set_name("twice");

array<byte, 64> buffer{};
yourClass.SerializeToArray(buffer.data(), buffer.size());

auto [myClass, _] = message_coder<Class>::decode(buffer);
EXPECT_EQ(myClass["name"_f], "class 101");
EXPECT_EQ(myClass["students"_f][2]["name"_f], "twice");
EXPECT_EQ(myClass["students"_f][2]["id"_f], 123);
EXPECT_EQ(myClass["students"_f][1], (Student{123456, "jerry"}));
EXPECT_EQ(myClass["students"_f][0], (Student{456, "tom"}));
}
27 changes: 27 additions & 0 deletions test/compatibility/message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2020-2021 PragmaTwice
//
// 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.

syntax = "proto3";

package pb;

message Student {
uint32 id = 1;
string name = 3;
}

message Class {
string name = 8;
repeated Student students = 3;
}

0 comments on commit 5dba0f9

Please sign in to comment.