From 73ab4718379c1d4ee2a9861c11adcce73dec4225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Mon, 30 Sep 2024 16:23:01 +0200 Subject: [PATCH] tests: more common --- miniconf/tests/common/mod.rs | 6 ++--- miniconf/tests/option.rs | 42 +++++++++--------------------- miniconf/tests/skipped.rs | 9 ------- miniconf/tests/structs.rs | 50 +++++++++++------------------------- 4 files changed, 30 insertions(+), 77 deletions(-) diff --git a/miniconf/tests/common/mod.rs b/miniconf/tests/common/mod.rs index d21cd196..05e92e59 100644 --- a/miniconf/tests/common/mod.rs +++ b/miniconf/tests/common/mod.rs @@ -1,4 +1,4 @@ -use miniconf::{JsonCoreSlashOwned, Path, TreeKey}; +use miniconf::{JsonCoreSlash, Path, TreeKey}; pub fn paths() -> Vec where @@ -15,9 +15,9 @@ where .collect() } -pub fn set_get(s: &mut M, path: &str, value: &[u8]) +pub fn set_get<'de, M, const Y: usize>(s: &mut M, path: &str, value: &'de [u8]) where - M: JsonCoreSlashOwned, + M: JsonCoreSlash<'de, Y>, { s.set_json(path, value).unwrap(); let mut buf = vec![0; value.len()]; diff --git a/miniconf/tests/option.rs b/miniconf/tests/option.rs index ed353b3a..a2b5a2be 100644 --- a/miniconf/tests/option.rs +++ b/miniconf/tests/option.rs @@ -1,4 +1,7 @@ -use miniconf::{Error, JsonCoreSlash, Path, Traversal, Tree, TreeKey}; +use miniconf::{Error, JsonCoreSlash, Traversal, Tree}; + +mod common; +use common::*; #[derive(PartialEq, Debug, Clone, Default, Tree)] struct Inner { @@ -11,26 +14,9 @@ struct Settings { value: Option, } -fn nodes, const Y: usize>(want: &[&str]) { - assert_eq!( - M::nodes::>() - .exact_size() - .map(|pn| { - let (p, n) = pn.unwrap(); - assert!(n.is_leaf()); - assert_eq!(p.chars().filter(|c| *c == p.separator()).count(), n.depth()); - p.into_inner() - }) - .collect::>(), - want - ); -} - #[test] fn just_option() { - let mut it = Option::::nodes::>().exact_size(); - assert_eq!(it.next().unwrap().unwrap().0.as_str(), ""); - assert_eq!(it.next(), None); + assert_eq!(paths::, 1>(), [""]); } #[test] @@ -58,21 +44,17 @@ fn option_get_set_none() { #[test] fn option_get_set_some() { let mut settings = Settings::default(); - let mut data = [0; 10]; // Check that if the option is Some, the value can be get or set. settings.value.replace(Inner { data: 5 }); - let len = settings.get_json("/value/data", &mut data).unwrap(); - assert_eq!(&data[..len], b"5"); - - settings.set_json("/value/data", b"7").unwrap(); + set_get(&mut settings, "/value/data", b"7"); assert_eq!(settings.value.unwrap().data, 7); } #[test] fn option_iterate_some_none() { - nodes::(&["/value/data"]); + assert_eq!(paths::(), ["/value/data"]); } #[test] @@ -81,15 +63,15 @@ fn option_test_normal_option() { struct S { data: Option, } - nodes::(&["/data"]); + assert_eq!(paths::(), ["/data"]); let mut s = S::default(); assert!(s.data.is_none()); - s.set_json("/data", b"7").unwrap(); + set_get(&mut s, "/data", b"7"); assert_eq!(s.data, Some(7)); - s.set_json("/data", b"null").unwrap(); + set_get(&mut s, "/data", b"null"); assert!(s.data.is_none()); } @@ -100,14 +82,14 @@ fn option_test_defer_option() { #[tree(depth = 1)] data: Option, } - nodes::(&["/data"]); + assert_eq!(paths::(), ["/data"]); let mut s = S::default(); assert!(s.data.is_none()); assert!(s.set_json("/data", b"7").is_err()); s.data = Some(0); - s.set_json("/data", b"7").unwrap(); + set_get(&mut s, "/data", b"7"); assert_eq!(s.data, Some(7)); assert!(s.set_json("/data", b"null").is_err()); diff --git a/miniconf/tests/skipped.rs b/miniconf/tests/skipped.rs index ed42f71b..cf34109a 100644 --- a/miniconf/tests/skipped.rs +++ b/miniconf/tests/skipped.rs @@ -31,15 +31,6 @@ fn path() { ); } -#[test] -fn skip_enum() { - #[allow(dead_code)] - #[derive(Tree)] - pub enum E { - A(i32, #[tree(skip)] i32), - } -} - #[test] fn skip_struct() { #[allow(dead_code)] diff --git a/miniconf/tests/structs.rs b/miniconf/tests/structs.rs index 0fb0c9c3..d8db57bc 100644 --- a/miniconf/tests/structs.rs +++ b/miniconf/tests/structs.rs @@ -1,7 +1,10 @@ use miniconf::{ - Deserialize, JsonCoreSlash, Path, Serialize, Tree, TreeDeserialize, TreeKey, TreeSerialize, + Deserialize, JsonCoreSlash, Serialize, Tree, TreeDeserialize, TreeKey, TreeSerialize, }; +mod common; +use common::*; + #[test] fn structs() { #[derive(Serialize, Deserialize, Tree, Default, PartialEq, Debug)] @@ -24,10 +27,10 @@ fn structs() { assert!(settings.set_json("/c/a", b"4").is_err()); // Inner settings can be updated atomically. - settings.set_json("/c", b"{\"a\": 5}").unwrap(); + set_get(&mut settings, "/c", b"{\"a\":5}"); // Deferred inner settings can be updated individually. - settings.set_json("/d/a", b"3").unwrap(); + set_get(&mut settings, "/d/a", b"3"); // It is not allowed to set a non-terminal node. assert!(settings.set_json("/d", b"{\"a\": 5").is_err()); @@ -41,43 +44,28 @@ fn structs() { assert_eq!(metadata.max_length("/"), "/d/a".len()); assert_eq!(metadata.count, 4); - assert_eq!( - Settings::nodes::>() - .exact_size() - .map(|p| p.unwrap().0.into_inner()) - .collect::>(), - vec!["/a", "/b", "/c", "/d/a"] - ); + assert_eq!(paths::(), ["/a", "/b", "/c", "/d/a"]); } #[test] fn empty_struct() { #[derive(Tree, Default)] struct Settings {} - assert!(Settings::nodes::>() - .exact_size() - .next() - .is_none()); + assert_eq!(paths::(), [""; 0]); } #[test] fn unit_struct() { #[derive(Tree, Default)] struct Settings; - assert!(Settings::nodes::>() - .exact_size() - .next() - .is_none()); + assert_eq!(paths::(), [""; 0]); } #[test] fn empty_tuple_struct() { #[derive(Tree, Default)] struct Settings(); - assert!(Settings::nodes::>() - .exact_size() - .next() - .is_none()); + assert_eq!(paths::(), [""; 0]); } #[test] @@ -88,7 +76,7 @@ fn borrowed() { a: &'a str, } let mut s = S { a: "foo" }; - s.set_json("/a", br#""bar""#).unwrap(); + set_get(&mut s, "/a", br#""bar""#); assert_eq!(s.a, "bar"); } @@ -99,20 +87,12 @@ fn tuple_struct() { let mut s = Settings::default(); - let mut buf = [0u8; 256]; - let len = s.get_json("/0", &mut buf).unwrap(); - assert_eq!(&buf[..len], b"0"); - - s.set_json("/1", b"3.0").unwrap(); + set_get(&mut s, "/0", br#"2"#); + assert_eq!(s.0, 2); + set_get(&mut s, "/1", br#"3.0"#); assert_eq!(s.1, 3.0); s.set_json("/2", b"3.0").unwrap_err(); s.set_json("/foo", b"3.0").unwrap_err(); - assert_eq!( - Settings::nodes::>() - .exact_size() - .map(|p| p.unwrap().0.into_inner()) - .collect::>(), - vec!["/0", "/1"] - ); + assert_eq!(paths::(), ["/0", "/1"]); }