From e7b7e88bc9ca09382983912c40984e90f5f02ba1 Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Fri, 25 Dec 2020 02:52:49 +0000 Subject: [PATCH] convert unrwap to expect and add testing --- taskchampion/src/utils.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/taskchampion/src/utils.rs b/taskchampion/src/utils.rs index aafe6f010..6747b6cbd 100644 --- a/taskchampion/src/utils.rs +++ b/taskchampion/src/utils.rs @@ -8,7 +8,7 @@ pub(crate) struct Key(uuid::Bytes); impl From<&[u8]> for Key { fn from(bytes: &[u8]) -> Key { - Key(bytes.try_into().unwrap()) + Key(bytes.try_into().expect("expected 16 bytes")) } } @@ -37,3 +37,24 @@ impl AsRef<[u8]> for Key { &self.0[..] } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_from_bytes() { + let k: Key = (&[1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16][..]).into(); + let u: Uuid = k.into(); + assert_eq!( + u, + Uuid::parse_str("01020304-0506-0708-090a-0b0c0d0e0f10").unwrap() + ); + } + + #[test] + #[should_panic] + fn test_from_bytes_bad_len() { + let _: Key = (&[1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11][..]).into(); + } +}