Merge pull request #178 from Vinatorul/issue166

Moved code from claptests to lib.rs, also added one test
This commit is contained in:
Kevin K. 2015-08-19 20:15:21 -04:00
commit f829445d67
2 changed files with 154 additions and 2 deletions

View file

@ -243,7 +243,6 @@ cmds = {'help short: ': ['{} -h'.format(_bin), _help],
'excluded first: ': ['{} -f -F'.format(_bin), _excluded],
'excluded last: ': ['{} -F -f'.format(_bin), _excluded_l],
'missing required: ': ['{} -F'.format(_bin), _required],
'F2(ll),O(s),P: ': ['{} value --flag --flag -o some'.format(_bin), _f2op],
'max_vals too many: ': ['{} --maxvals3 some other value too'.format(_bin), _max_vals_more],
'max_vals exact: ': ['{} --maxvals3 some other value'.format(_bin), _exact],
'max_vals less: ': ['{} --maxvals3 some other'.format(_bin), _exact],

View file

@ -407,6 +407,7 @@ mod fmt;
mod tests {
use super::{App, Arg, SubCommand};
use std::collections::HashSet;
use std::vec::Vec;
arg_enum!{
#[derive(Debug)]
@ -1340,4 +1341,156 @@ mod tests {
Arg::with_name("arg2").long("long")
]);
}
}
fn run_app_test(test: Vec<&'static str>) -> Vec<String> {
let m_val_names = ["one", "two"];
let args = "-o --option=[opt]... 'tests options'
[positional] 'tests positionals'";
let opt3_vals = ["fast", "slow"];
let pos3_vals = ["vi", "emacs"];
let matches = App::new("claptests")
// Test version from Cargo.toml
.version(&crate_version!()[..])
.about("tests clap library")
.author("Kevin K. <kbknapp@gmail.com>")
.args_from_usage(args)
.arg(Arg::from_usage("-f --flag... 'tests flags'")
.global(true))
.args(vec![
Arg::from_usage("[flag2] -F 'tests flags with exclusions'").conflicts_with("flag").requires("option2"),
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]
fn multiple_occurrences_of_flags(){
assert_eq!(run_app_test(vec!["","value","--flag","--flag","-o", "some"]),
vec!["flag present 2 times",
"option present 1 times with value: some",
"An option: some",
"positional present with value: value",
"flag2 NOT present",
"option2 maybe present with value of: Nothing",
"positional2 maybe present with value of: Nothing",
"option3 NOT present",
"positional3 NOT present",
"option present 1 times with value: some",
"An option: some",
"positional present with value: value",
"subcmd NOT present"
]);
}
}