From 9971428c08606a24ba6daa8bc58a470fca583a26 Mon Sep 17 00:00:00 2001
From: Casper Meijn <casper@meijn.net>
Date: Wed, 8 Jan 2025 10:14:30 +0100
Subject: [PATCH] test(nesting): Move tests to separate module (#1218)

- Move related tests to separate module
- Make config dependency explicit in `build.rs`
---
 tests/build.rs       |  3 +-
 tests/src/lib.rs     | 85 +++-----------------------------------------
 tests/src/nesting.rs | 76 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 82 insertions(+), 82 deletions(-)
 create mode 100644 tests/src/nesting.rs

diff --git a/tests/build.rs b/tests/build.rs
index 6bcd68862..06c9e0472 100644
--- a/tests/build.rs
+++ b/tests/build.rs
@@ -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();
 
diff --git a/tests/src/lib.rs b/tests/src/lib.rs
index 11c549423..6cd053498 100644
--- a/tests/src/lib.rs
+++ b/tests/src/lib.rs
@@ -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"));
 }
@@ -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"));
 }
@@ -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::*;
 
@@ -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> {
@@ -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};
diff --git a/tests/src/nesting.rs b/tests/src/nesting.rs
new file mode 100644
index 000000000..bab7ed0dd
--- /dev/null
+++ b/tests/src/nesting.rs
@@ -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());
+}