Skip to content

Commit

Permalink
test(recursive_oneof): Move tests to separate module (#1225)
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 Feb 3, 2025
1 parent a8208f6 commit ac98f0f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 40 deletions.
2 changes: 1 addition & 1 deletion tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn main() {
.compile_protos(&[src.join("nesting.proto")], includes)
.unwrap();

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

Expand Down
42 changes: 3 additions & 39 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ mod default_enum_value;
#[cfg(test)]
mod nesting;

#[cfg(test)]
mod recursive_oneof;

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

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

/// Also for testing custom attributes, but on oneofs.
///
/// Unfortunately, an OneOf field generates a companion module in the .rs file. There's no
Expand Down Expand Up @@ -388,29 +387,6 @@ mod tests {
set2.insert(msg2.field);
}

#[test]
fn test_deep_nesting_oneof() {
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
use crate::recursive_oneof::{a, A, C};

let mut a = Box::new(A {
kind: Some(a::Kind::C(C {})),
});
for _ in 0..depth {
a = Box::new(A {
kind: Some(a::Kind::A(a)),
});
}

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

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

#[test]
fn test_deep_nesting_group() {
fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> {
Expand All @@ -434,18 +410,6 @@ mod tests {
assert!(build_and_roundtrip(51).is_err());
}

#[test]
fn test_recursive_oneof() {
use crate::recursive_oneof::{a, A, B, C};
let _ = A {
kind: Some(a::Kind::B(Box::new(B {
a: Some(Box::new(A {
kind: Some(a::Kind::C(C {})),
})),
}))),
};
}

#[test]
fn test_267_regression() {
// Checks that skip_field will error appropriately when given a big stack of StartGroup
Expand Down
37 changes: 37 additions & 0 deletions tests/src/recursive_oneof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use alloc::boxed::Box;
use alloc::vec::Vec;
use prost::Message;

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

#[test]
fn test_recursive_oneof() {
let _ = A {
kind: Some(a::Kind::B(Box::new(B {
a: Some(Box::new(A {
kind: Some(a::Kind::C(C {})),
})),
}))),
};
}

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

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

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

0 comments on commit ac98f0f

Please sign in to comment.