From 0f5d3083d1ff8a5bf261b479881c71dfdd237e43 Mon Sep 17 00:00:00 2001 From: bbb651 Date: Tue, 21 Jan 2025 15:44:56 +0200 Subject: [PATCH] Simply tests for `one_or_many` de/serializer --- niri-ipc/src/utils.rs | 46 ++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/niri-ipc/src/utils.rs b/niri-ipc/src/utils.rs index 0ab98754a..4f3f1d427 100644 --- a/niri-ipc/src/utils.rs +++ b/niri-ipc/src/utils.rs @@ -30,55 +30,57 @@ pub(crate) mod one_or_many { #[cfg(test)] mod tests { + use std::fmt::Debug; + use serde_json::de::SliceRead; use serde_json::{Deserializer, Serializer, Value}; use super::*; + fn test_serialize(value: &Vec, expected: &str) { + let mut bytes = Vec::new(); + let mut serializer = Serializer::new(&mut bytes); + serialize(value, &mut serializer).expect("failed to serialize"); + assert_eq!(String::from_utf8_lossy(&bytes), expected); + } + + fn test_deserialize<'de, T>(value: &'de str, expected: &Vec) + where + T: Deserialize<'de> + Debug + PartialEq, + { + let mut deserailier = Deserializer::new(SliceRead::new(value.as_bytes())); + let result: Vec = deserialize(&mut deserailier).expect("failed to deserialize"); + assert_eq!(&result, expected); + } + #[test] fn serialize_one() { - let mut result = Vec::new(); - let mut serializer = Serializer::new(&mut result); - serialize(&vec![Value::Null], &mut serializer).expect("failed to serialize"); - assert_eq!(String::from_utf8_lossy(&result), "null"); + test_serialize(&vec![Value::Null], "null"); } #[test] fn deserialize_one() { - let mut deserailier = Deserializer::new(SliceRead::new("null".as_bytes())); - let result: Vec = deserialize(&mut deserailier).expect("failed to deserialize"); - assert_eq!(result, vec![Value::Null]); + test_deserialize("null", &vec![Value::Null]); } #[test] fn serialize_many() { - let mut result = Vec::new(); - let mut serializer = Serializer::new(&mut result); - serialize(&vec![Value::Null, Value::Null], &mut serializer) - .expect("failed to serialize"); - assert_eq!(String::from_utf8_lossy(&result), "[null,null]"); + test_serialize(&vec![Value::Null, Value::Null], "[null,null]"); } #[test] fn deserialize_many() { - let mut deserailier = Deserializer::new(SliceRead::new("[null,null]".as_bytes())); - let result: Vec = deserialize(&mut deserailier).expect("failed to deserialize"); - assert_eq!(result, vec![Value::Null, Value::Null]); + test_deserialize("[null,null]", &vec![Value::Null, Value::Null]); } #[test] fn serialize_none() { - let mut result = Vec::new(); - let mut serializer = Serializer::new(&mut result); - serialize(&Vec::::new(), &mut serializer).expect("failed to serialize"); - assert_eq!(String::from_utf8_lossy(&result), "[]"); + test_serialize(&Vec::::new(), "[]"); } #[test] fn deserialize_none() { - let mut deserailier = Deserializer::new(SliceRead::new("[]".as_bytes())); - let result: Vec = deserialize(&mut deserailier).expect("failed to deserialize"); - assert_eq!(result, Vec::::new()); + test_deserialize("[]", &Vec::::new()); } #[test]