• 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 <gtest/gtest.h>
9 #include "google/protobuf/test_messages_proto2.upb_minitable.h"
10 #include "upb/mini_table/field.h"
11 #include "upb/mini_table/message.h"
12 #include "upb/test/proto3_test.upb.h"
13 
TEST(MiniTableOneofTest,OneOfIteratorProto2)14 TEST(MiniTableOneofTest, OneOfIteratorProto2) {
15   constexpr int oneof_first_field_number = 111;
16   constexpr int oneof_test_field_number = 116;
17 
18   const upb_MiniTable* google_protobuf_table =
19       &protobuf_0test_0messages__proto2__TestAllTypesProto2_msg_init;
20   const upb_MiniTableField* field =
21       upb_MiniTable_FindFieldByNumber(google_protobuf_table, oneof_test_field_number);
22   ASSERT_TRUE(field != nullptr);
23   const upb_MiniTableField* ptr = upb_MiniTable_GetOneof(google_protobuf_table, field);
24   int field_num = oneof_first_field_number;
25   do {
26     EXPECT_EQ(upb_MiniTableField_Number(ptr), field_num++);
27   } while (upb_MiniTable_NextOneofField(google_protobuf_table, &ptr));
28 }
29 
TEST(MiniTableOneofTest,InitialFieldOneOf)30 TEST(MiniTableOneofTest, InitialFieldOneOf) {
31   const upb_MiniTable* table = &upb__test__TestOneOfInitialField_msg_init;
32   const upb_MiniTableField* field = upb_MiniTable_FindFieldByNumber(table, 1);
33   ASSERT_TRUE(field != nullptr);
34 
35   const upb_MiniTableField* ptr = upb_MiniTable_GetOneof(table, field);
36   EXPECT_TRUE(ptr == field);
37 }
38 
TEST(MiniTableOneofTest,InitialFieldNotOneOf)39 TEST(MiniTableOneofTest, InitialFieldNotOneOf) {
40   constexpr int test_field_number = 1;  // optional int that is not a oneof
41   const upb_MiniTable* google_protobuf_table =
42       &protobuf_0test_0messages__proto2__TestAllTypesProto2_msg_init;
43   const upb_MiniTableField* field =
44       upb_MiniTable_FindFieldByNumber(google_protobuf_table, test_field_number);
45   ASSERT_TRUE(field != nullptr);
46   const upb_MiniTableField* first_field =
47       upb_MiniTable_GetOneof(google_protobuf_table, field);
48   EXPECT_EQ(first_field, nullptr);
49 }
50