-
Notifications
You must be signed in to change notification settings - Fork 471
/
debug.rs
96 lines (88 loc) · 2.36 KB
/
debug.rs
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
89
90
91
92
93
94
95
96
//! Tests for our own Debug implementation.
//!
//! The tests check against expected output. This may be a bit fragile, but it is likely OK for
//! actual use.
use prost::alloc::{format, string::String};
// Borrow some types from other places.
#[cfg(feature = "std")]
use crate::message_encoding::Basic;
use crate::message_encoding::BasicEnumeration;
/// Some real-life message
#[test]
#[cfg(feature = "std")]
fn basic() {
let mut basic = Basic::default();
assert_eq!(
format!("{:?}", basic),
"Basic { \
int32: 0, \
bools: [], \
string: \"\", \
optional_string: None, \
enumeration: ZERO, \
enumeration_map: {}, \
string_map: {}, \
enumeration_btree_map: {}, \
string_btree_map: {}, \
oneof: None \
}"
);
basic
.enumeration_map
.insert(0, BasicEnumeration::TWO as i32);
basic.enumeration = 42;
assert_eq!(
format!("{:?}", basic),
"Basic { \
int32: 0, \
bools: [], \
string: \"\", \
optional_string: None, \
enumeration: 42, \
enumeration_map: {0: TWO}, \
string_map: {}, \
enumeration_btree_map: {}, \
string_btree_map: {}, \
oneof: None \
}"
);
}
/*
TODO(tokio-rs/prost#56):
/// A special case with a tuple struct
#[test]
fn tuple_struct() {
#[derive(Clone, PartialEq, Message)]
struct NewType(
#[prost(enumeration="BasicEnumeration", tag="5")]
i32,
);
assert_eq!(format!("{:?}", NewType(BasicEnumeration::TWO as i32)), "NewType(TWO)");
assert_eq!(format!("{:?}", NewType(42)), "NewType(42)");
}
*/
#[derive(Clone, PartialEq, prost::Oneof)]
pub enum OneofWithEnum {
#[prost(int32, tag = "8")]
Int(i32),
#[prost(string, tag = "9")]
String(String),
#[prost(enumeration = "BasicEnumeration", tag = "10")]
Enumeration(i32),
}
#[derive(Clone, PartialEq, prost::Message)]
struct MessageWithOneof {
#[prost(oneof = "OneofWithEnum", tags = "8, 9, 10")]
of: Option<OneofWithEnum>,
}
/// Enumerations inside oneofs
#[test]
fn oneof_with_enum() {
let msg = MessageWithOneof {
of: Some(OneofWithEnum::Enumeration(BasicEnumeration::TWO as i32)),
};
assert_eq!(
format!("{:?}", msg),
"MessageWithOneof { of: Some(Enumeration(TWO)) }"
);
}