From 3c64bb27dece286bb83aea5a1ec44d40b9a1b4c1 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Tue, 11 Nov 2025 17:18:03 -0700 Subject: [PATCH] some tests --- pttodoest/src/main.rs | 52 +++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/pttodoest/src/main.rs b/pttodoest/src/main.rs index f046551..8004353 100755 --- a/pttodoest/src/main.rs +++ b/pttodoest/src/main.rs @@ -159,15 +159,18 @@ impl File { pub fn persist_stage(&self) -> Result<(), String> { let persisted = self.events()?.snapshot()?; - let snapshot = serde_json::to_string(&persisted).unwrap(); - let snapshot = snapshot.as_str(); - let snapshot: serde_json::Value = serde_json::from_str(&snapshot).unwrap(); + let persisted = serde_json::to_string(&persisted).unwrap(); + let persisted = persisted.as_str(); + let persisted: serde_json::Value = serde_json::from_str(&persisted).unwrap(); + eprintln!("PERSISTED | {:?}", &persisted); let stage = self.stage()?; let stage = serde_json::to_string(&stage).unwrap(); let stage: serde_json::Value = serde_json::from_str(stage.as_str()).unwrap(); + eprintln!(" STAGE | {:?}", &stage); - let patches = json_patch::diff(&snapshot, &stage); + let patches = json_patch::diff(&persisted, &stage); + eprintln!(" PATCHES | {:?}", &patches); let deltas: Vec = patches .iter() .map(|patch| patch.clone()) @@ -234,7 +237,7 @@ mod test_file { use super::*; #[test] - fn test_file_empty() { + fn test_file_empty_empty() { tests::with_dir(|d| { tests::write_file(&d, "plain", "[]"); @@ -244,17 +247,38 @@ mod test_file { assert_eq!(0, f.stage().unwrap().len()); tests::file_contains(&d, "plain", "[]"); - f.stage_persisted().unwrap(); - assert_eq!(0, f.events().unwrap().0.len()); - assert_eq!(0, f.stage().unwrap().len()); - tests::file_contains(&d, "plain", "[]"); - f.persist_stage().unwrap(); assert_eq!(0, f.events().unwrap().0.len()); assert_eq!(0, f.stage().unwrap().len()); tests::file_contains(&d, "plain", "[]"); - assert!(false, "not impl"); + f.stage_persisted().unwrap(); + assert_eq!(0, f.events().unwrap().0.len()); + assert_eq!(0, f.stage().unwrap().len()); + tests::file_contains(&d, "plain", "[]"); + }); + } + + #[test] + fn test_file_empty_stage() { + tests::with_dir(|d| { + tests::write_file(&d, "plain", "[hello, world]"); + + let f = File::new(&d.path().join("plain").to_str().unwrap().to_string()); + + assert_eq!(0, f.events().unwrap().0.len()); + assert_eq!(2, f.stage().unwrap().len()); + tests::file_contains(&d, "plain", "[hello, world]"); + + f.persist_stage().unwrap(); + assert_eq!(2, f.events().unwrap().0.len()); + assert_eq!(2, f.stage().unwrap().len()); + tests::file_contains(&d, "plain", "[hello, world]"); + + f.stage_persisted().unwrap(); + assert_eq!(2, f.events().unwrap().0.len()); + assert_eq!(2, f.stage().unwrap().len()); + tests::file_contains(&d, "plain", "- hello\n- world"); }); } } @@ -463,8 +487,8 @@ mod tests { } pub fn file_contains(d: &tempdir::TempDir, fname: &str, content: &str) { - let mut f = std::fs::File::create(d.path().join(&fname)).unwrap(); - writeln!(f, "{}", &content).unwrap(); - f.sync_all().unwrap(); + let p = d.path().join(&fname); + let file_content = std::fs::read_to_string(p).unwrap(); + assert!(file_content.contains(content)); } }