clap/clap_lex/tests/testsuite/parsed.rs

197 lines
5.4 KiB
Rust
Raw Normal View History

use std::ffi::OsStr;
// Despite our design philosophy being to support completion generation, we aren't considering `-`
// the start of a long because there is no valid value to return.
#[test]
fn to_long_stdio() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
assert!(!next.is_long());
assert_eq!(next.to_long(), None);
}
#[test]
fn to_long_no_escape() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "--"]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
assert!(!next.is_long());
assert_eq!(next.to_long(), None);
}
#[test]
fn to_long_no_value() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "--long"]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(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() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "--long="]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(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(OsStr::new("")));
}
#[test]
fn to_long_with_value() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "--long=hello"]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(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(OsStr::new("hello")));
}
#[test]
fn to_short_stdio() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
assert!(!next.is_short());
assert!(next.to_short().is_none());
}
#[test]
fn to_short_escape() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "--"]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(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() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "--long"]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
assert!(!next.is_short());
assert!(next.to_short().is_none());
}
#[test]
fn to_short() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(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() {
for number in ["-10.0", "-1", "-100", "-3.5", "-1e10", "-1.3e10"] {
let raw = clap_lex::RawArgs::new(["bin", number]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
assert!(next.is_negative_number());
}
}
#[test]
fn is_positive_number() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "10.0"]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
assert!(!next.is_negative_number());
}
#[test]
fn is_not_number() {
for number in ["--10.0", "-..", "-2..", "-e", "-1e", "-1e10.2", "-.2"] {
let raw = clap_lex::RawArgs::new(["bin", number]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
assert!(
!next.is_negative_number(),
"`{number}` is mistakenly classified as a number"
);
}
}
#[test]
fn is_stdio() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
assert!(next.is_stdio());
}
#[test]
fn is_not_stdio() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "--"]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
assert!(!next.is_stdio());
}
#[test]
fn is_escape() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "--"]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
assert!(next.is_escape());
}
#[test]
fn is_not_escape() {
2022-04-15 18:48:58 +00:00
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
2024-05-04 19:59:40 +00:00
assert_eq!(raw.next_os(&mut cursor), Some(OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
assert!(!next.is_escape());
}