clap/tests/builder/regex.rs
Ed Page 045bf99ae0 test: Consolidate builder tests
This is prep for moving the derive tests.  Besides organizing the test
folder for each API, this should reduce link time at the cost of
re-compiling more when a test changes.
2021-11-30 10:07:05 -06:00

36 lines
955 B
Rust

#![cfg(feature = "regex")]
use clap::{App, Arg, ErrorKind};
use regex::{Regex, RegexSet};
#[test]
fn validator_regex() {
let priority = Regex::new(r"[A-C]").unwrap();
let m = App::new("prog")
.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());
assert_eq!(m.err().unwrap().kind, ErrorKind::ValueValidation)
}
#[test]
fn validator_regex_with_regex_set() {
let priority = RegexSet::new(&[r"[A-C]", r"[X-Z]"]).unwrap();
let m = App::new("prog")
.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());
assert_eq!(m.err().unwrap().kind, ErrorKind::ValueValidation)
}