Skip to content

Commit

Permalink
tests: fixing tests for options based on new 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Jan 23, 2016
1 parent 022bf1b commit 8476831
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
22 changes: 11 additions & 11 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,8 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
let mut val = None;
debug!("Does it contain '='...");
let arg = if full_arg.contains_byte(b'=') {
sdebugln!("Yes");
let (p0, p1) = full_arg.trim_left_matches(b'-').split_at_byte(b'=');
sdebugln!("Yes '{:?}'", p1);
val = Some(p1);
p0
} else {
Expand Down Expand Up @@ -995,23 +995,23 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
opt: &OptBuilder<'a, 'b>,
matcher: &mut ArgMatcher<'a>)
-> ClapResult<Option<&'a str>> {
debugln!("fn=parse_opt;");
validate_multiples!(self, opt, matcher);

debug!("Checking for val...");
if let Some(v) = val {
if !(opt.is_set(ArgSettings::EmptyValues) && v.len() == 0) {
if !opt.is_set(ArgSettings::EmptyValues) && v.len() == 0 {
sdebugln!("Found Empty - Error");
return Err(Error::empty_value(opt, &*self.create_current_usage(matcher)));
}
sdebugln!("Found");
try!(self.add_val_to_arg(opt, v, matcher));
}
} else { sdebugln!("None"); }

// // If it doesn't allow mutliples, (or a specific number of values), or we don't have any
// // values yet, we want to return the name of this arg so the next arg is parsed as a value
// // otherwise we're done getting values
// if opt.settings.is_set(ArgSettings::Multiple)
// || opt.num_vals.is_some()
// || !matcher.contains(&*opt.name) {
// return Ok(Some(opt.name));
// }
if (opt.is_set(ArgSettings::Multiple) || opt.num_vals().is_some())
|| val.is_none() {
return Ok(Some(opt.name));
}
Ok(None)
}

Expand Down
2 changes: 1 addition & 1 deletion src/osstringext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl OsStrExt2 for OsStr {
fn split_at_byte(&self, byte: u8) -> (&OsStr, &OsStr) {
let mut i = 0;
for b in self.as_bytes() {
if b == &byte { return (OsStr::from_bytes(&self.as_bytes()[..i]), OsStr::from_bytes(&self.as_bytes()[i..])); }
if b == &byte { return (OsStr::from_bytes(&self.as_bytes()[..i]), OsStr::from_bytes(&self.as_bytes()[i+1..])); }
i += 1;
}
(&*self, OsStr::from_bytes(&self.as_bytes()[self.len()..self.len()]))
Expand Down
12 changes: 8 additions & 4 deletions tests/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ fn required_group_missing_arg() {

#[test]
fn group_single_value() {
let m = App::new("group")
let r = App::new("group")
.args_from_usage("-f, --flag 'some flag'
-c, --color [color] 'some option'")
.group(ArgGroup::with_name("grp")
.args(&["flag", "color"]))
.get_matches_from(vec!["myprog", "-c", "blue"]);
.get_matches_from_safe(vec!["myprog", "-c", "blue"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("grp"));
assert_eq!(m.value_of("grp").unwrap(), "blue");
}
Expand Down Expand Up @@ -68,12 +70,14 @@ fn group_reqired_flags_empty() {

#[test]
fn group_multi_value_single_arg() {
let m = App::new("group")
let r = App::new("group")
.args_from_usage("-f, --flag 'some flag'
-c, --color [color]... 'some option'")
.group(ArgGroup::with_name("grp")
.args(&["flag", "color"]))
.get_matches_from(vec!["myprog", "-c", "blue", "red", "green"]);
.get_matches_from_safe(vec!["myprog", "-c", "blue", "red", "green"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("grp"));
assert_eq!(m.values_of("grp").unwrap().collect::<Vec<_>>(), &["blue", "red", "green"]);
}
33 changes: 23 additions & 10 deletions tests/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use clap::{App, Arg};

#[test]
fn opts_using_short() {
let m = App::new("opts")
let r = App::new("opts")
.args(&mut [
Arg::from_usage("-f [flag] 'some flag'"),
Arg::from_usage("-c [color] 'some other flag'")
])
.get_matches_from(vec!["myprog", "-f", "some", "-c", "other"]);
.get_matches_from_safe(vec!["myprog", "-f", "some", "-c", "other"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("flag"));
assert_eq!(m.value_of("flag").unwrap(), "some");
assert!(m.is_present("color"));
Expand All @@ -18,12 +20,14 @@ fn opts_using_short() {

#[test]
fn opts_using_long_space() {
let m = App::new("opts")
let r = App::new("opts")
.args(&[
Arg::from_usage("--flag [flag] 'some flag'"),
Arg::from_usage("--color [color] 'some other flag'")
])
.get_matches_from(vec!["myprog", "--flag", "some", "--color", "other"]);
.get_matches_from_safe(vec!["myprog", "--flag", "some", "--color", "other"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("flag"));
assert_eq!(m.value_of("flag").unwrap(), "some");
assert!(m.is_present("color"));
Expand All @@ -32,12 +36,14 @@ fn opts_using_long_space() {

#[test]
fn opts_using_long_equals() {
let m = App::new("opts")
let r = App::new("opts")
.args(&[
Arg::from_usage("--flag [flag] 'some flag'"),
Arg::from_usage("--color [color] 'some other flag'")
])
.get_matches_from(vec!["myprog", "--flag=some", "--color=other"]);
.get_matches_from_safe(vec!["myprog", "--flag=some", "--color=other"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("flag"));
assert_eq!(m.value_of("flag").unwrap(), "some");
assert!(m.is_present("color"));
Expand All @@ -46,23 +52,30 @@ fn opts_using_long_equals() {

#[test]
fn opts_using_mixed() {
let m = App::new("opts")
let r = App::new("opts")
.args(&[
Arg::from_usage("-f, --flag [flag] 'some flag'"),
Arg::from_usage("-c, --color [color] 'some other flag'")
])
.get_matches_from(vec!["myprog", "-f", "some", "--color", "other"]);
.get_matches_from_safe(vec!["myprog", "-f", "some", "--color", "other"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("flag"));
assert_eq!(m.value_of("flag").unwrap(), "some");
assert!(m.is_present("color"));
assert_eq!(m.value_of("color").unwrap(), "other");
}

let m = App::new("opts")
#[test]
fn opts_using_mixed2() {
let r = App::new("opts")
.args(&[
Arg::from_usage("-f, --flag [flag] 'some flag'"),
Arg::from_usage("-c, --color [color] 'some other flag'")
])
.get_matches_from(vec!["myprog", "--flag=some", "-c", "other"]);
.get_matches_from_safe(vec!["myprog", "--flag=some", "-c", "other"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("flag"));
assert_eq!(m.value_of("flag").unwrap(), "some");
assert!(m.is_present("color"));
Expand Down

0 comments on commit 8476831

Please sign in to comment.