use super::clap::{parse_matches, parsed_options, principal_text_value_parser, with_options};
use clap::{Arg, Args, Command};
use std::ffi::OsString;
pub fn render_help(mut command: Command) -> String {
command.render_help().to_string()
}
pub fn parse_test_args(
mut command: Command,
args: &[&str],
) -> Result<clap::ArgMatches, clap::Error> {
parse_matches(&mut command, args.iter().copied().map(OsString::from))
}
#[test]
fn typed_helpers_read_values_from_one_clap_match_tree() {
#[derive(Args)]
struct Options {
name: String,
#[arg(long)]
count: Option<u32>,
}
let mut command = with_options::<Options>(Command::new("icq").about("Inspect reports"));
assert_eq!(
command
.get_about()
.expect("command description")
.to_string(),
"Inspect reports"
);
assert!(command.get_long_about().is_none());
let detailed = with_options::<Options>(Command::new("icq").long_about("Detailed report help"));
assert!(detailed.get_about().is_none());
assert_eq!(
detailed
.get_long_about()
.expect("long description")
.to_string(),
"Detailed report help"
);
let matches = parse_matches(
&mut command,
[
OsString::from("report"),
OsString::from("--count"),
OsString::from("3"),
],
)
.expect("parse typed arguments");
let options: Options = parsed_options(&matches);
assert_eq!(options.name, "report");
assert_eq!(options.count, Some(3));
}
#[test]
fn principal_options_normalize_spelling_and_reject_invalid_identities() {
let command = Command::new("principal")
.arg(Arg::new("principal").value_parser(principal_text_value_parser()));
for (input, expected) in [
("aaaaa-aa", "aaaaa-aa"),
("AAAAA-AA", "aaaaa-aa"),
("2VXSX-FAE", "2vxsx-fae"),
] {
let matches = command
.clone()
.try_get_matches_from(["principal", input])
.unwrap();
assert_eq!(matches.get_one::<String>("principal").unwrap(), expected);
}
for input in ["aaaaaaa", " aaaaa-aa", "invalid", "é"] {
let error = command
.clone()
.try_get_matches_from(["principal", input])
.unwrap_err();
assert_eq!(error.kind(), clap::error::ErrorKind::ValueValidation);
}
}