use serde_json::json;
use super::*;
#[track_caller]
fn ok_value(raw: &str) -> Json {
value_of(raw).unwrap()
}
#[test]
fn a_value_is_toml_and_other_text_is_the_string_as_typed() {
assert_eq!(ok_value("5"), json!(5));
assert_eq!(ok_value("-2"), json!(-2));
assert_eq!(ok_value("1.5"), json!(1.5));
assert_eq!(ok_value("true"), json!(true));
assert_eq!(ok_value("false"), json!(false));
assert_eq!(ok_value("[\"a\", \"b\"]"), json!(["a", "b"]));
assert_eq!(ok_value("{ keep = 1 }"), json!({ "keep": 1 }));
assert_eq!(ok_value("\"5\""), json!("5"));
assert_eq!(ok_value("3s"), json!("3s"));
assert_eq!(ok_value("auto"), json!("auto"));
assert_eq!(ok_value("keep = 1"), json!("keep = 1"));
assert_eq!(ok_value(""), json!(""));
assert_eq!(
ok_value("{ git = { merging = \"ff\" } }"),
json!({ "git": { "merging": "ff" } })
);
}
#[test]
fn a_toml_datetime_is_the_text_it_spells() {
assert_eq!(
ok_value("1979-05-27T07:32:00Z"),
json!("1979-05-27T07:32:00Z")
);
assert_eq!(ok_value("1979-05-27"), json!("1979-05-27"));
assert_eq!(ok_value("07:32:00"), json!("07:32:00"));
let section = "since = 1979-05-27T07:32:00Z\nkeep = 1\n";
let plan = plan_section(
section.parse::<toml_edit::DocumentMut>().unwrap(),
"kelpie",
&["keep".to_string()],
&json!(2),
)
.unwrap();
assert_eq!(plan.table["since"], json!("1979-05-27T07:32:00Z"));
let plan = plan_section(
section.parse::<toml_edit::DocumentMut>().unwrap(),
"kelpie",
&[],
&json!({ "since": "1979-05-27T07:32:00Z", "keep": 1 }),
)
.unwrap();
let Write::DogSection { toml, .. } = &plan.write else {
unreachable!()
};
assert!(toml.contains("since = \"1979-05-27T07:32:00Z\""), "{toml}");
assert!(!toml.contains("null"), "{toml}");
}
#[test]
fn a_non_finite_number_is_refused_as_a_value() {
for raw in ["nan", "inf", "+inf", "-inf"] {
let error = value_of(raw).expect_err("`{raw}` has no JSON cell");
assert_eq!(error.code(), ExitCode::Usage);
assert!(
error.to_string().contains("no place in the JSON"),
"{error}"
);
assert!(error.to_string().contains("null"), "{error}");
}
assert_eq!(ok_value("\"nan\""), json!("nan"));
assert_eq!(ok_value("1.5"), json!(1.5));
assert_eq!(ok_value("6.02e23"), json!(6.02e23));
assert_eq!(ok_value("nan-ish"), json!("nan-ish"), "text, not a float");
}
#[test]
fn a_non_finite_number_inside_a_value_is_named_where_it_sits() {
let error = value_of("{ keep = nan }").expect_err("a nan inside a table is a nan");
assert!(error.to_string().contains("under `keep`"), "{error}");
let error = value_of("{ hours = { start = inf } }").expect_err("two levels down");
assert!(error.to_string().contains("under `hours.start`"), "{error}");
let error = value_of("[1, -inf]").expect_err("inside an array");
assert!(error.to_string().contains("under `[1]`"), "{error}");
}
#[test]
fn a_nan_a_section_already_holds_reads_as_the_text_it_spells() {
let section = "keep = nan\nmerge = \"ask\"\n";
let plan = plan_section(
section.parse::<toml_edit::DocumentMut>().unwrap(),
"kelpie",
&["merge".to_string()],
&json!("auto"),
)
.unwrap();
assert_eq!(plan.table["keep"], json!("nan"));
assert_eq!(plan.changes.len(), 1, "only the key named changed");
let Write::DogSection { toml, .. } = &plan.write else {
unreachable!()
};
assert!(
toml.contains("keep = nan"),
"{toml}: the cell is left alone"
);
}