Add tests to prove we won't panic

This commit is contained in:
Erick Tryzelaar 2021-12-15 15:01:16 -05:00 committed by Erick Tryzelaar
parent 8a7319a9b4
commit b2854ad9f1

View File

@ -1314,3 +1314,58 @@ fn redact_arg_values_does_not_warn_if_used() {
let actual = Cmd::redact_arg_values(&["program-name"], &["5"]).unwrap();
assert_eq!(actual, &["program-name", "speed"]);
}
#[test]
fn subcommand_does_not_panic() {
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand)]
enum SubCommandEnum {
Cmd(SubCommand),
}
#[derive(FromArgs, PartialEq, Debug)]
/// First subcommand.
#[argh(subcommand, name = "one")]
struct SubCommand {
#[argh(positional)]
/// how many x
x: usize,
}
#[derive(FromArgs, PartialEq, Debug)]
/// Second subcommand.
#[argh(subcommand, name = "two")]
struct SubCommandTwo {
#[argh(switch)]
/// whether to fooey
fooey: bool,
}
// Passing no subcommand name to an emum
assert_eq!(
SubCommandEnum::from_args(&[], &["5"]).unwrap_err(),
argh::EarlyExit { output: "no subcommand name".into(), status: Err(()) },
);
assert_eq!(
SubCommandEnum::redact_arg_values(&[], &["5"]).unwrap_err(),
argh::EarlyExit { output: "no subcommand name".into(), status: Err(()) },
);
// Passing unknown subcommand name to an emum
assert_eq!(
SubCommandEnum::from_args(&["fooey"], &["5"]).unwrap_err(),
argh::EarlyExit { output: "no subcommand matched".into(), status: Err(()) },
);
assert_eq!(
SubCommandEnum::redact_arg_values(&["fooey"], &["5"]).unwrap_err(),
argh::EarlyExit { output: "no subcommand matched".into(), status: Err(()) },
);
// Passing unknown subcommand name to a struct
assert_eq!(
SubCommand::redact_arg_values(&[], &["5"]).unwrap_err(),
argh::EarlyExit { output: "no subcommand name".into(), status: Err(()) },
);
}