2016-05-10 02:46:09 +00:00
|
|
|
#[allow(unused_imports, dead_code)]
|
|
|
|
mod test {
|
|
|
|
use std::str;
|
2017-01-03 04:05:23 +00:00
|
|
|
use std::io::{Cursor, Write};
|
2016-05-10 02:46:09 +00:00
|
|
|
|
|
|
|
use regex::Regex;
|
|
|
|
|
2018-10-19 20:42:13 +00:00
|
|
|
use clap::{App, Arg, ArgGroup};
|
2016-05-10 02:46:09 +00:00
|
|
|
|
2017-01-03 04:05:23 +00:00
|
|
|
fn compare<S, S2>(l: S, r: S2) -> bool
|
|
|
|
where S: AsRef<str>,
|
2017-02-21 04:51:20 +00:00
|
|
|
S2: AsRef<str>
|
|
|
|
{
|
2017-01-03 04:05:23 +00:00
|
|
|
let re = Regex::new("\x1b[^m]*m").unwrap();
|
|
|
|
// Strip out any mismatching \r character on windows that might sneak in on either side
|
2017-02-28 13:46:21 +00:00
|
|
|
let ls = l.as_ref().trim().replace("\r", "");
|
|
|
|
let rs = r.as_ref().trim().replace("\r", "");
|
|
|
|
let left = re.replace_all(&*ls, "");
|
|
|
|
let right = re.replace_all(&*rs, "");
|
2017-01-03 04:05:23 +00:00
|
|
|
let b = left == right;
|
|
|
|
if !b {
|
|
|
|
println!("");
|
|
|
|
println!("--> left");
|
|
|
|
println!("{}", left);
|
|
|
|
println!("--> right");
|
|
|
|
println!("{}", right);
|
|
|
|
println!("--")
|
|
|
|
}
|
|
|
|
b
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compare_output(l: App, args: &str, right: &str, stderr: bool) -> bool {
|
|
|
|
let mut buf = Cursor::new(Vec::with_capacity(50));
|
2018-10-19 20:42:13 +00:00
|
|
|
let res = l.try_get_matches_from(args.split(' ').collect::<Vec<_>>());
|
2017-01-03 04:05:23 +00:00
|
|
|
let err = res.unwrap_err();
|
|
|
|
err.write_to(&mut buf).unwrap();
|
|
|
|
let content = buf.into_inner();
|
|
|
|
let left = String::from_utf8(content).unwrap();
|
2018-01-25 02:08:14 +00:00
|
|
|
assert_eq!(stderr, err.use_stderr(),
|
|
|
|
"Should Use STDERR failed. Should be {} but is {}", stderr, err.use_stderr());
|
2017-01-03 04:05:23 +00:00
|
|
|
compare(left, right)
|
|
|
|
}
|
2018-01-25 02:08:14 +00:00
|
|
|
|
2018-01-09 15:16:56 +00:00
|
|
|
pub fn compare_output2(l: App, args: &str, right1: &str, right2: &str, stderr: bool) -> bool {
|
|
|
|
let mut buf = Cursor::new(Vec::with_capacity(50));
|
2018-10-19 20:42:13 +00:00
|
|
|
let res = l.try_get_matches_from(args.split(' ').collect::<Vec<_>>());
|
2018-01-09 15:16:56 +00:00
|
|
|
let err = res.unwrap_err();
|
|
|
|
err.write_to(&mut buf).unwrap();
|
|
|
|
let content = buf.into_inner();
|
|
|
|
let left = String::from_utf8(content).unwrap();
|
|
|
|
assert_eq!(stderr, err.use_stderr());
|
|
|
|
compare(&*left, right1) || compare(&*left, right2)
|
|
|
|
}
|
2017-01-03 04:05:23 +00:00
|
|
|
|
|
|
|
// Legacy tests from the pyhton script days
|
|
|
|
|
2019-04-05 02:06:23 +00:00
|
|
|
pub fn complex_app() -> App<'static> {
|
2016-05-10 02:46:09 +00:00
|
|
|
let opt3_vals = ["fast", "slow"];
|
|
|
|
let pos3_vals = ["vi", "emacs"];
|
|
|
|
App::new("clap-test")
|
|
|
|
.version("v1.4.8")
|
|
|
|
.about("tests clap library")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("-o --option=[opt]... 'tests options'")
|
|
|
|
.arg("[positional] 'tests positionals'")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-f --flag... 'tests flags'")
|
2016-05-10 02:46:09 +00:00
|
|
|
.global(true))
|
|
|
|
.args(&[
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("[flag2] -F 'tests flags with exclusions'").conflicts_with("flag").requires("long-option-2"),
|
|
|
|
Arg::from("--long-option-2 [option2] 'tests long options with exclusions'").conflicts_with("option").requires("positional2"),
|
|
|
|
Arg::from("[positional2] 'tests positionals with exclusions'"),
|
|
|
|
Arg::from("-O --Option [option3] 'specific vals'").possible_values(&opt3_vals),
|
|
|
|
Arg::from("[positional3]... 'tests specific values'").possible_values(&pos3_vals),
|
|
|
|
Arg::from("--multvals [one] [two] 'Tests mutliple values, not mult occs'"),
|
|
|
|
Arg::from("--multvalsmo... [one] [two] 'Tests mutliple values, and mult occs'"),
|
|
|
|
Arg::from("--minvals2 [minvals]... 'Tests 2 min vals'").min_values(2),
|
|
|
|
Arg::from("--maxvals3 [maxvals]... 'Tests 3 max vals'").max_values(3)
|
2016-05-10 02:46:09 +00:00
|
|
|
])
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("subcmd")
|
2016-05-10 02:46:09 +00:00
|
|
|
.about("tests subcommands")
|
|
|
|
.version("0.1")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("-o --option [scoption]... 'tests options'")
|
|
|
|
.arg("-s --subcmdarg [subcmdarg] 'tests other args'")
|
|
|
|
.arg("[scpositional] 'tests positionals'"))
|
2016-05-10 02:46:09 +00:00
|
|
|
}
|
2017-02-27 03:37:03 +00:00
|
|
|
}
|