From 8bee33e2478e4254a18cabd1da18e2a2326ed5ab Mon Sep 17 00:00:00 2001 From: Sawyer McLane Date: Sun, 12 Jan 2025 06:38:51 -0700 Subject: [PATCH] Add unit test for scene serialization and improve device name handling in settings --- src/scenes.rs | 20 ++++++++++++++++++++ src/settings.rs | 10 +++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/scenes.rs b/src/scenes.rs index 8d99380..9e583db 100644 --- a/src/scenes.rs +++ b/src/scenes.rs @@ -106,4 +106,24 @@ mod test { ); assert_eq!(scene.device_color_pairs[0].1, HSBK32::default()); } + + #[test] + fn test_serde_scene() { + let source = 1234; + let target = 5678; + let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 56700); + let mut bulb = BulbInfo::new(source, target, addr); + bulb.update(addr); + let scene = Scene::new( + vec![(DeviceInfo::Bulb(Box::new(bulb.clone())), HSBK32::default())], + "Test Scene".to_string(), + ); + let serialized = serde_json::to_string(&scene).unwrap(); + let deserialized: Scene = serde_json::from_str(&serialized).unwrap(); + assert_eq!(scene.name, deserialized.name); + assert_eq!( + scene.device_color_pairs.len(), + deserialized.device_color_pairs.len() + ); + } } diff --git a/src/settings.rs b/src/settings.rs index ff152a5..96eb4bc 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,3 +1,5 @@ +use std::ffi::CString; + use eframe::egui::{self, Context}; use serde::{Deserialize, Serialize}; @@ -312,7 +314,13 @@ impl MantleApp { if ui .checkbox( &mut selected, - device.name.data.as_ref().unwrap().to_str().unwrap(), + device + .name + .data + .as_ref() + .unwrap_or(&CString::default()) + .to_str() + .unwrap_or("Unknown Device"), ) .on_hover_text("Select device for the scene") .changed()