mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
test(help): Verify how version interacts
This commit is contained in:
parent
75e340a3d9
commit
9cc0299c0c
1 changed files with 158 additions and 6 deletions
|
@ -1,5 +1,7 @@
|
|||
use clap::{error::ErrorKind, ArgAction, Command};
|
||||
|
||||
use crate::utils;
|
||||
|
||||
fn common() -> Command {
|
||||
Command::new("foo")
|
||||
}
|
||||
|
@ -12,12 +14,16 @@ fn with_long_version() -> Command {
|
|||
common().long_version("3.0 (abcdefg)")
|
||||
}
|
||||
|
||||
fn with_both() -> Command {
|
||||
common().version("3.0").long_version("3.0 (abcdefg)")
|
||||
}
|
||||
|
||||
fn with_subcommand() -> Command {
|
||||
with_version().subcommand(Command::new("bar").subcommand(Command::new("baz")))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_version_flag_short() {
|
||||
fn version_short_flag_no_version() {
|
||||
let res = common().try_get_matches_from("foo -V".split(' '));
|
||||
|
||||
assert!(res.is_err());
|
||||
|
@ -26,7 +32,7 @@ fn no_version_flag_short() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn no_version_flag_long() {
|
||||
fn version_long_flag_no_version() {
|
||||
let res = common().try_get_matches_from("foo --version".split(' '));
|
||||
|
||||
assert!(res.is_err());
|
||||
|
@ -35,7 +41,7 @@ fn no_version_flag_long() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn version_flag_from_version_short() {
|
||||
fn version_short_flag_with_version() {
|
||||
let res = with_version().try_get_matches_from("foo -V".split(' '));
|
||||
|
||||
assert!(res.is_err());
|
||||
|
@ -45,7 +51,7 @@ fn version_flag_from_version_short() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn version_flag_from_version_long() {
|
||||
fn version_long_flag_with_version() {
|
||||
let res = with_version().try_get_matches_from("foo --version".split(' '));
|
||||
|
||||
assert!(res.is_err());
|
||||
|
@ -55,7 +61,7 @@ fn version_flag_from_version_long() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn version_flag_from_long_version_short() {
|
||||
fn version_short_flag_with_long_version() {
|
||||
let res = with_long_version().try_get_matches_from("foo -V".split(' '));
|
||||
|
||||
assert!(res.is_err());
|
||||
|
@ -65,7 +71,7 @@ fn version_flag_from_long_version_short() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn version_flag_from_long_version_long() {
|
||||
fn version_long_flag_with_long_version() {
|
||||
let res = with_long_version().try_get_matches_from("foo --version".split(' '));
|
||||
|
||||
assert!(res.is_err());
|
||||
|
@ -74,6 +80,152 @@ fn version_flag_from_long_version_long() {
|
|||
assert_eq!(err.to_string(), "foo 3.0 (abcdefg)\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn version_short_flag_with_both() {
|
||||
let res = with_both().try_get_matches_from("foo -V".split(' '));
|
||||
|
||||
assert!(res.is_err());
|
||||
let err = res.unwrap_err();
|
||||
assert_eq!(err.kind(), ErrorKind::DisplayVersion);
|
||||
assert_eq!(err.to_string(), "foo 3.0\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn version_long_flag_with_both() {
|
||||
let res = with_both().try_get_matches_from("foo --version".split(' '));
|
||||
|
||||
assert!(res.is_err());
|
||||
let err = res.unwrap_err();
|
||||
assert_eq!(err.kind(), ErrorKind::DisplayVersion);
|
||||
assert_eq!(err.to_string(), "foo 3.0 (abcdefg)\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_short_flag_no_version() {
|
||||
static EXPECTED: &str = "\
|
||||
foo
|
||||
|
||||
Usage:
|
||||
foo
|
||||
|
||||
Options:
|
||||
-h, --help Print help information
|
||||
";
|
||||
let cmd = common();
|
||||
utils::assert_output(cmd, "foo -h", EXPECTED, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_long_flag_no_version() {
|
||||
static EXPECTED: &str = "\
|
||||
foo
|
||||
|
||||
Usage:
|
||||
foo
|
||||
|
||||
Options:
|
||||
-h, --help Print help information
|
||||
";
|
||||
let cmd = common();
|
||||
utils::assert_output(cmd, "foo --help", EXPECTED, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_short_flag_with_version() {
|
||||
static EXPECTED: &str = "\
|
||||
foo 3.0
|
||||
|
||||
Usage:
|
||||
foo
|
||||
|
||||
Options:
|
||||
-h, --help Print help information
|
||||
-V, --version Print version information
|
||||
";
|
||||
let cmd = with_version();
|
||||
utils::assert_output(cmd, "foo -h", EXPECTED, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_long_flag_with_version() {
|
||||
static EXPECTED: &str = "\
|
||||
foo 3.0
|
||||
|
||||
Usage:
|
||||
foo
|
||||
|
||||
Options:
|
||||
-h, --help Print help information
|
||||
-V, --version Print version information
|
||||
";
|
||||
let cmd = with_version();
|
||||
utils::assert_output(cmd, "foo --help", EXPECTED, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_short_flag_with_long_version() {
|
||||
static EXPECTED: &str = "\
|
||||
foo 3.0 (abcdefg)
|
||||
|
||||
Usage:
|
||||
foo
|
||||
|
||||
Options:
|
||||
-h, --help Print help information
|
||||
-V, --version Print version information
|
||||
";
|
||||
let cmd = with_long_version();
|
||||
utils::assert_output(cmd, "foo -h", EXPECTED, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_long_flag_with_long_version() {
|
||||
static EXPECTED: &str = "\
|
||||
foo 3.0 (abcdefg)
|
||||
|
||||
Usage:
|
||||
foo
|
||||
|
||||
Options:
|
||||
-h, --help Print help information
|
||||
-V, --version Print version information
|
||||
";
|
||||
let cmd = with_long_version();
|
||||
utils::assert_output(cmd, "foo --help", EXPECTED, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_short_flag_with_both() {
|
||||
static EXPECTED: &str = "\
|
||||
foo 3.0
|
||||
|
||||
Usage:
|
||||
foo
|
||||
|
||||
Options:
|
||||
-h, --help Print help information
|
||||
-V, --version Print version information
|
||||
";
|
||||
let cmd = with_both();
|
||||
utils::assert_output(cmd, "foo -h", EXPECTED, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_long_flag_with_both() {
|
||||
static EXPECTED: &str = "\
|
||||
foo 3.0
|
||||
|
||||
Usage:
|
||||
foo
|
||||
|
||||
Options:
|
||||
-h, --help Print help information
|
||||
-V, --version Print version information
|
||||
";
|
||||
let cmd = with_both();
|
||||
utils::assert_output(cmd, "foo --help", EXPECTED, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(debug_assertions)]
|
||||
#[should_panic = "Command foo: Long option names must be unique for each argument, but '--version' is in use by both 'ver' and 'version' (call `cmd.disable_version_flag(true)` to remove the auto-generated `--version`)"]
|
||||
|
|
Loading…
Reference in a new issue