clap/tests/positionals.rs

135 lines
6 KiB
Rust
Raw Normal View History

2015-09-17 19:20:56 +00:00
extern crate clap;
use clap::{App, Arg, ErrorKind};
2015-09-17 19:20:56 +00:00
#[test]
fn positional() {
let m = App::new("positional")
.args(&[
2015-09-17 19:20:56 +00:00
Arg::from_usage("-f, --flag 'some flag'"),
Arg::with_name("positional")
.index(1)
])
.get_matches_from(vec!["", "-f", "test"]);
2015-09-17 19:20:56 +00:00
assert!(m.is_present("positional"));
assert!(m.is_present("flag"));
assert_eq!(m.value_of("positional").unwrap(), "test");
2015-09-17 19:20:56 +00:00
let m = App::new("positional")
.args(&[
2015-09-17 19:20:56 +00:00
Arg::from_usage("-f, --flag 'some flag'"),
Arg::with_name("positional")
.index(1)
])
.get_matches_from(vec!["", "test", "--flag"]);
2015-09-17 19:20:56 +00:00
assert!(m.is_present("positional"));
assert!(m.is_present("flag"));
assert_eq!(m.value_of("positional").unwrap(), "test");
2015-09-17 19:20:56 +00:00
}
#[test]
fn lots_o_vals() {
let r = App::new("opts")
.arg(
Arg::from_usage("[opt]... 'some pos'"),
)
.get_matches_from_safe(vec!["",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("opt"));
assert_eq!(m.values_of("opt").unwrap().collect::<Vec<_>>().len(), 297); // i.e. more than u8
}
2015-09-17 19:20:56 +00:00
#[test]
fn positional_multiple() {
let m = App::new("positional_multiple")
.args(&[
2015-09-17 19:20:56 +00:00
Arg::from_usage("-f, --flag 'some flag'"),
Arg::with_name("positional")
.index(1)
.multiple(true)
])
.get_matches_from(vec!["", "-f", "test1", "test2", "test3"]);
2015-09-17 19:20:56 +00:00
assert!(m.is_present("positional"));
assert!(m.is_present("flag"));
assert_eq!(&*m.values_of("positional").unwrap().collect::<Vec<_>>(), &["test1", "test2", "test3"]);
2015-09-17 19:20:56 +00:00
let m = App::new("positional_multiple")
.args(&[
2015-09-17 19:20:56 +00:00
Arg::from_usage("-f, --flag 'some flag'"),
Arg::with_name("positional")
.index(1)
.multiple(true)
])
.get_matches_from(vec!["", "test1", "test2", "test3", "--flag"]);
2015-09-17 19:20:56 +00:00
assert!(m.is_present("positional"));
assert!(m.is_present("flag"));
assert_eq!(&*m.values_of("positional").unwrap().collect::<Vec<_>>(), &["test1", "test2", "test3"]);
2015-09-17 19:20:56 +00:00
}
#[test]
fn positional_multiple_2() {
let result = App::new("positional_multiple")
.args(&[
2015-09-17 19:20:56 +00:00
Arg::from_usage("-f, --flag 'some flag'"),
Arg::with_name("positional")
.index(1)
])
.get_matches_from_safe(vec!["", "-f", "test1", "test2", "test3"]);
2015-09-17 19:20:56 +00:00
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::UnknownArgument);
2015-09-17 19:20:56 +00:00
}
#[test]
fn positional_possible_values() {
let m = App::new("positional_possible_values")
.args(&[
Arg::from_usage("-f, --flag 'some flag'"),
Arg::with_name("positional")
.index(1)
.possible_value("test123")
])
.get_matches_from(vec!["", "-f", "test123"]);
assert!(m.is_present("positional"));
assert!(m.is_present("flag"));
assert_eq!(&*m.values_of("positional").unwrap().collect::<Vec<_>>(), &["test123"]);
}
#[test]
fn create_positional() {
let _ = App::new("test")
.arg(Arg::with_name("test")
.index(1)
.help("testing testing"))
.get_matches();
}