File: debug_string_test.rs

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (88 lines) | stat: -rw-r--r-- 2,379 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC.  All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

use googletest::prelude::*;
use map_unittest_rust_proto::TestMapWithMessages;
use protobuf_upb::proto;
use unittest_rust_proto::{
    test_all_types::NestedEnum as NestedEnumProto2,
    test_all_types::NestedMessage as NestedMessageProto2, TestAllTypes as TestAllTypesProto2,
};

#[gtest]
fn test_debug_string() {
    let mut msg = proto!(TestAllTypesProto2 {
        optional_int32: 42,
        optional_string: "Hello World",
        optional_nested_enum: NestedEnumProto2::Bar,
        oneof_uint32: 452235,
        optional_nested_message: proto!(NestedMessageProto2 { bb: 100 }),
    });
    let mut repeated_string = msg.repeated_string_mut();
    repeated_string.push("Hello World");
    repeated_string.push("Hello World");
    repeated_string.push("Hello World");

    let mut msg_map = TestMapWithMessages::new();
    println!("EMPTY MSG: {:?}", msg_map); // Make sure that we can print an empty message. 
    msg_map.map_string_all_types_mut().insert("hello", msg.as_view());
    msg_map.map_string_all_types_mut().insert("fizz", msg.as_view());
    msg_map.map_string_all_types_mut().insert("boo", msg.as_view());

    println!("{:?}", msg_map);
    println!("{:?}", msg_map.as_view()); // Make sure that we can print as_view
    println!("{:?}", msg_map.as_mut()); // Make sure that we can print as_mut
    let golden = r#"12 {
  key: "hello"
  value {
    1: 42
    14: "Hello World"
    18 {
      1: 100
    }
    21: 2
    44: "Hello World"
    44: "Hello World"
    44: "Hello World"
    111: 452235
  }
}
12 {
  key: "fizz"
  value {
    1: 42
    14: "Hello World"
    18 {
      1: 100
    }
    21: 2
    44: "Hello World"
    44: "Hello World"
    44: "Hello World"
    111: 452235
  }
}
12 {
  key: "boo"
  value {
    1: 42
    14: "Hello World"
    18 {
      1: 100
    }
    21: 2
    44: "Hello World"
    44: "Hello World"
    44: "Hello World"
    111: 452235
  }
}
"#;
    // C strings are null terminated while Rust strings are not.
    let null_terminated_str = format!("{}\0", golden);
    assert_that!(format!("{:?}", msg_map), eq(null_terminated_str.as_str()));
}