use super::*;
#[test]
fn every_spelling_of_a_named_member_reaches_that_member() {
for spelling in ["wifi", "Wifi", "WIFI", "Wi Fi", "Wi-Fi", "WI_FI"] {
assert_eq!(
BarcodePayloadKind::new(spelling),
BarcodePayloadKind::Wifi,
"`{spelling}` names one kind"
);
}
}
#[test]
fn the_published_roster_is_the_table_the_doors_walk() {
let table: Vec<_> = ROSTER.iter().map(|(_, canonical)| *canonical).collect();
assert_eq!(table, BarcodePayloadKind::VARIANTS);
assert_eq!(BarcodePayloadKind::VARIANTS.len(), 10);
for (member, canonical) in ROSTER {
assert_eq!(member.as_str(), *canonical);
assert_eq!(BarcodePayloadKind::new(canonical), *member);
assert!(
member.is_registered(),
"{canonical} is a name this crate KNOWS"
);
assert!(!member.is_other());
}
}
#[test]
fn an_unnamed_kind_is_carried_folded_rather_than_refused() {
let seen = BarcodePayloadKind::new("Boarding Pass");
assert!(seen.is_other());
assert!(!seen.is_registered());
assert_eq!(seen.as_str(), "boardingpass");
assert_eq!(
seen,
BarcodePayloadKind::new("BOARDING-PASS"),
"two producers' spellings of one unknown kind are ONE value"
);
assert!(
!BarcodePayloadKind::VARIANTS.contains(&seen.as_str()),
"the escape is not a member of the roster"
);
}
#[test]
fn a_payload_kind_round_trips_through_a_cursor_and_canonicalises_a_forged_one() {
for member in [
BarcodePayloadKind::Wifi,
BarcodePayloadKind::Product,
BarcodePayloadKind::new("boarding pass"),
] {
let mut bytes = Vec::new();
CursorValue::write_cursor(&member, &mut bytes);
assert_eq!(
<BarcodePayloadKind as CursorValue>::read_cursor(&bytes),
Some(member)
);
}
assert_eq!(
<BarcodePayloadKind as CursorValue>::read_cursor(b"WI-FI"),
Some(BarcodePayloadKind::Wifi),
"a client's spelling becomes the named member rather than a second value beside it"
);
assert_eq!(
<BarcodePayloadKind as CursorValue>::read_cursor(&[0xff, 0xfe]),
None,
"…and bytes that are not text at all are the one refusal left"
);
}