Matthew Maurer | 43ab161 | 2020-06-02 11:15:01 -0700 | [diff] [blame] | 1 | #[allow(unused_imports, dead_code)] |
| 2 | mod test { |
| 3 | use std::str; |
| 4 | use std::io::{Cursor, Write}; |
| 5 | |
| 6 | use regex::Regex; |
| 7 | |
| 8 | use clap::{App, Arg, SubCommand, ArgGroup}; |
| 9 | |
| 10 | fn compare<S, S2>(l: S, r: S2) -> bool |
| 11 | where S: AsRef<str>, |
| 12 | S2: AsRef<str> |
| 13 | { |
| 14 | let re = Regex::new("\x1b[^m]*m").unwrap(); |
| 15 | // Strip out any mismatching \r character on windows that might sneak in on either side |
| 16 | let ls = l.as_ref().trim().replace("\r", ""); |
| 17 | let rs = r.as_ref().trim().replace("\r", ""); |
| 18 | let left = re.replace_all(&*ls, ""); |
| 19 | let right = re.replace_all(&*rs, ""); |
| 20 | let b = left == right; |
| 21 | if !b { |
| 22 | println!(); |
| 23 | println!("--> left"); |
| 24 | println!("{}", left); |
| 25 | println!("--> right"); |
| 26 | println!("{}", right); |
| 27 | println!("--") |
| 28 | } |
| 29 | b |
| 30 | } |
| 31 | |
| 32 | pub fn compare_output(l: App, args: &str, right: &str, stderr: bool) -> bool { |
| 33 | let mut buf = Cursor::new(Vec::with_capacity(50)); |
| 34 | let res = l.get_matches_from_safe(args.split(' ').collect::<Vec<_>>()); |
| 35 | let err = res.unwrap_err(); |
| 36 | err.write_to(&mut buf).unwrap(); |
| 37 | let content = buf.into_inner(); |
| 38 | let left = String::from_utf8(content).unwrap(); |
| 39 | assert_eq!(stderr, err.use_stderr()); |
| 40 | compare(left, right) |
| 41 | } |
| 42 | pub fn compare_output2(l: App, args: &str, right1: &str, right2: &str, stderr: bool) -> bool { |
| 43 | let mut buf = Cursor::new(Vec::with_capacity(50)); |
| 44 | let res = l.get_matches_from_safe(args.split(' ').collect::<Vec<_>>()); |
| 45 | let err = res.unwrap_err(); |
| 46 | err.write_to(&mut buf).unwrap(); |
| 47 | let content = buf.into_inner(); |
| 48 | let left = String::from_utf8(content).unwrap(); |
| 49 | assert_eq!(stderr, err.use_stderr()); |
| 50 | compare(&*left, right1) || compare(&*left, right2) |
| 51 | } |
| 52 | |
| 53 | // Legacy tests from the Python script days |
| 54 | |
| 55 | pub fn complex_app() -> App<'static, 'static> { |
| 56 | let args = "-o --option=[opt]... 'tests options' |
| 57 | [positional] 'tests positionals'"; |
| 58 | let opt3_vals = ["fast", "slow"]; |
| 59 | let pos3_vals = ["vi", "emacs"]; |
| 60 | App::new("clap-test") |
| 61 | .version("v1.4.8") |
| 62 | .about("tests clap library") |
| 63 | .author("Kevin K. <kbknapp@gmail.com>") |
| 64 | .args_from_usage(args) |
| 65 | .arg(Arg::from_usage("-f --flag... 'tests flags'") |
| 66 | .global(true)) |
| 67 | .args(&[ |
| 68 | Arg::from_usage("[flag2] -F 'tests flags with exclusions'").conflicts_with("flag").requires("long-option-2"), |
| 69 | Arg::from_usage("--long-option-2 [option2] 'tests long options with exclusions'").conflicts_with("option").requires("positional2"), |
| 70 | Arg::from_usage("[positional2] 'tests positionals with exclusions'"), |
| 71 | Arg::from_usage("-O --Option [option3] 'specific vals'").possible_values(&opt3_vals), |
| 72 | Arg::from_usage("[positional3]... 'tests specific values'").possible_values(&pos3_vals), |
| 73 | Arg::from_usage("--multvals [one] [two] 'Tests multiple values, not mult occs'"), |
| 74 | Arg::from_usage("--multvalsmo... [one] [two] 'Tests multiple values, and mult occs'"), |
| 75 | Arg::from_usage("--minvals2 [minvals]... 'Tests 2 min vals'").min_values(2), |
| 76 | Arg::from_usage("--maxvals3 [maxvals]... 'Tests 3 max vals'").max_values(3) |
| 77 | ]) |
| 78 | .subcommand(SubCommand::with_name("subcmd") |
| 79 | .about("tests subcommands") |
| 80 | .version("0.1") |
| 81 | .author("Kevin K. <kbknapp@gmail.com>") |
| 82 | .arg_from_usage("-o --option [scoption]... 'tests options'") |
| 83 | .arg_from_usage("-s --subcmdarg [subcmdarg] 'tests other args'") |
| 84 | .arg_from_usage("[scpositional] 'tests positionals'")) |
| 85 | } |
| 86 | } |