Skip to content

Commit

Permalink
test(nesting): Move tests to separate module (#1218)
Browse files Browse the repository at this point in the history
- Move related tests to separate module
- Make config dependency explicit in `build.rs`
  • Loading branch information
caspermeijn authored Jan 8, 2025
1 parent f1b6bbf commit 9971428
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 82 deletions.
3 changes: 2 additions & 1 deletion tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ fn main() {
.compile_protos(&[src.join("ident_conversion.proto")], includes)
.unwrap();

config
prost_build::Config::new()
.btree_map(["."])
.compile_protos(&[src.join("nesting.proto")], includes)
.unwrap();

Expand Down
85 changes: 4 additions & 81 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pub mod custom_attributes;
#[cfg(test)]
mod default_enum_value;

#[cfg(test)]
mod nesting;

mod test_enum_named_option_value {
include!(concat!(env!("OUT_DIR"), "/myenum.optionn.rs"));
}
Expand All @@ -91,10 +94,6 @@ pub mod foo {
}
}

pub mod nesting {
include!(concat!(env!("OUT_DIR"), "/nesting.rs"));
}

pub mod recursive_oneof {
include!(concat!(env!("OUT_DIR"), "/recursive_oneof.rs"));
}
Expand Down Expand Up @@ -266,7 +265,7 @@ mod tests {
use alloc::collections::{BTreeMap, BTreeSet};
use alloc::vec;
#[cfg(not(feature = "std"))]
use alloc::{borrow::ToOwned, boxed::Box, string::ToString};
use alloc::{boxed::Box, string::ToString};

use super::*;

Expand Down Expand Up @@ -389,40 +388,6 @@ mod tests {
set2.insert(msg2.field);
}

#[test]
fn test_nesting() {
use crate::nesting::{A, B};
let _ = A {
a: Some(Box::default()),
repeated_a: Vec::<A>::new(),
map_a: BTreeMap::<i32, A>::new(),
b: Some(Box::default()),
repeated_b: Vec::<B>::new(),
map_b: BTreeMap::<i32, B>::new(),
};
}

#[test]
fn test_deep_nesting() {
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
use crate::nesting::A;

let mut a = Box::<A>::default();
for _ in 0..depth {
let mut next = Box::<A>::default();
next.a = Some(a);
a = next;
}

let mut buf = Vec::new();
a.encode(&mut buf).unwrap();
A::decode(buf.as_slice()).map(|_| ())
}

assert!(build_and_roundtrip(100).is_ok());
assert!(build_and_roundtrip(101).is_err());
}

#[test]
fn test_deep_nesting_oneof() {
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
Expand Down Expand Up @@ -469,48 +434,6 @@ mod tests {
assert!(build_and_roundtrip(51).is_err());
}

#[test]
fn test_deep_nesting_repeated() {
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
use crate::nesting::C;

let mut c = C::default();
for _ in 0..depth {
let mut next = C::default();
next.r.push(c);
c = next;
}

let mut buf = Vec::new();
c.encode(&mut buf).unwrap();
C::decode(buf.as_slice()).map(|_| ())
}

assert!(build_and_roundtrip(100).is_ok());
assert!(build_and_roundtrip(101).is_err());
}

#[test]
fn test_deep_nesting_map() {
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
use crate::nesting::D;

let mut d = D::default();
for _ in 0..depth {
let mut next = D::default();
next.m.insert("foo".to_owned(), d);
d = next;
}

let mut buf = Vec::new();
d.encode(&mut buf).unwrap();
D::decode(buf.as_slice()).map(|_| ())
}

assert!(build_and_roundtrip(50).is_ok());
assert!(build_and_roundtrip(51).is_err());
}

#[test]
fn test_recursive_oneof() {
use crate::recursive_oneof::{a, A, B, C};
Expand Down
76 changes: 76 additions & 0 deletions tests/src/nesting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use prost::Message;

include!(concat!(env!("OUT_DIR"), "/nesting.rs"));

#[test]
fn test_nesting() {
let _ = A {
a: Some(Box::default()),
repeated_a: Vec::<A>::new(),
map_a: BTreeMap::<i32, A>::new(),
b: Some(Box::default()),
repeated_b: Vec::<B>::new(),
map_b: BTreeMap::<i32, B>::new(),
};
}

#[test]
fn test_deep_nesting() {
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
let mut a = Box::<A>::default();
for _ in 0..depth {
let mut next = Box::<A>::default();
next.a = Some(a);
a = next;
}

let mut buf = Vec::new();
a.encode(&mut buf).unwrap();
A::decode(buf.as_slice()).map(|_| ())
}

assert!(build_and_roundtrip(100).is_ok());
assert!(build_and_roundtrip(101).is_err());
}

#[test]
fn test_deep_nesting_repeated() {
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
let mut c = C::default();
for _ in 0..depth {
let mut next = C::default();
next.r.push(c);
c = next;
}

let mut buf = Vec::new();
c.encode(&mut buf).unwrap();
C::decode(buf.as_slice()).map(|_| ())
}

assert!(build_and_roundtrip(100).is_ok());
assert!(build_and_roundtrip(101).is_err());
}

#[test]
fn test_deep_nesting_map() {
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
let mut d = D::default();
for _ in 0..depth {
let mut next = D::default();
next.m.insert("foo".to_owned(), d);
d = next;
}

let mut buf = Vec::new();
d.encode(&mut buf).unwrap();
D::decode(buf.as_slice()).map(|_| ())
}

assert!(build_and_roundtrip(50).is_ok());
assert!(build_and_roundtrip(51).is_err());
}

0 comments on commit 9971428

Please sign in to comment.