mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
9a9aabc178
This commit is a tiny win in compiled code size of a final binary including `clap` which shaves off 19k of compiled code locally. Previously tokens were checked if they were a number by using `.parse::<f64>().is_ok()`, but parsing floats is relatively heavyweight in terms of code size. This replaces the check with a more naive "does this string have lots of ascii digits" check where the compiled size of this check should be much smaller.
173 lines
5.5 KiB
Rust
173 lines
5.5 KiB
Rust
#[test]
|
|
fn iter() {
|
|
let raw = clap_lex::RawArgs::new(["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::new(["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::new(["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_string_lossy();
|
|
|
|
assert_eq!(actual, "short");
|
|
}
|
|
|
|
#[test]
|
|
fn next_flag_with_value() {
|
|
let raw = clap_lex::RawArgs::new(["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_string_lossy();
|
|
|
|
assert_eq!(actual, "hort");
|
|
}
|
|
|
|
#[test]
|
|
fn next_flag_with_no_value() {
|
|
let raw = clap_lex::RawArgs::new(["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::new(["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_something() {
|
|
let raw = clap_lex::RawArgs::new(["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::new(["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_not_empty() {
|
|
let raw = clap_lex::RawArgs::new(["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::new(["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::new(["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(20000).unwrap_err();
|
|
|
|
assert!(shorts.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn is_negative_number() {
|
|
let raw = clap_lex::RawArgs::new(["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_negative_number());
|
|
}
|
|
|
|
#[test]
|
|
fn is_not_negaitve_number() {
|
|
let raw = clap_lex::RawArgs::new(["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_negative_number());
|
|
}
|