Skip to content

Commit

Permalink
A couple more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Oct 30, 2023
1 parent fc60b57 commit e9a18e7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
25 changes: 24 additions & 1 deletion tests/deser.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -133,6 +134,7 @@ struct RonConfig {
}

impl RonConfig {
#[allow(unused)]
fn get() -> RonConfig {
RonConfig {
primitives: Primitives::get(),
Expand All @@ -150,6 +152,7 @@ struct RonEnums {
}

impl RonEnums {
#[allow(unused)]
fn get() -> RonEnums {
RonEnums {
color: Color::Green,
Expand All @@ -174,3 +177,23 @@ enum RonMessage {
value: String,
},
}

#[test]
fn load_unknown() {
let file = Builder::new().suffix(".unk").tempfile().unwrap();
let r = load::<Config, _>(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")));
}
24 changes: 24 additions & 0 deletions tests/formats/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Config, _>(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"));
}

0 comments on commit e9a18e7

Please sign in to comment.