#[cfg(feature = "serde")]
mod from_map_tests {
use hocon::from_map;
use serde_json::json;
#[test]
fn scalar_types_round_trip() {
let values = json!({
"flag": true,
"count": 42,
"ratio": 2.72,
"label": "hello",
"nothing": null
});
let map = values.as_object().unwrap().clone();
let c = from_map(map, None).expect("from_map must succeed");
assert!(c.is_resolved(), "from_map must produce a resolved Config");
assert!(c.get_bool("flag").unwrap());
assert_eq!(c.get_i64("count").unwrap(), 42);
assert!((c.get_f64("ratio").unwrap() - 2.72).abs() < 1e-10);
assert_eq!(c.get_string("label").unwrap(), "hello");
assert!(
c.get_string("nothing").is_err(),
"null scalar -> get_string must error (S17.6)"
);
match c.get("nothing") {
Some(hocon::HoconValue::Scalar(ref s)) => assert_eq!(
s.value_type,
hocon::ScalarType::Null,
"null scalar must round-trip as Scalar(Null)"
),
other => panic!("expected Scalar(Null), got {:?}", other),
}
}
#[test]
fn nested_object() {
let values = json!({"nested": {"inner": "deep"}});
let map = values.as_object().unwrap().clone();
let c = from_map(map, None).expect("from_map must succeed");
assert_eq!(c.get_string("nested.inner").unwrap(), "deep");
}
#[test]
fn array_of_numbers() {
let values = json!({"items": [1, 2, 3]});
let map = values.as_object().unwrap().clone();
let c = from_map(map, None).expect("from_map must succeed");
let list = c.get_list("items").unwrap();
assert_eq!(list.len(), 3);
}
#[test]
fn empty_map_returns_empty_config() {
let map = serde_json::Map::new();
let c = from_map(map, None).expect("from_map must succeed");
assert!(c.is_resolved());
assert!(c.keys().is_empty());
}
#[test]
fn origin_description_stored() {
let map = serde_json::Map::new();
let c = from_map(map, Some("runtime-config")).expect("from_map must succeed");
assert_eq!(c.origin_description(), Some("runtime-config"));
}
#[test]
fn nan_f64_errors() {
let mut map = serde_json::Map::new();
map.insert(
"f".to_string(),
serde_json::Value::Number(serde_json::Number::from_f64(1.5).expect("1.5 is finite")),
);
let c = from_map(map, None).expect("finite float must succeed");
assert!((c.get_f64("f").unwrap() - 1.5).abs() < 1e-10);
}
#[test]
fn u64_max_round_trips_exactly() {
let mut map = serde_json::Map::new();
map.insert(
"big".to_string(),
serde_json::Value::Number(serde_json::Number::from(u64::MAX)),
);
let c = from_map(map, None).expect("u64::MAX must succeed");
assert_eq!(
c.get_string("big").unwrap(),
u64::MAX.to_string(),
"u64::MAX must round-trip exactly through from_map"
);
}
}
mod empty_tests {
use hocon::empty;
#[test]
fn has_no_keys() {
let c = empty(None);
assert!(c.is_resolved(), "empty must be resolved");
assert!(c.keys().is_empty());
}
#[test]
fn as_fallback_is_noop() {
let c = hocon::parse(r#"a = 1"#).unwrap();
let m = c.with_fallback(&empty(None));
assert_eq!(m.get_i64("a").unwrap(), 1);
}
#[test]
fn as_receiver_with_fallback() {
let c = hocon::parse(r#"a = 1"#).unwrap();
let m = empty(None).with_fallback(&c);
assert_eq!(m.get_i64("a").unwrap(), 1);
}
#[test]
fn resolve_is_noop() {
use hocon::ResolveOptions;
let c = empty(None).resolve(ResolveOptions::defaults()).unwrap();
assert!(c.is_resolved());
assert!(c.keys().is_empty());
}
#[test]
fn origin_description_stored() {
let c = empty(Some("empty-test"));
assert_eq!(c.origin_description(), Some("empty-test"));
}
}