Skip to content

Commit cbd09c7

Browse files
committed
tests: refactors the tests for propagating global values and flags as well as adds some tests for default values and flags
1 parent 0d6cd6e commit cbd09c7

File tree

1 file changed

+74
-75
lines changed

1 file changed

+74
-75
lines changed

tests/propagate_globals.rs

+74-75
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,27 @@ extern crate regex;
44
#[cfg(test)]
55
mod tests {
66
include!("../clap-test.rs");
7-
use clap::{App, Arg, SubCommand, AppSettings, ArgMatches};
8-
9-
fn get_global_arg() -> Arg<'static, 'static> {
10-
Arg::with_name("GLOBAL_ARG")
11-
.long("global-arg")
12-
.help(
13-
"Specifies something needed by the subcommands",
14-
)
15-
.global(true)
16-
.takes_value(true)
17-
}
18-
19-
fn get_global_flag() -> Arg<'static, 'static> {
20-
Arg::with_name("GLOBAL_FLAG")
21-
.long("global-flag")
22-
.help(
23-
"Specifies something needed by the subcommands",
24-
)
25-
.global(true)
26-
}
7+
use clap::{App, Arg, SubCommand, ArgMatches};
278

289
fn get_app() -> App<'static, 'static> {
2910
App::new("myprog")
30-
.global_setting(AppSettings::PropagateGlobalValuesDown)
31-
}
32-
33-
fn get_subcommands() -> App<'static, 'static> {
34-
SubCommand::with_name("outer")
35-
.subcommand(SubCommand::with_name("inner"))
11+
.arg(Arg::with_name("GLOBAL_ARG")
12+
.long("global-arg")
13+
.help(
14+
"Specifies something needed by the subcommands",
15+
)
16+
.global(true)
17+
.takes_value(true)
18+
.default_value("default_value"))
19+
.arg(Arg::with_name("GLOBAL_FLAG")
20+
.long("global-flag")
21+
.help(
22+
"Specifies something needed by the subcommands",
23+
)
24+
.multiple(true)
25+
.global(true))
26+
.subcommand(SubCommand::with_name("outer")
27+
.subcommand(SubCommand::with_name("inner")))
3628
}
3729

3830
fn get_matches(app: App<'static, 'static>, argv: &'static str) -> ArgMatches<'static> {
@@ -59,6 +51,10 @@ mod tests {
5951
get_outer_matches(m).value_of("GLOBAL_ARG") == val.into()
6052
}
6153

54+
fn top_can_access_flag(m: &ArgMatches<'static>, present: bool, occurrences: u64) -> bool {
55+
(m.is_present("GLOBAL_FLAG") == present) && (m.occurrences_of("GLOBAL_FLAG") == occurrences)
56+
}
57+
6258
fn inner_can_access_flag(m: &ArgMatches<'static>, present: bool, occurrences: u64) -> bool {
6359
let m = get_inner_matches(m);
6460
(m.is_present("GLOBAL_FLAG") == present) && (m.occurrences_of("GLOBAL_FLAG") == occurrences)
@@ -70,80 +66,83 @@ mod tests {
7066
}
7167

7268
#[test]
73-
fn global_arg_defined_top_level_used_top_level() {
74-
let app = get_app()
75-
.arg(get_global_arg())
76-
.subcommand(get_subcommands());
77-
78-
let m = get_matches(app, "myprog --global-arg=some_value outer inner");
69+
fn global_arg_used_top_level() {
70+
let m = get_matches(get_app(), "myprog --global-arg=some_value outer inner");
7971

8072
assert!(top_can_access_arg(&m, "some_value"));
8173
assert!(inner_can_access_arg(&m, "some_value"));
8274
assert!(outer_can_access_arg(&m, "some_value"));
8375
}
8476

8577
#[test]
86-
fn global_arg_defined_top_level_used_outer() {
87-
let app = get_app()
88-
.arg(get_global_arg())
89-
.subcommand(get_subcommands());
90-
91-
let m = get_matches(app, "myprog outer --global-arg=some_value inner");
78+
fn global_arg_used_outer() {
79+
let m = get_matches(get_app(), "myprog outer --global-arg=some_value inner");
9280

9381
assert!(top_can_access_arg(&m, "some_value"));
9482
assert!(inner_can_access_arg(&m, "some_value"));
9583
assert!(outer_can_access_arg(&m, "some_value"));
9684
}
9785

9886
#[test]
99-
fn global_arg_defined_top_level_used_inner() {
100-
let app = get_app()
101-
.arg(get_global_arg())
102-
.subcommand(get_subcommands());
87+
fn global_arg_used_inner() {
88+
let m = get_matches(get_app(), "myprog outer inner --global-arg=some_value");
10389

104-
let m = get_matches(app, "myprog outer inner --global-arg=some_value");
105-
106-
assert!(top_can_access_arg(&m, "some_value"), "{:?}", m);
90+
assert!(top_can_access_arg(&m, "some_value"));
10791
assert!(inner_can_access_arg(&m, "some_value"));
10892
assert!(outer_can_access_arg(&m, "some_value"));
10993
}
11094

111-
// #[test]
112-
// fn global_arg_defined_nested_used_top_level() {
113-
// let app = get_app()
114-
// .subcommand(get_subcommands()
115-
// .arg(get_global_arg()));
95+
#[test]
96+
fn global_arg_default_value() {
97+
let m = get_matches(get_app(), "myprog outer inner");
11698

117-
// let m = get_matches(app, "myprog --global-arg=some_value outer inner");
99+
assert!(top_can_access_arg(&m, "default_value"));
100+
assert!(inner_can_access_arg(&m, "default_value"));
101+
assert!(outer_can_access_arg(&m, "default_value"));
102+
}
118103

119-
// assert!(top_can_access_arg(&m, "some_value"));
120-
// assert!(inner_can_access_arg(&m, "some_value"));
121-
// assert!(outer_can_access_arg(&m, "some_value"));
122-
// }
104+
#[test]
105+
fn global_flag_used_top_level() {
106+
let m = get_matches(get_app(), "myprog --global-flag outer inner");
123107

124-
// #[test]
125-
// fn global_arg_defined_nested_used_outer() {
126-
// let app = get_app()
127-
// .subcommand(get_subcommands()
128-
// .arg(get_global_arg()));
108+
assert!(top_can_access_flag(&m, true, 1));
109+
assert!(inner_can_access_flag(&m, true, 1));
110+
assert!(outer_can_access_flag(&m, true, 1));
111+
}
129112

130-
// let m = get_matches(app, "myprog outer --global-arg=some_value inner");
113+
#[test]
114+
fn global_flag_used_outer() {
115+
let m = get_matches(get_app(), "myprog outer --global-flag inner");
131116

132-
// assert!(top_can_access_arg(&m, "some_value"));
133-
// assert!(inner_can_access_arg(&m, "some_value"));
134-
// assert!(outer_can_access_arg(&m, "some_value"));
135-
// }
117+
assert!(top_can_access_flag(&m, true, 1));
118+
assert!(inner_can_access_flag(&m, true, 1));
119+
assert!(outer_can_access_flag(&m, true, 1));
120+
}
136121

137-
// #[test]
138-
// fn global_arg_defined_nested_used_inner() {
139-
// let app = get_app()
140-
// .subcommand(get_subcommands()
141-
// .arg(get_global_arg()));
122+
#[test]
123+
fn global_flag_used_inner() {
124+
let m = get_matches(get_app(), "myprog outer inner --global-flag");
142125

143-
// let m = get_matches(app, "myprog outer inner --global-arg=some_value");
126+
assert!(top_can_access_flag(&m, true, 1));
127+
assert!(inner_can_access_flag(&m, true, 1));
128+
assert!(outer_can_access_flag(&m, true, 1));
129+
}
130+
131+
#[test]
132+
fn global_flag_2x_used_top_level() {
133+
let m = get_matches(get_app(), "myprog --global-flag --global-flag outer inner");
134+
135+
assert!(top_can_access_flag(&m, true, 2));
136+
assert!(inner_can_access_flag(&m, true, 2));
137+
assert!(outer_can_access_flag(&m, true, 2));
138+
}
144139

145-
// assert!(top_can_access_arg(&m, "some_value"));
146-
// assert!(inner_can_access_arg(&m, "some_value"));
147-
// assert!(outer_can_access_arg(&m, "some_value"));
148-
// }
140+
#[test]
141+
fn global_flag_2x_used_inner() {
142+
let m = get_matches(get_app(), "myprog outer inner --global-flag --global-flag");
143+
144+
assert!(top_can_access_flag(&m, true, 2));
145+
assert!(inner_can_access_flag(&m, true, 2));
146+
assert!(outer_can_access_flag(&m, true, 2));
147+
}
149148
}

0 commit comments

Comments
 (0)