• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 use googletest::prelude::*;
9 use map_unittest_rust_proto::TestMapWithMessages;
10 use protobuf_upb::proto;
11 use unittest_rust_proto::{
12     test_all_types::NestedEnum as NestedEnumProto2,
13     test_all_types::NestedMessage as NestedMessageProto2, TestAllTypes as TestAllTypesProto2,
14 };
15 
16 #[gtest]
test_debug_string()17 fn test_debug_string() {
18     let mut msg = proto!(TestAllTypesProto2 {
19         optional_int32: 42,
20         optional_string: "Hello World",
21         optional_nested_enum: NestedEnumProto2::Bar,
22         oneof_uint32: 452235,
23         optional_nested_message: proto!(NestedMessageProto2 { bb: 100 }),
24     });
25     let mut repeated_string = msg.repeated_string_mut();
26     repeated_string.push("Hello World");
27     repeated_string.push("Hello World");
28     repeated_string.push("Hello World");
29 
30     let mut msg_map = TestMapWithMessages::new();
31     println!("EMPTY MSG: {:?}", msg_map); // Make sure that we can print an empty message.
32     msg_map.map_string_all_types_mut().insert("hello", msg.as_view());
33     msg_map.map_string_all_types_mut().insert("fizz", msg.as_view());
34     msg_map.map_string_all_types_mut().insert("boo", msg.as_view());
35 
36     println!("{:?}", msg_map);
37     println!("{:?}", msg_map.as_view()); // Make sure that we can print as_view
38     println!("{:?}", msg_map.as_mut()); // Make sure that we can print as_mut
39     let golden = r#"12 {
40   key: "hello"
41   value {
42     1: 42
43     14: "Hello World"
44     18 {
45       1: 100
46     }
47     21: 2
48     44: "Hello World"
49     44: "Hello World"
50     44: "Hello World"
51     111: 452235
52   }
53 }
54 12 {
55   key: "fizz"
56   value {
57     1: 42
58     14: "Hello World"
59     18 {
60       1: 100
61     }
62     21: 2
63     44: "Hello World"
64     44: "Hello World"
65     44: "Hello World"
66     111: 452235
67   }
68 }
69 12 {
70   key: "boo"
71   value {
72     1: 42
73     14: "Hello World"
74     18 {
75       1: 100
76     }
77     21: 2
78     44: "Hello World"
79     44: "Hello World"
80     44: "Hello World"
81     111: 452235
82   }
83 }
84 "#;
85     // C strings are null terminated while Rust strings are not.
86     let null_terminated_str = format!("{}\0", golden);
87     assert_that!(format!("{:?}", msg_map), eq(null_terminated_str.as_str()));
88 }
89