Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(nesting): Move tests to separate module #1218

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
Loading