mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
test(lex): Verify behavior directly
While figuring out the API, `clap_lex` was tested by clap's tests. Now we are focusing on its API directly.
This commit is contained in:
parent
c3445e05b5
commit
913ec6d6ec
3 changed files with 410 additions and 0 deletions
21
clap_lex/tests/lexer.rs
Normal file
21
clap_lex/tests/lexer.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
#[test]
|
||||
fn insert() {
|
||||
let mut raw = clap_lex::RawArgs::from_iter(["bin", "a", "b", "c"]);
|
||||
let mut cursor = raw.cursor();
|
||||
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("a")));
|
||||
raw.insert(&mut cursor, &["1", "2", "3"]);
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("1")));
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("2")));
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("3")));
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("b")));
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("c")));
|
||||
|
||||
let mut cursor = raw.cursor();
|
||||
let rest = raw
|
||||
.remaining(&mut cursor)
|
||||
.map(|s| s.to_string_lossy())
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(rest, vec!["bin", "a", "1", "2", "3", "b", "c"]);
|
||||
}
|
191
clap_lex/tests/parsed.rs
Normal file
191
clap_lex/tests/parsed.rs
Normal file
|
@ -0,0 +1,191 @@
|
|||
#[test]
|
||||
#[should_panic] // Our design philosophy is to match X if it can be completed as X which we break here
|
||||
fn to_long_stdio() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(next.is_long());
|
||||
|
||||
let (key, value) = next.to_long().unwrap();
|
||||
assert_eq!(key, Ok(""));
|
||||
assert_eq!(value, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_long_escape() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "--"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(next.is_long());
|
||||
|
||||
let (key, value) = next.to_long().unwrap();
|
||||
assert_eq!(key, Ok(""));
|
||||
assert_eq!(value, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_long_no_value() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "--long"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(next.is_long());
|
||||
|
||||
let (key, value) = next.to_long().unwrap();
|
||||
assert_eq!(key, Ok("long"));
|
||||
assert_eq!(value, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_long_with_empty_value() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "--long="]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(next.is_long());
|
||||
|
||||
let (key, value) = next.to_long().unwrap();
|
||||
assert_eq!(key, Ok("long"));
|
||||
assert_eq!(value, Some(clap_lex::RawOsStr::from_str("")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_long_with_value() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "--long=hello"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(next.is_long());
|
||||
|
||||
let (key, value) = next.to_long().unwrap();
|
||||
assert_eq!(key, Ok("long"));
|
||||
assert_eq!(value, Some(clap_lex::RawOsStr::from_str("hello")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_short_stdio() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(next.is_short());
|
||||
|
||||
let mut shorts = next.to_short().unwrap();
|
||||
assert_eq!(shorts.next_value_os(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_short_escape() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "--"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(!next.is_short());
|
||||
|
||||
assert!(next.to_short().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_short_long() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "--long"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(!next.is_short());
|
||||
|
||||
assert!(next.to_short().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_short() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(next.is_short());
|
||||
|
||||
let shorts = next.to_short().unwrap();
|
||||
let actual: String = shorts.map(|s| s.unwrap()).collect();
|
||||
assert_eq!(actual, "short");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_negative_number() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-10.0"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(next.is_number());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_positive_number() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "10.0"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(next.is_number());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_not_number() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "--10.0"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(!next.is_number());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_stdio() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(next.is_stdio());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_not_stdio() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "--"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(!next.is_stdio());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_escape() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "--"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(next.is_escape());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_not_escape() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
|
||||
assert!(!next.is_escape());
|
||||
}
|
198
clap_lex/tests/shorts.rs
Normal file
198
clap_lex/tests/shorts.rs
Normal file
|
@ -0,0 +1,198 @@
|
|||
#[test]
|
||||
fn iter() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let shorts = next.to_short().unwrap();
|
||||
|
||||
let actual: String = shorts.map(|s| s.unwrap()).collect();
|
||||
assert_eq!(actual, "short");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn next_flag() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let mut shorts = next.to_short().unwrap();
|
||||
|
||||
let mut actual = String::new();
|
||||
actual.push(shorts.next_flag().unwrap().unwrap());
|
||||
actual.push(shorts.next_flag().unwrap().unwrap());
|
||||
actual.push(shorts.next_flag().unwrap().unwrap());
|
||||
actual.push(shorts.next_flag().unwrap().unwrap());
|
||||
actual.push(shorts.next_flag().unwrap().unwrap());
|
||||
assert_eq!(shorts.next_flag(), None);
|
||||
|
||||
assert_eq!(actual, "short");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn next_value_os() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let mut shorts = next.to_short().unwrap();
|
||||
|
||||
let actual = shorts.next_value_os().unwrap().to_str_lossy();
|
||||
|
||||
assert_eq!(actual, "short");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn next_flag_with_value() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let mut shorts = next.to_short().unwrap();
|
||||
|
||||
assert_eq!(shorts.next_flag().unwrap().unwrap(), 's');
|
||||
let actual = shorts.next_value_os().unwrap().to_str_lossy();
|
||||
|
||||
assert_eq!(actual, "hort");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn next_flag_with_no_value() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let mut shorts = next.to_short().unwrap();
|
||||
|
||||
assert_eq!(shorts.next_flag().unwrap().unwrap(), 's');
|
||||
assert_eq!(shorts.next_flag().unwrap().unwrap(), 'h');
|
||||
assert_eq!(shorts.next_flag().unwrap().unwrap(), 'o');
|
||||
assert_eq!(shorts.next_flag().unwrap().unwrap(), 'r');
|
||||
assert_eq!(shorts.next_flag().unwrap().unwrap(), 't');
|
||||
|
||||
assert_eq!(shorts.next_value_os(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn advance_by_nothing() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let mut shorts = next.to_short().unwrap();
|
||||
|
||||
assert_eq!(shorts.advance_by(0), Ok(()));
|
||||
|
||||
let actual: String = shorts.map(|s| s.unwrap()).collect();
|
||||
assert_eq!(actual, "short");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn advance_by_nothing_with_nothing() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let mut shorts = next.to_short().unwrap();
|
||||
|
||||
assert_eq!(shorts.advance_by(0), Ok(()));
|
||||
|
||||
let actual: String = shorts.map(|s| s.unwrap()).collect();
|
||||
assert_eq!(actual, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn advance_by_something() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let mut shorts = next.to_short().unwrap();
|
||||
|
||||
assert_eq!(shorts.advance_by(2), Ok(()));
|
||||
|
||||
let actual: String = shorts.map(|s| s.unwrap()).collect();
|
||||
assert_eq!(actual, "ort");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn advance_by_out_of_bounds() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let mut shorts = next.to_short().unwrap();
|
||||
|
||||
assert_eq!(shorts.advance_by(2000), Err(5));
|
||||
|
||||
let actual: String = shorts.map(|s| s.unwrap()).collect();
|
||||
assert_eq!(actual, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_empty() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let shorts = next.to_short().unwrap();
|
||||
|
||||
assert!(shorts.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_not_empty() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-hello"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let shorts = next.to_short().unwrap();
|
||||
|
||||
assert!(!shorts.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_partial_not_empty() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-hello"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let mut shorts = next.to_short().unwrap();
|
||||
shorts.advance_by(1).unwrap();
|
||||
|
||||
assert!(!shorts.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_exhausted_empty() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let mut shorts = next.to_short().unwrap();
|
||||
shorts.advance_by(20000).unwrap_err();
|
||||
|
||||
assert!(shorts.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_number() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-1.0"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let shorts = next.to_short().unwrap();
|
||||
|
||||
assert!(shorts.is_number());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_not_number() {
|
||||
let raw = clap_lex::RawArgs::from_iter(["bin", "-hello"]);
|
||||
let mut cursor = raw.cursor();
|
||||
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
|
||||
let next = raw.next(&mut cursor).unwrap();
|
||||
let shorts = next.to_short().unwrap();
|
||||
|
||||
assert!(!shorts.is_number());
|
||||
}
|
Loading…
Reference in a new issue