2020-02-04 08:10:53 +00:00
|
|
|
mod utils;
|
2016-01-13 17:03:27 +00:00
|
|
|
|
2017-01-03 04:05:23 +00:00
|
|
|
use std::str;
|
|
|
|
|
2018-11-14 17:12:34 +00:00
|
|
|
use clap::{App, AppSettings, ErrorKind};
|
2016-01-13 17:03:27 +00:00
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static VERSION: &str = "clap-test v1.4.8";
|
2016-05-30 08:49:13 +00:00
|
|
|
|
2016-01-13 17:03:27 +00:00
|
|
|
#[test]
|
|
|
|
fn version_short() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.author("Kevin K.")
|
|
|
|
.about("tests stuff")
|
|
|
|
.version("1.3")
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-V"]);
|
2016-01-13 17:03:27 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2019-11-26 17:39:12 +00:00
|
|
|
let err = m.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::VersionDisplayed);
|
2020-04-12 01:39:13 +00:00
|
|
|
assert_eq!(err.to_string(), "test 1.3");
|
2016-01-13 17:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn version_long() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.author("Kevin K.")
|
|
|
|
.about("tests stuff")
|
|
|
|
.version("1.3")
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--version"]);
|
2016-01-13 17:03:27 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2019-11-26 17:39:12 +00:00
|
|
|
let err = m.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::VersionDisplayed);
|
2020-04-12 01:39:13 +00:00
|
|
|
assert_eq!(err.to_string(), "test 1.3");
|
2016-01-13 17:03:27 +00:00
|
|
|
}
|
2016-05-30 08:49:13 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn complex_version_output() {
|
2017-01-03 04:05:23 +00:00
|
|
|
let mut a = App::new("clap-test").version("v1.4.8");
|
2018-10-19 20:42:13 +00:00
|
|
|
let _ = a.try_get_matches_from_mut(vec![""]);
|
2017-01-03 04:05:23 +00:00
|
|
|
|
2018-01-25 04:05:05 +00:00
|
|
|
// Now we check the output of print_version()
|
|
|
|
let mut ver = vec![];
|
|
|
|
a.write_version(&mut ver).unwrap();
|
|
|
|
assert_eq!(str::from_utf8(&ver).unwrap(), VERSION);
|
2016-05-30 08:49:13 +00:00
|
|
|
}
|
2017-04-04 23:54:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_ver() {
|
|
|
|
let m = App::new("test")
|
2018-11-09 01:34:13 +00:00
|
|
|
.setting(AppSettings::NoAutoVersion)
|
2017-04-04 23:54:20 +00:00
|
|
|
.author("Kevin K.")
|
|
|
|
.about("tests stuff")
|
|
|
|
.version("1.3")
|
2018-11-14 17:05:06 +00:00
|
|
|
.mut_arg("version", |a| {
|
|
|
|
a.short('v').long("version").help("some version")
|
|
|
|
})
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["test", "--version"]);
|
2017-04-04 23:54:20 +00:00
|
|
|
|
2018-11-09 01:34:13 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
|
2017-04-04 23:54:20 +00:00
|
|
|
assert!(m.unwrap().is_present("version"));
|
2018-01-25 04:05:05 +00:00
|
|
|
}
|