Skip to content

Commit 76d0049

Browse files
committed
test(parser): Verify defaulting on errors
1 parent 3f5c05c commit 76d0049

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

tests/builder/ignore_errors.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,38 @@ use super::utils;
44

55
#[test]
66
fn single_short_arg_without_value() {
7-
let cmd = Command::new("cmd").ignore_errors(true).arg(arg!(
8-
-c --config <FILE> "Sets a custom config file"
9-
));
7+
let cmd = Command::new("cmd")
8+
.ignore_errors(true)
9+
.arg(arg!(
10+
-c --config <FILE> "Sets a custom config file"
11+
))
12+
.arg(arg!(--"unset-flag"));
1013

1114
let r = cmd.try_get_matches_from(vec!["cmd", "-c" /* missing: , "config file" */]);
1215

1316
assert!(r.is_ok(), "unexpected error: {r:?}");
1417
let m = r.unwrap();
1518
assert!(m.contains_id("config"));
19+
assert_eq!(m.get_one::<String>("config").cloned(), None);
20+
assert_eq!(m.get_one::<bool>("unset-flag").copied(), Some(false));
1621
}
1722

1823
#[test]
1924
fn single_long_arg_without_value() {
20-
let cmd = Command::new("cmd").ignore_errors(true).arg(arg!(
21-
-c --config <FILE> "Sets a custom config file"
22-
));
25+
let cmd = Command::new("cmd")
26+
.ignore_errors(true)
27+
.arg(arg!(
28+
-c --config <FILE> "Sets a custom config file"
29+
))
30+
.arg(arg!(--"unset-flag"));
2331

2432
let r = cmd.try_get_matches_from(vec!["cmd", "--config" /* missing: , "config file" */]);
2533

2634
assert!(r.is_ok(), "unexpected error: {r:?}");
2735
let m = r.unwrap();
2836
assert!(m.contains_id("config"));
37+
assert_eq!(m.get_one::<String>("config").cloned(), None);
38+
assert_eq!(m.get_one::<bool>("unset-flag").copied(), Some(false));
2939
}
3040

3141
#[test]
@@ -38,7 +48,8 @@ fn multiple_args_and_final_arg_without_value() {
3848
.arg(arg!(
3949
-x --stuff <FILE> "Sets a custom stuff file"
4050
))
41-
.arg(arg!(f: -f "Flag").action(ArgAction::SetTrue));
51+
.arg(arg!(f: -f "Flag").action(ArgAction::SetTrue))
52+
.arg(arg!(--"unset-flag"));
4253

4354
let r = cmd.try_get_matches_from(vec![
4455
"cmd", "-c", "file", "-f", "-x", /* missing: , "some stuff" */
@@ -50,8 +61,9 @@ fn multiple_args_and_final_arg_without_value() {
5061
m.get_one::<String>("config").map(|v| v.as_str()),
5162
Some("file")
5263
);
53-
assert!(*m.get_one::<bool>("f").expect("defaulted by clap"));
64+
assert_eq!(m.get_one::<bool>("f").copied(), Some(true));
5465
assert_eq!(m.get_one::<String>("stuff").map(|v| v.as_str()), None);
66+
assert_eq!(m.get_one::<bool>("unset-flag").copied(), Some(false));
5567
}
5668

5769
#[test]
@@ -64,7 +76,8 @@ fn multiple_args_and_intermittent_arg_without_value() {
6476
.arg(arg!(
6577
-x --stuff <FILE> "Sets a custom stuff file"
6678
))
67-
.arg(arg!(f: -f "Flag").action(ArgAction::SetTrue));
79+
.arg(arg!(f: -f "Flag").action(ArgAction::SetTrue))
80+
.arg(arg!(--"unset-flag"));
6881

6982
let r = cmd.try_get_matches_from(vec![
7083
"cmd", "-x", /* missing: ,"some stuff" */
@@ -77,8 +90,9 @@ fn multiple_args_and_intermittent_arg_without_value() {
7790
m.get_one::<String>("config").map(|v| v.as_str()),
7891
Some("file")
7992
);
80-
assert!(*m.get_one::<bool>("f").expect("defaulted by clap"));
93+
assert_eq!(m.get_one::<bool>("f").copied(), Some(true));
8194
assert_eq!(m.get_one::<String>("stuff").map(|v| v.as_str()), None);
95+
assert_eq!(m.get_one::<bool>("unset-flag").copied(), Some(false));
8296
}
8397

8498
#[test]
@@ -100,9 +114,11 @@ fn subcommand() {
100114
.long("stuff")
101115
.action(ArgAction::Set)
102116
.help("stuf value"),
103-
),
117+
)
118+
.arg(arg!(--"unset-flag")),
104119
)
105-
.arg(Arg::new("other").long("other"));
120+
.arg(Arg::new("other").long("other"))
121+
.arg(arg!(--"unset-flag"));
106122

107123
let m = cmd
108124
.try_get_matches_from(vec![
@@ -125,6 +141,9 @@ fn subcommand() {
125141
sub_m.get_one::<String>("stuff").map(|v| v.as_str()),
126142
Some("some other val")
127143
);
144+
assert_eq!(sub_m.get_one::<bool>("unset-flag").copied(), Some(false));
145+
146+
assert_eq!(m.get_one::<bool>("unset-flag").copied(), Some(false));
128147
}
129148

130149
#[test]

0 commit comments

Comments
 (0)