From 64459076ab4dafd186c41e869daf1a4125364d04 Mon Sep 17 00:00:00 2001 From: Casper Meijn Date: Fri, 7 Feb 2025 10:05:39 +0100 Subject: [PATCH] test(groups): Move tests to separate module (#1234) - Move related tests to separate module - Make config dependency explicit in `build.rs` --- tests/build.rs | 2 +- tests/src/groups.rs | 106 ++++++++++++++++++++++++++++++++++++++++++ tests/src/lib.rs | 109 ++------------------------------------------ 3 files changed, 110 insertions(+), 107 deletions(-) create mode 100644 tests/src/groups.rs diff --git a/tests/build.rs b/tests/build.rs index 23722fad4..f39cab1c4 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -87,7 +87,7 @@ fn main() { .compile_protos(&[src.join("enum_keyword_variant.proto")], includes) .unwrap(); - config + prost_build::Config::new() .compile_protos(&[src.join("groups.proto")], includes) .unwrap(); diff --git a/tests/src/groups.rs b/tests/src/groups.rs new file mode 100644 index 000000000..da6330dee --- /dev/null +++ b/tests/src/groups.rs @@ -0,0 +1,106 @@ +include!(concat!(env!("OUT_DIR"), "/groups.rs")); + +use alloc::boxed::Box; +use alloc::string::ToString; +use alloc::vec::Vec; +use prost::Message; + +use crate::check_message; + +#[test] +fn test_group() { + // optional group + let msg1_bytes = &[0x0B, 0x10, 0x20, 0x0C]; + + let msg1 = Test1 { + groupa: Some(test1::GroupA { i2: Some(32) }), + }; + + let mut bytes = Vec::new(); + msg1.encode(&mut bytes).unwrap(); + assert_eq!(&bytes, msg1_bytes); + + // skip group while decoding + let data: &[u8] = &[ + 0x0B, // start group (tag=1) + 0x30, 0x01, // unused int32 (tag=6) + 0x2B, 0x30, 0xFF, 0x01, 0x2C, // unused group (tag=5) + 0x10, 0x20, // int32 (tag=2) + 0x0C, // end group (tag=1) + ]; + assert_eq!(Test1::decode(data), Ok(msg1)); + + // repeated group + let msg2_bytes: &[u8] = &[ + 0x20, 0x40, 0x2B, 0x30, 0xFF, 0x01, 0x2C, 0x2B, 0x30, 0x01, 0x2C, 0x38, 0x64, + ]; + + let msg2 = Test2 { + i14: Some(64), + groupb: Vec::from([ + test2::GroupB { i16: Some(255) }, + test2::GroupB { i16: Some(1) }, + ]), + i17: Some(100), + }; + + let mut bytes = Vec::new(); + msg2.encode(&mut bytes).unwrap(); + assert_eq!(bytes.as_slice(), msg2_bytes); + + assert_eq!(Test2::decode(msg2_bytes), Ok(msg2)); +} + +#[test] +fn test_group_oneof() { + let msg = OneofGroup { + i1: Some(42), + field: Some(oneof_group::Field::S2("foo".to_string())), + }; + check_message(&msg); + + let msg = OneofGroup { + i1: Some(42), + field: Some(oneof_group::Field::G(oneof_group::G { + i2: None, + s1: "foo".to_string(), + t1: None, + })), + }; + check_message(&msg); + + let msg = OneofGroup { + i1: Some(42), + field: Some(oneof_group::Field::G(oneof_group::G { + i2: Some(99), + s1: "foo".to_string(), + t1: Some(Test1 { + groupa: Some(test1::GroupA { i2: None }), + }), + })), + }; + check_message(&msg); + + check_message(&OneofGroup::default()); +} + +#[test] +fn test_deep_nesting_group() { + fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> { + let mut a = NestedGroup2::default(); + for _ in 0..depth { + a = NestedGroup2 { + optionalgroup: Some(Box::new(nested_group2::OptionalGroup { + nested_group: Some(a), + })), + }; + } + + let mut buf = Vec::new(); + a.encode(&mut buf).unwrap(); + NestedGroup2::decode(buf.as_slice()).map(|_| ()) + } + + assert!(build_and_roundtrip(50).is_ok()); + assert!(build_and_roundtrip(51).is_err()); +} diff --git a/tests/src/lib.rs b/tests/src/lib.rs index 8b1df85fa..adf68a4a3 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -75,6 +75,9 @@ mod nesting; #[cfg(test)] mod recursive_oneof; +#[cfg(test)] +mod groups; + mod test_enum_named_option_value { include!(concat!(env!("OUT_DIR"), "/myenum.optionn.rs")); } @@ -106,10 +109,6 @@ pub mod oneof_attributes { include!(concat!(env!("OUT_DIR"), "/foo.custom.one_of_attrs.rs")); } -pub mod groups { - include!(concat!(env!("OUT_DIR"), "/groups.rs")); -} - pub mod proto3 { pub mod presence { include!(concat!(env!("OUT_DIR"), "/proto3.presence.rs")); @@ -263,8 +262,6 @@ mod tests { use alloc::collections::{BTreeMap, BTreeSet}; use alloc::vec; - #[cfg(not(feature = "std"))] - use alloc::{boxed::Box, string::ToString}; use super::*; @@ -387,29 +384,6 @@ mod tests { set2.insert(msg2.field); } - #[test] - fn test_deep_nesting_group() { - fn build_and_roundtrip(depth: usize) -> Result<(), prost::DecodeError> { - use crate::groups::{nested_group2::OptionalGroup, NestedGroup2}; - - let mut a = NestedGroup2::default(); - for _ in 0..depth { - a = NestedGroup2 { - optionalgroup: Some(Box::new(OptionalGroup { - nested_group: Some(a), - })), - }; - } - - let mut buf = Vec::new(); - a.encode(&mut buf).unwrap(); - NestedGroup2::decode(buf.as_slice()).map(|_| ()) - } - - assert!(build_and_roundtrip(50).is_ok()); - assert!(build_and_roundtrip(51).is_err()); - } - #[test] fn test_267_regression() { // Checks that skip_field will error appropriately when given a big stack of StartGroup @@ -426,83 +400,6 @@ mod tests { assert_eq!(msg.name, r#"["unknown"]"#); } - #[test] - fn test_group() { - // optional group - let msg1_bytes = &[0x0B, 0x10, 0x20, 0x0C]; - - let msg1 = groups::Test1 { - groupa: Some(groups::test1::GroupA { i2: Some(32) }), - }; - - let mut bytes = Vec::new(); - msg1.encode(&mut bytes).unwrap(); - assert_eq!(&bytes, msg1_bytes); - - // skip group while decoding - let data: &[u8] = &[ - 0x0B, // start group (tag=1) - 0x30, 0x01, // unused int32 (tag=6) - 0x2B, 0x30, 0xFF, 0x01, 0x2C, // unused group (tag=5) - 0x10, 0x20, // int32 (tag=2) - 0x0C, // end group (tag=1) - ]; - assert_eq!(groups::Test1::decode(data), Ok(msg1)); - - // repeated group - let msg2_bytes: &[u8] = &[ - 0x20, 0x40, 0x2B, 0x30, 0xFF, 0x01, 0x2C, 0x2B, 0x30, 0x01, 0x2C, 0x38, 0x64, - ]; - - let msg2 = groups::Test2 { - i14: Some(64), - groupb: vec![ - groups::test2::GroupB { i16: Some(255) }, - groups::test2::GroupB { i16: Some(1) }, - ], - i17: Some(100), - }; - - let mut bytes = Vec::new(); - msg2.encode(&mut bytes).unwrap(); - assert_eq!(bytes.as_slice(), msg2_bytes); - - assert_eq!(groups::Test2::decode(msg2_bytes), Ok(msg2)); - } - - #[test] - fn test_group_oneof() { - let msg = groups::OneofGroup { - i1: Some(42), - field: Some(groups::oneof_group::Field::S2("foo".to_string())), - }; - check_message(&msg); - - let msg = groups::OneofGroup { - i1: Some(42), - field: Some(groups::oneof_group::Field::G(groups::oneof_group::G { - i2: None, - s1: "foo".to_string(), - t1: None, - })), - }; - check_message(&msg); - - let msg = groups::OneofGroup { - i1: Some(42), - field: Some(groups::oneof_group::Field::G(groups::oneof_group::G { - i2: Some(99), - s1: "foo".to_string(), - t1: Some(groups::Test1 { - groupa: Some(groups::test1::GroupA { i2: None }), - }), - })), - }; - check_message(&msg); - - check_message(&groups::OneofGroup::default()); - } - #[test] fn test_proto3_presence() { let msg = proto3::presence::A {