Skip to content

Commit

Permalink
add any test
Browse files Browse the repository at this point in the history
  • Loading branch information
huangminghuang committed Jan 13, 2024
1 parent 9da015b commit 1678128
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
16 changes: 5 additions & 11 deletions include/hpp_proto/pb_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1953,23 +1953,17 @@ concept is_any = requires(T &obj) {
return write_proto(msg, any.value);
}

[[nodiscard]] errc unpack_any(concepts::is_any auto &&any, auto &&msg) {
[[nodiscard]] errc unpack_any(concepts::is_any auto &&any, auto &&msg, concepts::memory_resource auto &...ctx) {
if (std::string_view{any.type_url}.ends_with(message_name(msg))) {
return read_proto(msg, any.value);
return read_proto(msg, any.value, ctx...);
}
return std::errc::invalid_argument;
}

[[nodiscard]] errc pack_any(concepts::is_any auto &any, auto &&msg, concepts::memory_resource auto &mr) {
[[nodiscard]] errc pack_any(concepts::is_any auto &any, auto &&msg, concepts::memory_resource auto &...ctx) {
any.type_url = message_type_url(msg);
return write_proto(msg, make_growable(mr, any.value));
}

[[nodiscard]] errc unpack_any(concepts::is_any auto &&any, auto &&msg, concepts::memory_resource auto &mr) {
if (std::string_view{any.type_url}.ends_with(message_name(msg))) {
return read_proto(msg, any.value, mr);
}
return std::errc::invalid_argument;
decltype(auto) v = make_growable(aux_contexts{ctx...}, any.value);
return write_proto(msg, v);
}

} // namespace proto
Expand Down
22 changes: 18 additions & 4 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,31 @@ add_test(NAME map_test

set_property(TEST map_test PROPERTY ENVIRONMENT "ASAN_OPTIONS=detect_container_overflow=0")


add_library(any_test_lib INTERFACE ${protobuf_SOURCE_DIR}/src/google/protobuf/any_test.proto )
add_library(any_test_lib INTERFACE ${protobuf_SOURCE_DIR}/src/google/protobuf/any_test.proto)
target_include_directories(any_test_lib INTERFACE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/../include)
protobuf_generate(
TARGET any_test_lib
LANGUAGE hpp
IMPORT_DIRS ${protobuf_SOURCE_DIR}/src)

gen_proto_descriptor_set(any_test.bin
add_library(non_owning_any_test_lib INTERFACE
${protobuf_SOURCE_DIR}/src/google/protobuf/any.proto
${protobuf_SOURCE_DIR}/src/google/protobuf/any_test.proto
${protobuf_SOURCE_DIR}/src/google/protobuf/field_mask.proto)
target_include_directories(non_owning_any_test_lib INTERFACE ${CMAKE_CURRENT_BINARY_DIR})
protobuf_generate(
TARGET non_owning_any_test_lib
LANGUAGE hpp
IMPORT_DIRS ${protobuf_SOURCE_DIR}/src
PROTOS ${protobuf_SOURCE_DIR}/src/google/protobuf/any_test.proto)
PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/non_owning
PLUGIN_OPTIONS non_owning,root_namespace=non_owning,top_directory=non_owning)

add_executable(any_test any_test.cpp)
target_link_libraries(any_test PRIVATE Boost::ut hpp_proto::libhpp_proto any_test_lib non_owning_any_test_lib)

add_test(NAME any_test
COMMAND any_test
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

add_executable(dynamic_serializer_tests dynamic_serializer_tests.cpp)
target_link_libraries(dynamic_serializer_tests PRIVATE unittest_proto3_proto_lib map_unittest_proto_lib Boost::ut glaze::glaze gpb_proto_json any_test_lib)
Expand Down
51 changes: 51 additions & 0 deletions tests/any_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "google/protobuf/any_test.pb.hpp"
#include <google/protobuf/field_mask.pb.hpp>
#include "non_owning/google/protobuf/any_test.pb.hpp"
#include "non_owning/google/protobuf/field_mask.pb.hpp"

#include <boost/ut.hpp>
#include "test_util.h"

using namespace boost::ut;

suite test_any = [] {
"any"_test = [] {
protobuf_unittest::TestAny message;
google::protobuf::FieldMask fm{.paths = {"/usr/share", "/usr/local/share"}};
expect(hpp::proto::pack_any(message.any_value.emplace(), fm).success());

std::vector<char> buf;
expect(hpp::proto::write_proto(message, buf).success());

protobuf_unittest::TestAny message2;
expect(hpp::proto::read_proto(message2, buf).success());
google::protobuf::FieldMask fm2;
expect(hpp::proto::unpack_any(message2.any_value.value(), fm2).success());
expect(fm == fm2);
};

"non_owning_any"_test = [] {
using namespace std::string_view_literals;

monotonic_buffer_resource mr{1024};

non_owning::protobuf_unittest::TestAny message;
std::array<std::string_view, 2> paths{"/usr/share"sv, "/usr/local/share"sv};
non_owning::google::protobuf::FieldMask fm{.paths = paths};
expect(hpp::proto::pack_any(message.any_value.emplace(), fm, mr).success());

std::vector<char> buf;
expect(hpp::proto::write_proto(message, buf).success());

non_owning::protobuf_unittest::TestAny message2;
expect(hpp::proto::read_proto(message2, buf, mr).success());
non_owning::google::protobuf::FieldMask fm2;
expect(hpp::proto::unpack_any(message2.any_value.value(), fm2, mr).success());
expect(std::ranges::equal(paths, fm2.paths));
};
};

int main() {
const auto result = boost::ut::cfg<>.run({.report_errors = true}); // explicitly run registered test suites and report errors
return static_cast<int>(result);
}

0 comments on commit 1678128

Please sign in to comment.