mirror of
https://github.com/clap-rs/clap
synced 2024-12-15 07:12:32 +00:00
tests: rewrote test for multiple occurrences
This commit is contained in:
parent
7cb5e7f3f5
commit
133a26c2fb
1 changed files with 56 additions and 147 deletions
201
src/lib.rs
201
src/lib.rs
|
@ -964,156 +964,65 @@ mod tests {
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_app_test(test: Vec<&'static str>) -> Vec<String> {
|
#[test]
|
||||||
let m_val_names = ["one", "two"];
|
fn multiple_occurrences_of_flags_long() {
|
||||||
let args = "-o --option=[opt]... 'tests options'
|
let m = App::new("multiple_occurrences")
|
||||||
[positional] 'tests positionals'";
|
.arg(Arg::from_usage("--multflag 'allowed multiple flag'")
|
||||||
let opt3_vals = ["fast", "slow"];
|
.multiple(true))
|
||||||
let pos3_vals = ["vi", "emacs"];
|
.arg(Arg::from_usage("--flag 'disallowed multiple flag'"))
|
||||||
let matches = App::new("claptests")
|
.get_matches_from(vec![
|
||||||
// Test version from Cargo.toml
|
"",
|
||||||
.version(&crate_version!()[..])
|
"--multflag",
|
||||||
.about("tests clap library")
|
"--flag",
|
||||||
.author("Kevin K. <kbknapp@gmail.com>")
|
"--multflag"
|
||||||
.args_from_usage(args)
|
]);
|
||||||
.arg(Arg::from_usage("-f --flag... 'tests flags'")
|
assert!(m.is_present("multflag"));
|
||||||
.global(true))
|
assert_eq!(m.occurrences_of("multflag"), 2);
|
||||||
.args(vec![
|
assert!(m.is_present("flag"));
|
||||||
Arg::from_usage("[flag2] -F 'tests flags with exclusions'").conflicts_with("flag").requires("option2"),
|
assert_eq!(m.occurrences_of("flag"), 1)
|
||||||
Arg::from_usage("--long-option-2 [option2] 'tests long options with exclusions'").conflicts_with("option").requires("positional2"),
|
|
||||||
Arg::from_usage("[positional2] 'tests positionals with exclusions'"),
|
|
||||||
Arg::from_usage("-O --Option [option3] 'tests options with specific value sets'").possible_values(&opt3_vals),
|
|
||||||
Arg::from_usage("[positional3]... 'tests positionals with specific values'").possible_values(&pos3_vals),
|
|
||||||
Arg::from_usage("--multvals [multvals] 'Tests mutliple values, not mult occs'").value_names(&m_val_names),
|
|
||||||
Arg::from_usage("--multvalsmo [multvalsmo]... 'Tests mutliple values, not mult occs'").value_names(&m_val_names),
|
|
||||||
Arg::from_usage("--minvals2 [minvals]... 'Tests 2 min vals'").min_values(2),
|
|
||||||
Arg::from_usage("--maxvals3 [maxvals]... 'Tests 3 max vals'").max_values(3)
|
|
||||||
])
|
|
||||||
.subcommand(SubCommand::with_name("subcmd")
|
|
||||||
.about("tests subcommands")
|
|
||||||
.version("0.1")
|
|
||||||
.author("Kevin K. <kbknapp@gmail.com>")
|
|
||||||
.arg_from_usage("-o --option [scoption]... 'tests options'")
|
|
||||||
.arg_from_usage("[scpositional] 'tests positionals'"))
|
|
||||||
.get_matches_from(test);
|
|
||||||
|
|
||||||
|
|
||||||
let mut result = Vec::<String>::new();
|
|
||||||
|
|
||||||
if matches.is_present("flag") {
|
|
||||||
result.push(format!("flag present {} times", matches.occurrences_of("flag")));
|
|
||||||
} else {
|
|
||||||
result.push(format!("flag NOT present"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches.is_present("opt") {
|
|
||||||
if let Some(v) = matches.value_of("opt") {
|
|
||||||
result.push(format!("option present {} times with value: {}",matches.occurrences_of("opt"), v));
|
|
||||||
}
|
|
||||||
if let Some(ref ov) = matches.values_of("opt") {
|
|
||||||
for o in ov {
|
|
||||||
result.push(format!("An option: {}", o));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
result.push(format!("option NOT present"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(p) = matches.value_of("positional") {
|
|
||||||
result.push(format!("positional present with value: {}", p));
|
|
||||||
} else {
|
|
||||||
result.push(format!("positional NOT present"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches.is_present("flag2") {
|
|
||||||
result.push(format!("flag2 present"));
|
|
||||||
result.push(format!("option2 present with value of: {}", matches.value_of("option2").unwrap()));
|
|
||||||
result.push(format!("positional2 present with value of: {}", matches.value_of("positional2").unwrap()));
|
|
||||||
} else {
|
|
||||||
result.push(format!("flag2 NOT present"));
|
|
||||||
result.push(format!("option2 maybe present with value of: {}", matches.value_of("option2").unwrap_or("Nothing")));
|
|
||||||
result.push(format!("positional2 maybe present with value of: {}", matches.value_of("positional2").unwrap_or("Nothing")));
|
|
||||||
}
|
|
||||||
|
|
||||||
match matches.value_of("option3").unwrap_or("") {
|
|
||||||
"fast" => result.push(format!("option3 present quickly")),
|
|
||||||
"slow" => result.push(format!("option3 present slowly")),
|
|
||||||
_ => result.push(format!("option3 NOT present"))
|
|
||||||
}
|
|
||||||
|
|
||||||
match matches.value_of("positional3").unwrap_or("") {
|
|
||||||
"vi" => result.push(format!("positional3 present in vi mode")),
|
|
||||||
"emacs" => result.push(format!("positional3 present in emacs mode")),
|
|
||||||
_ => result.push(format!("positional3 NOT present"))
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches.is_present("opt") {
|
|
||||||
if let Some(v) = matches.value_of("opt") {
|
|
||||||
result.push(format!("option present {} times with value: {}",matches.occurrences_of("opt"), v));
|
|
||||||
}
|
|
||||||
if let Some(ref ov) = matches.values_of("opt") {
|
|
||||||
for o in ov {
|
|
||||||
result.push(format!("An option: {}", o));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
result.push(format!("option NOT present"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(p) = matches.value_of("positional") {
|
|
||||||
result.push(format!("positional present with value: {}", p));
|
|
||||||
} else {
|
|
||||||
result.push(format!("positional NOT present"));
|
|
||||||
}
|
|
||||||
if matches.is_present("subcmd") {
|
|
||||||
result.push(format!("subcmd present"));
|
|
||||||
if let Some(matches) = matches.subcommand_matches("subcmd") {
|
|
||||||
if matches.is_present("flag") {
|
|
||||||
result.push(format!("flag present {} times", matches.occurrences_of("flag")));
|
|
||||||
} else {
|
|
||||||
result.push(format!("flag NOT present"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches.is_present("scoption") {
|
|
||||||
if let Some(v) = matches.value_of("scoption") {
|
|
||||||
result.push(format!("scoption present with value: {}", v));
|
|
||||||
}
|
|
||||||
if let Some(ref ov) = matches.values_of("scoption") {
|
|
||||||
for o in ov {
|
|
||||||
result.push(format!("An scoption: {}", o));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
result.push(format!("scoption NOT present"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(p) = matches.value_of("scpositional") {
|
|
||||||
result.push(format!("scpositional present with value: {}", p));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
result.push(format!("subcmd NOT present"));
|
|
||||||
}
|
|
||||||
result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn multiple_occurrences_of_flags() {
|
fn multiple_occurrences_of_flags_short() {
|
||||||
assert_eq!(run_app_test(vec!["","value","--flag","--flag","-o", "some"]),
|
let m = App::new("multiple_occurrences")
|
||||||
vec!["flag present 2 times",
|
.arg(Arg::from_usage("-m --multflag 'allowed multiple flag'")
|
||||||
"option present 1 times with value: some",
|
.multiple(true))
|
||||||
"An option: some",
|
.arg(Arg::from_usage("-f --flag 'disallowed multiple flag'"))
|
||||||
"positional present with value: value",
|
.get_matches_from(vec![
|
||||||
"flag2 NOT present",
|
"",
|
||||||
"option2 maybe present with value of: Nothing",
|
"-m",
|
||||||
"positional2 maybe present with value of: Nothing",
|
"-f",
|
||||||
"option3 NOT present",
|
"-m"
|
||||||
"positional3 NOT present",
|
|
||||||
"option present 1 times with value: some",
|
|
||||||
"An option: some",
|
|
||||||
"positional present with value: value",
|
|
||||||
"subcmd NOT present"
|
|
||||||
]);
|
]);
|
||||||
|
assert!(m.is_present("multflag"));
|
||||||
|
assert_eq!(m.occurrences_of("multflag"), 2);
|
||||||
|
assert!(m.is_present("flag"));
|
||||||
|
assert_eq!(m.occurrences_of("flag"), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn multiple_occurrences_of_flags_mixed() {
|
||||||
|
let m = App::new("multiple_occurrences")
|
||||||
|
.arg(Arg::from_usage("-m, --multflag1 'allowed multiple flag'")
|
||||||
|
.multiple(true))
|
||||||
|
.arg(Arg::from_usage("-n, --multflag2 'another allowed multiple flag'")
|
||||||
|
.multiple(true))
|
||||||
|
.arg(Arg::from_usage("-f, --flag 'disallowed multiple flag'"))
|
||||||
|
.get_matches_from(vec![
|
||||||
|
"",
|
||||||
|
"-m",
|
||||||
|
"-f",
|
||||||
|
"-n",
|
||||||
|
"--multflag1",
|
||||||
|
"-m",
|
||||||
|
"--multflag2"
|
||||||
|
]);
|
||||||
|
assert!(m.is_present("multflag1"));
|
||||||
|
assert_eq!(m.occurrences_of("multflag1"), 3);
|
||||||
|
assert!(m.is_present("multflag2"));
|
||||||
|
assert_eq!(m.occurrences_of("multflag2"), 2);
|
||||||
|
assert!(m.is_present("flag"));
|
||||||
|
assert_eq!(m.occurrences_of("flag"), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue