Lines
100 %
Functions
Branches
use forms::{Action, Field, Form, Kind};
use super::{form_command, values_of};
const FIELDS: &[Field] = &[
Field {
key: "period",
label: "Period",
kind: Kind::Choice {
options: &[("month", "Month"), ("year", "Year")],
default: Some("month"),
},
required: false,
key: "mode",
label: "Mode",
options: &[("all", "All"), ("any", "Any")],
default: None,
required: true,
];
const FORM: Form = Form {
name: "choice-test",
title: "Choice test",
fields: FIELDS,
action: Action::Query,
};
#[test]
fn a_known_key_is_accepted() -> Result<(), clap::Error> {
let matches = form_command("q", "Q", &FORM).try_get_matches_from(["q", "--period", "year"])?;
let values = values_of(&FORM, &matches);
assert_eq!(values.text("period"), Some("year"));
assert_eq!(values.text("mode"), None);
Ok(())
}
fn an_unknown_key_is_a_clap_error() {
let error = form_command("q", "Q", &FORM)
.try_get_matches_from(["q", "--mode", "some"])
.err();
assert_eq!(
error.map(|e| e.kind()),
Some(clap::error::ErrorKind::InvalidValue)
);
fn a_label_is_not_a_key() {
let result = form_command("q", "Q", &FORM).try_get_matches_from(["q", "--period", "Year"]);
assert!(result.is_err());
fn help_lists_the_options_and_the_default() {
let help = form_command("q", "Q", &FORM).render_long_help().to_string();
assert!(
help.contains("One of: month (Month), year (Year); default: month"),
"{help}"
assert!(help.contains("One of: all (All), any (Any)"), "{help}");
assert!(help.contains("[possible values: month, year]"), "{help}");