use core::num::NonZeroU32;
use super::{Binding, Bridge, DeclaredSearch, Member, Seat, SharedQuestion, TrySharedQuestion};
struct Handwritten;
impl DeclaredSearch for Handwritten {}
#[test]
fn a_hand_written_recipe_states_the_honest_nothing() {
assert!(<Handwritten as DeclaredSearch>::SEATS.is_empty());
assert!(<Handwritten as DeclaredSearch>::MEMBERS.is_empty());
assert!(<Handwritten as DeclaredSearch>::LIMIT.is_none());
}
struct Travel;
impl DeclaredSearch for Travel {
const SEATS: &'static [Seat] = &[Seat::of("text")];
const MEMBERS: &'static [Member] = &[
Member::of("subs")
.weighted(1.0)
.deep(50)
.bound(&[Binding::of("zh").shared(Bridge::Total)]),
Member::of("shots")
.weighted(2.0)
.deep(100)
.floored()
.bound(&[
Binding::of("text").shared(Bridge::Fallible).floored(),
Binding::of("image"),
]),
];
const LIMIT: Option<NonZeroU32> = NonZeroU32::new(20);
}
#[test]
fn a_stated_recipe_carries_its_members_in_declaration_order() {
const SEATS: &[Seat] = <Travel as DeclaredSearch>::SEATS;
const MEMBERS: &[Member] = <Travel as DeclaredSearch>::MEMBERS;
assert_eq!(SEATS.len(), 1);
assert_eq!(SEATS[0].name(), "text");
assert_eq!(MEMBERS[0].field(), "subs");
assert_eq!(MEMBERS[0].weight(), Some(1.0));
assert_eq!(MEMBERS[0].depth(), NonZeroU32::new(50));
assert!(!MEMBERS[0].is_floored(), "no floor word, no floor");
assert_eq!(MEMBERS[1].field(), "shots");
assert_eq!(MEMBERS[1].weight(), Some(2.0));
assert!(MEMBERS[1].is_floored());
assert_eq!(
<Travel as DeclaredSearch>::LIMIT,
NonZeroU32::new(20),
"the head's own number, where the head named one"
);
}
#[test]
fn an_arm_row_carries_the_crossing_the_word_chose() {
const ARMS: &[Binding] = <Travel as DeclaredSearch>::MEMBERS[1].bindings();
assert_eq!(ARMS[0].name(), "text");
assert_eq!(ARMS[0].bridge(), Some(Bridge::Fallible));
assert!(ARMS[0].is_floored());
assert_eq!(ARMS[1].name(), "image");
assert_eq!(
ARMS[1].bridge(),
None,
"an arm that wrote no `shared` is asked its own question"
);
}
#[test]
fn every_bridge_states_whether_it_can_fail_and_who_wrote_it() {
const TABLE: [(Bridge, bool, bool); 4] = [
(Bridge::Total, false, false),
(Bridge::Fallible, true, false),
(Bridge::Hook, false, true),
(Bridge::FallibleHook, true, true),
];
for (bridge, fallible, hooked) in TABLE {
assert_eq!(bridge.fallible(), fallible, "{bridge:?}");
assert_eq!(bridge.hooked(), hooked, "{bridge:?}");
}
}
#[test]
fn the_rows_are_const_constructible_outside_an_expansion() {
const ROSTER: &[Member] = &[Member::of("subs"), Member::of("shots").weighted(2.0)];
assert_eq!(ROSTER[0].field(), "subs");
assert_eq!(ROSTER[0].weight(), None, "no word, no coefficient stated");
assert_eq!(ROSTER[1].weight(), Some(2.0));
assert!(ROSTER[1].bindings().is_empty());
}
struct Text;
struct Point;
#[derive(Debug, PartialEq)]
struct MemberQuestion(String);
impl SharedQuestion<Text, str> for MemberQuestion {
fn from_shared(shared: &str) -> Option<Self> {
(!shared.is_empty()).then(|| Self(shared.to_owned()))
}
}
impl SharedQuestion<Point, [f32]> for MemberQuestion {
fn from_shared(shared: &[f32]) -> Option<Self> {
Some(Self(format!("{} coordinates", shared.len())))
}
}
#[derive(Debug, PartialEq)]
struct Picky(String);
impl TrySharedQuestion<Text, str> for Picky {
type Error = &'static str;
fn try_from_shared(shared: &str) -> Result<Option<Self>, Self::Error> {
if shared.len() > 3 {
Ok(Some(Self(shared.to_owned())))
} else {
Err("too short to ask")
}
}
}
#[test]
fn the_seat_marker_is_what_keeps_two_crossings_apart() {
assert_eq!(
<MemberQuestion as SharedQuestion<Text, str>>::from_shared("harbour"),
Some(MemberQuestion("harbour".to_owned())),
);
assert_eq!(
<MemberQuestion as SharedQuestion<Point, [f32]>>::from_shared(&[0.0, 1.0]),
Some(MemberQuestion("2 coordinates".to_owned())),
);
}
#[test]
fn a_member_with_nothing_to_ask_answers_none() {
assert_eq!(
<MemberQuestion as SharedQuestion<Text, str>>::from_shared(""),
None,
);
}
#[test]
fn the_try_face_absorbs_the_total_one() {
fn asked<Q, S>(shared: &S) -> Result<Option<Q>, Q::Error>
where
Q: TrySharedQuestion<Text, S>,
S: ?Sized,
{
Q::try_from_shared(shared)
}
assert_eq!(
asked::<MemberQuestion, str>("harbour"),
Ok(Some(MemberQuestion("harbour".to_owned()))),
);
assert_eq!(
asked::<Picky, str>("harbour"),
Ok(Some(Picky("harbour".to_owned())))
);
assert_eq!(asked::<Picky, str>("no"), Err("too short to ask"));
}
#[test]
fn a_total_crossing_refuses_with_a_type_that_has_no_value() {
const _: fn(
core::convert::Infallible,
) -> <MemberQuestion as TrySharedQuestion<Text, str>>::Error = core::convert::identity;
}