diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2020-01-07 11:18:04 +0000 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2020-01-08 09:20:25 -0800 |
commit | 5e20a29b4fdc8a2d442d1093681b396dcb4b816b (patch) | |
tree | 55ab083fa8999d2ccbb5e921c1ffe52560dca152 /clap/tests/macros.rs | |
parent | 203e691f46d591a2cc8acdfd850fa9f5b0fb8a98 (diff) | |
download | nitrocli-5e20a29b4fdc8a2d442d1093681b396dcb4b816b.tar.gz nitrocli-5e20a29b4fdc8a2d442d1093681b396dcb4b816b.tar.bz2 |
Add structopt dependency in version 0.3.7
This patch series replaces argparse with structopt in the argument
handling code. As a first step, we need structopt as a dependency.
Import subrepo structopt/:structopt at efbdda4753592e27bc430fb01f7b9650b2f3174d
Import subrepo bitflags/:bitflags at 30668016aca6bd3b02c766e8347e0b4080d4c296
Import subrepo clap/:clap at 784524f7eb193e35f81082cc69454c8c21b948f7
Import subrepo heck/:heck at 093d56fbf001e1506e56dbfa38631d99b1066df1
Import subrepo proc-macro-error/:proc-macro-error at 6c4cfe79a622c5de8ae68557993542be46eacae2
Import subrepo proc-macro2/:proc-macro2 at d5d48eddca4566e5438e8a2cbed4a74e049544de
Import subrepo quote/:quote at 727436c6c137b20f0f34dde5d8fda2679b9747ad
Import subrepo rustversion/:rustversion at 0c5663313516263059ce9059ef81fc7a1cf655ca
Import subrepo syn-mid/:syn-mid at 5d3d85414a9e6674e1857ec22a87b96e04a6851a
Import subrepo syn/:syn at e87c27e87f6f4ef8919d0372bdb056d53ef0d8f3
Import subrepo textwrap/:textwrap at abcd618beae3f74841032aa5b53c1086b0a57ca2
Import subrepo unicode-segmentation/:unicode-segmentation at 637c9874c4fe0c205ff27787faf150a40295c6c3
Import subrepo unicode-width/:unicode-width at 3033826f8bf05e82724140a981d5941e48fce393
Import subrepo unicode-xid/:unicode-xid at 4baae9fffb156ba229665b972a9cd5991787ceb7
Diffstat (limited to 'clap/tests/macros.rs')
-rwxr-xr-x | clap/tests/macros.rs | 391 |
1 files changed, 391 insertions, 0 deletions
diff --git a/clap/tests/macros.rs b/clap/tests/macros.rs new file mode 100755 index 0000000..0cdcb52 --- /dev/null +++ b/clap/tests/macros.rs @@ -0,0 +1,391 @@ +#[macro_use] +extern crate clap; + +use clap::ErrorKind; + +#[test] +fn basic() { + clap_app!(claptests => + (version: "0.1") + (about: "tests clap library") + (author: "Kevin K. <kbknapp@gmail.com>") + (@arg opt: -o --option +takes_value ... "tests options") + (@arg positional: index(1) "tests positionals") + (@arg flag: -f --flag ... +global "tests flags") + (@arg flag2: -F conflicts_with[flag] requires[option2] + "tests flags with exclusions") + (@arg option2: --long_option_2 conflicts_with[option] requires[positional2] + "tests long options with exclusions") + (@arg positional2: index(2) "tests positionals with exclusions") + (@arg option3: -O --Option +takes_value possible_value[fast slow] + "tests options with specific value sets") + (@arg positional3: index(3) ... possible_value[vi emacs] + "tests positionals with specific values") + (@arg multvals: --multvals +takes_value value_name[one two] + "Tests multiple values, not mult occs") + (@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two] + "Tests multiple values, not mult occs") + (@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals") + (@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals") + (@subcommand subcmd => + (about: "tests subcommands") + (version: "0.1") + (author: "Kevin K. <kbknapp@gmail.com>") + (@arg scoption: -o --option ... +takes_value "tests options") + (@arg scpositional: index(1) "tests positionals")) + ); +} + +#[test] +fn quoted_app_name() { + let app = clap_app!(("app name with spaces-and-hyphens") => + (version: "0.1") + (about: "tests clap library") + (author: "Kevin K. <kbknapp@gmail.com>") + (@arg opt: -o --option +takes_value ... "tests options") + (@arg positional: index(1) "tests positionals") + (@arg flag: -f --flag ... +global "tests flags") + (@arg flag2: -F conflicts_with[flag] requires[option2] + "tests flags with exclusions") + (@arg option2: --long_option_2 conflicts_with[option] requires[positional2] + "tests long options with exclusions") + (@arg positional2: index(2) "tests positionals with exclusions") + (@arg option3: -O --Option +takes_value possible_value[fast slow] + "tests options with specific value sets") + (@arg positional3: index(3) ... possible_value[vi emacs] + "tests positionals with specific values") + (@arg multvals: --multvals +takes_value value_name[one two] + "Tests multiple values, not mult occs") + (@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two] + "Tests multiple values, not mult occs") + (@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals") + (@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals") + (@subcommand subcmd => + (about: "tests subcommands") + (version: "0.1") + (author: "Kevin K. <kbknapp@gmail.com>") + (@arg scoption: -o --option ... +takes_value "tests options") + (@arg scpositional: index(1) "tests positionals")) + ); + + assert_eq!(app.p.meta.name, "app name with spaces-and-hyphens"); + + let mut help_text = vec![]; + app.write_help(&mut help_text).expect("Could not write help text."); + let help_text = String::from_utf8(help_text).expect("Help text is not valid utf-8"); + assert!(help_text.starts_with("app name with spaces-and-hyphens 0.1\n")); +} + +#[test] +fn quoted_arg_long_name() { + let app = clap_app!(claptests => + (version: "0.1") + (about: "tests clap library") + (author: "Kevin K. <kbknapp@gmail.com>") + (@arg opt: -o --option +takes_value ... "tests options") + (@arg positional: index(1) "tests positionals") + (@arg flag: -f --flag ... +global "tests flags") + (@arg flag2: -F conflicts_with[flag] requires[option2] + "tests flags with exclusions") + (@arg option2: --("long-option-2") conflicts_with[option] requires[positional2] + "tests long options with exclusions") + (@arg positional2: index(2) "tests positionals with exclusions") + (@arg option3: -O --Option +takes_value possible_value[fast slow] + "tests options with specific value sets") + (@arg positional3: index(3) ... possible_value[vi emacs] + "tests positionals with specific values") + (@arg multvals: --multvals +takes_value value_name[one two] + "Tests multiple values, not mult occs") + (@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two] + "Tests multiple values, not mult occs") + (@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals") + (@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals") + (@subcommand subcmd => + (about: "tests subcommands") + (version: "0.1") + (author: "Kevin K. <kbknapp@gmail.com>") + (@arg scoption: -o --option ... +takes_value "tests options") + (@arg scpositional: index(1) "tests positionals")) + ); + + let matches = app.get_matches_from_safe(vec!["bin_name", "value1", "value2", "--long-option-2"]) + .expect("Expected to successfully match the given args."); + assert!(matches.is_present("option2")); +} + +#[test] +fn quoted_arg_name() { + let app = clap_app!(claptests => + (version: "0.1") + (about: "tests clap library") + (author: "Kevin K. <kbknapp@gmail.com>") + (@arg opt: -o --option +takes_value ... "tests options") + (@arg ("positional-arg"): index(1) "tests positionals") + (@arg flag: -f --flag ... +global "tests flags") + (@arg flag2: -F conflicts_with[flag] requires[option2] + "tests flags with exclusions") + (@arg option2: --("long-option-2") conflicts_with[option] requires[positional2] + "tests long options with exclusions") + (@arg positional2: index(2) "tests positionals with exclusions") + (@arg option3: -O --Option +takes_value possible_value[fast slow] + "tests options with specific value sets") + (@arg ("positional-3"): index(3) ... possible_value[vi emacs] + "tests positionals with specific values") + (@arg multvals: --multvals +takes_value value_name[one two] + "Tests multiple values, not mult occs") + (@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two] + "Tests multiple values, not mult occs") + (@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals") + (@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals") + (@subcommand subcmd => + (about: "tests subcommands") + (version: "0.1") + (author: "Kevin K. <kbknapp@gmail.com>") + (@arg scoption: -o --option ... +takes_value "tests options") + (@arg scpositional: index(1) "tests positionals")) + ); + + let matches = app.get_matches_from_safe(vec!["bin_name", "value1", "value2", "--long-option-2"]) + .expect("Expected to successfully match the given args."); + assert!(matches.is_present("option2")); +} + +#[test] +fn group_macro() { + let app = clap_app!(claptests => + (version: "0.1") + (about: "tests clap library") + (author: "Kevin K. <kbknapp@gmail.com>") + (@group difficulty => + (@arg hard: -h --hard "Sets hard mode") + (@arg normal: -n --normal "Sets normal mode") + (@arg easy: -e --easy "Sets easy mode") + ) + ); + + let result = app.get_matches_from_safe(vec!["bin_name", "--hard"]); + assert!(result.is_ok()); + let matches = result.expect("Expected to successfully match the given args."); + assert!(matches.is_present("difficulty")); + assert!(matches.is_present("hard")); +} + +#[test] +fn group_macro_set_multiple() { + let app = clap_app!(claptests => + (version: "0.1") + (about: "tests clap library") + (author: "Kevin K. <kbknapp@gmail.com>") + (@group difficulty +multiple => + (@arg hard: -h --hard "Sets hard mode") + (@arg normal: -n --normal "Sets normal mode") + (@arg easy: -e --easy "Sets easy mode") + ) + ); + + let result = app.get_matches_from_safe(vec!["bin_name", "--hard", "--easy"]); + assert!(result.is_ok()); + let matches = result.expect("Expected to successfully match the given args."); + assert!(matches.is_present("difficulty")); + assert!(matches.is_present("hard")); + assert!(matches.is_present("easy")); + assert!(!matches.is_present("normal")); +} + +#[test] +fn group_macro_set_not_multiple() { + let app = clap_app!(claptests => + (version: "0.1") + (about: "tests clap library") + (author: "Kevin K. <kbknapp@gmail.com>") + (@group difficulty !multiple => + (@arg hard: -h --hard "Sets hard mode") + (@arg normal: -n --normal "Sets normal mode") + (@arg easy: -e --easy "Sets easy mode") + ) + ); + + let result = app.get_matches_from_safe(vec!["bin_name", "--hard", "--easy"]); + assert!(result.is_err()); + let err = result.unwrap_err(); + assert_eq!(err.kind, ErrorKind::ArgumentConflict); +} + +#[test] +fn group_macro_set_required() { + let app = clap_app!(claptests => + (version: "0.1") + (about: "tests clap library") + (author: "Kevin K. <kbknapp@gmail.com>") + (@group difficulty +required => + (@arg hard: -h --hard "Sets hard mode") + (@arg normal: -n --normal "Sets normal mode") + (@arg easy: -e --easy "Sets easy mode") + ) + ); + + let result = app.get_matches_from_safe(vec!["bin_name"]); + assert!(result.is_err()); + let err = result.unwrap_err(); + assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); +} + +#[test] +fn group_macro_set_not_required() { + let app = clap_app!(claptests => + (version: "0.1") + (about: "tests clap library") + (author: "Kevin K. <kbknapp@gmail.com>") + (@group difficulty !required => + (@arg hard: -h --hard "Sets hard mode") + (@arg normal: -n --normal "Sets normal mode") + (@arg easy: -e --easy "Sets easy mode") + ) + ); + + let result = app.get_matches_from_safe(vec!["bin_name"]); + assert!(result.is_ok()); + let matches = result.expect("Expected to successfully match the given args."); + assert!(!matches.is_present("difficulty")); +} + +#[test] +fn multiarg() { + let app = || clap_app!( + claptests => + (@arg flag: --flag "value") + (@arg multiarg: --multiarg + default_value("flag-unset") default_value_if("flag", None, "flag-set") + "multiarg") + (@arg multiarg2: --multiarg2 + default_value("flag-unset") default_value_if("flag", None, "flag-set",) + "multiarg2") + ); + + let matches = app() + .get_matches_from_safe(vec!["bin_name"]) + .expect("match failed"); + assert_eq!(matches.value_of("multiarg"), Some("flag-unset")); + assert_eq!(matches.value_of("multiarg2"), Some("flag-unset")); + + let matches = app() + .get_matches_from_safe(vec!["bin_name", "--flag"]) + .expect("match failed"); + assert_eq!(matches.value_of("multiarg"), Some("flag-set")); + assert_eq!(matches.value_of("multiarg2"), Some("flag-set")); +} + +#[test] +fn arg_enum() { + // Helper macros to avoid repetition + macro_rules! test_greek { + ($arg_enum:item, $tests:block) => {{ + $arg_enum + // FromStr implementation + assert!("Charlie".parse::<Greek>().is_err()); + // Display implementation + assert_eq!(format!("{}", Greek::Alpha), "Alpha"); + assert_eq!(format!("{}", Greek::Bravo), "Bravo"); + // fn variants() + assert_eq!(Greek::variants(), ["Alpha", "Bravo"]); + // rest of tests + $tests + }}; + } + macro_rules! test_greek_no_meta { + {$arg_enum:item} => { + test_greek!($arg_enum, { + // FromStr implementation + assert!("Alpha".parse::<Greek>().is_ok()); + assert!("Bravo".parse::<Greek>().is_ok()); + }) + }; + } + macro_rules! test_greek_meta { + {$arg_enum:item} => { + test_greek!($arg_enum, { + // FromStr implementation + assert_eq!("Alpha".parse::<Greek>(), Ok(Greek::Alpha)); + assert_eq!("Bravo".parse::<Greek>(), Ok(Greek::Bravo)); + }) + }; + } + + // Tests for each pattern + // meta NO, pub NO, trailing comma NO + test_greek_no_meta!{ + arg_enum!{ + enum Greek { + Alpha, + Bravo + } + } + }; + // meta NO, pub NO, trailing comma YES + test_greek_no_meta!{ + arg_enum!{ + enum Greek { + Alpha, + Bravo, + } + } + }; + // meta NO, pub YES, trailing comma NO + test_greek_no_meta!{ + arg_enum!{ + pub enum Greek { + Alpha, + Bravo + } + } + }; + // meta NO, pub YES, trailing comma YES + test_greek_no_meta!{ + arg_enum!{ + pub enum Greek { + Alpha, + Bravo, + } + } + }; + // meta YES, pub NO, trailing comma NO + test_greek_meta!{ + arg_enum!{ + #[derive(Debug, PartialEq, Copy, Clone)] + enum Greek { + Alpha, + Bravo + } + } + }; + // meta YES, pub NO, trailing comma YES + test_greek_meta!{ + arg_enum!{ + #[derive(Debug, PartialEq, Copy, Clone)] + enum Greek { + Alpha, + Bravo, + } + } + }; + // meta YES, pub YES, trailing comma NO + test_greek_meta!{ + arg_enum!{ + #[derive(Debug, PartialEq, Copy, Clone)] + pub enum Greek { + Alpha, + Bravo + } + } + }; + // meta YES, pub YES, trailing comma YES + test_greek_meta!{ + arg_enum!{ + #[derive(Debug, PartialEq, Copy, Clone)] + pub enum Greek { + Alpha, + Bravo, + } + } + }; +} |