clap/tests/builder/regex.rs

37 lines
978 B
Rust
Raw Normal View History

2021-10-16 21:05:42 +00:00
#![cfg(feature = "regex")]
2022-02-12 03:48:29 +00:00
use clap::{error::ErrorKind, Arg, Command};
2021-10-16 21:05:42 +00:00
use regex::{Regex, RegexSet};
#[test]
fn validator_regex() {
let priority = Regex::new(r"[A-C]").unwrap();
2022-02-12 03:48:29 +00:00
let m = Command::new("prog")
2021-10-16 21:05:42 +00:00
.arg(
Arg::new("priority")
.index(1)
.validator_regex(priority, "A, B or C are allowed"),
)
.try_get_matches_from(vec!["prog", "12345"]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.err().unwrap().kind(), ErrorKind::ValueValidation)
2021-10-16 21:05:42 +00:00
}
#[test]
fn validator_regex_with_regex_set() {
let priority = RegexSet::new(&[r"[A-C]", r"[X-Z]"]).unwrap();
2022-02-12 03:48:29 +00:00
let m = Command::new("prog")
2021-10-16 21:05:42 +00:00
.arg(
Arg::new("priority")
.index(1)
.validator_regex(priority, "A, B, C, X, Y or Z are allowed"),
)
.try_get_matches_from(vec!["prog", "12345"]);
assert!(m.is_err());
2022-01-25 22:19:28 +00:00
assert_eq!(m.err().unwrap().kind(), ErrorKind::ValueValidation)
2021-10-16 21:05:42 +00:00
}