1 use super::utils;
2
3 use clap::{arg, Arg, ArgAction, Command};
4
5 #[test]
single_alias_of_option()6 fn single_alias_of_option() {
7 let a = Command::new("single_alias")
8 .arg(
9 Arg::new("alias")
10 .long("alias")
11 .action(ArgAction::Set)
12 .help("single alias")
13 .alias("new-opt"),
14 )
15 .try_get_matches_from(vec!["", "--new-opt", "cool"]);
16 assert!(a.is_ok(), "{}", a.unwrap_err());
17 let a = a.unwrap();
18 assert!(a.contains_id("alias"));
19 assert_eq!(
20 a.get_one::<String>("alias").map(|v| v.as_str()).unwrap(),
21 "cool"
22 );
23 }
24
25 #[test]
multiple_aliases_of_option()26 fn multiple_aliases_of_option() {
27 let a = Command::new("multiple_aliases").arg(
28 Arg::new("aliases")
29 .long("aliases")
30 .action(ArgAction::Set)
31 .help("multiple aliases")
32 .aliases(["alias1", "alias2", "alias3"]),
33 );
34 let long = a
35 .clone()
36 .try_get_matches_from(vec!["", "--aliases", "value"]);
37 assert!(long.is_ok(), "{}", long.unwrap_err());
38 let long = long.unwrap();
39
40 let als1 = a
41 .clone()
42 .try_get_matches_from(vec!["", "--alias1", "value"]);
43 assert!(als1.is_ok(), "{}", als1.unwrap_err());
44 let als1 = als1.unwrap();
45
46 let als2 = a
47 .clone()
48 .try_get_matches_from(vec!["", "--alias2", "value"]);
49 assert!(als2.is_ok(), "{}", als2.unwrap_err());
50 let als2 = als2.unwrap();
51
52 let als3 = a
53 .clone()
54 .try_get_matches_from(vec!["", "--alias3", "value"]);
55 assert!(als3.is_ok(), "{}", als3.unwrap_err());
56 let als3 = als3.unwrap();
57
58 assert!(long.contains_id("aliases"));
59 assert!(als1.contains_id("aliases"));
60 assert!(als2.contains_id("aliases"));
61 assert!(als3.contains_id("aliases"));
62 assert_eq!(
63 long.get_one::<String>("aliases")
64 .map(|v| v.as_str())
65 .unwrap(),
66 "value"
67 );
68 assert_eq!(
69 als1.get_one::<String>("aliases")
70 .map(|v| v.as_str())
71 .unwrap(),
72 "value"
73 );
74 assert_eq!(
75 als2.get_one::<String>("aliases")
76 .map(|v| v.as_str())
77 .unwrap(),
78 "value"
79 );
80 assert_eq!(
81 als3.get_one::<String>("aliases")
82 .map(|v| v.as_str())
83 .unwrap(),
84 "value"
85 );
86 }
87
88 #[test]
get_aliases()89 fn get_aliases() {
90 let a = Arg::new("aliases")
91 .long("aliases")
92 .action(ArgAction::Set)
93 .help("multiple aliases")
94 .aliases(["alias1", "alias2", "alias3"])
95 .short_aliases(['a', 'b', 'c'])
96 .visible_aliases(["alias4", "alias5", "alias6"])
97 .visible_short_aliases(['d', 'e', 'f']);
98
99 assert!(a.get_short_and_visible_aliases().is_none());
100 assert_eq!(
101 a.get_long_and_visible_aliases().unwrap(),
102 ["aliases", "alias4", "alias5", "alias6"]
103 );
104 assert_eq!(
105 a.get_visible_aliases().unwrap(),
106 ["alias4", "alias5", "alias6"]
107 );
108 assert_eq!(
109 a.get_all_aliases().unwrap(),
110 ["alias1", "alias2", "alias3", "alias4", "alias5", "alias6"]
111 );
112 assert_eq!(a.get_visible_short_aliases().unwrap(), vec!['d', 'e', 'f']);
113 assert_eq!(
114 a.get_all_short_aliases().unwrap(),
115 vec!['a', 'b', 'c', 'd', 'e', 'f']
116 );
117 }
118
119 #[test]
single_alias_of_flag()120 fn single_alias_of_flag() {
121 let a = Command::new("test")
122 .arg(
123 Arg::new("flag")
124 .long("flag")
125 .alias("alias")
126 .action(ArgAction::SetTrue),
127 )
128 .try_get_matches_from(vec!["", "--alias"]);
129 assert!(a.is_ok(), "{}", a.unwrap_err());
130 let a = a.unwrap();
131 assert!(*a.get_one::<bool>("flag").expect("defaulted by clap"));
132 }
133
134 #[test]
multiple_aliases_of_flag()135 fn multiple_aliases_of_flag() {
136 let a = Command::new("test").arg(
137 Arg::new("flag")
138 .long("flag")
139 .aliases(["invisible", "set", "of", "cool", "aliases"])
140 .action(ArgAction::SetTrue),
141 );
142
143 let flag = a.clone().try_get_matches_from(vec!["", "--flag"]);
144 assert!(flag.is_ok(), "{}", flag.unwrap_err());
145 let flag = flag.unwrap();
146
147 let inv = a.clone().try_get_matches_from(vec!["", "--invisible"]);
148 assert!(inv.is_ok(), "{}", inv.unwrap_err());
149 let inv = inv.unwrap();
150
151 let cool = a.clone().try_get_matches_from(vec!["", "--cool"]);
152 assert!(cool.is_ok(), "{}", cool.unwrap_err());
153 let cool = cool.unwrap();
154
155 let als = a.clone().try_get_matches_from(vec!["", "--aliases"]);
156 assert!(als.is_ok(), "{}", als.unwrap_err());
157 let als = als.unwrap();
158
159 assert!(*flag.get_one::<bool>("flag").expect("defaulted by clap"));
160 assert!(*inv.get_one::<bool>("flag").expect("defaulted by clap"));
161 assert!(*cool.get_one::<bool>("flag").expect("defaulted by clap"));
162 assert!(*als.get_one::<bool>("flag").expect("defaulted by clap"));
163 }
164
165 #[test]
alias_on_a_subcommand_option()166 fn alias_on_a_subcommand_option() {
167 let m = Command::new("test")
168 .subcommand(
169 Command::new("some").arg(
170 Arg::new("test")
171 .short('t')
172 .long("test")
173 .action(ArgAction::Set)
174 .alias("opt")
175 .help("testing testing"),
176 ),
177 )
178 .arg(Arg::new("other").long("other").aliases(["o1", "o2", "o3"]))
179 .try_get_matches_from(vec!["test", "some", "--opt", "awesome"])
180 .unwrap();
181
182 assert!(m.subcommand_matches("some").is_some());
183 let sub_m = m.subcommand_matches("some").unwrap();
184 assert!(sub_m.contains_id("test"));
185 assert_eq!(
186 sub_m.get_one::<String>("test").map(|v| v.as_str()).unwrap(),
187 "awesome"
188 );
189 }
190
191 #[test]
invisible_arg_aliases_help_output()192 fn invisible_arg_aliases_help_output() {
193 static SC_INVISIBLE_ALIAS_HELP: &str = "\
194 Some help
195
196 Usage: ct test [OPTIONS]
197
198 Options:
199 -o, --opt <opt>
200 -f, --flag
201 -h, --help Print help
202 -V, --version Print version
203 ";
204
205 let cmd = Command::new("ct").author("Salim Afiune").subcommand(
206 Command::new("test")
207 .about("Some help")
208 .version("1.2")
209 .arg(
210 Arg::new("opt")
211 .long("opt")
212 .short('o')
213 .action(ArgAction::Set)
214 .aliases(["invisible", "als1", "more"]),
215 )
216 .arg(arg!(-f - -flag).aliases(["unseeable", "flg1", "anyway"])),
217 );
218 utils::assert_output(cmd, "ct test --help", SC_INVISIBLE_ALIAS_HELP, false);
219 }
220
221 #[test]
visible_arg_aliases_help_output()222 fn visible_arg_aliases_help_output() {
223 static SC_VISIBLE_ALIAS_HELP: &str = "\
224 Some help
225
226 Usage: ct test [OPTIONS]
227
228 Options:
229 -o, --opt <opt> [aliases: visible]
230 -f, --flag [aliases: v_flg, flag2, flg3]
231 -h, --help Print help
232 -V, --version Print version
233 ";
234
235 let cmd = Command::new("ct").author("Salim Afiune").subcommand(
236 Command::new("test")
237 .about("Some help")
238 .version("1.2")
239 .arg(
240 Arg::new("opt")
241 .long("opt")
242 .short('o')
243 .action(ArgAction::Set)
244 .alias("invisible")
245 .visible_alias("visible"),
246 )
247 .arg(
248 Arg::new("flg")
249 .long("flag")
250 .short('f')
251 .action(ArgAction::SetTrue)
252 .visible_aliases(["v_flg", "flag2", "flg3"]),
253 ),
254 );
255 utils::assert_output(cmd, "ct test --help", SC_VISIBLE_ALIAS_HELP, false);
256 }
257