Skip to content

Commit

Permalink
tests: cleans up certain test cases litle by little
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Jan 9, 2018
1 parent 03e413d commit 7ac5a5a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 40 deletions.
2 changes: 0 additions & 2 deletions src/app/mod.rs
@@ -1,6 +1,4 @@
mod settings;
#[macro_use]
mod macros;
pub mod parser;
mod meta;
mod help;
Expand Down
2 changes: 1 addition & 1 deletion tests/global_args.rs
Expand Up @@ -4,7 +4,7 @@ extern crate regex;
#[cfg(test)]
mod tests {
include!("../clap-test.rs");
use clap::{App, Arg, SubCommand, ArgMatches};
use clap::{App, Arg, SubCommand};

fn get_app() -> App<'static, 'static> {
App::new("myprog")
Expand Down
42 changes: 21 additions & 21 deletions tests/groups.rs
Expand Up @@ -55,36 +55,45 @@ fn non_existing_arg() {

#[test]
fn group_single_value() {
let m = App::new("group")
let res = 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!["", "-c", "blue"]);
.get_matches_from_safe(vec!["", "-c", "blue"]);
assert!(res.is_ok());

let m = res.unwrap();
assert!(m.is_present("grp"));
assert_eq!(m.value_of("grp").unwrap(), "blue");
}

#[test]
fn group_single_flag() {
let m = App::new("group")
let res = 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!["", "-f"]);
.get_matches_from_safe(vec!["", "-f"]);
assert!(res.is_ok());

let m = res.unwrap();
assert!(m.is_present("grp"));
assert!(m.value_of("grp").is_none());
}

#[test]
fn group_empty() {
let m = App::new("group")
let res = 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![""]);
.get_matches_from_safe(vec![""]);
assert!(res.is_ok());

let m = res.unwrap();
assert!(!m.is_present("grp"));
assert!(m.value_of("grp").is_none());
}
Expand All @@ -105,12 +114,15 @@ fn group_reqired_flags_empty() {

#[test]
fn group_multi_value_single_arg() {
let m = App::new("group")
let res = 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!["", "-c", "blue", "red", "green"]);
.get_matches_from_safe(vec!["", "-c", "blue", "red", "green"]);
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);

let m = res.unwrap();
assert!(m.is_present("grp"));
assert_eq!(&*m.values_of("grp").unwrap().collect::<Vec<_>>(), &["blue", "red", "green"]);
}
Expand Down Expand Up @@ -148,19 +160,7 @@ fn req_group_with_conflict_usage_string() {
.args(&["base", "delete"])
.required(true));

assert!(test::compare_output(app, "clap-test --delete base", REQ_GROUP_CONFLICT_REV, true));
}

#[test]
fn req_group_with_conflict_rev_usage_string() {
let app = App::new("req_group")
.arg(Arg::from_usage("[base] 'Base commit'").conflicts_with("delete"))
.arg(Arg::from_usage("-d, --delete 'Remove the base commit information'"))
.group(ArgGroup::with_name("base_or_delete")
.args(&["base", "delete"])
.required(true));

assert!(test::compare_output(app, "clap-test base --delete", REQ_GROUP_CONFLICT_USAGE, true));
assert!(test::compare_output2(app, "clap-test --delete base", REQ_GROUP_CONFLICT_REV, REQ_GROUP_CONFLICT_USAGE, true));
}

#[test]
Expand Down
7 changes: 5 additions & 2 deletions tests/multiple_values.rs
Expand Up @@ -1106,13 +1106,16 @@ fn multiple_value_terminator_option_other_arg() {

#[test]
fn multiple_vals_with_hyphen() {
let m = App::new("do")
let res = App::new("do")
.arg(Arg::with_name("cmds")
.multiple(true)
.allow_hyphen_values(true)
.value_terminator(";"))
.arg(Arg::with_name("location"))
.get_matches_from(vec!["do", "find", "-type", "f", "-name", "special", ";", "/home/clap"]);
.get_matches_from_safe(vec!["do", "find", "-type", "f", "-name", "special", ";", "/home/clap"]);
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);

let m = res.unwrap();
let cmds: Vec<_> = m.values_of("cmds").unwrap().collect();
assert_eq!(&cmds, &["find", "-type", "f", "-name", "special"]);
assert_eq!(m.value_of("location"), Some("/home/clap"));
Expand Down
9 changes: 4 additions & 5 deletions tests/posix_compatible.rs
Expand Up @@ -166,14 +166,13 @@ fn pos_required_overridden_by_flag() {
#[test]
fn require_overriden_2() {
let m = App::new("require_overriden")
.arg(Arg::with_name("flag")
.index(1)
.arg(Arg::with_name("req_pos")
.required(true))
.arg(Arg::from_usage("-c, --color 'other flag'")
.overrides_with("flag"))
.get_matches_from(vec!["", "-c", "flag"]);
.overrides_with("req_pos"))
.get_matches_from(vec!["", "-c", "req_pos"]);
assert!(!m.is_present("color"));
assert!(m.is_present("flag"));
assert!(m.is_present("req_pos"));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion tests/possible_values.rs
Expand Up @@ -3,7 +3,7 @@ extern crate regex;

include!("../clap-test.rs");

#[allow(unsused_imports)]
#[allow(unused_imports)]
use std::ascii::AsciiExt;

use clap::{App, Arg, ErrorKind};
Expand Down
26 changes: 18 additions & 8 deletions tests/subcommands.rs
Expand Up @@ -32,22 +32,22 @@ SUBCOMMANDS:
test Some help";

#[cfg(feature = "suggestions")]
static DYM: &'static str = "error: The subcommand 'subcm' wasn't recognized
\tDid you mean 'subcmd'?
static DYM_SUBCMD: &'static str = "error: The subcommand 'subcm' wasn't recognized
Did you mean 'subcmd'?
If you believe you received this message in error, try re-running with 'clap-test -- subcm'
If you believe you received this message in error, try re-running with 'dym -- subcm'
USAGE:
clap-test [FLAGS] [OPTIONS] [ARGS] [SUBCOMMAND]
dym [SUBCOMMAND]
For more information try --help";

#[cfg(feature = "suggestions")]
static DYM2: &'static str = "error: Found argument '--subcm' which wasn't expected, or isn't valid in this context
static DYM_ARG: &'static str = "error: Found argument '--subcm' which wasn't expected, or isn't valid in this context
\tDid you mean to put '--subcmdarg' after the subcommand 'subcmd'?
USAGE:
clap-test [FLAGS] [OPTIONS] [ARGS] [SUBCOMMAND]
dym [SUBCOMMAND]
For more information try --help";

Expand Down Expand Up @@ -129,8 +129,18 @@ fn multiple_aliases() {
#[test]
#[cfg(feature="suggestions")]
fn subcmd_did_you_mean_output() {
assert!(test::compare_output(test::complex_app(), "clap-test subcm", DYM, true));
assert!(test::compare_output(test::complex_app(), "clap-test --subcm foo", DYM2, true));
let app = App::new("dym")
.subcommand(SubCommand::with_name("subcmd"));
assert!(test::compare_output(app, "dym subcm", DYM_SUBCMD, true));
}

#[test]
#[cfg(feature="suggestions")]
fn subcmd_did_you_mean_output_arg() {
let app = App::new("dym")
.subcommand(SubCommand::with_name("subcmd")
.arg_from_usage("-s --subcmdarg [subcmdarg] 'tests'") );
assert!(test::compare_output(app, "dym --subcm foo", DYM_ARG, true));
}

#[test]
Expand Down

0 comments on commit 7ac5a5a

Please sign in to comment.