use newt_interaction::{
controls_are_decision_shaped, ChoiceOption, Control, ControlId, ControlKind,
InteractionDefinition, InteractionKind, OptionId, Requirement, SemanticRole,
};
fn option(id: &str, role: SemanticRole) -> ChoiceOption {
ChoiceOption {
id: OptionId::new(id).unwrap(),
role,
label: id.to_string(),
key: String::new(),
aliases: Vec::new(),
}
}
fn control(kind: ControlKind) -> Control {
Control {
id: ControlId::new("decision").unwrap(),
kind,
label: String::new(),
requirement: Requirement::Required,
}
}
fn choice(options: Vec<ChoiceOption>) -> Vec<Control> {
vec![control(ControlKind::Choice { options })]
}
#[test]
fn only_a_two_option_grant_refuse_pair_is_decision_shaped() {
let allow_deny = choice(vec![
option("y", SemanticRole::Allow),
option("n", SemanticRole::Deny),
]);
assert!(controls_are_decision_shaped(&allow_deny));
assert!(controls_are_decision_shaped(&choice(vec![
option("n", SemanticRole::Cancel),
option("y", SemanticRole::Allow),
])));
assert!(!controls_are_decision_shaped(&choice(vec![
option("python", SemanticRole::Value),
option("rust", SemanticRole::Value),
])));
assert!(!controls_are_decision_shaped(&choice(vec![
option("y", SemanticRole::Allow),
option("n", SemanticRole::Deny),
option("c", SemanticRole::Cancel),
])));
assert!(!controls_are_decision_shaped(&[control(
ControlKind::Toggle
)]));
assert!(!controls_are_decision_shaped(&[]));
}
#[test]
#[should_panic(expected = "is InteractionKind::Confirm")]
fn a_decision_declared_as_a_choice_is_refused() {
let _ = InteractionDefinition::new(
InteractionKind::Choice,
"delete everything?",
choice(vec![
option("y", SemanticRole::Allow),
option("n", SemanticRole::Deny),
]),
);
}
#[test]
#[should_panic(expected = "carries a binary decision")]
fn a_three_way_pick_declared_as_a_confirm_is_refused() {
let _ = InteractionDefinition::new(
InteractionKind::Confirm,
"which one?",
choice(vec![
option("a", SemanticRole::Allow),
option("b", SemanticRole::Deny),
option("c", SemanticRole::Cancel),
]),
);
}
#[test]
fn the_shapes_the_guard_permits_still_construct() {
let _ = InteractionDefinition::new(
InteractionKind::Confirm,
"delete everything?",
choice(vec![
option("y", SemanticRole::Allow),
option("n", SemanticRole::Deny),
]),
);
let _ = InteractionDefinition::new(
InteractionKind::Choice,
"which one?",
choice(vec![
option("a", SemanticRole::Allow),
option("b", SemanticRole::Deny),
option("c", SemanticRole::Cancel),
]),
);
let _ = InteractionDefinition::new(
InteractionKind::Confirm,
"delete?",
vec![control(ControlKind::Toggle)],
);
let _ = InteractionDefinition::new(
InteractionKind::Form,
"remember this?",
vec![control(ControlKind::Toggle)],
);
let _ = InteractionDefinition::new(InteractionKind::Confirm, "body", Vec::new());
}