mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 23:02:31 +00:00
tests: refactors the tests for propagating global values and flags as well as adds some tests for default values and flags
This commit is contained in:
parent
0d6cd6e7d9
commit
cbd09c7fec
1 changed files with 74 additions and 75 deletions
|
@ -4,35 +4,27 @@ extern crate regex;
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
include!("../clap-test.rs");
|
||||
use clap::{App, Arg, SubCommand, AppSettings, ArgMatches};
|
||||
use clap::{App, Arg, SubCommand, ArgMatches};
|
||||
|
||||
fn get_global_arg() -> Arg<'static, 'static> {
|
||||
Arg::with_name("GLOBAL_ARG")
|
||||
fn get_app() -> App<'static, 'static> {
|
||||
App::new("myprog")
|
||||
.arg(Arg::with_name("GLOBAL_ARG")
|
||||
.long("global-arg")
|
||||
.help(
|
||||
"Specifies something needed by the subcommands",
|
||||
)
|
||||
.global(true)
|
||||
.takes_value(true)
|
||||
}
|
||||
|
||||
fn get_global_flag() -> Arg<'static, 'static> {
|
||||
Arg::with_name("GLOBAL_FLAG")
|
||||
.default_value("default_value"))
|
||||
.arg(Arg::with_name("GLOBAL_FLAG")
|
||||
.long("global-flag")
|
||||
.help(
|
||||
"Specifies something needed by the subcommands",
|
||||
)
|
||||
.global(true)
|
||||
}
|
||||
|
||||
fn get_app() -> App<'static, 'static> {
|
||||
App::new("myprog")
|
||||
.global_setting(AppSettings::PropagateGlobalValuesDown)
|
||||
}
|
||||
|
||||
fn get_subcommands() -> App<'static, 'static> {
|
||||
SubCommand::with_name("outer")
|
||||
.subcommand(SubCommand::with_name("inner"))
|
||||
.multiple(true)
|
||||
.global(true))
|
||||
.subcommand(SubCommand::with_name("outer")
|
||||
.subcommand(SubCommand::with_name("inner")))
|
||||
}
|
||||
|
||||
fn get_matches(app: App<'static, 'static>, argv: &'static str) -> ArgMatches<'static> {
|
||||
|
@ -59,6 +51,10 @@ mod tests {
|
|||
get_outer_matches(m).value_of("GLOBAL_ARG") == val.into()
|
||||
}
|
||||
|
||||
fn top_can_access_flag(m: &ArgMatches<'static>, present: bool, occurrences: u64) -> bool {
|
||||
(m.is_present("GLOBAL_FLAG") == present) && (m.occurrences_of("GLOBAL_FLAG") == occurrences)
|
||||
}
|
||||
|
||||
fn inner_can_access_flag(m: &ArgMatches<'static>, present: bool, occurrences: u64) -> bool {
|
||||
let m = get_inner_matches(m);
|
||||
(m.is_present("GLOBAL_FLAG") == present) && (m.occurrences_of("GLOBAL_FLAG") == occurrences)
|
||||
|
@ -70,12 +66,8 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn global_arg_defined_top_level_used_top_level() {
|
||||
let app = get_app()
|
||||
.arg(get_global_arg())
|
||||
.subcommand(get_subcommands());
|
||||
|
||||
let m = get_matches(app, "myprog --global-arg=some_value outer inner");
|
||||
fn global_arg_used_top_level() {
|
||||
let m = get_matches(get_app(), "myprog --global-arg=some_value outer inner");
|
||||
|
||||
assert!(top_can_access_arg(&m, "some_value"));
|
||||
assert!(inner_can_access_arg(&m, "some_value"));
|
||||
|
@ -83,12 +75,8 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn global_arg_defined_top_level_used_outer() {
|
||||
let app = get_app()
|
||||
.arg(get_global_arg())
|
||||
.subcommand(get_subcommands());
|
||||
|
||||
let m = get_matches(app, "myprog outer --global-arg=some_value inner");
|
||||
fn global_arg_used_outer() {
|
||||
let m = get_matches(get_app(), "myprog outer --global-arg=some_value inner");
|
||||
|
||||
assert!(top_can_access_arg(&m, "some_value"));
|
||||
assert!(inner_can_access_arg(&m, "some_value"));
|
||||
|
@ -96,54 +84,65 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn global_arg_defined_top_level_used_inner() {
|
||||
let app = get_app()
|
||||
.arg(get_global_arg())
|
||||
.subcommand(get_subcommands());
|
||||
fn global_arg_used_inner() {
|
||||
let m = get_matches(get_app(), "myprog outer inner --global-arg=some_value");
|
||||
|
||||
let m = get_matches(app, "myprog outer inner --global-arg=some_value");
|
||||
|
||||
assert!(top_can_access_arg(&m, "some_value"), "{:?}", m);
|
||||
assert!(top_can_access_arg(&m, "some_value"));
|
||||
assert!(inner_can_access_arg(&m, "some_value"));
|
||||
assert!(outer_can_access_arg(&m, "some_value"));
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn global_arg_defined_nested_used_top_level() {
|
||||
// let app = get_app()
|
||||
// .subcommand(get_subcommands()
|
||||
// .arg(get_global_arg()));
|
||||
#[test]
|
||||
fn global_arg_default_value() {
|
||||
let m = get_matches(get_app(), "myprog outer inner");
|
||||
|
||||
// let m = get_matches(app, "myprog --global-arg=some_value outer inner");
|
||||
|
||||
// assert!(top_can_access_arg(&m, "some_value"));
|
||||
// assert!(inner_can_access_arg(&m, "some_value"));
|
||||
// assert!(outer_can_access_arg(&m, "some_value"));
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn global_arg_defined_nested_used_outer() {
|
||||
// let app = get_app()
|
||||
// .subcommand(get_subcommands()
|
||||
// .arg(get_global_arg()));
|
||||
|
||||
// let m = get_matches(app, "myprog outer --global-arg=some_value inner");
|
||||
|
||||
// assert!(top_can_access_arg(&m, "some_value"));
|
||||
// assert!(inner_can_access_arg(&m, "some_value"));
|
||||
// assert!(outer_can_access_arg(&m, "some_value"));
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn global_arg_defined_nested_used_inner() {
|
||||
// let app = get_app()
|
||||
// .subcommand(get_subcommands()
|
||||
// .arg(get_global_arg()));
|
||||
|
||||
// let m = get_matches(app, "myprog outer inner --global-arg=some_value");
|
||||
|
||||
// assert!(top_can_access_arg(&m, "some_value"));
|
||||
// assert!(inner_can_access_arg(&m, "some_value"));
|
||||
// assert!(outer_can_access_arg(&m, "some_value"));
|
||||
// }
|
||||
assert!(top_can_access_arg(&m, "default_value"));
|
||||
assert!(inner_can_access_arg(&m, "default_value"));
|
||||
assert!(outer_can_access_arg(&m, "default_value"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn global_flag_used_top_level() {
|
||||
let m = get_matches(get_app(), "myprog --global-flag outer inner");
|
||||
|
||||
assert!(top_can_access_flag(&m, true, 1));
|
||||
assert!(inner_can_access_flag(&m, true, 1));
|
||||
assert!(outer_can_access_flag(&m, true, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn global_flag_used_outer() {
|
||||
let m = get_matches(get_app(), "myprog outer --global-flag inner");
|
||||
|
||||
assert!(top_can_access_flag(&m, true, 1));
|
||||
assert!(inner_can_access_flag(&m, true, 1));
|
||||
assert!(outer_can_access_flag(&m, true, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn global_flag_used_inner() {
|
||||
let m = get_matches(get_app(), "myprog outer inner --global-flag");
|
||||
|
||||
assert!(top_can_access_flag(&m, true, 1));
|
||||
assert!(inner_can_access_flag(&m, true, 1));
|
||||
assert!(outer_can_access_flag(&m, true, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn global_flag_2x_used_top_level() {
|
||||
let m = get_matches(get_app(), "myprog --global-flag --global-flag outer inner");
|
||||
|
||||
assert!(top_can_access_flag(&m, true, 2));
|
||||
assert!(inner_can_access_flag(&m, true, 2));
|
||||
assert!(outer_can_access_flag(&m, true, 2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn global_flag_2x_used_inner() {
|
||||
let m = get_matches(get_app(), "myprog outer inner --global-flag --global-flag");
|
||||
|
||||
assert!(top_can_access_flag(&m, true, 2));
|
||||
assert!(inner_can_access_flag(&m, true, 2));
|
||||
assert!(outer_can_access_flag(&m, true, 2));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue