From a4a00b03e3a1ab5ee976b1d4d7dc998c58ce4d88 Mon Sep 17 00:00:00 2001 From: Alexander Kuvaev Date: Sun, 6 Sep 2015 13:46:58 +0300 Subject: [PATCH] tests: add tests for flag and groups conflicts --- clap-tests/run_tests.py | 4 +-- tests/conflicts.rs | 61 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tests/conflicts.rs diff --git a/clap-tests/run_tests.py b/clap-tests/run_tests.py index 9bfe234e..921eacdd 100755 --- a/clap-tests/run_tests.py +++ b/clap-tests/run_tests.py @@ -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], diff --git a/tests/conflicts.rs b/tests/conflicts.rs new file mode 100644 index 00000000..2ec0ff6a --- /dev/null +++ b/tests/conflicts.rs @@ -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); +} \ No newline at end of file