From a4a00b03e3a1ab5ee976b1d4d7dc998c58ce4d88 Mon Sep 17 00:00:00 2001 From: Alexander Kuvaev Date: Sun, 6 Sep 2015 13:46:58 +0300 Subject: [PATCH 1/6] 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 From 7e3e69647111e086a0706c7f177a4452dcdfdb81 Mon Sep 17 00:00:00 2001 From: Alexander Kuvaev Date: Sun, 6 Sep 2015 22:11:18 +0300 Subject: [PATCH 2/6] tests: Add sub_command_negate_requred false-negative test --- tests/app_settings.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/app_settings.rs b/tests/app_settings.rs index f741e929..171394a1 100644 --- a/tests/app_settings.rs +++ b/tests/app_settings.rs @@ -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::().ok().unwrap(), AppSettings::SubcommandsNegateReqs); From 952fa4d2466d2c8df37ee1aa0bb30a84ed43301e Mon Sep 17 00:00:00 2001 From: Alexander Kuvaev Date: Sun, 6 Sep 2015 22:22:37 +0300 Subject: [PATCH 3/6] tests: conflict override tests --- tests/posix_compatible.rs | 58 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/tests/posix_compatible.rs b/tests/posix_compatible.rs index a6fa6559..7b70c84e 100644 --- a/tests/posix_compatible.rs +++ b/tests/posix_compatible.rs @@ -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,60 @@ 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")); } \ No newline at end of file From 7fdcb4761aa664ff8d9a559c50c213bb8966f22b Mon Sep 17 00:00:00 2001 From: Alexander Kuvaev Date: Sun, 6 Sep 2015 22:34:37 +0300 Subject: [PATCH 4/6] tests: Added requirement override tests --- tests/posix_compatible.rs | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/posix_compatible.rs b/tests/posix_compatible.rs index 7b70c84e..e49049dc 100644 --- a/tests/posix_compatible.rs +++ b/tests/posix_compatible.rs @@ -147,4 +147,59 @@ fn conflict_overriden_4() { 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); } \ No newline at end of file From 7a2375bceacd2b98c311e3c686674f0d61af6965 Mon Sep 17 00:00:00 2001 From: Alexander Kuvaev Date: Sun, 6 Sep 2015 23:08:13 +0300 Subject: [PATCH 5/6] tests: add tests for requires --- tests/posix_compatible.rs | 2 +- tests/require.rs | 170 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 tests/require.rs diff --git a/tests/posix_compatible.rs b/tests/posix_compatible.rs index e49049dc..f63a18a2 100644 --- a/tests/posix_compatible.rs +++ b/tests/posix_compatible.rs @@ -1,6 +1,6 @@ extern crate clap; -use clap::{App, Arg, ClapErrorType }; +use clap::{App, Arg, ClapErrorType}; #[test] fn posix_compatible_flags_long() { diff --git a/tests/require.rs b/tests/require.rs new file mode 100644 index 00000000..33966182 --- /dev/null +++ b/tests/require.rs @@ -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")); +} \ No newline at end of file From 5ef59a7a1965b1712226a6fc8e90c3ff7ee29f3e Mon Sep 17 00:00:00 2001 From: Alexander Kuvaev Date: Sun, 6 Sep 2015 23:34:15 +0300 Subject: [PATCH 6/6] tests: fixed yaml test --- tests/app.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/app.yml b/tests/app.yml index 093fbc53..a7dde858 100644 --- a/tests/app.yml +++ b/tests/app.yml @@ -69,7 +69,7 @@ args: multiple: true help: Tests 3 max vals max_values: 3 -groups: +arg_groups: - test: args: - maxvals3