Skip to content

Commit 7ac5a5a

Browse files
committed
tests: cleans up certain test cases litle by little
1 parent 03e413d commit 7ac5a5a

File tree

7 files changed

+50
-40
lines changed

7 files changed

+50
-40
lines changed

src/app/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
mod settings;
2-
#[macro_use]
3-
mod macros;
42
pub mod parser;
53
mod meta;
64
mod help;

tests/global_args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate regex;
44
#[cfg(test)]
55
mod tests {
66
include!("../clap-test.rs");
7-
use clap::{App, Arg, SubCommand, ArgMatches};
7+
use clap::{App, Arg, SubCommand};
88

99
fn get_app() -> App<'static, 'static> {
1010
App::new("myprog")

tests/groups.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,45 @@ fn non_existing_arg() {
5555

5656
#[test]
5757
fn group_single_value() {
58-
let m = App::new("group")
58+
let res = App::new("group")
5959
.args_from_usage("-f, --flag 'some flag'
6060
-c, --color [color] 'some option'")
6161
.group(ArgGroup::with_name("grp")
6262
.args(&["flag", "color"]))
63-
.get_matches_from(vec!["", "-c", "blue"]);
63+
.get_matches_from_safe(vec!["", "-c", "blue"]);
64+
assert!(res.is_ok());
65+
66+
let m = res.unwrap();
6467
assert!(m.is_present("grp"));
6568
assert_eq!(m.value_of("grp").unwrap(), "blue");
6669
}
6770

6871
#[test]
6972
fn group_single_flag() {
70-
let m = App::new("group")
73+
let res = App::new("group")
7174
.args_from_usage("-f, --flag 'some flag'
7275
-c, --color [color] 'some option'")
7376
.group(ArgGroup::with_name("grp")
7477
.args(&["flag", "color"]))
75-
.get_matches_from(vec!["", "-f"]);
78+
.get_matches_from_safe(vec!["", "-f"]);
79+
assert!(res.is_ok());
80+
81+
let m = res.unwrap();
7682
assert!(m.is_present("grp"));
7783
assert!(m.value_of("grp").is_none());
7884
}
7985

8086
#[test]
8187
fn group_empty() {
82-
let m = App::new("group")
88+
let res = App::new("group")
8389
.args_from_usage("-f, --flag 'some flag'
8490
-c, --color [color] 'some option'")
8591
.group(ArgGroup::with_name("grp")
8692
.args(&["flag", "color"]))
87-
.get_matches_from(vec![""]);
93+
.get_matches_from_safe(vec![""]);
94+
assert!(res.is_ok());
95+
96+
let m = res.unwrap();
8897
assert!(!m.is_present("grp"));
8998
assert!(m.value_of("grp").is_none());
9099
}
@@ -105,12 +114,15 @@ fn group_reqired_flags_empty() {
105114

106115
#[test]
107116
fn group_multi_value_single_arg() {
108-
let m = App::new("group")
117+
let res = App::new("group")
109118
.args_from_usage("-f, --flag 'some flag'
110119
-c, --color [color]... 'some option'")
111120
.group(ArgGroup::with_name("grp")
112121
.args(&["flag", "color"]))
113-
.get_matches_from(vec!["", "-c", "blue", "red", "green"]);
122+
.get_matches_from_safe(vec!["", "-c", "blue", "red", "green"]);
123+
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);
124+
125+
let m = res.unwrap();
114126
assert!(m.is_present("grp"));
115127
assert_eq!(&*m.values_of("grp").unwrap().collect::<Vec<_>>(), &["blue", "red", "green"]);
116128
}
@@ -148,19 +160,7 @@ fn req_group_with_conflict_usage_string() {
148160
.args(&["base", "delete"])
149161
.required(true));
150162

151-
assert!(test::compare_output(app, "clap-test --delete base", REQ_GROUP_CONFLICT_REV, true));
152-
}
153-
154-
#[test]
155-
fn req_group_with_conflict_rev_usage_string() {
156-
let app = App::new("req_group")
157-
.arg(Arg::from_usage("[base] 'Base commit'").conflicts_with("delete"))
158-
.arg(Arg::from_usage("-d, --delete 'Remove the base commit information'"))
159-
.group(ArgGroup::with_name("base_or_delete")
160-
.args(&["base", "delete"])
161-
.required(true));
162-
163-
assert!(test::compare_output(app, "clap-test base --delete", REQ_GROUP_CONFLICT_USAGE, true));
163+
assert!(test::compare_output2(app, "clap-test --delete base", REQ_GROUP_CONFLICT_REV, REQ_GROUP_CONFLICT_USAGE, true));
164164
}
165165

166166
#[test]

tests/multiple_values.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1106,13 +1106,16 @@ fn multiple_value_terminator_option_other_arg() {
11061106

11071107
#[test]
11081108
fn multiple_vals_with_hyphen() {
1109-
let m = App::new("do")
1109+
let res = App::new("do")
11101110
.arg(Arg::with_name("cmds")
11111111
.multiple(true)
11121112
.allow_hyphen_values(true)
11131113
.value_terminator(";"))
11141114
.arg(Arg::with_name("location"))
1115-
.get_matches_from(vec!["do", "find", "-type", "f", "-name", "special", ";", "/home/clap"]);
1115+
.get_matches_from_safe(vec!["do", "find", "-type", "f", "-name", "special", ";", "/home/clap"]);
1116+
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);
1117+
1118+
let m = res.unwrap();
11161119
let cmds: Vec<_> = m.values_of("cmds").unwrap().collect();
11171120
assert_eq!(&cmds, &["find", "-type", "f", "-name", "special"]);
11181121
assert_eq!(m.value_of("location"), Some("/home/clap"));

tests/posix_compatible.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,13 @@ fn pos_required_overridden_by_flag() {
166166
#[test]
167167
fn require_overriden_2() {
168168
let m = App::new("require_overriden")
169-
.arg(Arg::with_name("flag")
170-
.index(1)
169+
.arg(Arg::with_name("req_pos")
171170
.required(true))
172171
.arg(Arg::from_usage("-c, --color 'other flag'")
173-
.overrides_with("flag"))
174-
.get_matches_from(vec!["", "-c", "flag"]);
172+
.overrides_with("req_pos"))
173+
.get_matches_from(vec!["", "-c", "req_pos"]);
175174
assert!(!m.is_present("color"));
176-
assert!(m.is_present("flag"));
175+
assert!(m.is_present("req_pos"));
177176
}
178177

179178
#[test]

tests/possible_values.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate regex;
33

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

6-
#[allow(unsused_imports)]
6+
#[allow(unused_imports)]
77
use std::ascii::AsciiExt;
88

99
use clap::{App, Arg, ErrorKind};

tests/subcommands.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,22 @@ SUBCOMMANDS:
3232
test Some help";
3333

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

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

@@ -129,8 +129,18 @@ fn multiple_aliases() {
129129
#[test]
130130
#[cfg(feature="suggestions")]
131131
fn subcmd_did_you_mean_output() {
132-
assert!(test::compare_output(test::complex_app(), "clap-test subcm", DYM, true));
133-
assert!(test::compare_output(test::complex_app(), "clap-test --subcm foo", DYM2, true));
132+
let app = App::new("dym")
133+
.subcommand(SubCommand::with_name("subcmd"));
134+
assert!(test::compare_output(app, "dym subcm", DYM_SUBCMD, true));
135+
}
136+
137+
#[test]
138+
#[cfg(feature="suggestions")]
139+
fn subcmd_did_you_mean_output_arg() {
140+
let app = App::new("dym")
141+
.subcommand(SubCommand::with_name("subcmd")
142+
.arg_from_usage("-s --subcmdarg [subcmdarg] 'tests'") );
143+
assert!(test::compare_output(app, "dym --subcm foo", DYM_ARG, true));
134144
}
135145

136146
#[test]

0 commit comments

Comments
 (0)