use std::path::PathBuf;
fn fixture_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/testdata/hocon/units-default")
}
fn load(name: &str) -> hocon::Config {
let path = fixture_dir().join(name);
hocon::parse_file(&path).unwrap_or_else(|e| panic!("failed to load {}: {}", name, e))
}
#[test]
fn ud01_duration_bare() {
let cfg = load("ud01-duration-bare.conf");
assert_eq!(
cfg.get_duration("t").unwrap(),
std::time::Duration::from_millis(500),
"ud01: bare '500' must default to 500 ms"
);
}
#[test]
fn ud02_duration_leading_ws() {
let cfg = load("ud02-duration-leading-ws.conf");
assert_eq!(
cfg.get_duration("t").unwrap(),
std::time::Duration::from_millis(500),
"ud02: leading-WS '\" 500\"' must default to 500 ms"
);
}
#[test]
fn ud03_duration_trailing_ws() {
let cfg = load("ud03-duration-trailing-ws.conf");
assert_eq!(
cfg.get_duration("t").unwrap(),
std::time::Duration::from_millis(500),
"ud03: trailing-WS '\"500 \"' must default to 500 ms"
);
}
#[test]
fn ud04_duration_both_ws() {
let cfg = load("ud04-duration-both-ws.conf");
assert_eq!(
cfg.get_duration("t").unwrap(),
std::time::Duration::from_millis(500),
"ud04: both-WS '\" 500 \"' must default to 500 ms"
);
}
#[test]
fn ud05_duration_fractional() {
let cfg = load("ud05-duration-fractional.conf");
assert_eq!(
cfg.get_duration("t").unwrap().as_nanos(),
500_500_000,
"ud05: fractional '500.5' must yield 500_500_000 nanos (Lightbend Double path)"
);
}
#[test]
fn ud06_duration_negative() {
let cfg = load("ud06-duration-negative.conf");
assert!(
cfg.get_duration("t").is_err(),
"ud06 (rs-specific): get_duration(\"-500\") must Err (std::time::Duration is unsigned)"
);
}
#[test]
fn ud07_duration_with_unit() {
let cfg = load("ud07-duration-with-unit.conf");
assert_eq!(
cfg.get_duration("t").unwrap(),
std::time::Duration::from_millis(500),
"ud07 (regression): explicit unit '500ms' must still parse correctly"
);
}
#[test]
fn ud08_duration_ws_between() {
let cfg = load("ud08-duration-ws-between.conf");
assert_eq!(
cfg.get_duration("t").unwrap(),
std::time::Duration::from_millis(500),
"ud08 (regression): WS between number and unit must parse correctly"
);
}
#[test]
fn up01_period_bare() {
let cfg = load("up01-period-bare.conf");
assert_eq!(
cfg.get_period("p").unwrap(),
hocon::Period::new(0, 0, 7),
"up01: bare '7' must default to 7 days"
);
}
#[test]
fn up02_period_leading_trailing_ws() {
let cfg = load("up02-period-leading-trailing-ws.conf");
assert_eq!(
cfg.get_period("p").unwrap(),
hocon::Period::new(0, 0, 7),
"up02: whitespace-padded '\" 7 \"' must default to 7 days"
);
}
#[test]
fn up03_period_fractional_rejected() {
let cfg = load("up03-period-fractional-rejected.conf");
assert!(
cfg.get_period("p").is_err(),
"up03: fractional '7.5' must Err (period is integer-only per Lightbend Integer.parseInt)"
);
}
#[test]
fn up04_period_negative() {
let cfg = load("up04-period-negative.conf");
assert_eq!(
cfg.get_period("p").unwrap(),
hocon::Period::new(0, 0, -7),
"up04: negative '-7' must yield Period {{ years:0, months:0, days:-7 }}"
);
}
#[test]
fn up05_period_with_unit() {
let cfg = load("up05-period-with-unit.conf");
assert_eq!(
cfg.get_period("p").unwrap(),
hocon::Period::new(0, 0, 49),
"up05 (regression): '7w' must yield Period {{ years:0, months:0, days:49 }}"
);
}
#[test]
fn ub01_bytes_bare() {
let cfg = load("ub01-bytes-bare.conf");
assert_eq!(
cfg.get_bytes("b").unwrap(),
1024,
"ub01: bare '1024' must default to 1024 bytes"
);
}
#[test]
fn ub02_bytes_leading_trailing_ws() {
let cfg = load("ub02-bytes-leading-trailing-ws.conf");
assert_eq!(
cfg.get_bytes("b").unwrap(),
1024,
"ub02: whitespace-padded '\" 1024 \"' must yield 1024 bytes"
);
}
#[test]
fn ub03_bytes_fractional_truncated() {
let cfg = load("ub03-bytes-fractional-truncated.conf");
assert_eq!(
cfg.get_bytes("b").unwrap(),
1024,
"ub03: fractional '1024.5' must truncate to 1024 bytes (not round to 1025)"
);
}
#[test]
fn ub04_bytes_negative_accessor_rejects() {
let cfg = load("ub04-bytes-negative-accessor-rejects.conf");
assert!(
cfg.get_bytes("b").is_err(),
"ub04: negative byte size '-1' must Err at accessor (positive-only invariant)"
);
}
#[test]
fn ub05_bytes_with_unit() {
let cfg = load("ub05-bytes-with-unit.conf");
assert_eq!(
cfg.get_bytes("b").unwrap(),
1_048_576,
"ub05: '1024K' must yield 1_048_576 bytes (K = 1024 binary, HOCON.md L1385 Lightbend ground truth)"
);
}
#[test]
fn ub06_bytes_empty_rejected() {
let cfg = load("ub06-bytes-empty-rejected.conf");
assert!(
cfg.get_bytes("b").is_err(),
"ub06: empty string must Err (no number present)"
);
}
#[test]
fn un01_empty_duration() {
let cfg = load("un01-empty-duration.conf");
assert!(
cfg.get_duration("t").is_err(),
"un01: empty string must Err for getDuration"
);
}
#[test]
fn un02_ws_only_duration() {
let cfg = load("un02-ws-only-duration.conf");
assert!(
cfg.get_duration("t").is_err(),
"un02: whitespace-only string must Err for getDuration"
);
}
#[test]
fn un03_unit_only_duration() {
let cfg = load("un03-unit-only-duration.conf");
assert!(
cfg.get_duration("t").is_err(),
"un03: unit-only 'ms' must Err for getDuration (number required)"
);
}