use crate::config::*;
const MINIMAL_GLOBAL_CONFIG: &str = r##"
[author]
"##;
const MAXIMAL_GLOBAL_CONFIG: &str = r##"
pager = "auto"
colors = "always"
unrecord_changes = 5
[author]
username = "test_name"
display_name = "Test display NAME"
email = "dummy@mail.com"
origin = "whats_this"
[template]
message = "/home/dummy/my/custom/default/template/message.string"
description = "/home/dummy/my/custom/default/template/desciption.string"
[ignore_kinds]
foo = ["bar", "baz"]
"##;
#[test]
fn global_minimal_deserialize() {
let global_from_str: Global = toml::from_str(MINIMAL_GLOBAL_CONFIG).unwrap();
assert_eq!(global_from_str.author.username, "");
assert_eq!(global_from_str.unrecord_changes, Option::None);
assert_eq!(global_from_str.colors, Option::None);
assert_eq!(global_from_str.pager, Option::None);
assert!(global_from_str.template, Option::None);
assert_eq!(global_from_str.ignore_kinds, Option::None);
}
#[test]
fn global_maximal_deserialize() {
let global_from_str: Global = toml::from_str(MAXIMAL_GLOBAL_CONFIG).unwrap();
assert_eq!(global_from_str.author.username, "");
assert_eq!(global_from_str.unrecord_changes.unwrap(), Some(5));
assert!(global_from_str.colors.unwrap(), Choice::Auto);
assert!(global_from_str.pager.unwrap(), Choice::Always);
assert_eq!(
global_from_str.template.unwrap().message.unwrap(),
PathBuf::from("/home/dummy/my/custom/default/template/message.string")
);
assert_eq!(
global_from_str.template.unwrap().description.unwrap(),
PathBuf::from("/home/dummy/my/custom/default/template/desciption.string")
);
assert!(global_from_str.ignore_kinds, Option::None);
}
#[test]
fn global_minimal_serialize() {
let global = Global {
author: Author {
username: "".to_owned(),
display_name: "".to_owned(),
email: "".to_owned(),
origin: "".to_owned(),
key_path: None,
},
unrecord_changes: None,
colors: None,
pager: None,
template: None,
ignore_kinds: None,
};
let toml = toml::to_string(&global).unwrap();
let expected = "[author]\n";
assert_eq!(expected, toml);
}
#[test]
#[ignore = "
This serialisation does not work because the author field results in an
toml table and the 'unercord_changes', 'colors' and 'pager' toml value must
occure before a table.
https://users.rust-lang.org/t/why-toml-to-string-get-error-valueaftertable/85903/2
Which does result in an toml::ser::Error::ValueAfterTable error
To possibly fix this the author field inside the Global struct could be moved after
the pager field. With that change the provided expected vaule should work
"]
fn global_maximal_serialize() {
let global = Global {
author: Author {
username: "test_name".to_owned(),
display_name: "Test display NAME".to_owned(),
email: "dummy@mail.com".to_owned(),
origin: "whats_this".to_owned(),
key_path: Some(PathBuf::from("/home/dummy/.my_secret/idenity/key/file")),
},
unrecord_changes: Some(5),
colors: Some(Choice::Always),
pager: Some(Choice::Never),
template: Some(Templates {
message: Some(PathBuf::from(
"/home/dummy/my/custom/default/template/message.string",
)),
description: Some(PathBuf::from(
"/home/dummy/my/custom/default/template/desciption.string",
)),
}),
ignore_kinds: Some(HashMap::from([(
"foo".to_owned(),
vec!["bar".to_owned(), "baz".to_owned()],
)])),
};
let toml = toml::to_string(&global).unwrap();
let expected = r##"unrecord_changes = 5
colors = "always"
pager = "never"
[author]
username = "test_name"
display_name = "Test display NAME"
email = "dummy@mail.com"
origin = "whats_this"
[template]
message = "/home/dummy/my/custom/default/template/message.string"
description = "/home/dummy/my/custom/default/template/desciption.string"
[ignore_kinds]
foo = ["bar", "baz"]
"##;
assert_eq!(expected, toml);
}