clap/tests/builder/empty_values.rs

134 lines
3.7 KiB
Rust
Raw Normal View History

use crate::utils;
2022-02-12 03:48:29 +00:00
use clap::{error::ErrorKind, Arg, Command};
#[test]
fn empty_values() {
2022-02-12 03:48:29 +00:00
let m = Command::new("config")
.arg(Arg::new("config").long("config").takes_value(true))
.try_get_matches_from(&["config", "--config", ""])
.unwrap();
assert_eq!(m.value_of("config"), Some(""));
}
#[test]
fn empty_values_with_equals() {
2022-02-12 03:48:29 +00:00
let m = Command::new("config")
.arg(Arg::new("config").long("config").takes_value(true))
.try_get_matches_from(&["config", "--config="])
.unwrap();
assert_eq!(m.value_of("config"), Some(""));
2022-02-12 03:48:29 +00:00
let m = Command::new("config")
.arg(Arg::new("config").short('c').takes_value(true))
.try_get_matches_from(&["config", "-c="])
.unwrap();
assert_eq!(m.value_of("config"), Some(""))
}
#[test]
fn no_empty_values() {
2022-02-12 03:48:29 +00:00
let m = Command::new("config")
.arg(
Arg::new("config")
.long("config")
.takes_value(true)
.forbid_empty_values(true),
)
.try_get_matches_from(&["config", "--config", ""]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::EmptyValue);
2022-02-12 03:48:29 +00:00
let m = Command::new("config")
.arg(
Arg::new("config")
.short('c')
.takes_value(true)
.forbid_empty_values(true),
)
.try_get_matches_from(&["config", "-c", ""]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::EmptyValue)
}
#[test]
fn no_empty_values_with_equals() {
2022-02-12 03:48:29 +00:00
let m = Command::new("config")
.arg(
Arg::new("config")
.long("config")
.takes_value(true)
.forbid_empty_values(true),
)
.try_get_matches_from(&["config", "--config="]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::EmptyValue);
2022-02-12 03:48:29 +00:00
let m = Command::new("config")
.arg(
Arg::new("config")
.short('c')
.takes_value(true)
.forbid_empty_values(true),
)
.try_get_matches_from(&["config", "-c="]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::EmptyValue);
}
#[test]
fn no_empty_values_without_equals() {
2022-02-12 03:48:29 +00:00
let m = Command::new("config")
.arg(
Arg::new("config")
.long("config")
.takes_value(true)
.forbid_empty_values(true),
)
.try_get_matches_from(&["config", "--config"]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::EmptyValue);
2022-02-12 03:48:29 +00:00
let m = Command::new("config")
.arg(
Arg::new("config")
.short('c')
.takes_value(true)
.forbid_empty_values(true),
)
.try_get_matches_from(&["config", "-c"]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::EmptyValue)
}
#[test]
fn no_empty_values_without_equals_but_requires_equals() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new("config").arg(
Arg::new("config")
.long("config")
.takes_value(true)
.forbid_empty_values(true)
.require_equals(true),
);
2022-02-14 21:47:20 +00:00
let m = cmd.clone().try_get_matches_from(&["config", "--config"]);
// Should error on no equals rather than empty value.
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::NoEquals);
static NO_EUQALS_ERROR: &str =
"error: Equal sign is needed when assigning values to '--config=<config>'.
USAGE:
config [OPTIONS]
For more information try --help
";
assert!(utils::compare_output(
2022-02-14 21:47:20 +00:00
cmd,
"config --config",
NO_EUQALS_ERROR,
true
));
}