use std::collections::BTreeMap;
use std::fmt::Debug;
use proptest::arbitrary::Arbitrary;
use proptest::collection::{btree_map, vec};
use proptest::prelude::{BoxedStrategy, Just, Strategy, any};
use proptest::prop_oneof;
use proptest::test_runner::TestCaseError;
use crate::schema::{HasSchema, Schema, Unknown};
pub fn from_schema<A, I, E, S>(schema: Schema<A, I, E>, wire: S) -> impl Strategy<Value = A>
where
A: Debug,
S: Strategy<Value = I>,
{
wire.prop_filter_map("rejected by the schema", move |i| schema.decode(i).ok())
}
pub fn arb_schema<T>() -> impl Strategy<Value = T::A>
where
T: HasSchema,
T::A: Debug,
T::I: Arbitrary,
{
from_schema(T::schema(), any::<T::I>())
}
pub fn arb_unknown() -> impl Strategy<Value = Unknown> {
let leaf = prop_oneof![
Just(Unknown::Null),
any::<bool>().prop_map(Unknown::Bool),
any::<i64>().prop_map(Unknown::I64),
(-1.0e9..1.0e9f64).prop_map(Unknown::F64),
"[ -~]{0,12}".prop_map(Unknown::String),
];
leaf.prop_recursive(3, 32, 4, |inner| {
prop_oneof![
vec(inner.clone(), 0..4).prop_map(Unknown::Array),
btree_map("[a-z]{1,6}", inner, 0..4).prop_map(Unknown::Object),
]
})
}
pub fn unknown_object(
fields: impl IntoIterator<Item = (&'static str, BoxedStrategy<Unknown>)>,
) -> impl Strategy<Value = Unknown> {
let (names, strategies): (Vec<&'static str>, Vec<BoxedStrategy<Unknown>>) =
fields.into_iter().unzip();
strategies.prop_map(move |values| {
Unknown::Object(
names
.iter()
.map(|n| (*n).to_owned())
.zip(values)
.collect::<BTreeMap<_, _>>(),
)
})
}
pub fn prop_assert_roundtrip<A, I, E>(
schema: &Schema<A, I, E>,
value: A,
) -> Result<(), TestCaseError>
where
A: Clone + Debug + PartialEq,
{
match schema.decode(schema.encode(value.clone())) {
Ok(back) if back == value => Ok(()),
Ok(back) => Err(TestCaseError::fail(format!(
"round trip changed the value: {value:?} came back as {back:?}"
))),
Err(e) => Err(TestCaseError::fail(format!(
"round trip failed to decode {value:?}: {e:?}"
))),
}
}
#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;
use crate::schema::{ParseError, filter, i64, string};
struct Port;
impl HasSchema for Port {
type A = u16;
type I = i64;
type E = ();
fn schema() -> Schema<u16, i64, ()> {
Schema::make(
|n: i64| u16::try_from(n).map_err(|_| ParseError::new("", "port out of range")),
i64::from,
|_u: &Unknown| Err(ParseError::new("", "unused")),
)
}
}
fn short_name() -> Schema<String, String, ()> {
filter(string::<()>(), |s: &String| s.len() <= 4, "too long")
}
proptest! {
#[test]
fn from_schema_yields_only_accepted_values(s in from_schema(short_name(), "[a-z]{0,6}")) {
prop_assert!(s.len() <= 4);
}
#[test]
fn has_schema_values_roundtrip(port in from_schema(Port::schema(), 0..70_000i64)) {
prop_assert_roundtrip(&Port::schema(), port)?;
}
#[test]
fn i64_schema_roundtrips(n in arb_schema::<I64Wire>()) {
prop_assert_roundtrip(&i64::<()>(), n)?;
}
#[test]
fn unknown_trees_have_finite_numbers(u in arb_unknown()) {
fn finite(u: &Unknown) -> bool {
match u {
Unknown::F64(f) => f.is_finite(),
Unknown::Array(xs) => xs.iter().all(finite),
Unknown::Object(m) => m.values().all(finite),
_ => true,
}
}
prop_assert!(finite(&u));
}
#[test]
fn unknown_object_has_exactly_the_fields(u in unknown_object([
("a", any::<bool>().prop_map(Unknown::Bool).boxed()),
("b", Just(Unknown::Null).boxed()),
])) {
let Unknown::Object(m) = u else { panic!("not an object") };
prop_assert_eq!(m.keys().map(String::as_str).collect::<Vec<_>>(), vec!["a", "b"]);
}
}
struct I64Wire;
impl HasSchema for I64Wire {
type A = i64;
type I = i64;
type E = ();
fn schema() -> Schema<i64, i64, ()> {
i64::<()>()
}
}
#[test]
fn roundtrip_reports_a_lossy_schema() {
let lossy: Schema<i64, i64, ()> = Schema::make(|n: i64| Ok(n / 2), |n| n, |_u: &Unknown| Ok(0));
let err = prop_assert_roundtrip(&lossy, 8).unwrap_err();
assert!(err.to_string().contains("came back as 4"), "{err}");
let rejecting: Schema<i64, i64, ()> = Schema::make(
|_n: i64| Err(ParseError::new("", "never")),
|n| n,
|_u: &Unknown| Ok(0),
);
assert!(prop_assert_roundtrip(&rejecting, 1).is_err());
}
}