clap/tests/builder/possible_values.rs

409 lines
11 KiB
Rust
Raw Normal View History

use crate::utils;
2022-02-12 03:48:29 +00:00
use clap::{error::ErrorKind, Arg, Command, PossibleValue};
2017-11-28 12:30:06 +00:00
#[cfg(feature = "suggestions")]
2020-09-24 14:02:10 +00:00
static PV_ERROR: &str = "error: \"slo\" isn't a valid value for '-O <option>'
\t[possible values: slow, fast, \"ludicrous speed\"]
2020-09-24 14:02:10 +00:00
\tDid you mean \"slow\"?
USAGE:
2020-09-24 14:02:10 +00:00
clap-test -O <option>
For more information try --help
";
2017-11-28 12:30:06 +00:00
#[cfg(not(feature = "suggestions"))]
2021-12-08 03:18:35 +00:00
static PV_ERROR: &str = "error: \"slo\" isn't a valid value for '-O <option>'
\t[possible values: slow, fast, \"ludicrous speed\"]
USAGE:
2020-09-24 14:02:10 +00:00
clap-test -O <option>
For more information try --help
";
2020-09-24 14:02:10 +00:00
#[cfg(feature = "suggestions")]
static PV_ERROR_ESCAPED: &str = "error: \"ludicrous\" isn't a valid value for '-O <option>'
\t[possible values: slow, fast, \"ludicrous speed\"]
2020-09-24 14:02:10 +00:00
\tDid you mean \"ludicrous speed\"?
USAGE:
clap-test -O <option>
For more information try --help
";
2020-09-24 14:02:10 +00:00
#[cfg(not(feature = "suggestions"))]
2021-12-08 03:18:35 +00:00
static PV_ERROR_ESCAPED: &str = "error: \"ludicrous\" isn't a valid value for '-O <option>'
\t[possible values: slow, fast, \"ludicrous speed\"]
2020-09-24 14:02:10 +00:00
USAGE:
clap-test -O <option>
For more information try --help
";
#[test]
fn possible_values_of_positional() {
2022-02-12 03:48:29 +00:00
let m = Command::new("possible_values")
.arg(Arg::new("positional").index(1).possible_value("test123"))
.try_get_matches_from(vec!["myprog", "test123"]);
2021-12-27 19:57:38 +00:00
assert!(m.is_ok(), "{}", m.unwrap_err());
let m = m.unwrap();
assert!(m.is_present("positional"));
assert_eq!(m.value_of("positional"), Some("test123"));
}
#[test]
fn possible_value_arg_value() {
2022-02-12 03:48:29 +00:00
let m = Command::new("possible_values")
.arg(
Arg::new("arg_value").index(1).possible_value(
PossibleValue::new("test123")
.hide(false)
.help("It's just a test"),
),
)
.try_get_matches_from(vec!["myprog", "test123"]);
2021-12-27 19:57:38 +00:00
assert!(m.is_ok(), "{}", m.unwrap_err());
let m = m.unwrap();
assert!(m.is_present("arg_value"));
assert_eq!(m.value_of("arg_value"), Some("test123"));
}
#[test]
fn possible_values_of_positional_fail() {
2022-02-12 03:48:29 +00:00
let m = Command::new("possible_values")
.arg(Arg::new("positional").index(1).possible_value("test123"))
.try_get_matches_from(vec!["myprog", "notest"]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
}
#[test]
fn possible_values_of_positional_multiple() {
2022-02-12 03:48:29 +00:00
let m = Command::new("possible_values")
2017-11-28 12:30:06 +00:00
.arg(
Arg::new("positional")
2017-11-28 12:30:06 +00:00
.index(1)
2021-02-24 15:07:57 +00:00
.takes_value(true)
2017-11-28 12:30:06 +00:00
.possible_value("test123")
.possible_value("test321")
2021-06-16 05:28:25 +00:00
.multiple_values(true),
2017-11-28 12:30:06 +00:00
)
.try_get_matches_from(vec!["myprog", "test123", "test321"]);
2021-12-27 19:57:38 +00:00
assert!(m.is_ok(), "{}", m.unwrap_err());
let m = m.unwrap();
assert!(m.is_present("positional"));
2017-11-28 12:30:06 +00:00
assert_eq!(
m.values_of("positional").unwrap().collect::<Vec<_>>(),
vec!["test123", "test321"]
);
}
#[test]
fn possible_values_of_positional_multiple_fail() {
2022-02-12 03:48:29 +00:00
let m = Command::new("possible_values")
2017-11-28 12:30:06 +00:00
.arg(
Arg::new("positional")
2017-11-28 12:30:06 +00:00
.index(1)
2021-02-24 15:07:57 +00:00
.takes_value(true)
2017-11-28 12:30:06 +00:00
.possible_value("test123")
.possible_value("test321")
2021-06-16 05:28:25 +00:00
.multiple_values(true),
2017-11-28 12:30:06 +00:00
)
.try_get_matches_from(vec!["myprog", "test123", "notest"]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
}
#[test]
fn possible_values_of_option() {
2022-02-12 03:48:29 +00:00
let m = Command::new("possible_values")
2017-11-28 12:30:06 +00:00
.arg(
Arg::new("option")
.short('o')
.long("option")
2017-11-28 12:30:06 +00:00
.takes_value(true)
.possible_value("test123"),
)
.try_get_matches_from(vec!["myprog", "--option", "test123"]);
2021-12-27 19:57:38 +00:00
assert!(m.is_ok(), "{}", m.unwrap_err());
let m = m.unwrap();
assert!(m.is_present("option"));
assert_eq!(m.value_of("option"), Some("test123"));
}
#[test]
fn possible_values_of_option_fail() {
2022-02-12 03:48:29 +00:00
let m = Command::new("possible_values")
2017-11-28 12:30:06 +00:00
.arg(
Arg::new("option")
.short('o')
.long("option")
2017-11-28 12:30:06 +00:00
.takes_value(true)
.possible_value("test123"),
)
.try_get_matches_from(vec!["myprog", "--option", "notest"]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
}
#[test]
fn possible_values_of_option_multiple() {
2022-02-12 03:48:29 +00:00
let m = Command::new("possible_values")
2017-11-28 12:30:06 +00:00
.arg(
Arg::new("option")
.short('o')
.long("option")
2017-11-28 12:30:06 +00:00
.takes_value(true)
.possible_value("test123")
.possible_value("test321")
2021-06-16 05:28:25 +00:00
.multiple_occurrences(true),
2017-11-28 12:30:06 +00:00
)
.try_get_matches_from(vec!["", "--option", "test123", "--option", "test321"]);
2021-12-27 19:57:38 +00:00
assert!(m.is_ok(), "{}", m.unwrap_err());
let m = m.unwrap();
assert!(m.is_present("option"));
2017-11-28 12:30:06 +00:00
assert_eq!(
m.values_of("option").unwrap().collect::<Vec<_>>(),
vec!["test123", "test321"]
);
}
#[test]
fn possible_values_of_option_multiple_fail() {
2022-02-12 03:48:29 +00:00
let m = Command::new("possible_values")
2017-11-28 12:30:06 +00:00
.arg(
Arg::new("option")
.short('o')
.long("option")
2017-11-28 12:30:06 +00:00
.takes_value(true)
.possible_value("test123")
.possible_value("test321")
2021-06-16 05:28:25 +00:00
.multiple_occurrences(true),
2017-11-28 12:30:06 +00:00
)
.try_get_matches_from(vec!["", "--option", "test123", "--option", "notest"]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
}
#[test]
fn possible_values_output() {
2022-04-29 20:32:25 +00:00
utils::assert_output(
2022-02-12 03:48:29 +00:00
Command::new("test").arg(Arg::new("option").short('O').possible_values([
2020-09-25 08:38:35 +00:00
"slow",
"fast",
2022-04-29 20:32:25 +00:00
"ludicrous speed",
2020-09-25 08:38:35 +00:00
])),
2017-11-28 12:30:06 +00:00
"clap-test -O slo",
PV_ERROR,
2022-04-29 20:32:25 +00:00
true,
);
}
2021-09-26 20:46:23 +00:00
#[test]
fn possible_values_alias_output() {
2022-04-29 20:32:25 +00:00
utils::assert_output(
2022-02-12 03:48:29 +00:00
Command::new("test").arg(
2021-09-26 20:46:23 +00:00
Arg::new("option")
.short('O')
.possible_value("slow")
.possible_value(PossibleValue::new("fast").alias("fost"))
2022-04-29 20:32:25 +00:00
.possible_value(PossibleValue::new("ludicrous speed").aliases(["ls", "lcs"])),
2021-09-26 20:46:23 +00:00
),
"clap-test -O slo",
PV_ERROR,
2022-04-29 20:32:25 +00:00
true,
);
2021-09-26 20:46:23 +00:00
}
#[test]
fn possible_values_hidden_output() {
2022-04-29 20:32:25 +00:00
utils::assert_output(
2022-02-12 03:48:29 +00:00
Command::new("test").arg(
Arg::new("option")
.short('O')
.possible_values(["slow", "fast"])
.possible_value(PossibleValue::new("ludicrous speed"))
2022-04-29 20:32:25 +00:00
.possible_value(PossibleValue::new("forbidden speed").hide(true)),
),
"clap-test -O slo",
PV_ERROR,
2022-04-29 20:32:25 +00:00
true,
);
}
2020-09-24 14:02:10 +00:00
#[test]
fn escaped_possible_values_output() {
2022-04-29 20:32:25 +00:00
utils::assert_output(
2022-02-12 03:48:29 +00:00
Command::new("test").arg(Arg::new("option").short('O').possible_values([
2020-09-25 08:38:35 +00:00
"slow",
"fast",
2022-04-29 20:32:25 +00:00
"ludicrous speed",
2020-09-25 08:38:35 +00:00
])),
2020-09-24 14:02:10 +00:00
"clap-test -O ludicrous",
PV_ERROR_ESCAPED,
2022-04-29 20:32:25 +00:00
true,
);
2020-09-24 14:02:10 +00:00
}
#[test]
fn missing_possible_value_error() {
2022-04-29 20:32:25 +00:00
utils::assert_output(
2022-02-12 03:48:29 +00:00
Command::new("test").arg(
Arg::new("option")
.short('O')
.possible_value("slow")
.possible_value(PossibleValue::new("fast").alias("fost"))
.possible_value(PossibleValue::new("ludicrous speed"))
2022-04-29 20:32:25 +00:00
.possible_value(PossibleValue::new("forbidden speed").hide(true)),
),
"clap-test -O",
MISSING_PV_ERROR,
2022-04-29 20:32:25 +00:00
true,
);
}
static MISSING_PV_ERROR: &str =
"error: The argument '-O <option>' requires a value but none was supplied
\t[possible values: slow, fast, \"ludicrous speed\"]
USAGE:
clap-test [OPTIONS]
For more information try --help
";
2021-09-26 20:46:23 +00:00
#[test]
fn alias() {
2022-02-12 03:48:29 +00:00
let m = Command::new("pv")
2021-09-26 20:46:23 +00:00
.arg(
Arg::new("option")
.short('o')
.long("option")
2021-09-26 20:46:23 +00:00
.takes_value(true)
.possible_value(PossibleValue::new("test123").alias("123"))
2021-09-26 20:46:23 +00:00
.possible_value("test321")
.ignore_case(true),
2021-09-26 20:46:23 +00:00
)
.try_get_matches_from(vec!["pv", "--option", "123"]);
2021-12-27 19:57:38 +00:00
assert!(m.is_ok(), "{}", m.unwrap_err());
2021-09-26 20:46:23 +00:00
assert!(m.unwrap().value_of("option").unwrap().eq("123"));
}
#[test]
fn aliases() {
2022-02-12 03:48:29 +00:00
let m = Command::new("pv")
2021-09-26 20:46:23 +00:00
.arg(
Arg::new("option")
.short('o')
.long("option")
2021-09-26 20:46:23 +00:00
.takes_value(true)
.possible_value(PossibleValue::new("test123").aliases(["1", "2", "3"]))
2021-09-26 20:46:23 +00:00
.possible_value("test321")
.ignore_case(true),
2021-09-26 20:46:23 +00:00
)
.try_get_matches_from(vec!["pv", "--option", "2"]);
2021-12-27 19:57:38 +00:00
assert!(m.is_ok(), "{}", m.unwrap_err());
2021-09-26 20:46:23 +00:00
assert!(m.unwrap().value_of("option").unwrap().eq("2"));
}
#[test]
fn ignore_case() {
2022-02-12 03:48:29 +00:00
let m = Command::new("pv")
2017-11-28 12:30:06 +00:00
.arg(
Arg::new("option")
.short('o')
.long("option")
2017-11-28 12:30:06 +00:00
.takes_value(true)
.possible_value("test123")
.possible_value("test321")
.ignore_case(true),
2017-11-28 12:30:06 +00:00
)
.try_get_matches_from(vec!["pv", "--option", "TeSt123"]);
2021-12-27 19:57:38 +00:00
assert!(m.is_ok(), "{}", m.unwrap_err());
2018-11-14 17:05:06 +00:00
assert!(m
.unwrap()
.value_of("option")
.unwrap()
.eq_ignore_ascii_case("test123"));
}
#[test]
fn ignore_case_fail() {
2022-02-12 03:48:29 +00:00
let m = Command::new("pv")
2017-11-28 12:30:06 +00:00
.arg(
Arg::new("option")
.short('o')
.long("option")
2017-11-28 12:30:06 +00:00
.takes_value(true)
.possible_value("test123")
.possible_value("test321"),
)
.try_get_matches_from(vec!["pv", "--option", "TeSt123"]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
}
#[test]
fn ignore_case_multiple() {
2022-02-12 03:48:29 +00:00
let m = Command::new("pv")
2017-11-28 12:30:06 +00:00
.arg(
Arg::new("option")
.short('o')
.long("option")
2017-11-28 12:30:06 +00:00
.takes_value(true)
.possible_value("test123")
.possible_value("test321")
2021-06-16 05:28:25 +00:00
.multiple_values(true)
.ignore_case(true),
2017-11-28 12:30:06 +00:00
)
.try_get_matches_from(vec!["pv", "--option", "TeSt123", "teST123", "tESt321"]);
2021-12-27 19:57:38 +00:00
assert!(m.is_ok(), "{}", m.unwrap_err());
2017-11-28 12:30:06 +00:00
assert_eq!(
m.unwrap().values_of("option").unwrap().collect::<Vec<_>>(),
&["TeSt123", "teST123", "tESt321"]
);
}
#[test]
fn ignore_case_multiple_fail() {
2022-02-12 03:48:29 +00:00
let m = Command::new("pv")
2017-11-28 12:30:06 +00:00
.arg(
Arg::new("option")
.short('o')
.long("option")
2017-11-28 12:30:06 +00:00
.takes_value(true)
.possible_value("test123")
.possible_value("test321")
2021-06-16 05:28:25 +00:00
.multiple_values(true),
2017-11-28 12:30:06 +00:00
)
.try_get_matches_from(vec!["pv", "--option", "test123", "teST123", "test321"]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
2017-11-28 12:30:06 +00:00
}