use super::*;
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Disposition: u32 {
const DEFAULT = 0x1u32;
const FORCED = 0x2u32;
const HEARING_IMPAIRED = 0x4u32;
}
}
impl FlagsValue for Disposition {
const GRAPHQL_NAME: &'static str = "Disposition";
const FIELDS: &'static [&'static str] = &["default", "forced", "hearingImpaired"];
}
#[test]
fn every_bit_carries_its_word_its_field_and_its_value() {
let table: Vec<_> = bits::<Disposition>().collect();
assert_eq!(
table
.iter()
.map(|bit| (bit.word, bit.field))
.collect::<Vec<_>>(),
[
("DEFAULT", "default"),
("FORCED", "forced"),
("HEARING_IMPAIRED", "hearingImpaired"),
]
);
assert_eq!(
table.iter().map(|bit| bit.value).collect::<Vec<_>>(),
[
Disposition::DEFAULT,
Disposition::FORCED,
Disposition::HEARING_IMPAIRED
]
);
}
#[test]
fn the_mask_is_every_declared_bit_and_no_other() {
assert_eq!(all::<Disposition>().bits(), 0b111);
assert_eq!(empty::<Disposition>().bits(), 0);
assert!(all::<Disposition>().is_all());
assert!(empty::<Disposition>().is_empty());
}
#[test]
fn the_text_form_is_the_declared_words_in_declaration_order() {
assert_eq!(text(&Disposition::empty()).as_str(), "");
assert_eq!(text(&Disposition::FORCED).as_str(), "FORCED");
assert_eq!(
text(&(Disposition::HEARING_IMPAIRED | Disposition::DEFAULT)).as_str(),
"DEFAULT | HEARING_IMPAIRED",
"declaration order, not the order the operands were written in"
);
}
#[test]
fn a_single_bit_takes_its_declared_word_whole() {
assert_eq!(
text(&Disposition::HEARING_IMPAIRED),
Utf8Bytes::from_static("HEARING_IMPAIRED")
);
}
#[test]
fn the_name_list_is_the_set_bits_words() {
let listed = names(&(Disposition::DEFAULT | Disposition::HEARING_IMPAIRED));
assert_eq!(
listed.iter().map(Utf8Bytes::as_str).collect::<Vec<_>>(),
["DEFAULT", "HEARING_IMPAIRED"]
);
assert!(names(&Disposition::empty()).is_empty());
}
#[test]
fn the_list_the_text_and_the_bits_read_one_value() {
let expected = Disposition::DEFAULT | Disposition::FORCED;
assert_eq!(
of_words::<Disposition>(["DEFAULT", "FORCED"]).expect("both words are declared"),
expected
);
assert_eq!(
of_text::<Disposition>("DEFAULT | FORCED").expect("the text form is the declared grammar"),
expected
);
assert_eq!(
of_bits::<Disposition>(0b11).expect("both bits are declared"),
expected
);
}
#[test]
fn the_text_door_reads_a_hex_token_within_the_declared_mask() {
assert_eq!(
of_text::<Disposition>("0x3").expect("both bits are declared"),
Disposition::DEFAULT | Disposition::FORCED
);
assert_eq!(
of_text::<Disposition>("DEFAULT | 0x4").expect("a name and a token in one text"),
Disposition::DEFAULT | Disposition::HEARING_IMPAIRED
);
}
#[test]
fn one_name_is_the_text_form_of_length_one() {
assert_eq!(
of_text::<Disposition>("FORCED").expect("one declared word"),
Disposition::FORCED
);
assert_eq!(
of_text::<Disposition>("").expect("the empty text is the empty set"),
Disposition::empty()
);
}
#[test]
fn every_door_refuses_a_bit_the_declaration_never_named() {
let by_word =
of_words::<Disposition>(["DEFAULT", "COMMENTARY"]).expect_err("`COMMENTARY` is not declared");
assert_eq!(by_word.word(), "COMMENTARY");
let by_token = of_text::<Disposition>("0x8").expect_err("bit 3 is not declared");
assert_eq!(by_token.word(), "0x8");
let by_bits = of_bits::<Disposition>(0b1001).expect_err("bit 3 is not declared");
assert_eq!(
by_bits.word(),
"0x8",
"the UNDECLARED bits, not the whole value"
);
}
#[test]
fn an_ill_formed_text_is_refused_whole() {
let refused =
of_text::<Disposition>("DEFAULT |").expect_err("a trailing separator is not a flag");
assert_eq!(refused.word(), "DEFAULT |");
}
#[test]
fn the_refusal_names_the_declared_words_and_the_whole_mask() {
let listed = vocabulary::<Disposition>();
for word in ["`DEFAULT`", "`FORCED`", "`HEARING_IMPAIRED`"] {
assert!(listed.contains(word), "{listed}");
}
assert!(
listed.contains("DEFAULT | FORCED | HEARING_IMPAIRED"),
"{listed}"
);
}
#[test]
fn is_all_and_equality_with_the_mask_are_one_question() {
for bits in 0u32..=0b111 {
let value = Disposition::from_bits(bits).expect("every pattern inside the mask is declared");
assert_eq!(value.is_all(), value == all::<Disposition>(), "{bits:#b}");
}
}