clap/tests/builder/empty_values.rs
Ed Page 16b0362807 fix(error):! Merge EmptyValue into InvalidValue
There isn't a reason to programmatically differentiate them so this
merges them simplify programamtic cases and to hopefully reduce binary
size.
2022-07-22 09:06:01 -05:00

128 lines
4 KiB
Rust

use super::utils;
use clap::{error::ErrorKind, Arg, Command};
#[test]
fn empty_values() {
let m = Command::new("config")
.arg(Arg::new("config").long("config").takes_value(true))
.try_get_matches_from(&["config", "--config", ""])
.unwrap();
assert_eq!(m.get_one::<String>("config").map(|v| v.as_str()), Some(""));
}
#[test]
fn empty_values_with_equals() {
let m = Command::new("config")
.arg(Arg::new("config").long("config").takes_value(true))
.try_get_matches_from(&["config", "--config="])
.unwrap();
assert_eq!(m.get_one::<String>("config").map(|v| v.as_str()), Some(""));
let m = Command::new("config")
.arg(Arg::new("config").short('c').takes_value(true))
.try_get_matches_from(&["config", "-c="])
.unwrap();
assert_eq!(m.get_one::<String>("config").map(|v| v.as_str()), Some(""))
}
#[test]
fn no_empty_values() {
let m = Command::new("config")
.arg(
Arg::new("config")
.long("config")
.takes_value(true)
.value_parser(clap::builder::NonEmptyStringValueParser::new()),
)
.try_get_matches_from(&["config", "--config", ""]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
let m = Command::new("config")
.arg(
Arg::new("config")
.short('c')
.takes_value(true)
.value_parser(clap::builder::NonEmptyStringValueParser::new()),
)
.try_get_matches_from(&["config", "-c", ""]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue)
}
#[test]
fn no_empty_values_with_equals() {
let m = Command::new("config")
.arg(
Arg::new("config")
.long("config")
.takes_value(true)
.value_parser(clap::builder::NonEmptyStringValueParser::new()),
)
.try_get_matches_from(&["config", "--config="]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
let m = Command::new("config")
.arg(
Arg::new("config")
.short('c')
.takes_value(true)
.value_parser(clap::builder::NonEmptyStringValueParser::new()),
)
.try_get_matches_from(&["config", "-c="]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
}
#[test]
fn no_empty_values_without_equals() {
let m = Command::new("config")
.arg(
Arg::new("config")
.long("config")
.takes_value(true)
.value_parser(clap::builder::NonEmptyStringValueParser::new()),
)
.try_get_matches_from(&["config", "--config"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
let m = Command::new("config")
.arg(
Arg::new("config")
.short('c')
.takes_value(true)
.value_parser(clap::builder::NonEmptyStringValueParser::new()),
)
.try_get_matches_from(&["config", "-c"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue)
}
#[test]
fn no_empty_values_without_equals_but_requires_equals() {
let cmd = Command::new("config").arg(
Arg::new("config")
.long("config")
.takes_value(true)
.value_parser(clap::builder::NonEmptyStringValueParser::new())
.require_equals(true),
);
let m = cmd.clone().try_get_matches_from(&["config", "--config"]);
// Should error on no equals rather than empty value.
assert!(m.is_err());
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
";
utils::assert_output(cmd, "config --config", NO_EUQALS_ERROR, true);
}