-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathvalidators.rs
66 lines (56 loc) · 1.84 KB
/
validators.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use clap::{Arg, Command};
#[cfg(debug_assertions)]
#[test]
#[should_panic = "Argument 'test' has both `validator` and `validator_os` set which is not allowed"]
fn both_validator_and_validator_os() {
let _ = Command::new("test")
.arg(
Arg::new("test")
.validator(|val| val.parse::<u32>().map_err(|e| e.to_string()))
.validator_os(|val| {
val.to_str()
.unwrap()
.parse::<u32>()
.map_err(|e| e.to_string())
}),
)
.try_get_matches_from(&["cmd", "1"]);
}
#[test]
fn test_validator_fromstr_trait() {
use std::str::FromStr;
let matches = Command::new("test")
.arg(Arg::new("from_str").validator(u32::from_str))
.try_get_matches_from(&["cmd", "1234"])
.expect("match failed");
assert_eq!(matches.value_of_t::<u32>("from_str").ok(), Some(1234));
}
#[test]
fn test_validator_msg_newline() {
let res = Command::new("test")
.arg(Arg::new("test").validator(|val| val.parse::<u32>().map_err(|e| e.to_string())))
.try_get_matches_from(&["cmd", "f"]);
assert!(res.is_err());
let err = res.unwrap_err();
assert!(
err.to_string()
.contains("Invalid value \"f\" for '<test>': invalid digit found in string"),
"{}",
err
);
// This message is the only thing that gets printed -- make sure it ends with a newline
let msg = format!("{}", err);
assert!(msg.ends_with('\n'));
}
#[test]
fn stateful_validator() {
let mut state = false;
Command::new("test")
.arg(Arg::new("test").validator(|val| {
state = true;
val.parse::<u32>().map_err(|e| e.to_string())
}))
.try_get_matches_from(&["cmd", "10"])
.unwrap();
assert!(state);
}