• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include "google/protobuf/hpb/backend/upb/interop.h"
9 
10 #include <gtest/gtest.h>
11 #include "google/protobuf/compiler/hpb/tests/test_model.upb.h"
12 #include "google/protobuf/compiler/hpb/tests/test_model.upb.proto.h"
13 #include "upb/mem/arena.h"
14 #include "upb/message/message.h"
15 
16 namespace hpb::testing {
17 namespace {
18 using ::hpb_unittest::protos::TestModel;
19 
TEST(CppGeneratedCode,InteropMoveMessage)20 TEST(CppGeneratedCode, InteropMoveMessage) {
21   // Generate message (simulating message created in another VM/language)
22   upb_Arena* source_arena = upb_Arena_New();
23   hpb_unittest_TestModel* message = hpb_unittest_TestModel_new(source_arena);
24   ASSERT_NE(message, nullptr);
25   hpb_unittest_TestModel_set_int_value_with_default(message, 123);
26 
27   // Move ownership.
28   TestModel model = hpb::interop::upb::MoveMessage<TestModel>(
29       (upb_Message*)message, source_arena);
30   // Now that we have moved ownership, free original arena.
31   upb_Arena_Free(source_arena);
32   EXPECT_EQ(model.int_value_with_default(), 123);
33 }
34 
35 }  // namespace
36 }  // namespace hpb::testing
37