use knf::{Env, EnvValue, Map, Number, Value, interpolate};
use proptest::prelude::*;
struct NoEnv;
impl Env for NoEnv {
fn lookup(&self, _name: &str) -> Option<EnvValue> {
None
}
}
fn arb_value() -> impl Strategy<Value = Value> {
let leaf = prop_oneof![
Just(Value::Null),
any::<bool>().prop_map(Value::Bool),
any::<i64>().prop_map(|n| Value::Number(Number::I64(n))),
"[a-z{} :.]{0,6}".prop_map(Value::String),
Just(Value::Datetime("1979-05-27T07:32:00Z".to_string())),
];
leaf.prop_recursive(4, 24, 3, |inner| {
prop_oneof![
prop::collection::vec(inner.clone(), 0..3).prop_map(Value::Array),
arb_object(inner),
]
})
}
fn arb_object(inner: impl Strategy<Value = Value>) -> impl Strategy<Value = Value> {
prop::collection::vec(("[a-c]{1,2}", inner), 0..3)
.prop_map(|entries| Value::Object(entries.into_iter().collect::<Map>()))
}
fn arb_doc() -> impl Strategy<Value = Value> {
arb_object(arb_value())
}
proptest! {
#[test]
fn a_document_without_a_dollar_is_unchanged(doc in arb_doc()) {
prop_assert_eq!(interpolate(doc.clone(), &NoEnv).expect("nothing to resolve"), doc);
}
}