1use super::utils;
2
3use std::str;
4
5use clap::{Arg, ArgAction, Command};
6
7#[test]
8fn no_derive_order() {
9    static NO_DERIVE_ORDER: &str = "\
10Usage: test [OPTIONS]
11
12Options:
13      --flag_a               second flag
14      --flag_b               first flag
15  -h, --help                 Print help
16      --option_a <option_a>  second option
17      --option_b <option_b>  first option
18  -V, --version              Print version
19";
20
21    let cmd = Command::new("test")
22        .version("1.2")
23        .next_display_order(None)
24        .args([
25            Arg::new("flag_b")
26                .long("flag_b")
27                .help("first flag")
28                .action(ArgAction::SetTrue),
29            Arg::new("option_b")
30                .long("option_b")
31                .action(ArgAction::Set)
32                .help("first option"),
33            Arg::new("flag_a")
34                .long("flag_a")
35                .help("second flag")
36                .action(ArgAction::SetTrue),
37            Arg::new("option_a")
38                .long("option_a")
39                .action(ArgAction::Set)
40                .help("second option"),
41        ]);
42
43    utils::assert_output(cmd, "test --help", NO_DERIVE_ORDER, false);
44}
45
46#[test]
47fn derive_order() {
48    static UNIFIED_HELP_AND_DERIVE: &str = "\
49Usage: test [OPTIONS]
50
51Options:
52      --flag_b               first flag
53      --option_b <option_b>  first option
54      --flag_a               second flag
55      --option_a <option_a>  second option
56  -h, --help                 Print help
57  -V, --version              Print version
58";
59
60    let cmd = Command::new("test").version("1.2").args([
61        Arg::new("flag_b")
62            .long("flag_b")
63            .help("first flag")
64            .action(ArgAction::SetTrue),
65        Arg::new("option_b")
66            .long("option_b")
67            .action(ArgAction::Set)
68            .help("first option"),
69        Arg::new("flag_a")
70            .long("flag_a")
71            .help("second flag")
72            .action(ArgAction::SetTrue),
73        Arg::new("option_a")
74            .long("option_a")
75            .action(ArgAction::Set)
76            .help("second option"),
77    ]);
78
79    utils::assert_output(cmd, "test --help", UNIFIED_HELP_AND_DERIVE, false);
80}
81
82#[test]
83fn derive_order_next_order() {
84    static HELP: &str = "\
85Usage: test [OPTIONS]
86
87Options:
88      --flag_b               first flag
89      --option_b <option_b>  first option
90  -h, --help                 Print help
91  -V, --version              Print version
92      --flag_a               second flag
93      --option_a <option_a>  second option
94";
95
96    let cmd = Command::new("test")
97        .version("1.2")
98        .next_display_order(10000)
99        .arg(
100            Arg::new("flag_a")
101                .long("flag_a")
102                .help("second flag")
103                .action(ArgAction::SetTrue),
104        )
105        .arg(
106            Arg::new("option_a")
107                .long("option_a")
108                .action(ArgAction::Set)
109                .help("second option"),
110        )
111        .next_display_order(10)
112        .arg(
113            Arg::new("flag_b")
114                .long("flag_b")
115                .help("first flag")
116                .action(ArgAction::SetTrue),
117        )
118        .arg(
119            Arg::new("option_b")
120                .long("option_b")
121                .action(ArgAction::Set)
122                .help("first option"),
123        );
124
125    utils::assert_output(cmd, "test --help", HELP, false);
126}
127
128#[test]
129fn derive_order_no_next_order() {
130    static HELP: &str = "\
131Usage: test [OPTIONS]
132
133Options:
134      --flag_a               first flag
135      --flag_b               second flag
136  -h, --help                 Print help
137      --option_a <option_a>  first option
138      --option_b <option_b>  second option
139  -V, --version              Print version
140";
141
142    let cmd = Command::new("test")
143        .version("1.2")
144        .next_display_order(None)
145        .arg(
146            Arg::new("flag_a")
147                .long("flag_a")
148                .help("first flag")
149                .action(ArgAction::SetTrue),
150        )
151        .arg(
152            Arg::new("option_a")
153                .long("option_a")
154                .action(ArgAction::Set)
155                .help("first option"),
156        )
157        .arg(
158            Arg::new("flag_b")
159                .long("flag_b")
160                .help("second flag")
161                .action(ArgAction::SetTrue),
162        )
163        .arg(
164            Arg::new("option_b")
165                .long("option_b")
166                .action(ArgAction::Set)
167                .help("second option"),
168        );
169
170    utils::assert_output(cmd, "test --help", HELP, false);
171}
172
173#[test]
174fn derive_order_subcommand_propagate() {
175    static UNIFIED_DERIVE_SC_PROP: &str = "\
176Usage: test sub [OPTIONS]
177
178Options:
179      --flag_b               first flag
180      --option_b <option_b>  first option
181      --flag_a               second flag
182      --option_a <option_a>  second option
183  -h, --help                 Print help
184  -V, --version              Print version
185";
186
187    let cmd = Command::new("test").subcommand(
188        Command::new("sub").version("1.2").args([
189            Arg::new("flag_b")
190                .long("flag_b")
191                .help("first flag")
192                .action(ArgAction::SetTrue),
193            Arg::new("option_b")
194                .long("option_b")
195                .action(ArgAction::Set)
196                .help("first option"),
197            Arg::new("flag_a")
198                .long("flag_a")
199                .help("second flag")
200                .action(ArgAction::SetTrue),
201            Arg::new("option_a")
202                .long("option_a")
203                .action(ArgAction::Set)
204                .help("second option"),
205        ]),
206    );
207
208    utils::assert_output(cmd, "test sub --help", UNIFIED_DERIVE_SC_PROP, false);
209}
210
211#[test]
212fn derive_order_subcommand_propagate_with_explicit_display_order() {
213    static UNIFIED_DERIVE_SC_PROP_EXPLICIT_ORDER: &str = "\
214Usage: test sub [OPTIONS]
215
216Options:
217      --flag_a               second flag
218      --flag_b               first flag
219      --option_b <option_b>  first option
220      --option_a <option_a>  second option
221  -h, --help                 Print help
222  -V, --version              Print version
223";
224
225    let cmd = Command::new("test").subcommand(
226        Command::new("sub").version("1.2").args([
227            Arg::new("flag_b")
228                .long("flag_b")
229                .help("first flag")
230                .action(ArgAction::SetTrue),
231            Arg::new("option_b")
232                .long("option_b")
233                .action(ArgAction::Set)
234                .help("first option"),
235            Arg::new("flag_a")
236                .long("flag_a")
237                .help("second flag")
238                .display_order(0)
239                .action(ArgAction::SetTrue),
240            Arg::new("option_a")
241                .long("option_a")
242                .action(ArgAction::Set)
243                .help("second option"),
244        ]),
245    );
246
247    utils::assert_output(
248        cmd,
249        "test sub --help",
250        UNIFIED_DERIVE_SC_PROP_EXPLICIT_ORDER,
251        false,
252    );
253}
254
255#[test]
256fn subcommand_sorted_display_order() {
257    static SUBCMD_ALPHA_ORDER: &str = "\
258Usage: test [COMMAND]
259
260Commands:
261  a1    blah a1
262  b1    blah b1
263  help  Print this message or the help of the given subcommand(s)
264
265Options:
266  -h, --help     Print help
267  -V, --version  Print version
268";
269
270    let app_subcmd_alpha_order = Command::new("test")
271        .version("1")
272        .next_display_order(None)
273        .subcommands(vec![
274            Command::new("b1")
275                .about("blah b1")
276                .arg(Arg::new("test").short('t').action(ArgAction::SetTrue)),
277            Command::new("a1")
278                .about("blah a1")
279                .arg(Arg::new("roster").short('r').action(ArgAction::SetTrue)),
280        ]);
281
282    utils::assert_output(
283        app_subcmd_alpha_order,
284        "test --help",
285        SUBCMD_ALPHA_ORDER,
286        false,
287    );
288}
289
290#[test]
291fn subcommand_derived_display_order() {
292    static SUBCMD_DECL_ORDER: &str = "\
293Usage: test [COMMAND]
294
295Commands:
296  b1    blah b1
297  a1    blah a1
298  help  Print this message or the help of the given subcommand(s)
299
300Options:
301  -h, --help     Print help
302  -V, --version  Print version
303";
304
305    let app_subcmd_decl_order = Command::new("test").version("1").subcommands(vec![
306        Command::new("b1")
307            .about("blah b1")
308            .arg(Arg::new("test").short('t').action(ArgAction::SetTrue)),
309        Command::new("a1")
310            .about("blah a1")
311            .arg(Arg::new("roster").short('r').action(ArgAction::SetTrue)),
312    ]);
313
314    utils::assert_output(
315        app_subcmd_decl_order,
316        "test --help",
317        SUBCMD_DECL_ORDER,
318        false,
319    );
320}
321