Skip to content

Commit

Permalink
put serde test under feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ordian committed Sep 14, 2023
1 parent 0e0c9af commit a3c4d5e
Showing 1 changed file with 45 additions and 37 deletions.
82 changes: 45 additions & 37 deletions bounded-collections/src/bounded_btree_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ where
#[cfg(test)]
mod test {
use super::*;
use crate::{alloc::string::ToString as _, ConstU32};
use crate::ConstU32;
use alloc::{vec, vec::Vec};
use codec::CompactLen;

Expand Down Expand Up @@ -586,48 +586,56 @@ mod test {
}
}

#[test]
fn test_serializer() {
let mut c = BoundedBTreeSet::<u32, ConstU32<6>>::new();
c.try_insert(0).unwrap();
c.try_insert(1).unwrap();
c.try_insert(2).unwrap();

assert_eq!(serde_json::json!(&c).to_string(), r#"[0,1,2]"#);
}
#[cfg(feature = "serde")]
mod serde {
use super::*;
use crate::alloc::string::ToString as _;

#[test]
fn test_deserializer() {
let c: Result<BoundedBTreeSet<u32, ConstU32<6>>, serde_json::error::Error> = serde_json::from_str(r#"[0,1,2]"#);
assert!(c.is_ok());
let c = c.unwrap();
#[test]
fn test_serializer() {
let mut c = BoundedBTreeSet::<u32, ConstU32<6>>::new();
c.try_insert(0).unwrap();
c.try_insert(1).unwrap();
c.try_insert(2).unwrap();

assert_eq!(c.len(), 3);
assert!(c.contains(&0));
assert!(c.contains(&1));
assert!(c.contains(&2));
}
assert_eq!(serde_json::json!(&c).to_string(), r#"[0,1,2]"#);
}

#[test]
fn test_deserializer_bound() {
let c: Result<BoundedBTreeSet<u32, ConstU32<3>>, serde_json::error::Error> = serde_json::from_str(r#"[0,1,2]"#);
assert!(c.is_ok());
let c = c.unwrap();
#[test]
fn test_deserializer() {
let c: Result<BoundedBTreeSet<u32, ConstU32<6>>, serde_json::error::Error> =
serde_json::from_str(r#"[0,1,2]"#);
assert!(c.is_ok());
let c = c.unwrap();

assert_eq!(c.len(), 3);
assert!(c.contains(&0));
assert!(c.contains(&1));
assert!(c.contains(&2));
}

assert_eq!(c.len(), 3);
assert!(c.contains(&0));
assert!(c.contains(&1));
assert!(c.contains(&2));
}
#[test]
fn test_deserializer_bound() {
let c: Result<BoundedBTreeSet<u32, ConstU32<3>>, serde_json::error::Error> =
serde_json::from_str(r#"[0,1,2]"#);
assert!(c.is_ok());
let c = c.unwrap();

assert_eq!(c.len(), 3);
assert!(c.contains(&0));
assert!(c.contains(&1));
assert!(c.contains(&2));
}

#[test]
fn test_deserializer_failed() {
let c: Result<BoundedBTreeSet<u32, ConstU32<4>>, serde_json::error::Error> =
serde_json::from_str(r#"[0,1,2,3,4]"#);
#[test]
fn test_deserializer_failed() {
let c: Result<BoundedBTreeSet<u32, ConstU32<4>>, serde_json::error::Error> =
serde_json::from_str(r#"[0,1,2,3,4]"#);

match c {
Err(msg) => assert_eq!(msg.to_string(), "out of bounds at line 1 column 11"),
_ => unreachable!("deserializer must raise error"),
match c {
Err(msg) => assert_eq!(msg.to_string(), "out of bounds at line 1 column 11"),
_ => unreachable!("deserializer must raise error"),
}
}
}
}

0 comments on commit a3c4d5e

Please sign in to comment.