2023-03-25 06:59:24 +00:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
2022-04-15 17:46:07 +00:00
|
|
|
// 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.
|
2022-04-15 16:58:31 +00:00
|
|
|
#[test]
|
|
|
|
fn to_long_stdio() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "-"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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();
|
|
|
|
|
2022-04-15 17:46:07 +00:00
|
|
|
assert!(!next.is_long());
|
2022-04-15 16:58:31 +00:00
|
|
|
|
2022-04-15 17:46:07 +00:00
|
|
|
assert_eq!(next.to_long(), None);
|
2022-04-15 16:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-04-27 17:36:42 +00:00
|
|
|
fn to_long_no_escape() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "--"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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();
|
|
|
|
|
2022-04-27 17:36:42 +00:00
|
|
|
assert!(!next.is_long());
|
2022-04-15 16:58:31 +00:00
|
|
|
|
2022-04-27 17:36:42 +00:00
|
|
|
assert_eq!(next.to_long(), None);
|
2022-04-15 16:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_long_no_value() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "--long"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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();
|
2023-03-25 08:42:04 +00:00
|
|
|
assert_eq!(key, Ok("long"));
|
2022-04-15 16:58:31 +00:00
|
|
|
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="]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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();
|
2023-03-25 08:42:04 +00:00
|
|
|
assert_eq!(key, Ok("long"));
|
2023-03-25 06:59:24 +00:00
|
|
|
assert_eq!(value, Some(OsStr::new("")));
|
2022-04-15 16:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_long_with_value() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "--long=hello"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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();
|
2023-03-25 08:42:04 +00:00
|
|
|
assert_eq!(key, Ok("long"));
|
2023-03-25 06:59:24 +00:00
|
|
|
assert_eq!(value, Some(OsStr::new("hello")));
|
2022-04-15 16:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_short_stdio() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "-"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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();
|
|
|
|
|
2022-04-27 17:36:42 +00:00
|
|
|
assert!(!next.is_short());
|
2022-04-15 16:58:31 +00:00
|
|
|
|
2022-04-27 17:36:42 +00:00
|
|
|
assert!(next.to_short().is_none());
|
2022-04-15 16:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_short_escape() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "--"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "--long"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "-10.0"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "10.0"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "--10.0"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "-"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "--"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "--"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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() {
|
2022-04-15 18:48:58 +00:00
|
|
|
let raw = clap_lex::RawArgs::new(["bin", "-"]);
|
2022-04-15 16:58:31 +00:00
|
|
|
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());
|
|
|
|
}
|