Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
j-tai committed Nov 27, 2019
1 parent a6704cc commit 0c26a0e
Showing 1 changed file with 77 additions and 10 deletions.
87 changes: 77 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,16 +653,6 @@ mod tests {
assert_eq!(opts.arg_str(), None);
}

#[test]
#[should_panic]
fn multiple_values() {
let args = ["-a", "ay", "ay2", "bar"];
let opts = Options::new(&args);
assert_eq!(opts.next(), Some(Ok(Opt::Short('a'))));
assert_eq!(opts.value_str(), Ok("ay"));
let _ = opts.value_str(); // cannot get 2 values
}

#[test]
fn no_positional() {
let args = ["-a", "ay"];
Expand Down Expand Up @@ -790,4 +780,81 @@ mod tests {
}))
);
}

#[test]
fn subcommand() {
let args = ["-a", "cmd", "-b", "arg"];
let opts = Options::new(&args);
assert_eq!(opts.next(), Some(Ok(Opt::Short('a'))));
assert_eq!(opts.next(), None);
assert_eq!(opts.arg_str(), Some(&"cmd"));
assert_eq!(opts.next(), Some(Ok(Opt::Short('b'))));
assert_eq!(opts.next(), None);
assert_eq!(opts.arg_str(), Some(&"arg"));
}

// Things you shouldn't need too often

#[test]
fn keep_retrieving_options() {
let args = ["-a", "ay", "ay2", "bar"];
let opts = Options::new(&args);
assert_eq!(opts.next(), Some(Ok(Opt::Short('a'))));
assert_eq!(opts.next(), None);
assert_eq!(opts.next(), None);
assert_eq!(opts.next(), None);
assert_eq!(opts.next(), None);
}

#[test]
fn keep_retrieving_options_2() {
let args = ["-a", "--", "-b", "--see"];
let opts = Options::new(&args);
assert_eq!(opts.next(), Some(Ok(Opt::Short('a'))));
assert_eq!(opts.next(), None);
assert_eq!(opts.next(), Some(Ok(Opt::Short('b'))));
assert_eq!(opts.next(), Some(Ok(Opt::Long("see"))));
assert_eq!(opts.next(), None);
}

// Things you definitely shouldn't do

#[test]
#[should_panic]
fn keep_taking_values() {
let args = ["-a", "ay", "ay2", "bar"];
let opts = Options::new(&args);
let _ = opts.next(); // -a
let _ = opts.value_str(); // ay
let _ = opts.value_str(); // panic: cannot get 2 values
}

#[test]
fn keep_taking_args() {
let args = ["-a", "ay"];
let opts = Options::new(&args);
assert_eq!(opts.arg_str(), Some(&"-a"));
assert_eq!(opts.arg_str(), Some(&"ay"));
assert_eq!(opts.arg_str(), None);
assert_eq!(opts.arg_str(), None);
assert_eq!(opts.arg_str(), None);
assert_eq!(opts.arg_str(), None);
}

#[test]
#[should_panic]
fn value_without_option() {
let args = ["-a", "ay"];
let opts = Options::new(&args);
let _ = opts.value_str(); // no option retrieved yet
}

#[test]
#[should_panic]
fn value_after_arg() {
let args = ["ay", "bee"];
let opts = Options::new(&args);
let _ = opts.arg_str(); // ay
let _ = opts.value_str(); // no option retrieved yet
}
}

0 comments on commit 0c26a0e

Please sign in to comment.