From e9a18e747685f14edd78d7de15057339e1010535 Mon Sep 17 00:00:00 2001 From: "John T. Wodder II" Date: Mon, 30 Oct 2023 09:24:34 -0400 Subject: [PATCH] A couple more tests --- tests/deser.rs | 25 ++++++++++++++++++++++++- tests/formats/json.rs | 24 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/tests/deser.rs b/tests/deser.rs index f658797..64a010f 100644 --- a/tests/deser.rs +++ b/tests/deser.rs @@ -1,7 +1,8 @@ -#![allow(unused)] mod formats; +use cfgfifo::*; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; +use tempfile::Builder; #[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] struct Config { @@ -133,6 +134,7 @@ struct RonConfig { } impl RonConfig { + #[allow(unused)] fn get() -> RonConfig { RonConfig { primitives: Primitives::get(), @@ -150,6 +152,7 @@ struct RonEnums { } impl RonEnums { + #[allow(unused)] fn get() -> RonEnums { RonEnums { color: Color::Green, @@ -174,3 +177,23 @@ enum RonMessage { value: String, }, } + +#[test] +fn load_unknown() { + let file = Builder::new().suffix(".unk").tempfile().unwrap(); + let r = load::(file); + let Err(LoadError::Identify(e)) = r else { + panic!("load() did not fail with Identify error: {r:?}"); + }; + assert_eq!(e, IdentifyError::Unknown(String::from("unk"))); +} + +#[test] +fn dump_unknown() { + let file = Builder::new().suffix(".unk").tempfile().unwrap(); + let r = dump(&file, &Config::get()); + let Err(DumpError::Identify(e)) = r else { + panic!("dump() did not fail with Identify error: {r:?}"); + }; + assert_eq!(e, IdentifyError::Unknown(String::from("unk"))); +} diff --git a/tests/formats/json.rs b/tests/formats/json.rs index 94efc69..018a406 100644 --- a/tests/formats/json.rs +++ b/tests/formats/json.rs @@ -110,3 +110,27 @@ fn dump_to_file() { assert_eq!(s, format!("{JSON}\n")); assert!(s.ends_with("}\n")); } + +#[test] +fn fallback_load() { + let mut file = Builder::new().suffix(".unk").tempfile().unwrap(); + writeln!(file, "{JSON}").unwrap(); + file.flush().unwrap(); + file.rewind().unwrap(); + let cfg = Cfgfifo::new().fallback(Some(Format::Json)); + let r = cfg.load::(file); + assert_eq!(r.unwrap(), Config::get()); +} + +#[test] +fn fallback_dump() { + let mut file = Builder::new().suffix(".unk").tempfile().unwrap(); + let cfg = Cfgfifo::new().fallback(Some(Format::Json)); + let r = cfg.dump(&file, &Config::get()); + assert!(r.is_ok()); + file.flush().unwrap(); + file.rewind().unwrap(); + let s = read_to_string(file).unwrap(); + assert_eq!(s, format!("{JSON}\n")); + assert!(s.ends_with("}\n")); +}