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/tests.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/tests.rs')
-rw-r--r-- | clap/tests/tests.rs | 435 |
1 files changed, 435 insertions, 0 deletions
diff --git a/clap/tests/tests.rs b/clap/tests/tests.rs new file mode 100644 index 0000000..0ad825a --- /dev/null +++ b/clap/tests/tests.rs @@ -0,0 +1,435 @@ +#[macro_use] +extern crate clap; +extern crate regex; + +use std::io::Write; +use std::str; + +include!("../clap-test.rs"); + +use clap::{App, Arg}; + +static SCF2OP: &'static str = "flag present 2 times +option NOT present +positional NOT present +flag2 NOT present +option2 maybe present with value of: Nothing +positional2 maybe present with value of: Nothing +option3 NOT present +positional3 NOT present +option NOT present +positional NOT present +subcmd present +flag present 2 times +scoption present with value: some +An scoption: some +scpositional present with value: value +"; + +static SCFOP: &'static str = "flag present 1 times +option NOT present +positional NOT present +flag2 NOT present +option2 maybe present with value of: Nothing +positional2 maybe present with value of: Nothing +option3 NOT present +positional3 NOT present +option NOT present +positional NOT present +subcmd present +flag present 1 times +scoption present with value: some +An scoption: some +scpositional present with value: value +"; + +static O2P: &'static str = "flag NOT present +option present 2 times with value: some +An option: some +An option: other +positional present with value: value +flag2 NOT present +option2 maybe present with value of: Nothing +positional2 maybe present with value of: Nothing +option3 NOT present +positional3 NOT present +option present 2 times with value: some +An option: some +An option: other +positional present with value: value +subcmd NOT present +"; + +static F2OP: &'static str = "flag present 2 times +option present 1 times with value: some +An option: some +positional present with value: value +flag2 NOT present +option2 maybe present with value of: Nothing +positional2 maybe present with value of: Nothing +option3 NOT present +positional3 NOT present +option present 1 times with value: some +An option: some +positional present with value: value +subcmd NOT present +"; + +static FOP: &'static str = "flag present 1 times +option present 1 times with value: some +An option: some +positional present with value: value +flag2 NOT present +option2 maybe present with value of: Nothing +positional2 maybe present with value of: Nothing +option3 NOT present +positional3 NOT present +option present 1 times with value: some +An option: some +positional present with value: value +subcmd NOT present +"; + +pub fn check_complex_output(args: &str, out: &str) { + let mut w = vec![]; + let matches = test::complex_app().get_matches_from(args.split(' ').collect::<Vec<_>>()); + if matches.is_present("flag") { + writeln!(w, "flag present {} times", matches.occurrences_of("flag")).unwrap(); + } else { + writeln!(w, "flag NOT present").unwrap(); + } + + if matches.is_present("option") { + if let Some(v) = matches.value_of("option") { + writeln!(w, "option present {} times with value: {}",matches.occurrences_of("option"), v).unwrap(); + } + if let Some(ov) = matches.values_of("option") { + for o in ov { + writeln!(w, "An option: {}", o).unwrap(); + } + } + } else { + writeln!(w, "option NOT present").unwrap(); + } + + if let Some(p) = matches.value_of("positional") { + writeln!(w, "positional present with value: {}", p).unwrap(); + } else { + writeln!(w, "positional NOT present").unwrap(); + } + + if matches.is_present("flag2") { + writeln!(w, "flag2 present").unwrap(); + writeln!(w, "option2 present with value of: {}", matches.value_of("long-option-2").unwrap()).unwrap(); + writeln!(w, "positional2 present with value of: {}", matches.value_of("positional2").unwrap()).unwrap(); + } else { + writeln!(w, "flag2 NOT present").unwrap(); + writeln!(w, "option2 maybe present with value of: {}", matches.value_of("long-option-2").unwrap_or("Nothing")).unwrap(); + writeln!(w, "positional2 maybe present with value of: {}", matches.value_of("positional2").unwrap_or("Nothing")).unwrap(); + } + + let _ = match matches.value_of("Option3").unwrap_or("") { + "fast" => writeln!(w, "option3 present quickly"), + "slow" => writeln!(w, "option3 present slowly"), + _ => writeln!(w, "option3 NOT present") + }; + + let _ = match matches.value_of("positional3").unwrap_or("") { + "vi" => writeln!(w, "positional3 present in vi mode"), + "emacs" => writeln!(w, "positional3 present in emacs mode"), + _ => writeln!(w, "positional3 NOT present") + }; + + if matches.is_present("option") { + if let Some(v) = matches.value_of("option") { + writeln!(w, "option present {} times with value: {}",matches.occurrences_of("option"), v).unwrap(); + } + if let Some(ov) = matches.values_of("option") { + for o in ov { + writeln!(w, "An option: {}", o).unwrap(); + } + } + } else { + writeln!(w, "option NOT present").unwrap(); + } + + if let Some(p) = matches.value_of("positional") { + writeln!(w, "positional present with value: {}", p).unwrap(); + } else { + writeln!(w, "positional NOT present").unwrap(); + } + if matches.is_present("subcmd") { + writeln!(w, "subcmd present").unwrap(); + if let Some(matches) = matches.subcommand_matches("subcmd") { + if matches.is_present("flag") { + writeln!(w, "flag present {} times", matches.occurrences_of("flag")).unwrap(); + } else { + writeln!(w, "flag NOT present").unwrap(); + } + + if matches.is_present("option") { + if let Some(v) = matches.value_of("option") { + writeln!(w, "scoption present with value: {}", v).unwrap(); + } + if let Some(ov) = matches.values_of("option") { + for o in ov { + writeln!(w, "An scoption: {}", o).unwrap(); + } + } + } else { + writeln!(w, "scoption NOT present").unwrap(); + } + + if let Some(p) = matches.value_of("scpositional") { + writeln!(w, "scpositional present with value: {}", p).unwrap(); + } + } + } else { + writeln!(w, "subcmd NOT present").unwrap(); + } + + let res = str::from_utf8(&w).unwrap(); + assert_eq!(res, out); +} + +arg_enum!{ + #[derive(Debug)] + enum Val1 { + ValOne, + ValTwo + } +} +arg_enum!{ + #[derive(Debug)] + pub enum Val2 { + ValOne, + ValTwo + } +} +arg_enum!{ + enum Val3 { + ValOne, + ValTwo + } +} +arg_enum!{ + pub enum Val4 { + ValOne, + ValTwo + } +} + +#[test] +fn test_enums() { + let v1_lower = "valone"; + let v1_camel = "ValOne"; + + let v1_lp = v1_lower.parse::<Val1>().unwrap(); + let v1_cp = v1_camel.parse::<Val1>().unwrap(); + match v1_lp { + Val1::ValOne => (), + _ => panic!("Val1 didn't parse correctly"), + } + match v1_cp { + Val1::ValOne => (), + _ => panic!("Val1 didn't parse correctly"), + } + let v1_lp = v1_lower.parse::<Val2>().unwrap(); + let v1_cp = v1_camel.parse::<Val2>().unwrap(); + match v1_lp { + Val2::ValOne => (), + _ => panic!("Val1 didn't parse correctly"), + } + match v1_cp { + Val2::ValOne => (), + _ => panic!("Val1 didn't parse correctly"), + } + let v1_lp = v1_lower.parse::<Val3>().unwrap(); + let v1_cp = v1_camel.parse::<Val3>().unwrap(); + match v1_lp { + Val3::ValOne => (), + _ => panic!("Val1 didn't parse correctly"), + } + match v1_cp { + Val3::ValOne => (), + _ => panic!("Val1 didn't parse correctly"), + } + let v1_lp = v1_lower.parse::<Val4>().unwrap(); + let v1_cp = v1_camel.parse::<Val4>().unwrap(); + match v1_lp { + Val4::ValOne => (), + _ => panic!("Val1 didn't parse correctly"), + } + match v1_cp { + Val4::ValOne => (), + _ => panic!("Val1 didn't parse correctly"), + } +} + +#[test] +fn create_app() { + let _ = + App::new("test").version("1.0").author("kevin").about("does awesome things").get_matches_from(vec![""]); +} + +#[test] +fn add_multiple_arg() { + let _ = App::new("test") + .args(&[ + Arg::with_name("test").short("s"), + Arg::with_name("test2").short("l")]) + .get_matches_from(vec![""]); +} +#[test] +fn flag_x2_opt() { + check_complex_output("clap-test value -f -f -o some", +"flag present 2 times +option present 1 times with value: some +An option: some +positional present with value: value +flag2 NOT present +option2 maybe present with value of: Nothing +positional2 maybe present with value of: Nothing +option3 NOT present +positional3 NOT present +option present 1 times with value: some +An option: some +positional present with value: value +subcmd NOT present +"); +} + +#[test] +fn long_opt_x2_pos() { + check_complex_output("clap-test value --option some --option other", O2P); +} + +#[test] +fn long_opt_eq_x2_pos() { + check_complex_output("clap-test value --option=some --option=other", O2P); +} + +#[test] +fn short_opt_x2_pos() { + check_complex_output("clap-test value -o some -o other", O2P); +} + +#[test] +fn short_opt_eq_x2_pos() { + check_complex_output("clap-test value -o=some -o=other", O2P); +} + +#[test] +fn short_flag_x2_comb_short_opt_pos() { + check_complex_output("clap-test value -ff -o some", F2OP); +} + +#[test] +fn short_flag_short_opt_pos() { + check_complex_output("clap-test value -f -o some", FOP); +} + +#[test] +fn long_flag_long_opt_pos() { + check_complex_output("clap-test value --flag --option some", FOP); +} + +#[test] +fn long_flag_long_opt_eq_pos() { + check_complex_output("clap-test value --flag --option=some", FOP); +} + +#[test] +fn sc_long_flag_long_opt() { + check_complex_output("clap-test subcmd value --flag --option some", SCFOP); +} + +#[test] +fn sc_long_flag_short_opt_pos() { + check_complex_output("clap-test subcmd value --flag -o some", SCFOP); +} + +#[test] +fn sc_long_flag_long_opt_eq_pos() { + check_complex_output("clap-test subcmd value --flag --option=some", SCFOP); +} + +#[test] +fn sc_short_flag_long_opt_pos() { + check_complex_output("clap-test subcmd value -f --option some", SCFOP); +} + +#[test] +fn sc_short_flag_short_opt_pos() { + check_complex_output("clap-test subcmd value -f -o some", SCFOP); +} + +#[test] +fn sc_short_flag_short_opt_eq_pos() { + check_complex_output("clap-test subcmd value -f -o=some", SCFOP); +} + +#[test] +fn sc_short_flag_long_opt_eq_pos() { + check_complex_output("clap-test subcmd value -f --option=some", SCFOP); +} + +#[test] +fn sc_short_flag_x2_comb_long_opt_pos() { + check_complex_output("clap-test subcmd value -ff --option some", SCF2OP); +} + +#[test] +fn sc_short_flag_x2_comb_short_opt_pos() { + check_complex_output("clap-test subcmd value -ff -o some", SCF2OP); +} + +#[test] +fn sc_short_flag_x2_comb_long_opt_eq_pos() { + check_complex_output("clap-test subcmd value -ff --option=some", SCF2OP); +} + +#[test] +fn sc_short_flag_x2_comb_short_opt_eq_pos() { + check_complex_output("clap-test subcmd value -ff -o=some", SCF2OP); +} + +#[test] +fn sc_long_flag_x2_long_opt_pos() { + check_complex_output("clap-test subcmd value --flag --flag --option some", SCF2OP); +} + +#[test] +fn sc_long_flag_x2_short_opt_pos() { + check_complex_output("clap-test subcmd value --flag --flag -o some", SCF2OP); +} + +#[test] +fn sc_long_flag_x2_short_opt_eq_pos() { + check_complex_output("clap-test subcmd value --flag --flag -o=some", SCF2OP); +} + +#[test] +fn sc_long_flag_x2_long_opt_eq_pos() { + check_complex_output("clap-test subcmd value --flag --flag --option=some", SCF2OP); +} + +#[test] +fn sc_short_flag_x2_long_opt_pos() { + check_complex_output("clap-test subcmd value -f -f --option some", SCF2OP); +} + +#[test] +fn sc_short_flag_x2_short_opt_pos() { + check_complex_output("clap-test subcmd value -f -f -o some", SCF2OP); +} + +#[test] +fn sc_short_flag_x2_short_opt_eq_pos() { + check_complex_output("clap-test subcmd value -f -f -o=some", SCF2OP); +} + +#[test] +fn sc_short_flag_x2_long_opt_eq_pos() { + check_complex_output("clap-test subcmd value -f -f --option=some", SCF2OP); +} |