1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2024 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 <stddef.h>
9
10 #include <string>
11
12 #include <gtest/gtest.h>
13 #include "absl/log/absl_log.h"
14 #include "upb/base/string_view.h"
15 #include "upb/base/upcast.h"
16 #include "upb/mem/arena.h"
17 #include "upb/message/message.h"
18 #include "upb/mini_table/message.h"
19 #include "upb/test/test.upb.h"
20 #include "upb/test/test.upb_minitable.h"
21 #include "upb/text/debug_string.h"
22
TEST(TextNoReflection,Extensions)23 TEST(TextNoReflection, Extensions) {
24 const upb_MiniTable* mt_main = upb_0test__ModelWithExtensions_msg_init_ptr;
25 upb_Arena* arena = upb_Arena_New();
26
27 upb_test_ModelExtension1* extension1 = upb_test_ModelExtension1_new(arena);
28 upb_test_ModelExtension1_set_str(extension1,
29 upb_StringView_FromString("Hello"));
30
31 upb_test_ModelExtension2* extension2 = upb_test_ModelExtension2_new(arena);
32 upb_test_ModelExtension2_set_i(extension2, 5);
33
34 upb_test_ModelWithExtensions* msg = upb_test_ModelWithExtensions_new(arena);
35
36 upb_test_ModelExtension1_set_model_ext(msg, extension1, arena);
37 upb_test_ModelExtension2_set_model_ext(msg, extension2, arena);
38
39 // Convert to a type of upb_Message*
40 upb_Message* input = UPB_UPCAST(msg);
41 // Resizing/reallocation of the buffer is not necessary since we're only
42 // testing that we get the expected debug string.
43 char* buf = new char[100];
44 int options =
45 UPB_TXTENC_NOSORT; // Does not matter, but maps will not be sorted.
46 size_t size = 100;
47 size_t real_size = upb_DebugString(input, mt_main, options, buf, size);
48 ABSL_LOG(INFO) << "Buffer: \n"
49 << buf << "\n"
50 << "Size:" << real_size << "\n";
51 std::string golden = R"([4135] {
52 9: 5
53 }
54 [1547] {
55 25: "Hello"
56 }
57 )";
58 ASSERT_EQ(buf[real_size], '\0');
59 std::string str(buf);
60 ASSERT_EQ(buf, golden);
61 delete[] buf;
62 upb_Arena_Free(arena);
63 }