use async_graphql::InputType as _;
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"];
}
fn read(value: async_graphql::Value) -> Result<Disposition, String> {
FlagsInput::<Disposition>::parse(Some(value))
.map(FlagsInput::into_inner)
.map_err(|error| error.into_server_error(Default::default()).message)
}
#[test]
fn the_object_and_the_operand_take_two_names_from_one_word() {
assert_eq!(
<FlagsView<Disposition> as async_graphql::OutputType>::type_name(),
"Disposition"
);
assert_eq!(
<FlagsInput<Disposition> as async_graphql::InputType>::type_name(),
"DispositionInput"
);
}
#[test]
fn the_operand_reads_a_list_an_integer_and_the_text_form() {
let expected = Disposition::DEFAULT | Disposition::FORCED;
for (spelling, value) in [
("list", async_graphql::value!(["DEFAULT", "FORCED"])),
("integer", async_graphql::value!(3)),
("text", async_graphql::value!("DEFAULT | FORCED")),
("one name", async_graphql::value!("DEFAULT | FORCED")),
] {
assert_eq!(
read(value).unwrap_or_else(|error| panic!("{spelling}: {error}")),
expected,
"{spelling}"
);
}
}
#[test]
fn the_operand_renders_the_text_form_whatever_arrived() {
for value in [
async_graphql::value!(["DEFAULT", "FORCED"]),
async_graphql::value!(3),
async_graphql::value!("FORCED | DEFAULT"),
] {
let parsed = FlagsInput::<Disposition>::parse(Some(value)).expect("a declared operand");
assert_eq!(
parsed.to_value(),
async_graphql::Value::String("DEFAULT | FORCED".to_owned())
);
}
}
#[test]
fn an_unquoted_word_is_refused_naming_the_three_forms() {
let refused = read(async_graphql::Value::Enum(async_graphql::Name::new(
"DEFAULT",
)))
.expect_err("an enum literal is not one of the three forms");
assert!(refused.contains("is none of the three"), "{refused}");
}
#[test]
fn every_refusal_names_the_declared_words() {
for (label, value) in [
("an undeclared word", async_graphql::value!(["COMMENTARY"])),
("an undeclared bit", async_graphql::value!(8)),
("a negative number", async_graphql::value!(-1)),
("a boolean", async_graphql::value!(true)),
("a non-string element", async_graphql::value!([1])),
] {
let refused = read(value).expect_err(label);
assert!(refused.contains("`DEFAULT`"), "{label}: {refused}");
}
}
#[test]
fn a_number_past_the_declared_width_is_refused() {
let refused = read(async_graphql::value!(4_294_967_296u64)).expect_err("2^32 is not a u32");
assert!(refused.contains("does not fit the width"), "{refused}");
}
#[test]
fn the_object_face_publishes_twelve_fixed_fields_then_the_fan() {
assert_eq!(
fixed_fields().collect::<Vec<_>>(),
[
"bits",
"text",
"names",
"isEmpty",
"isAll",
"complement",
"contains",
"intersects",
"intersection",
"union",
"difference",
"symmetricDifference",
]
);
assert_eq!(
crate::flags::bits::<Disposition>()
.map(|bit| bit.field)
.collect::<Vec<_>>(),
["default", "forced", "hearingImpaired"]
);
}
#[test]
fn the_self_returning_fields_are_the_five_that_derive_a_value() {
let nested: Vec<_> = fixed_fields()
.filter(|field| is_self_returning(field))
.collect();
assert_eq!(
nested,
[
"complement",
"intersection",
"union",
"difference",
"symmetricDifference"
]
);
}
#[test]
fn the_derivation_table_is_the_vendored_set_algebra() {
let value = Disposition::DEFAULT | Disposition::FORCED;
let operand = Disposition::FORCED | Disposition::HEARING_IMPAIRED;
assert_eq!(
derive(value, "complement", Disposition::empty()),
Some(Disposition::HEARING_IMPAIRED)
);
assert_eq!(
derive(value, "intersection", operand),
Some(Disposition::FORCED)
);
assert_eq!(derive(value, "union", operand), Some(Disposition::all()));
assert_eq!(
derive(value, "difference", operand),
Some(Disposition::DEFAULT)
);
assert_eq!(
derive(value, SYMMETRIC_DIFFERENCE, operand),
Some(Disposition::DEFAULT | Disposition::HEARING_IMPAIRED)
);
assert_eq!(derive(value, "bits", operand), None);
}
#[test]
fn the_predicates_answer_and_the_empty_operand_is_vacuous() {
let value = Disposition::DEFAULT;
assert_eq!(
predicate(value, "contains", Disposition::DEFAULT),
Some(true)
);
assert_eq!(
predicate(value, "contains", Disposition::FORCED),
Some(false)
);
assert_eq!(
predicate(value, "contains", Disposition::empty()),
Some(true)
);
assert_eq!(
predicate(value, "intersects", Disposition::empty()),
Some(false)
);
assert_eq!(predicate(value, "text", Disposition::empty()), None);
}