blob: e2ac8c719682b476548d857a9415b092dffe8c5c [file] [log] [blame]
Kevin Kf17e1502016-01-25 20:56:371use clap::{App, Arg, ArgSettings};
Alexander Kuvaev0cfecd32015-08-27 21:03:452
3#[test]
4fn flag_using_short() {
5 let m = App::new("flag")
Kevin K0031d782016-01-21 06:48:306 .args(&[
Corentin Henry4f602b72018-04-21 18:59:197 Arg::from("-f, --flag 'some flag'"),
8 Arg::from("-c, --color 'some other flag'"),
Kevin K1ab10272018-01-25 04:05:059 ])
Kevin Kf17e1502016-01-25 20:56:3710 .get_matches_from(vec!["", "-f", "-c"]);
Alexander Kuvaev0cfecd32015-08-27 21:03:4511 assert!(m.is_present("flag"));
12 assert!(m.is_present("color"));
13}
14
15#[test]
Kevin K07dfdd02016-02-02 12:43:5316fn lots_o_flags_sep() {
17 let r = App::new("opts")
Corentin Henry4f602b72018-04-21 18:59:1918 .arg(Arg::from("-o... 'some flag'"))
Kevin K03333802018-10-19 20:42:1319 .try_get_matches_from(vec![
Kevin K1ab10272018-01-25 04:05:0520 "", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
21 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
22 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
23 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
24 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
25 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
26 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
27 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
28 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
29 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
30 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
31 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
32 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
33 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
34 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
35 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
36 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
37 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
38 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
39 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
40 "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
41 "-o", "-o", "-o",
42 ]);
Kevin K97fd3f12018-01-31 20:15:0143 assert!(r.is_ok(), "{:?}", r.unwrap_err().kind);
Kevin K07dfdd02016-02-02 12:43:5344 let m = r.unwrap();
45 assert!(m.is_present("o"));
46 assert_eq!(m.occurrences_of("o"), 297); // i.e. more than u8
47}
48
49#[test]
50fn lots_o_flags_combined() {
51 let r = App::new("opts")
Corentin Henry4f602b72018-04-21 18:59:1952 .arg(Arg::from("-o... 'some flag'"))
Kevin K03333802018-10-19 20:42:1353 .try_get_matches_from(vec![
Kevin K1ab10272018-01-25 04:05:0554 "",
Kevin K07dfdd02016-02-02 12:43:5355 "-oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
56 "-oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
57 "-oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
58 "-oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
59 "-ooooooooooooooooooooooooooooooooooooooooo",
Kevin K1ab10272018-01-25 04:05:0560 ]);
Kevin K97fd3f12018-01-31 20:15:0161 assert!(r.is_ok(), "{:?}", r.unwrap_err().kind);
Kevin K07dfdd02016-02-02 12:43:5362 let m = r.unwrap();
63 assert!(m.is_present("o"));
64 assert_eq!(m.occurrences_of("o"), 297); // i.e. more than u8
65}
66
67#[test]
Alexander Kuvaev0cfecd32015-08-27 21:03:4568fn flag_using_long() {
69 let m = App::new("flag")
Kevin K0031d782016-01-21 06:48:3070 .args(&[
Corentin Henry4f602b72018-04-21 18:59:1971 Arg::from("--flag 'some flag'"),
72 Arg::from("--color 'some other flag'"),
Kevin K1ab10272018-01-25 04:05:0573 ])
Kevin Kf17e1502016-01-25 20:56:3774 .get_matches_from(vec!["", "--flag", "--color"]);
Alexander Kuvaev0cfecd32015-08-27 21:03:4575 assert!(m.is_present("flag"));
76 assert!(m.is_present("color"));
77}
78
79#[test]
80fn flag_using_mixed() {
81 let m = App::new("flag")
Kevin K0031d782016-01-21 06:48:3082 .args(&[
Corentin Henry4f602b72018-04-21 18:59:1983 Arg::from("-f, --flag 'some flag'"),
84 Arg::from("-c, --color 'some other flag'"),
Kevin K1ab10272018-01-25 04:05:0585 ])
Kevin Kf17e1502016-01-25 20:56:3786 .get_matches_from(vec!["", "-f", "--color"]);
Alexander Kuvaev0cfecd32015-08-27 21:03:4587 assert!(m.is_present("flag"));
88 assert!(m.is_present("color"));
89
90 let m = App::new("flag")
Kevin K0031d782016-01-21 06:48:3091 .args(&[
Corentin Henry4f602b72018-04-21 18:59:1992 Arg::from("-f, --flag 'some flag'"),
93 Arg::from("-c, --color 'some other flag'"),
Kevin K1ab10272018-01-25 04:05:0594 ])
Kevin Kf17e1502016-01-25 20:56:3795 .get_matches_from(vec!["", "--flag", "-c"]);
Alexander Kuvaev0cfecd32015-08-27 21:03:4596 assert!(m.is_present("flag"));
97 assert!(m.is_present("color"));
98}
99
100#[test]
101fn multiple_flags_in_single() {
102 let m = App::new("multe_flags")
Kevin K0031d782016-01-21 06:48:30103 .args(&[
Corentin Henry4f602b72018-04-21 18:59:19104 Arg::from("-f, --flag 'some flag'"),
105 Arg::from("-c, --color 'some other flag'"),
106 Arg::from("-d, --debug 'another other flag'"),
Kevin K1ab10272018-01-25 04:05:05107 ])
Kevin Kf17e1502016-01-25 20:56:37108 .get_matches_from(vec!["", "-fcd"]);
Alexander Kuvaev0cfecd32015-08-27 21:03:45109 assert!(m.is_present("flag"));
110 assert!(m.is_present("color"));
111 assert!(m.is_present("debug"));
112}
113
114#[test]
Alexander Kuvaev0cfecd32015-08-27 21:03:45115fn short_flag_misspel() {
Corentin Henry4f602b72018-04-21 18:59:19116 let a = Arg::from("-f1, --flag 'some flag'");
Kevin Kefb3b282018-01-25 02:08:14117 assert_eq!(a.name, "flag");
118 assert_eq!(a.short.unwrap(), 'f');
119 assert_eq!(a.long.unwrap(), "flag");
120 assert_eq!(a.help.unwrap(), "some flag");
Kevin K97fd3f12018-01-31 20:15:01121 assert!(!a.is_set(ArgSettings::MultipleOccurrences));
Kevin Kefb3b282018-01-25 02:08:14122 assert!(a.val_names.is_none());
123 assert!(a.num_vals.is_none());
Alexander Kuvaev0cfecd32015-08-27 21:03:45124}
125
126#[test]
Alexander Kuvaev0cfecd32015-08-27 21:03:45127fn short_flag_name_missing() {
Corentin Henry4f602b72018-04-21 18:59:19128 let a = Arg::from("-f 'some flag'");
Kevin Kefb3b282018-01-25 02:08:14129 assert_eq!(a.name, "f");
130 assert_eq!(a.short.unwrap(), 'f');
131 assert!(a.long.is_none());
132 assert_eq!(a.help.unwrap(), "some flag");
Kevin K97fd3f12018-01-31 20:15:01133 assert!(!a.is_set(ArgSettings::MultipleOccurrences));
Kevin Kefb3b282018-01-25 02:08:14134 assert!(a.val_names.is_none());
135 assert!(a.num_vals.is_none());
Kevin Kf17e1502016-01-25 20:56:37136}