Skip to content

Commit 9971428

Browse files
authored
test(nesting): Move tests to separate module (#1218)
- Move related tests to separate module - Make config dependency explicit in `build.rs`
1 parent f1b6bbf commit 9971428

File tree

3 files changed

+82
-82
lines changed

3 files changed

+82
-82
lines changed

tests/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ fn main() {
4646
.compile_protos(&[src.join("ident_conversion.proto")], includes)
4747
.unwrap();
4848

49-
config
49+
prost_build::Config::new()
50+
.btree_map(["."])
5051
.compile_protos(&[src.join("nesting.proto")], includes)
5152
.unwrap();
5253

tests/src/lib.rs

Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ pub mod custom_attributes;
6969
#[cfg(test)]
7070
mod default_enum_value;
7171

72+
#[cfg(test)]
73+
mod nesting;
74+
7275
mod test_enum_named_option_value {
7376
include!(concat!(env!("OUT_DIR"), "/myenum.optionn.rs"));
7477
}
@@ -91,10 +94,6 @@ pub mod foo {
9194
}
9295
}
9396

94-
pub mod nesting {
95-
include!(concat!(env!("OUT_DIR"), "/nesting.rs"));
96-
}
97-
9897
pub mod recursive_oneof {
9998
include!(concat!(env!("OUT_DIR"), "/recursive_oneof.rs"));
10099
}
@@ -266,7 +265,7 @@ mod tests {
266265
use alloc::collections::{BTreeMap, BTreeSet};
267266
use alloc::vec;
268267
#[cfg(not(feature = "std"))]
269-
use alloc::{borrow::ToOwned, boxed::Box, string::ToString};
268+
use alloc::{boxed::Box, string::ToString};
270269

271270
use super::*;
272271

@@ -389,40 +388,6 @@ mod tests {
389388
set2.insert(msg2.field);
390389
}
391390

392-
#[test]
393-
fn test_nesting() {
394-
use crate::nesting::{A, B};
395-
let _ = A {
396-
a: Some(Box::default()),
397-
repeated_a: Vec::<A>::new(),
398-
map_a: BTreeMap::<i32, A>::new(),
399-
b: Some(Box::default()),
400-
repeated_b: Vec::<B>::new(),
401-
map_b: BTreeMap::<i32, B>::new(),
402-
};
403-
}
404-
405-
#[test]
406-
fn test_deep_nesting() {
407-
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
408-
use crate::nesting::A;
409-
410-
let mut a = Box::<A>::default();
411-
for _ in 0..depth {
412-
let mut next = Box::<A>::default();
413-
next.a = Some(a);
414-
a = next;
415-
}
416-
417-
let mut buf = Vec::new();
418-
a.encode(&mut buf).unwrap();
419-
A::decode(buf.as_slice()).map(|_| ())
420-
}
421-
422-
assert!(build_and_roundtrip(100).is_ok());
423-
assert!(build_and_roundtrip(101).is_err());
424-
}
425-
426391
#[test]
427392
fn test_deep_nesting_oneof() {
428393
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
@@ -469,48 +434,6 @@ mod tests {
469434
assert!(build_and_roundtrip(51).is_err());
470435
}
471436

472-
#[test]
473-
fn test_deep_nesting_repeated() {
474-
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
475-
use crate::nesting::C;
476-
477-
let mut c = C::default();
478-
for _ in 0..depth {
479-
let mut next = C::default();
480-
next.r.push(c);
481-
c = next;
482-
}
483-
484-
let mut buf = Vec::new();
485-
c.encode(&mut buf).unwrap();
486-
C::decode(buf.as_slice()).map(|_| ())
487-
}
488-
489-
assert!(build_and_roundtrip(100).is_ok());
490-
assert!(build_and_roundtrip(101).is_err());
491-
}
492-
493-
#[test]
494-
fn test_deep_nesting_map() {
495-
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
496-
use crate::nesting::D;
497-
498-
let mut d = D::default();
499-
for _ in 0..depth {
500-
let mut next = D::default();
501-
next.m.insert("foo".to_owned(), d);
502-
d = next;
503-
}
504-
505-
let mut buf = Vec::new();
506-
d.encode(&mut buf).unwrap();
507-
D::decode(buf.as_slice()).map(|_| ())
508-
}
509-
510-
assert!(build_and_roundtrip(50).is_ok());
511-
assert!(build_and_roundtrip(51).is_err());
512-
}
513-
514437
#[test]
515438
fn test_recursive_oneof() {
516439
use crate::recursive_oneof::{a, A, B, C};

tests/src/nesting.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use alloc::borrow::ToOwned;
2+
use alloc::boxed::Box;
3+
use alloc::collections::BTreeMap;
4+
use alloc::vec::Vec;
5+
use prost::Message;
6+
7+
include!(concat!(env!("OUT_DIR"), "/nesting.rs"));
8+
9+
#[test]
10+
fn test_nesting() {
11+
let _ = A {
12+
a: Some(Box::default()),
13+
repeated_a: Vec::<A>::new(),
14+
map_a: BTreeMap::<i32, A>::new(),
15+
b: Some(Box::default()),
16+
repeated_b: Vec::<B>::new(),
17+
map_b: BTreeMap::<i32, B>::new(),
18+
};
19+
}
20+
21+
#[test]
22+
fn test_deep_nesting() {
23+
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
24+
let mut a = Box::<A>::default();
25+
for _ in 0..depth {
26+
let mut next = Box::<A>::default();
27+
next.a = Some(a);
28+
a = next;
29+
}
30+
31+
let mut buf = Vec::new();
32+
a.encode(&mut buf).unwrap();
33+
A::decode(buf.as_slice()).map(|_| ())
34+
}
35+
36+
assert!(build_and_roundtrip(100).is_ok());
37+
assert!(build_and_roundtrip(101).is_err());
38+
}
39+
40+
#[test]
41+
fn test_deep_nesting_repeated() {
42+
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
43+
let mut c = C::default();
44+
for _ in 0..depth {
45+
let mut next = C::default();
46+
next.r.push(c);
47+
c = next;
48+
}
49+
50+
let mut buf = Vec::new();
51+
c.encode(&mut buf).unwrap();
52+
C::decode(buf.as_slice()).map(|_| ())
53+
}
54+
55+
assert!(build_and_roundtrip(100).is_ok());
56+
assert!(build_and_roundtrip(101).is_err());
57+
}
58+
59+
#[test]
60+
fn test_deep_nesting_map() {
61+
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
62+
let mut d = D::default();
63+
for _ in 0..depth {
64+
let mut next = D::default();
65+
next.m.insert("foo".to_owned(), d);
66+
d = next;
67+
}
68+
69+
let mut buf = Vec::new();
70+
d.encode(&mut buf).unwrap();
71+
D::decode(buf.as_slice()).map(|_| ())
72+
}
73+
74+
assert!(build_and_roundtrip(50).is_ok());
75+
assert!(build_and_roundtrip(51).is_err());
76+
}

0 commit comments

Comments
 (0)