Skip to content

Commit 6445907

Browse files
authored
test(groups): Move tests to separate module (#1234)
- Move related tests to separate module - Make config dependency explicit in `build.rs`
1 parent 3de8526 commit 6445907

File tree

3 files changed

+110
-107
lines changed

3 files changed

+110
-107
lines changed

tests/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn main() {
8787
.compile_protos(&[src.join("enum_keyword_variant.proto")], includes)
8888
.unwrap();
8989

90-
config
90+
prost_build::Config::new()
9191
.compile_protos(&[src.join("groups.proto")], includes)
9292
.unwrap();
9393

tests/src/groups.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
include!(concat!(env!("OUT_DIR"), "/groups.rs"));
2+
3+
use alloc::boxed::Box;
4+
use alloc::string::ToString;
5+
use alloc::vec::Vec;
6+
use prost::Message;
7+
8+
use crate::check_message;
9+
10+
#[test]
11+
fn test_group() {
12+
// optional group
13+
let msg1_bytes = &[0x0B, 0x10, 0x20, 0x0C];
14+
15+
let msg1 = Test1 {
16+
groupa: Some(test1::GroupA { i2: Some(32) }),
17+
};
18+
19+
let mut bytes = Vec::new();
20+
msg1.encode(&mut bytes).unwrap();
21+
assert_eq!(&bytes, msg1_bytes);
22+
23+
// skip group while decoding
24+
let data: &[u8] = &[
25+
0x0B, // start group (tag=1)
26+
0x30, 0x01, // unused int32 (tag=6)
27+
0x2B, 0x30, 0xFF, 0x01, 0x2C, // unused group (tag=5)
28+
0x10, 0x20, // int32 (tag=2)
29+
0x0C, // end group (tag=1)
30+
];
31+
assert_eq!(Test1::decode(data), Ok(msg1));
32+
33+
// repeated group
34+
let msg2_bytes: &[u8] = &[
35+
0x20, 0x40, 0x2B, 0x30, 0xFF, 0x01, 0x2C, 0x2B, 0x30, 0x01, 0x2C, 0x38, 0x64,
36+
];
37+
38+
let msg2 = Test2 {
39+
i14: Some(64),
40+
groupb: Vec::from([
41+
test2::GroupB { i16: Some(255) },
42+
test2::GroupB { i16: Some(1) },
43+
]),
44+
i17: Some(100),
45+
};
46+
47+
let mut bytes = Vec::new();
48+
msg2.encode(&mut bytes).unwrap();
49+
assert_eq!(bytes.as_slice(), msg2_bytes);
50+
51+
assert_eq!(Test2::decode(msg2_bytes), Ok(msg2));
52+
}
53+
54+
#[test]
55+
fn test_group_oneof() {
56+
let msg = OneofGroup {
57+
i1: Some(42),
58+
field: Some(oneof_group::Field::S2("foo".to_string())),
59+
};
60+
check_message(&msg);
61+
62+
let msg = OneofGroup {
63+
i1: Some(42),
64+
field: Some(oneof_group::Field::G(oneof_group::G {
65+
i2: None,
66+
s1: "foo".to_string(),
67+
t1: None,
68+
})),
69+
};
70+
check_message(&msg);
71+
72+
let msg = OneofGroup {
73+
i1: Some(42),
74+
field: Some(oneof_group::Field::G(oneof_group::G {
75+
i2: Some(99),
76+
s1: "foo".to_string(),
77+
t1: Some(Test1 {
78+
groupa: Some(test1::GroupA { i2: None }),
79+
}),
80+
})),
81+
};
82+
check_message(&msg);
83+
84+
check_message(&OneofGroup::default());
85+
}
86+
87+
#[test]
88+
fn test_deep_nesting_group() {
89+
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
90+
let mut a = NestedGroup2::default();
91+
for _ in 0..depth {
92+
a = NestedGroup2 {
93+
optionalgroup: Some(Box::new(nested_group2::OptionalGroup {
94+
nested_group: Some(a),
95+
})),
96+
};
97+
}
98+
99+
let mut buf = Vec::new();
100+
a.encode(&mut buf).unwrap();
101+
NestedGroup2::decode(buf.as_slice()).map(|_| ())
102+
}
103+
104+
assert!(build_and_roundtrip(50).is_ok());
105+
assert!(build_and_roundtrip(51).is_err());
106+
}

tests/src/lib.rs

Lines changed: 3 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ mod nesting;
7575
#[cfg(test)]
7676
mod recursive_oneof;
7777

78+
#[cfg(test)]
79+
mod groups;
80+
7881
mod test_enum_named_option_value {
7982
include!(concat!(env!("OUT_DIR"), "/myenum.optionn.rs"));
8083
}
@@ -106,10 +109,6 @@ pub mod oneof_attributes {
106109
include!(concat!(env!("OUT_DIR"), "/foo.custom.one_of_attrs.rs"));
107110
}
108111

109-
pub mod groups {
110-
include!(concat!(env!("OUT_DIR"), "/groups.rs"));
111-
}
112-
113112
pub mod proto3 {
114113
pub mod presence {
115114
include!(concat!(env!("OUT_DIR"), "/proto3.presence.rs"));
@@ -263,8 +262,6 @@ mod tests {
263262

264263
use alloc::collections::{BTreeMap, BTreeSet};
265264
use alloc::vec;
266-
#[cfg(not(feature = "std"))]
267-
use alloc::{boxed::Box, string::ToString};
268265

269266
use super::*;
270267

@@ -387,29 +384,6 @@ mod tests {
387384
set2.insert(msg2.field);
388385
}
389386

390-
#[test]
391-
fn test_deep_nesting_group() {
392-
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
393-
use crate::groups::{nested_group2::OptionalGroup, NestedGroup2};
394-
395-
let mut a = NestedGroup2::default();
396-
for _ in 0..depth {
397-
a = NestedGroup2 {
398-
optionalgroup: Some(Box::new(OptionalGroup {
399-
nested_group: Some(a),
400-
})),
401-
};
402-
}
403-
404-
let mut buf = Vec::new();
405-
a.encode(&mut buf).unwrap();
406-
NestedGroup2::decode(buf.as_slice()).map(|_| ())
407-
}
408-
409-
assert!(build_and_roundtrip(50).is_ok());
410-
assert!(build_and_roundtrip(51).is_err());
411-
}
412-
413387
#[test]
414388
fn test_267_regression() {
415389
// Checks that skip_field will error appropriately when given a big stack of StartGroup
@@ -426,83 +400,6 @@ mod tests {
426400
assert_eq!(msg.name, r#"["unknown"]"#);
427401
}
428402

429-
#[test]
430-
fn test_group() {
431-
// optional group
432-
let msg1_bytes = &[0x0B, 0x10, 0x20, 0x0C];
433-
434-
let msg1 = groups::Test1 {
435-
groupa: Some(groups::test1::GroupA { i2: Some(32) }),
436-
};
437-
438-
let mut bytes = Vec::new();
439-
msg1.encode(&mut bytes).unwrap();
440-
assert_eq!(&bytes, msg1_bytes);
441-
442-
// skip group while decoding
443-
let data: &[u8] = &[
444-
0x0B, // start group (tag=1)
445-
0x30, 0x01, // unused int32 (tag=6)
446-
0x2B, 0x30, 0xFF, 0x01, 0x2C, // unused group (tag=5)
447-
0x10, 0x20, // int32 (tag=2)
448-
0x0C, // end group (tag=1)
449-
];
450-
assert_eq!(groups::Test1::decode(data), Ok(msg1));
451-
452-
// repeated group
453-
let msg2_bytes: &[u8] = &[
454-
0x20, 0x40, 0x2B, 0x30, 0xFF, 0x01, 0x2C, 0x2B, 0x30, 0x01, 0x2C, 0x38, 0x64,
455-
];
456-
457-
let msg2 = groups::Test2 {
458-
i14: Some(64),
459-
groupb: vec![
460-
groups::test2::GroupB { i16: Some(255) },
461-
groups::test2::GroupB { i16: Some(1) },
462-
],
463-
i17: Some(100),
464-
};
465-
466-
let mut bytes = Vec::new();
467-
msg2.encode(&mut bytes).unwrap();
468-
assert_eq!(bytes.as_slice(), msg2_bytes);
469-
470-
assert_eq!(groups::Test2::decode(msg2_bytes), Ok(msg2));
471-
}
472-
473-
#[test]
474-
fn test_group_oneof() {
475-
let msg = groups::OneofGroup {
476-
i1: Some(42),
477-
field: Some(groups::oneof_group::Field::S2("foo".to_string())),
478-
};
479-
check_message(&msg);
480-
481-
let msg = groups::OneofGroup {
482-
i1: Some(42),
483-
field: Some(groups::oneof_group::Field::G(groups::oneof_group::G {
484-
i2: None,
485-
s1: "foo".to_string(),
486-
t1: None,
487-
})),
488-
};
489-
check_message(&msg);
490-
491-
let msg = groups::OneofGroup {
492-
i1: Some(42),
493-
field: Some(groups::oneof_group::Field::G(groups::oneof_group::G {
494-
i2: Some(99),
495-
s1: "foo".to_string(),
496-
t1: Some(groups::Test1 {
497-
groupa: Some(groups::test1::GroupA { i2: None }),
498-
}),
499-
})),
500-
};
501-
check_message(&msg);
502-
503-
check_message(&groups::OneofGroup::default());
504-
}
505-
506403
#[test]
507404
fn test_proto3_presence() {
508405
let msg = proto3::presence::A {

0 commit comments

Comments
 (0)