clap/tests/groups.rs

238 lines
6.3 KiB
Rust
Raw Normal View History

extern crate clap;
extern crate regex;
include!("../clap-test.rs");
use clap::{App, Arg, ArgGroup, ErrorKind};
static REQ_GROUP_USAGE: &'static str = "error: The following required arguments were not provided:
<base|--delete>
USAGE:
clap-test <base|--delete>
For more information try --help";
2018-01-25 04:05:05 +00:00
static REQ_GROUP_CONFLICT_USAGE: &'static str =
"error: The argument '<base>' cannot be used with '--delete'
USAGE:
clap-test <base|--delete>
For more information try --help";
2018-01-25 04:05:05 +00:00
static REQ_GROUP_CONFLICT_REV: &'static str =
"error: The argument '--delete' cannot be used with 'base'
USAGE:
clap-test <base|--delete>
For more information try --help";
#[test]
fn required_group_missing_arg() {
2015-09-28 20:23:17 +00:00
let result = App::new("group")
2018-01-25 04:05:05 +00:00
.args_from_usage(
"-f, --flag 'some flag'
-c, --color 'some other flag'",
)
.group(
ArgGroup::with_name("req")
.args(&["flag", "color"])
.required(true),
)
.get_matches_from_safe(vec![""]);
2015-09-28 20:23:17 +00:00
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
}
#[test]
#[should_panic]
fn non_existing_arg() {
let _ = App::new("group")
2018-01-25 04:05:05 +00:00
.args_from_usage(
"-f, --flag 'some flag'
-c, --color 'some other flag'",
)
.group(
ArgGroup::with_name("req")
.args(&["flg", "color"])
.required(true),
)
.get_matches_from_safe(vec![""]);
}
#[test]
fn group_single_value() {
let res = App::new("group")
2018-01-25 04:05:05 +00:00
.args_from_usage(
"-f, --flag 'some flag'
-c, --color [color] 'some option'",
)
.group(ArgGroup::with_name("grp").args(&["flag", "color"]))
.get_matches_from_safe(vec!["", "-c", "blue"]);
assert!(res.is_ok());
let m = res.unwrap();
assert!(m.is_present("grp"));
assert_eq!(m.value_of("grp").unwrap(), "blue");
}
#[test]
fn group_single_flag() {
let res = App::new("group")
2018-01-25 04:05:05 +00:00
.args_from_usage(
"-f, --flag 'some flag'
-c, --color [color] 'some option'",
)
.group(ArgGroup::with_name("grp").args(&["flag", "color"]))
.get_matches_from_safe(vec!["", "-f"]);
assert!(res.is_ok());
let m = res.unwrap();
assert!(m.is_present("grp"));
assert!(m.value_of("grp").is_none());
}
#[test]
fn group_empty() {
let res = App::new("group")
2018-01-25 04:05:05 +00:00
.args_from_usage(
"-f, --flag 'some flag'
-c, --color [color] 'some option'",
)
.group(ArgGroup::with_name("grp").args(&["flag", "color"]))
.get_matches_from_safe(vec![""]);
assert!(res.is_ok());
let m = res.unwrap();
assert!(!m.is_present("grp"));
assert!(m.value_of("grp").is_none());
}
#[test]
fn group_reqired_flags_empty() {
let result = App::new("group")
2018-01-25 04:05:05 +00:00
.args_from_usage(
"-f, --flag 'some flag'
-c, --color 'some option'",
)
.group(
ArgGroup::with_name("grp")
.required(true)
.args(&["flag", "color"]),
)
.get_matches_from_safe(vec![""]);
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
}
#[test]
fn group_multi_value_single_arg() {
let res = App::new("group")
2018-01-25 04:05:05 +00:00
.args_from_usage(
"-f, --flag 'some flag'
-c, --color [color]... 'some option'",
)
.group(ArgGroup::with_name("grp").args(&["flag", "color"]))
.get_matches_from_safe(vec!["", "-c", "blue", "red", "green"]);
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);
let m = res.unwrap();
assert!(m.is_present("grp"));
2018-01-25 04:05:05 +00:00
assert_eq!(
&*m.values_of("grp").unwrap().collect::<Vec<_>>(),
&["blue", "red", "green"]
);
}
#[test]
fn empty_group() {
let r = App::new("empty_group")
2018-04-21 18:59:19 +00:00
.arg(Arg::from("-f, --flag 'some flag'"))
2018-01-25 04:05:05 +00:00
.group(ArgGroup::with_name("vers").required(true))
.get_matches_from_safe(vec!["empty_prog"]);
assert!(r.is_err());
let err = r.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
}
#[test]
fn req_group_usage_string() {
let app = App::new("req_group")
2018-01-25 04:05:05 +00:00
.args_from_usage(
"[base] 'Base commit'
-d, --delete 'Remove the base commit information'",
)
.group(
ArgGroup::with_name("base_or_delete")
.args(&["base", "delete"])
.required(true),
);
assert!(test::compare_output(
app,
"clap-test",
REQ_GROUP_USAGE,
true
));
}
2016-05-09 20:00:44 +00:00
#[test]
fn req_group_with_conflict_usage_string() {
let app = App::new("req_group")
2018-04-21 18:59:19 +00:00
.arg(Arg::from("[base] 'Base commit'").conflicts_with("delete"))
.arg(Arg::from(
2018-01-25 04:05:05 +00:00
"-d, --delete 'Remove the base commit information'",
))
.group(
ArgGroup::with_name("base_or_delete")
.args(&["base", "delete"])
.required(true),
);
assert!(test::compare_output2(
app,
"clap-test --delete base",
REQ_GROUP_CONFLICT_REV,
REQ_GROUP_CONFLICT_USAGE,
true
));
2016-05-09 20:00:44 +00:00
}
#[test]
fn required_group_multiple_args() {
let result = App::new("group")
2018-01-25 04:05:05 +00:00
.args_from_usage(
"-f, --flag 'some flag'
-c, --color 'some other flag'",
)
.group(
ArgGroup::with_name("req")
.args(&["flag", "color"])
.required(true)
.multiple(true),
)
.get_matches_from_safe(vec!["group", "-f", "-c"]);
assert!(result.is_ok());
let m = result.unwrap();
assert!(m.is_present("flag"));
assert!(m.is_present("color"));
}
#[test]
fn group_multiple_args_error() {
let result = App::new("group")
2018-01-25 04:05:05 +00:00
.args_from_usage(
"-f, --flag 'some flag'
-c, --color 'some other flag'",
)
.group(ArgGroup::with_name("req").args(&["flag", "color"]))
.get_matches_from_safe(vec!["group", "-f", "-c"]);
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
}