]> git.proxmox.com Git - rustc.git/blob - src/vendor/clap/clap-test.rs
New upstream version 1.20.0+dfsg1
[rustc.git] / src / vendor / clap / clap-test.rs
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
43 // Legacy tests from the pyhton script days
44
45 pub fn complex_app() -> App<'static, 'static> {
46 let args = "-o --option=[opt]... 'tests options'
47 [positional] 'tests positionals'";
48 let opt3_vals = ["fast", "slow"];
49 let pos3_vals = ["vi", "emacs"];
50 App::new("clap-test")
51 .version("v1.4.8")
52 .about("tests clap library")
53 .author("Kevin K. <kbknapp@gmail.com>")
54 .args_from_usage(args)
55 .arg(Arg::from_usage("-f --flag... 'tests flags'")
56 .global(true))
57 .args(&[
58 Arg::from_usage("[flag2] -F 'tests flags with exclusions'").conflicts_with("flag").requires("long-option-2"),
59 Arg::from_usage("--long-option-2 [option2] 'tests long options with exclusions'").conflicts_with("option").requires("positional2"),
60 Arg::from_usage("[positional2] 'tests positionals with exclusions'"),
61 Arg::from_usage("-O --Option [option3] 'specific vals'").possible_values(&opt3_vals),
62 Arg::from_usage("[positional3]... 'tests specific values'").possible_values(&pos3_vals),
63 Arg::from_usage("--multvals [one] [two] 'Tests mutliple values, not mult occs'"),
64 Arg::from_usage("--multvalsmo... [one] [two] 'Tests mutliple values, and mult occs'"),
65 Arg::from_usage("--minvals2 [minvals]... 'Tests 2 min vals'").min_values(2),
66 Arg::from_usage("--maxvals3 [maxvals]... 'Tests 3 max vals'").max_values(3)
67 ])
68 .subcommand(SubCommand::with_name("subcmd")
69 .about("tests subcommands")
70 .version("0.1")
71 .author("Kevin K. <kbknapp@gmail.com>")
72 .arg_from_usage("-o --option [scoption]... 'tests options'")
73 .arg_from_usage("-s --subcmdarg [subcmdarg] 'tests other args'")
74 .arg_from_usage("[scpositional] 'tests positionals'"))
75 }
76 }