use std::path::PathBuf;
#[test]
fn issue68_adjacent_dots_in_key_is_rejected() {
assert!(hocon::parse("a..b: 3").is_err());
}
#[test]
fn issue68_leading_dot_in_key_is_rejected() {
assert!(hocon::parse(".a: 3").is_err());
}
#[test]
fn issue68_triple_dots_in_key_is_rejected() {
assert!(hocon::parse("a...c: 4").is_err());
}
#[test]
fn issue68_adjacent_dots_nested_in_object_is_rejected() {
assert!(hocon::parse("o { a..b: 3 }").is_err());
}
#[test]
fn issue68_triple_dots_before_quoted_empty_is_rejected() {
assert!(hocon::parse("a...c.\"\": 4").is_err());
}
#[test]
fn issue68_quoted_empty_segment_still_parses() {
let cfg = hocon::parse("a.\"\".b: 3").unwrap();
let a = match cfg.get("a") {
Some(hocon::HoconValue::Object(m)) => m,
other => panic!("a must be an object, got {:?}", other),
};
let empty = match a.get("") {
Some(hocon::HoconValue::Object(m)) => m,
other => panic!(
"the empty-string element must be an object, got {:?}",
other
),
};
match empty.get("b") {
Some(hocon::HoconValue::Scalar(sv)) => assert_eq!(sv.raw, "3"),
other => panic!("a.\"\".b must be 3, got {:?}", other),
}
}
#[test]
fn issue68_trailing_dot_in_key_still_rejected() {
assert!(hocon::parse("a.: 3").is_err());
}
#[test]
fn issue68_substitution_path_forms_still_rejected() {
for input in ["y = ${?a..b}", "y = ${?.a}", "y = ${?a.}", "y = ${a..b}"] {
assert!(
hocon::parse(input).is_err(),
"{} must be rejected (empty segment in substitution path)",
input
);
}
}
#[test]
fn issue68_path_whitespace_forms_are_unaffected() {
let cfg = hocon::parse("a . b = 1").unwrap();
assert_eq!(cfg.get_config("a ").unwrap().get_i64(" b").unwrap(), 1);
let cfg = hocon::parse("a. .b = 1").unwrap();
assert_eq!(
cfg.get_config("a")
.unwrap()
.get_config(" ")
.unwrap()
.get_i64("b")
.unwrap(),
1
);
}
#[test]
fn issue68_ordinary_dotted_keys_still_parse() {
let cfg = hocon::parse("a.b: 3\nc.d.e: 4").unwrap();
assert_eq!(cfg.get_i64("a.b").unwrap(), 3);
assert_eq!(cfg.get_i64("c.d.e").unwrap(), 4);
}
#[test]
fn issue68_backtick_in_value_is_rejected() {
assert!(hocon::parse("a = `t`").is_err());
}
#[test]
fn issue68_backtick_in_key_is_rejected() {
assert!(hocon::parse("`k` = 1").is_err());
}
#[test]
fn issue68_backtick_mid_token_is_rejected() {
assert!(hocon::parse("a = x`y").is_err());
}
#[test]
fn issue68_backtick_inside_quotes_still_parses() {
let cfg = hocon::parse("a = \"x`y\"").unwrap();
assert_eq!(cfg.get_string("a").unwrap(), "x`y");
}
#[test]
fn issue68_parens_in_unquoted_strings_still_allowed() {
let cfg = hocon::parse("a = foo(bar)").unwrap();
assert_eq!(cfg.get_string("a").unwrap(), "foo(bar)");
}
fn fixture_dir(group: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(format!("tests/testdata/hocon/{}", group))
}
fn expected_dir(group: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(format!("tests/testdata/expected/{}", group))
}
fn assert_fixture_group_parity(group: &str) {
let dir = fixture_dir(group);
let expected = expected_dir(group);
let mut stems: Vec<String> = std::fs::read_dir(&dir)
.unwrap_or_else(|e| panic!("{} missing ({}); run `make testdata`", dir.display(), e))
.filter_map(|e| {
let p = e.ok()?.path();
(p.extension()? == "conf").then(|| p.file_stem()?.to_str().map(str::to_string))?
})
.collect();
stems.sort();
assert!(!stems.is_empty(), "no fixtures in {}", dir.display());
for stem in stems {
let path = dir.join(format!("{}.conf", stem));
let must_error = expected.join(format!("{}.error", stem)).exists();
let must_succeed = expected.join(format!("{}-expected.json", stem)).exists();
assert!(
must_error ^ must_succeed,
"{}: expected exactly one sidecar (.error xor -expected.json)",
stem
);
let result = hocon::parse_file(&path);
if must_error {
assert!(
result.is_err(),
"{}: must be rejected (has .error sidecar), got {:?}",
stem,
result.map(|c| c.keys().into_iter().map(str::to_string).collect::<Vec<_>>())
);
} else if let Err(e) = result {
panic!(
"{}: must parse (has -expected.json sidecar), got {}",
stem, e
);
}
}
}
#[test]
fn issue68_path_empty_segment_fixtures() {
assert_fixture_group_parity("path-empty-segment");
}
#[test]
fn issue68_unquoted_forbidden_fixtures() {
assert_fixture_group_parity("unquoted-forbidden");
}