mirror of
https://github.com/clap-rs/clap
synced 2024-11-14 00:27:13 +00:00
Merge pull request #227 from Vinatorul/dev
Switching run_tests.py to Rust tests
This commit is contained in:
commit
72ff0b9c17
6 changed files with 361 additions and 6 deletions
|
@ -240,8 +240,8 @@ _bin = './target/release/claptests'
|
|||
cmds = {#'help short: ': ['{} -h'.format(_bin), _help],
|
||||
#'help long: ': ['{} --help'.format(_bin), _help],
|
||||
'help subcmd: ': ['{} help'.format(_bin), _help],
|
||||
'excluded first: ': ['{} -f -F'.format(_bin), _excluded],
|
||||
'excluded last: ': ['{} -F -f'.format(_bin), _excluded_l],
|
||||
#'excluded first: ': ['{} -f -F'.format(_bin), _excluded],
|
||||
#'excluded last: ': ['{} -F -f'.format(_bin), _excluded_l],
|
||||
'missing required: ': ['{} -F'.format(_bin), _required],
|
||||
'max_vals too many: ': ['{} --maxvals3 some other value too'.format(_bin), _max_vals_more],
|
||||
'max_vals exact: ': ['{} --maxvals3 some other value'.format(_bin), _exact],
|
||||
|
|
|
@ -69,7 +69,7 @@ args:
|
|||
multiple: true
|
||||
help: Tests 3 max vals
|
||||
max_values: 3
|
||||
groups:
|
||||
arg_groups:
|
||||
- test:
|
||||
args:
|
||||
- maxvals3
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
extern crate clap;
|
||||
|
||||
use clap::{App, Arg, SubCommand, AppSettings};
|
||||
use clap::{App, Arg, SubCommand, AppSettings, ClapErrorType};
|
||||
|
||||
#[test]
|
||||
fn sub_command_negate_requred() {
|
||||
|
@ -10,10 +10,23 @@ fn sub_command_negate_requred() {
|
|||
.required(true)
|
||||
.index(1))
|
||||
.subcommand(SubCommand::with_name("sub1"))
|
||||
.subcommand(SubCommand::with_name("sub1"))
|
||||
.get_matches_from(vec!["", "sub1"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sub_command_negate_requred_2() {
|
||||
let result = App::new("sub_command_negate")
|
||||
.setting(AppSettings::SubcommandsNegateReqs)
|
||||
.arg(Arg::with_name("test")
|
||||
.required(true)
|
||||
.index(1))
|
||||
.subcommand(SubCommand::with_name("sub1"))
|
||||
.get_matches_from_safe(vec![""]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::MissingRequiredArgument);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn app_settings_fromstr() {
|
||||
assert_eq!("subcommandsnegatereqs".parse::<AppSettings>().ok().unwrap(), AppSettings::SubcommandsNegateReqs);
|
||||
|
|
61
tests/conflicts.rs
Normal file
61
tests/conflicts.rs
Normal file
|
@ -0,0 +1,61 @@
|
|||
extern crate clap;
|
||||
|
||||
use clap::{App, Arg, ClapErrorType, ArgGroup};
|
||||
|
||||
#[test]
|
||||
fn flag_conflict() {
|
||||
let result = App::new("flag_conflict")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.conflicts_with("other"))
|
||||
.arg(Arg::from_usage("-o, --other 'some flag'"))
|
||||
.get_matches_from_safe(vec!["", "-f", "-o"]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::ArgumentConflict);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flag_conflict_2() {
|
||||
let result = App::new("flag_conflict")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.conflicts_with("other"))
|
||||
.arg(Arg::from_usage("-o, --other 'some flag'"))
|
||||
.get_matches_from_safe(vec!["", "-o", "-f"]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::ArgumentConflict);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn group_conflict() {
|
||||
let result = App::new("group_conflict")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.conflicts_with("gr"))
|
||||
.arg_group(ArgGroup::with_name("gr")
|
||||
.required(true)
|
||||
.add("some")
|
||||
.add("other"))
|
||||
.arg(Arg::from_usage("--some 'some arg'"))
|
||||
.arg(Arg::from_usage("--other 'other arg'"))
|
||||
.get_matches_from_safe(vec!["", "--other", "-f"]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::ArgumentConflict);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn group_conflict_2() {
|
||||
let result = App::new("group_conflict")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.conflicts_with("gr"))
|
||||
.arg_group(ArgGroup::with_name("gr")
|
||||
.required(true)
|
||||
.add("some")
|
||||
.add("other"))
|
||||
.arg(Arg::from_usage("--some 'some arg'"))
|
||||
.arg(Arg::from_usage("--other 'other arg'"))
|
||||
.get_matches_from_safe(vec!["", "-f", "--some"]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::ArgumentConflict);
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
extern crate clap;
|
||||
|
||||
use clap::{App, Arg};
|
||||
use clap::{App, Arg, ClapErrorType};
|
||||
|
||||
#[test]
|
||||
fn posix_compatible_flags_long() {
|
||||
|
@ -91,4 +91,115 @@ fn posix_compatible_opts_short() {
|
|||
assert!(!m.is_present("color"));
|
||||
assert!(m.is_present("flag"));
|
||||
assert_eq!(m.value_of("flag").unwrap(), "other");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conflict_overriden() {
|
||||
let m = App::new("conflict_overriden")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.conflicts_with("debug"))
|
||||
.arg(Arg::from_usage("-d, --debug 'other flag'"))
|
||||
.arg(Arg::from_usage("-c, --color 'third flag'")
|
||||
.mutually_overrides_with("flag"))
|
||||
.get_matches_from(vec!["", "-f", "-c", "-d"]);
|
||||
assert!(m.is_present("color"));
|
||||
assert!(!m.is_present("flag"));
|
||||
assert!(m.is_present("debug"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conflict_overriden_2() {
|
||||
let result = App::new("conflict_overriden")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.conflicts_with("debug"))
|
||||
.arg(Arg::from_usage("-d, --debug 'other flag'"))
|
||||
.arg(Arg::from_usage("-c, --color 'third flag'")
|
||||
.mutually_overrides_with("flag"))
|
||||
.get_matches_from_safe(vec!["", "-f", "-d", "-c"]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::ArgumentConflict);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conflict_overriden_3() {
|
||||
let result = App::new("conflict_overriden")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.conflicts_with("debug"))
|
||||
.arg(Arg::from_usage("-d, --debug 'other flag'"))
|
||||
.arg(Arg::from_usage("-c, --color 'third flag'")
|
||||
.mutually_overrides_with("flag"))
|
||||
.get_matches_from_safe(vec!["", "-d", "-c", "-f"]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::ArgumentConflict);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conflict_overriden_4() {
|
||||
let m = App::new("conflict_overriden")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.conflicts_with("debug"))
|
||||
.arg(Arg::from_usage("-d, --debug 'other flag'"))
|
||||
.arg(Arg::from_usage("-c, --color 'third flag'")
|
||||
.mutually_overrides_with("flag"))
|
||||
.get_matches_from(vec!["", "-d", "-f", "-c"]);
|
||||
assert!(m.is_present("color"));
|
||||
assert!(!m.is_present("flag"));
|
||||
assert!(m.is_present("debug"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn require_overriden() {
|
||||
let result = App::new("require_overriden")
|
||||
.arg(Arg::with_name("flag")
|
||||
.index(1)
|
||||
.required(true))
|
||||
.arg(Arg::from_usage("-c, --color 'other flag'")
|
||||
.mutually_overrides_with("flag"))
|
||||
.get_matches_from_safe(vec!["", "flag", "-c"]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::MissingRequiredArgument);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn require_overriden_2() {
|
||||
let m = App::new("require_overriden")
|
||||
.arg(Arg::with_name("flag")
|
||||
.index(1)
|
||||
.required(true))
|
||||
.arg(Arg::from_usage("-c, --color 'other flag'")
|
||||
.mutually_overrides_with("flag"))
|
||||
.get_matches_from(vec!["", "-c", "flag"]);
|
||||
assert!(!m.is_present("color"));
|
||||
assert!(m.is_present("flag"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn require_overriden_3() {
|
||||
let m = App::new("require_overriden")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.requires("debug"))
|
||||
.arg(Arg::from_usage("-d, --debug 'other flag'"))
|
||||
.arg(Arg::from_usage("-c, --color 'third flag'")
|
||||
.mutually_overrides_with("flag"))
|
||||
.get_matches_from(vec!["", "-f", "-c"]);
|
||||
assert!(m.is_present("color"));
|
||||
assert!(!m.is_present("flag"));
|
||||
assert!(!m.is_present("debug"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn require_overriden_4() {
|
||||
let result = App::new("require_overriden")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.requires("debug"))
|
||||
.arg(Arg::from_usage("-d, --debug 'other flag'"))
|
||||
.arg(Arg::from_usage("-c, --color 'third flag'")
|
||||
.mutually_overrides_with("flag"))
|
||||
.get_matches_from_safe(vec!["", "-c", "-f"]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::MissingRequiredArgument);
|
||||
}
|
170
tests/require.rs
Normal file
170
tests/require.rs
Normal file
|
@ -0,0 +1,170 @@
|
|||
extern crate clap;
|
||||
|
||||
use clap::{App, Arg, ClapErrorType, ArgGroup};
|
||||
|
||||
#[test]
|
||||
fn flag_required() {
|
||||
let result = App::new("flag_required")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.requires("color"))
|
||||
.arg(Arg::from_usage("-c, --color 'third flag'"))
|
||||
.get_matches_from_safe(vec!["", "-f"]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::MissingRequiredArgument);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flag_required_2() {
|
||||
let m = App::new("flag_required")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.requires("color"))
|
||||
.arg(Arg::from_usage("-c, --color 'third flag'"))
|
||||
.get_matches_from(vec!["", "-f", "-c"]);
|
||||
assert!(m.is_present("color"));
|
||||
assert!(m.is_present("flag"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn option_required() {
|
||||
let result = App::new("option_required")
|
||||
.arg(Arg::from_usage("-f [flag] 'some flag'")
|
||||
.requires("color"))
|
||||
.arg(Arg::from_usage("-c [color] 'third flag'"))
|
||||
.get_matches_from_safe(vec!["", "-f", "val"]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::MissingRequiredArgument);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn option_required_2() {
|
||||
let m = App::new("option_required")
|
||||
.arg(Arg::from_usage("-f [flag] 'some flag'")
|
||||
.requires("color"))
|
||||
.arg(Arg::from_usage("-c [color] 'third flag'"))
|
||||
.get_matches_from(vec!["", "-f", "val", "-c", "other_val"]);
|
||||
assert!(m.is_present("color"));
|
||||
assert_eq!(m.value_of("color").unwrap(), "other_val");
|
||||
assert!(m.is_present("flag"));
|
||||
assert_eq!(m.value_of("flag").unwrap(), "val");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn positional_required() {
|
||||
let result = App::new("positional_required")
|
||||
.arg(Arg::with_name("flag")
|
||||
.index(1)
|
||||
.required(true))
|
||||
.get_matches_from_safe(vec![""]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::MissingRequiredArgument);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn positional_required_2() {
|
||||
let m = App::new("positional_required")
|
||||
.arg(Arg::with_name("flag")
|
||||
.index(1)
|
||||
.required(true))
|
||||
.get_matches_from(vec!["", "someval"]);
|
||||
assert!(m.is_present("flag"));
|
||||
assert_eq!(m.value_of("flag").unwrap(), "someval");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn group_required() {
|
||||
let result = App::new("group_required")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'"))
|
||||
.arg_group(ArgGroup::with_name("gr")
|
||||
.required(true)
|
||||
.add("some")
|
||||
.add("other"))
|
||||
.arg(Arg::from_usage("--some 'some arg'"))
|
||||
.arg(Arg::from_usage("--other 'other arg'"))
|
||||
.get_matches_from_safe(vec!["", "-f"]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::MissingRequiredArgument);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn group_required_2() {
|
||||
let m = App::new("group_required")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'"))
|
||||
.arg_group(ArgGroup::with_name("gr")
|
||||
.required(true)
|
||||
.add("some")
|
||||
.add("other"))
|
||||
.arg(Arg::from_usage("--some 'some arg'"))
|
||||
.arg(Arg::from_usage("--other 'other arg'"))
|
||||
.get_matches_from(vec!["", "-f", "--some"]);
|
||||
assert!(m.is_present("some"));
|
||||
assert!(!m.is_present("other"));
|
||||
assert!(m.is_present("flag"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn group_required_3() {
|
||||
let m = App::new("group_required")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'"))
|
||||
.arg_group(ArgGroup::with_name("gr")
|
||||
.required(true)
|
||||
.add("some")
|
||||
.add("other"))
|
||||
.arg(Arg::from_usage("--some 'some arg'"))
|
||||
.arg(Arg::from_usage("--other 'other arg'"))
|
||||
.get_matches_from(vec!["", "-f", "--other"]);
|
||||
assert!(!m.is_present("some"));
|
||||
assert!(m.is_present("other"));
|
||||
assert!(m.is_present("flag"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arg_require_group() {
|
||||
let result = App::new("arg_require_group")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.requires("gr"))
|
||||
.arg_group(ArgGroup::with_name("gr")
|
||||
.add("some")
|
||||
.add("other"))
|
||||
.arg(Arg::from_usage("--some 'some arg'"))
|
||||
.arg(Arg::from_usage("--other 'other arg'"))
|
||||
.get_matches_from_safe(vec!["", "-f"]);
|
||||
assert!(result.is_err());
|
||||
let err = result.err().unwrap();
|
||||
assert_eq!(err.error_type, ClapErrorType::MissingRequiredArgument);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arg_require_group_2() {
|
||||
let m = App::new("arg_require_group")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.requires("gr"))
|
||||
.arg_group(ArgGroup::with_name("gr")
|
||||
.add("some")
|
||||
.add("other"))
|
||||
.arg(Arg::from_usage("--some 'some arg'"))
|
||||
.arg(Arg::from_usage("--other 'other arg'"))
|
||||
.get_matches_from(vec!["", "-f", "--some"]);
|
||||
assert!(m.is_present("some"));
|
||||
assert!(!m.is_present("other"));
|
||||
assert!(m.is_present("flag"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arg_require_group_3() {
|
||||
let m = App::new("arg_require_group")
|
||||
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
||||
.requires("gr"))
|
||||
.arg_group(ArgGroup::with_name("gr")
|
||||
.add("some")
|
||||
.add("other"))
|
||||
.arg(Arg::from_usage("--some 'some arg'"))
|
||||
.arg(Arg::from_usage("--other 'other arg'"))
|
||||
.get_matches_from(vec!["", "-f", "--other"]);
|
||||
assert!(!m.is_present("some"));
|
||||
assert!(m.is_present("other"));
|
||||
assert!(m.is_present("flag"));
|
||||
}
|
Loading…
Reference in a new issue