Skip to content

Commit c10231d

Browse files
committed
test(derive): Rely on snapshot testing
`contains` assertions can be fairly brittle, easy to miss when wrong, and hard to update.
1 parent 0096ace commit c10231d

13 files changed

+540
-92
lines changed

tests/derive/app_name.rs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use clap::CommandFactory;
22
use clap::Parser;
3+
use snapbox::assert_data_eq;
4+
use snapbox::prelude::*;
5+
use snapbox::str;
36

47
use crate::utils::get_help;
58
use crate::utils::get_long_help;
@@ -12,7 +15,13 @@ fn app_name_in_short_help_from_struct() {
1215

1316
let help = get_help::<MyApp>();
1417

15-
assert!(help.contains("my-cmd"));
18+
assert_data_eq!(help, str![[r#"
19+
Usage: my-cmd
20+
21+
Options:
22+
-h, --help Print help
23+
24+
"#]].raw());
1625
}
1726

1827
#[test]
@@ -23,7 +32,13 @@ fn app_name_in_long_help_from_struct() {
2332

2433
let help = get_help::<MyApp>();
2534

26-
assert!(help.contains("my-cmd"));
35+
assert_data_eq!(help, str![[r#"
36+
Usage: my-cmd
37+
38+
Options:
39+
-h, --help Print help
40+
41+
"#]].raw());
2742
}
2843

2944
#[test]
@@ -34,7 +49,13 @@ fn app_name_in_short_help_from_enum() {
3449

3550
let help = get_help::<MyApp>();
3651

37-
assert!(help.contains("my-cmd"));
52+
assert_data_eq!(help, str![[r#"
53+
Usage: my-cmd
54+
55+
Options:
56+
-h, --help Print help
57+
58+
"#]].raw());
3859
}
3960

4061
#[test]
@@ -45,7 +66,14 @@ fn app_name_in_long_help_from_enum() {
4566

4667
let help = get_long_help::<MyApp>();
4768

48-
assert!(help.contains("my-cmd"));
69+
assert_data_eq!(help, str![[r#"
70+
Usage: my-cmd
71+
72+
Options:
73+
-h, --help
74+
Print help
75+
76+
"#]].raw());
4977
}
5078

5179
#[test]
@@ -56,7 +84,10 @@ fn app_name_in_short_version_from_struct() {
5684

5785
let version = MyApp::command().render_version();
5886

59-
assert!(version.contains("my-cmd"));
87+
assert_data_eq!(version, str![[r#"
88+
my-cmd
89+
90+
"#]].raw());
6091
}
6192

6293
#[test]
@@ -67,7 +98,10 @@ fn app_name_in_long_version_from_struct() {
6798

6899
let version = MyApp::command().render_long_version();
69100

70-
assert!(version.contains("my-cmd"));
101+
assert_data_eq!(version, str![[r#"
102+
my-cmd
103+
104+
"#]].raw());
71105
}
72106

73107
#[test]
@@ -78,7 +112,10 @@ fn app_name_in_short_version_from_enum() {
78112

79113
let version = MyApp::command().render_version();
80114

81-
assert!(version.contains("my-cmd"));
115+
assert_data_eq!(version, str![[r#"
116+
my-cmd
117+
118+
"#]].raw());
82119
}
83120

84121
#[test]
@@ -89,5 +126,8 @@ fn app_name_in_long_version_from_enum() {
89126

90127
let version = MyApp::command().render_long_version();
91128

92-
assert!(version.contains("my-cmd"));
129+
assert_data_eq!(version, str![[r#"
130+
my-cmd
131+
132+
"#]].raw());
93133
}

tests/derive/arguments.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
1313
// MIT/Apache 2.0 license.
1414

15-
use clap::Parser;
16-
1715
use crate::utils::get_help;
1816

17+
use clap::Parser;
18+
use snapbox::assert_data_eq;
19+
use snapbox::prelude::*;
20+
use snapbox::str;
21+
1922
#[test]
2023
fn required_argument() {
2124
#[derive(Parser, PartialEq, Debug)]
@@ -54,7 +57,17 @@ fn auto_value_name() {
5457

5558
let help = get_help::<Opt>();
5659

57-
assert!(help.contains("MY_SPECIAL_ARG"));
60+
assert_data_eq!(help, str![[r#"
61+
Usage: clap <MY_SPECIAL_ARG>
62+
63+
Arguments:
64+
<MY_SPECIAL_ARG>
65+
66+
Options:
67+
-h, --help Print help
68+
69+
"#]].raw());
70+
5871
// Ensure the implicit `num_vals` is just 1
5972
assert_eq!(
6073
Opt { my_special_arg: 10 },
@@ -72,8 +85,17 @@ fn explicit_value_name() {
7285

7386
let help = get_help::<Opt>();
7487

75-
assert!(help.contains("BROWNIE_POINTS"));
76-
assert!(!help.contains("MY_SPECIAL_ARG"));
88+
assert_data_eq!(help, str![[r#"
89+
Usage: clap <BROWNIE_POINTS>
90+
91+
Arguments:
92+
<BROWNIE_POINTS>
93+
94+
Options:
95+
-h, --help Print help
96+
97+
"#]].raw());
98+
7799
// Ensure the implicit `num_vals` is just 1
78100
assert_eq!(
79101
Opt { my_special_arg: 10 },

tests/derive/author_version_about.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
1313
// MIT/Apache 2.0 license.
1414

15-
use crate::utils;
16-
1715
use clap::Parser;
16+
use snapbox::assert_data_eq;
17+
use snapbox::prelude::*;
18+
use snapbox::str;
19+
20+
use crate::utils;
1821

1922
#[test]
2023
fn no_author_version_about() {
@@ -35,9 +38,20 @@ fn use_env() {
3538
struct Opt {}
3639

3740
let output = utils::get_long_help::<Opt>();
38-
assert!(output.starts_with("clap"));
39-
assert!(output
40-
.contains("A simple to use, efficient, and full-featured Command Line Argument Parser"));
41+
assert_data_eq!(output, str![[r#"
42+
clap 4.5.43
43+
A simple to use, efficient, and full-featured Command Line Argument Parser
44+
45+
Usage: clap
46+
47+
Options:
48+
-h, --help
49+
Print help
50+
51+
-V, --version
52+
Print version
53+
54+
"#]].raw());
4155
}
4256

4357
#[test]
@@ -50,5 +64,17 @@ fn explicit_version_not_str_lit() {
5064
pub(crate) struct Opt {}
5165

5266
let output = utils::get_long_help::<Opt>();
53-
assert!(output.contains("custom version"));
67+
assert_data_eq!(output, str![[r#"
68+
clap custom version
69+
70+
Usage: clap
71+
72+
Options:
73+
-h, --help
74+
Print help
75+
76+
-V, --version
77+
Print version
78+
79+
"#]].raw());
5480
}

tests/derive/custom_string_parsers.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
1313
// MIT/Apache 2.0 license.
1414

15-
use clap::Parser;
16-
1715
use std::num::ParseIntError;
1816
use std::path::PathBuf;
1917

18+
use clap::Parser;
19+
use snapbox::assert_data_eq;
20+
use snapbox::prelude::*;
21+
use snapbox::str;
22+
2023
#[derive(Parser, PartialEq, Debug)]
2124
struct PathOpt {
2225
#[arg(short, long)]
@@ -82,11 +85,12 @@ fn test_parse_hex() {
8285
);
8386

8487
let err = HexOpt::try_parse_from(["test", "-n", "gg"]).unwrap_err();
85-
assert!(
86-
err.to_string().contains("invalid digit found in string"),
87-
"{}",
88-
err
89-
);
88+
assert_data_eq!(err.to_string(), str![[r#"
89+
error: invalid value 'gg' for '-n <NUMBER>': invalid digit found in string
90+
91+
For more information, try '--help'.
92+
93+
"#]].raw());
9094
}
9195

9296
#[derive(Debug)]

0 commit comments

Comments
 (0)