Skip to content

Commit

Permalink
tests: more common
Browse files Browse the repository at this point in the history
  • Loading branch information
jordens committed Sep 30, 2024
1 parent eb4d328 commit 73ab471
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 77 deletions.
6 changes: 3 additions & 3 deletions miniconf/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use miniconf::{JsonCoreSlashOwned, Path, TreeKey};
use miniconf::{JsonCoreSlash, Path, TreeKey};

pub fn paths<M, const Y: usize>() -> Vec<String>
where
Expand All @@ -15,9 +15,9 @@ where
.collect()
}

pub fn set_get<M, const Y: usize>(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<Y>,
M: JsonCoreSlash<'de, Y>,
{
s.set_json(path, value).unwrap();
let mut buf = vec![0; value.len()];
Expand Down
42 changes: 12 additions & 30 deletions miniconf/tests/option.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -11,26 +14,9 @@ struct Settings {
value: Option<Inner>,
}

fn nodes<M: miniconf::TreeKey<Y>, const Y: usize>(want: &[&str]) {
assert_eq!(
M::nodes::<Path<String, '/'>>()
.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::<Vec<_>>(),
want
);
}

#[test]
fn just_option() {
let mut it = Option::<u32>::nodes::<Path<String, '/'>>().exact_size();
assert_eq!(it.next().unwrap().unwrap().0.as_str(), "");
assert_eq!(it.next(), None);
assert_eq!(paths::<Option<u32>, 1>(), [""]);
}

#[test]
Expand Down Expand Up @@ -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::<Settings, 3>(&["/value/data"]);
assert_eq!(paths::<Settings, 3>(), ["/value/data"]);
}

#[test]
Expand All @@ -81,15 +63,15 @@ fn option_test_normal_option() {
struct S {
data: Option<u32>,
}
nodes::<S, 1>(&["/data"]);
assert_eq!(paths::<S, 1>(), ["/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());
}

Expand All @@ -100,14 +82,14 @@ fn option_test_defer_option() {
#[tree(depth = 1)]
data: Option<u32>,
}
nodes::<S, 2>(&["/data"]);
assert_eq!(paths::<S, 2>(), ["/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());
Expand Down
9 changes: 0 additions & 9 deletions miniconf/tests/skipped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
50 changes: 15 additions & 35 deletions miniconf/tests/structs.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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());
Expand All @@ -41,43 +44,28 @@ fn structs() {
assert_eq!(metadata.max_length("/"), "/d/a".len());
assert_eq!(metadata.count, 4);

assert_eq!(
Settings::nodes::<Path<String, '/'>>()
.exact_size()
.map(|p| p.unwrap().0.into_inner())
.collect::<Vec<_>>(),
vec!["/a", "/b", "/c", "/d/a"]
);
assert_eq!(paths::<Settings, 2>(), ["/a", "/b", "/c", "/d/a"]);
}

#[test]
fn empty_struct() {
#[derive(Tree, Default)]
struct Settings {}
assert!(Settings::nodes::<Path<String, '/'>>()
.exact_size()
.next()
.is_none());
assert_eq!(paths::<Settings, 1>(), [""; 0]);
}

#[test]
fn unit_struct() {
#[derive(Tree, Default)]
struct Settings;
assert!(Settings::nodes::<Path<String, '/'>>()
.exact_size()
.next()
.is_none());
assert_eq!(paths::<Settings, 1>(), [""; 0]);
}

#[test]
fn empty_tuple_struct() {
#[derive(Tree, Default)]
struct Settings();
assert!(Settings::nodes::<Path<String, '/'>>()
.exact_size()
.next()
.is_none());
assert_eq!(paths::<Settings, 1>(), [""; 0]);
}

#[test]
Expand All @@ -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");
}

Expand All @@ -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::<Path<String, '/'>>()
.exact_size()
.map(|p| p.unwrap().0.into_inner())
.collect::<Vec<_>>(),
vec!["/0", "/1"]
);
assert_eq!(paths::<Settings, 1>(), ["/0", "/1"]);
}

0 comments on commit 73ab471

Please sign in to comment.