Skip to content

Commit e5c6993

Browse files
author
Alex Saveau
committed
test: Long flags inference
Signed-off-by: Alex Saveau <[email protected]>
1 parent cb2d2bc commit e5c6993

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

tests/builder/app_settings.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::ffi::OsString;
22

3-
use super::utils;
4-
53
use clap::{arg, error::ErrorKind, Arg, ArgAction, Command};
64

5+
use super::utils;
6+
77
static ALLOW_EXT_SC: &str = "\
88
Usage: clap-test [COMMAND]
99
@@ -253,6 +253,51 @@ fn infer_subcommands_pass_exact_match() {
253253
assert_eq!(m.subcommand_name(), Some("test"));
254254
}
255255

256+
#[test]
257+
fn infer_subcommands_pass_conflicting_aliases() {
258+
let m = Command::new("prog")
259+
.infer_subcommands(true)
260+
.subcommand(Command::new("test").aliases(["testa", "t", "testb"]))
261+
.try_get_matches_from(vec!["prog", "te"]);
262+
assert!(m.is_err(), "{:#?}", m.unwrap());
263+
}
264+
265+
#[test]
266+
#[should_panic(expected = "internal error")]
267+
fn infer_long_flag_pass_conflicting_aliases() {
268+
let m = Command::new("prog")
269+
.infer_subcommands(true)
270+
.subcommand(
271+
Command::new("c")
272+
.long_flag("test")
273+
.long_flag_aliases(["testa", "t", "testb"]),
274+
)
275+
.try_get_matches_from(vec!["prog", "--te"]);
276+
assert!(m.is_err(), "{:#?}", m.unwrap());
277+
}
278+
279+
#[test]
280+
#[should_panic(expected = "internal error")]
281+
fn infer_long_flag() {
282+
let m = Command::new("prog")
283+
.infer_subcommands(true)
284+
.subcommand(Command::new("test").long_flag("testa"))
285+
.try_get_matches_from(vec!["prog", "--te"])
286+
.unwrap();
287+
assert_eq!(m.subcommand_name(), Some("test"));
288+
}
289+
290+
#[test]
291+
fn infer_subcommands_long_flag_fail_with_args2() {
292+
let m = Command::new("prog")
293+
.infer_subcommands(true)
294+
.subcommand(Command::new("a").long_flag("test"))
295+
.subcommand(Command::new("b").long_flag("temp"))
296+
.try_get_matches_from(vec!["prog", "--te"]);
297+
assert!(m.is_err(), "{:#?}", m.unwrap());
298+
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnknownArgument);
299+
}
300+
256301
#[cfg(feature = "suggestions")]
257302
#[test]
258303
fn infer_subcommands_fail_suggestions() {

0 commit comments

Comments
 (0)