Skip to content

Commit cba0ec1

Browse files
committed
tests(Usage Parser): adds and fixes tests for usage parser
1 parent fa3a2f8 commit cba0ec1

File tree

9 files changed

+1062
-950
lines changed

9 files changed

+1062
-950
lines changed

clap-tests/run_tests.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
-V, --version Prints version information
2323
2424
OPTIONS:
25+
-O, --Option <option3> tests options with specific value sets [values: fast slow]
26+
--long-option-2 <option2> tests long options with exclusions
2527
--maxvals3 <maxvals>... Tests 3 max vals
2628
--minvals2 <minvals>... Tests 2 min vals
2729
--multvals <one> <two> Tests mutliple values, not mult occs
28-
--multvalsmo <one> <two> Tests mutliple values, not mult occs
30+
--multvalsmo <one> <two> Tests mutliple values, and mult occs
2931
-o, --option <opt>... tests options
30-
--long-option-2 <option2> tests long options with exclusions
31-
-O, --Option <option3> tests options with specific value sets [values: fast slow]
3232
3333
ARGS:
3434
positional tests positionals
@@ -51,7 +51,7 @@
5151
5252
For more information try --help'''
5353

54-
_arg_dym_usage = '''error: The argument '--optio' wasn't recognized, or isn't valid in this context
54+
_arg_dym_usage = '''error: Found argument '--optio' which wasn't expected, or isn't valid in this context
5555
\tDid you mean --option ?
5656
5757
USAGE:

clap-tests/src/main.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use clap::{App, Arg, SubCommand};
55

66

77
fn main() {
8-
let m_val_names = ["one", "two"];
98
let args = "-o --option=[opt]... 'tests options'
109
[positional] 'tests positionals'";
1110
let opt3_vals = ["fast", "slow"];
@@ -18,13 +17,13 @@ fn main() {
1817
.arg(Arg::from_usage("-f --flag... 'tests flags'")
1918
.global(true))
2019
.args(&[
21-
Arg::from_usage("[flag2] -F 'tests flags with exclusions'").conflicts_with("flag").requires("option2"),
20+
Arg::from_usage("[flag2] -F 'tests flags with exclusions'").conflicts_with("flag").requires("long-option-2"),
2221
Arg::from_usage("--long-option-2 [option2] 'tests long options with exclusions'").conflicts_with("option").requires("positional2"),
2322
Arg::from_usage("[positional2] 'tests positionals with exclusions'"),
2423
Arg::from_usage("-O --Option [option3] 'tests options with specific value sets'").possible_values(&opt3_vals),
2524
Arg::from_usage("[positional3]... 'tests positionals with specific values'").possible_values(&pos3_vals),
26-
Arg::from_usage("--multvals [multvals] 'Tests mutliple values, not mult occs'").value_names(&m_val_names),
27-
Arg::from_usage("--multvalsmo [multvalsmo]... 'Tests mutliple values, not mult occs'").value_names(&m_val_names),
25+
Arg::from_usage("--multvals [one] [two] 'Tests mutliple values, not mult occs'"),
26+
Arg::from_usage("--multvalsmo... [one] [two] 'Tests mutliple values, and mult occs'"),
2827
Arg::from_usage("--minvals2 [minvals]... 'Tests 2 min vals'").min_values(2),
2928
Arg::from_usage("--maxvals3 [maxvals]... 'Tests 3 max vals'").max_values(3)
3029
])
@@ -42,11 +41,11 @@ fn main() {
4241
println!("flag NOT present");
4342
}
4443

45-
if matches.is_present("opt") {
46-
if let Some(v) = matches.value_of("opt") {
47-
println!("option present {} times with value: {}",matches.occurrences_of("opt"), v);
44+
if matches.is_present("option") {
45+
if let Some(v) = matches.value_of("option") {
46+
println!("option present {} times with value: {}",matches.occurrences_of("option"), v);
4847
}
49-
if let Some(ov) = matches.values_of("opt") {
48+
if let Some(ov) = matches.values_of("option") {
5049
for o in ov {
5150
println!("An option: {}", o);
5251
}
@@ -63,15 +62,15 @@ fn main() {
6362

6463
if matches.is_present("flag2") {
6564
println!("flag2 present");
66-
println!("option2 present with value of: {}", matches.value_of("option2").unwrap());
65+
println!("option2 present with value of: {}", matches.value_of("long-option-2").unwrap());
6766
println!("positional2 present with value of: {}", matches.value_of("positional2").unwrap());
6867
} else {
6968
println!("flag2 NOT present");
70-
println!("option2 maybe present with value of: {}", matches.value_of("option2").unwrap_or("Nothing"));
69+
println!("option2 maybe present with value of: {}", matches.value_of("long-option-2").unwrap_or("Nothing"));
7170
println!("positional2 maybe present with value of: {}", matches.value_of("positional2").unwrap_or("Nothing"));
7271
}
7372

74-
match matches.value_of("option3").unwrap_or("") {
73+
match matches.value_of("Option3").unwrap_or("") {
7574
"fast" => println!("option3 present quickly"),
7675
"slow" => println!("option3 present slowly"),
7776
_ => println!("option3 NOT present")
@@ -83,11 +82,11 @@ fn main() {
8382
_ => println!("positional3 NOT present")
8483
}
8584

86-
if matches.is_present("opt") {
87-
if let Some(v) = matches.value_of("opt") {
88-
println!("option present {} times with value: {}",matches.occurrences_of("opt"), v);
85+
if matches.is_present("option") {
86+
if let Some(v) = matches.value_of("option") {
87+
println!("option present {} times with value: {}",matches.occurrences_of("option"), v);
8988
}
90-
if let Some(ov) = matches.values_of("opt") {
89+
if let Some(ov) = matches.values_of("option") {
9190
for o in ov {
9291
println!("An option: {}", o);
9392
}
@@ -110,11 +109,11 @@ fn main() {
110109
println!("flag NOT present");
111110
}
112111

113-
if matches.is_present("scoption") {
114-
if let Some(v) = matches.value_of("scoption") {
112+
if matches.is_present("option") {
113+
if let Some(v) = matches.value_of("option") {
115114
println!("scoption present with value: {}", v);
116115
}
117-
if let Some(ov) = matches.values_of("scoption") {
116+
if let Some(ov) = matches.values_of("option") {
118117
for o in ov {
119118
println!("An scoption: {}", o);
120119
}

tests/flags.rs

+147-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate clap;
22

3-
use clap::{App, Arg};
3+
use clap::{App, Arg, ArgSettings};
44

55
#[test]
66
fn flag_using_short() {
@@ -9,7 +9,7 @@ fn flag_using_short() {
99
Arg::from_usage("-f, --flag 'some flag'"),
1010
Arg::from_usage("-c, --color 'some other flag'")
1111
])
12-
.get_matches_from(vec!["myprog", "-f", "-c"]);
12+
.get_matches_from(vec!["", "-f", "-c"]);
1313
assert!(m.is_present("flag"));
1414
assert!(m.is_present("color"));
1515
}
@@ -21,7 +21,7 @@ fn flag_using_long() {
2121
Arg::from_usage("--flag 'some flag'"),
2222
Arg::from_usage("--color 'some other flag'")
2323
])
24-
.get_matches_from(vec!["myprog", "--flag", "--color"]);
24+
.get_matches_from(vec!["", "--flag", "--color"]);
2525
assert!(m.is_present("flag"));
2626
assert!(m.is_present("color"));
2727
}
@@ -33,7 +33,7 @@ fn flag_using_mixed() {
3333
Arg::from_usage("-f, --flag 'some flag'"),
3434
Arg::from_usage("-c, --color 'some other flag'")
3535
])
36-
.get_matches_from(vec!["myprog", "-f", "--color"]);
36+
.get_matches_from(vec!["", "-f", "--color"]);
3737
assert!(m.is_present("flag"));
3838
assert!(m.is_present("color"));
3939

@@ -42,7 +42,7 @@ fn flag_using_mixed() {
4242
Arg::from_usage("-f, --flag 'some flag'"),
4343
Arg::from_usage("-c, --color 'some other flag'")
4444
])
45-
.get_matches_from(vec!["myprog", "--flag", "-c"]);
45+
.get_matches_from(vec!["", "--flag", "-c"]);
4646
assert!(m.is_present("flag"));
4747
assert!(m.is_present("color"));
4848
}
@@ -55,22 +55,157 @@ fn multiple_flags_in_single() {
5555
Arg::from_usage("-c, --color 'some other flag'"),
5656
Arg::from_usage("-d, --debug 'another other flag'")
5757
])
58-
.get_matches_from(vec!["myprog", "-fcd"]);
58+
.get_matches_from(vec!["", "-fcd"]);
5959
assert!(m.is_present("flag"));
6060
assert!(m.is_present("color"));
6161
assert!(m.is_present("debug"));
6262
}
6363

6464
#[test]
65-
#[should_panic]
6665
fn short_flag_misspel() {
67-
App::new("short_flag")
68-
.arg(Arg::from_usage("-f1, --flag 'some flag'"));
66+
let a = Arg::from_usage("-f1, --flag 'some flag'");
67+
assert_eq!(a.name, "flag");
68+
assert_eq!(a.short.unwrap(), 'f');
69+
assert_eq!(a.long.unwrap(), "flag");
70+
assert_eq!(a.help.unwrap(), "some flag");
71+
assert!(!a.is_set(ArgSettings::Multiple));
72+
assert!(a.val_names.is_none());
73+
assert!(a.num_vals.is_none());
6974
}
7075

7176
#[test]
72-
#[should_panic]
7377
fn short_flag_name_missing() {
74-
App::new("short_flag")
75-
.arg(Arg::from_usage("-f 'some flag'"));
78+
let a = Arg::from_usage("-f 'some flag'");
79+
assert_eq!(a.name, "f");
80+
assert_eq!(a.short.unwrap(), 'f');
81+
assert!(a.long.is_none());
82+
assert_eq!(a.help.unwrap(), "some flag");
83+
assert!(!a.is_set(ArgSettings::Multiple));
84+
assert!(a.val_names.is_none());
85+
assert!(a.num_vals.is_none());
86+
87+
}
88+
89+
#[test]
90+
fn create_flag() {
91+
let _ = App::new("test")
92+
.arg(Arg::with_name("test")
93+
.short("t")
94+
.long("test")
95+
.help("testing testing"))
96+
.get_matches();
97+
}
98+
99+
#[test]
100+
fn create_flag_usage() {
101+
let a = Arg::from_usage("[flag] -f 'some help info'");
102+
assert_eq!(a.name, "flag");
103+
assert_eq!(a.short.unwrap(), 'f');
104+
assert!(a.long.is_none());
105+
assert_eq!(a.help.unwrap(), "some help info");
106+
assert!(!a.is_set(ArgSettings::Multiple));
107+
assert!(a.val_names.is_none());
108+
assert!(a.num_vals.is_none());
109+
110+
let b = Arg::from_usage("[flag] --flag 'some help info'");
111+
assert_eq!(b.name, "flag");
112+
assert_eq!(b.long.unwrap(), "flag");
113+
assert!(b.short.is_none());
114+
assert_eq!(b.help.unwrap(), "some help info");
115+
assert!(!b.is_set(ArgSettings::Multiple));
116+
assert!(a.val_names.is_none());
117+
assert!(a.num_vals.is_none());
118+
119+
let b = Arg::from_usage("--flag 'some help info'");
120+
assert_eq!(b.name, "flag");
121+
assert_eq!(b.long.unwrap(), "flag");
122+
assert!(b.short.is_none());
123+
assert_eq!(b.help.unwrap(), "some help info");
124+
assert!(!b.is_set(ArgSettings::Multiple));
125+
assert!(b.val_names.is_none());
126+
assert!(b.num_vals.is_none());
127+
128+
let c = Arg::from_usage("[flag] -f --flag 'some help info'");
129+
assert_eq!(c.name, "flag");
130+
assert_eq!(c.short.unwrap(), 'f');
131+
assert_eq!(c.long.unwrap(), "flag");
132+
assert_eq!(c.help.unwrap(), "some help info");
133+
assert!(!c.is_set(ArgSettings::Multiple));
134+
assert!(c.val_names.is_none());
135+
assert!(c.num_vals.is_none());
136+
137+
let d = Arg::from_usage("[flag] -f... 'some help info'");
138+
assert_eq!(d.name, "flag");
139+
assert_eq!(d.short.unwrap(), 'f');
140+
assert!(d.long.is_none());
141+
assert_eq!(d.help.unwrap(), "some help info");
142+
assert!(d.is_set(ArgSettings::Multiple));
143+
assert!(d.val_names.is_none());
144+
assert!(d.num_vals.is_none());
145+
146+
let e = Arg::from_usage("[flag] -f --flag... 'some help info'");
147+
assert_eq!(e.name, "flag");
148+
assert_eq!(e.long.unwrap(), "flag");
149+
assert_eq!(e.short.unwrap(), 'f');
150+
assert_eq!(e.help.unwrap(), "some help info");
151+
assert!(e.is_set(ArgSettings::Multiple));
152+
assert!(e.val_names.is_none());
153+
assert!(e.num_vals.is_none());
154+
155+
let e = Arg::from_usage("-f --flag... 'some help info'");
156+
assert_eq!(e.name, "flag");
157+
assert_eq!(e.long.unwrap(), "flag");
158+
assert_eq!(e.short.unwrap(), 'f');
159+
assert_eq!(e.help.unwrap(), "some help info");
160+
assert!(e.is_set(ArgSettings::Multiple));
161+
assert!(e.val_names.is_none());
162+
assert!(e.num_vals.is_none());
163+
164+
let e = Arg::from_usage("--flags");
165+
assert_eq!(e.name, "flags");
166+
assert_eq!(e.long.unwrap(), "flags");
167+
assert!(e.val_names.is_none());
168+
assert!(e.num_vals.is_none());
169+
170+
let e = Arg::from_usage("--flags...");
171+
assert_eq!(e.name, "flags");
172+
assert_eq!(e.long.unwrap(), "flags");
173+
assert!(e.is_set(ArgSettings::Multiple));
174+
assert!(e.val_names.is_none());
175+
assert!(e.num_vals.is_none());
176+
177+
let e = Arg::from_usage("[flags] -f");
178+
assert_eq!(e.name, "flags");
179+
assert_eq!(e.short.unwrap(), 'f');
180+
assert!(e.val_names.is_none());
181+
assert!(e.num_vals.is_none());
182+
183+
let e = Arg::from_usage("[flags] -f...");
184+
assert_eq!(e.name, "flags");
185+
assert_eq!(e.short.unwrap(), 'f');
186+
assert!(e.is_set(ArgSettings::Multiple));
187+
assert!(e.val_names.is_none());
188+
assert!(e.num_vals.is_none());
189+
190+
let a = Arg::from_usage("-f 'some help info'");
191+
assert_eq!(a.name, "f");
192+
assert_eq!(a.short.unwrap(), 'f');
193+
assert!(a.long.is_none());
194+
assert_eq!(a.help.unwrap(), "some help info");
195+
assert!(!a.is_set(ArgSettings::Multiple));
196+
assert!(a.val_names.is_none());
197+
assert!(a.num_vals.is_none());
198+
199+
let e = Arg::from_usage("-f");
200+
assert_eq!(e.name, "f");
201+
assert_eq!(e.short.unwrap(), 'f');
202+
assert!(e.val_names.is_none());
203+
assert!(e.num_vals.is_none());
204+
205+
let e = Arg::from_usage("-f...");
206+
assert_eq!(e.name, "f");
207+
assert_eq!(e.short.unwrap(), 'f');
208+
assert!(e.is_set(ArgSettings::Multiple));
209+
assert!(e.val_names.is_none());
210+
assert!(e.num_vals.is_none());
76211
}

0 commit comments

Comments
 (0)