use turkos_os_shell_words::{Error, Words}; #[test] fn os_shell_words_quotes_escapes_empty_and_utf8() { let words = Words::<64, 6>::parse("echo 'hello world' \"a b\" c\\ d '' çay") .unwrap(); assert_eq!(words.len(), 6); assert_eq!((0..6).map(|i| words.get(i).unwrap()).collect::>(), ["echo", "hello world", "a b", "c d", "", "çay"]); let joined = Words::<16, 2>::parse("ab'cd'\"ef\"").unwrap(); assert_eq!(joined.get(0), Some("abcdef")); } #[test] fn os_shell_words_rejects_unterminated_escape_nul_and_capacity() { assert!(matches!(Words::<16, 3>::parse("echo 'bad"), Err(Error::UnterminatedQuote))); assert!(matches!(Words::<16, 3>::parse("echo \\"), Err(Error::TrailingEscape))); assert!(matches!(Words::<16, 3>::parse("echo \0x"), Err(Error::Nul))); assert!(matches!(Words::<3, 3>::parse("abcd"), Err(Error::TooManyBytes))); assert!(matches!(Words::<8, 1>::parse("a b"), Err(Error::TooManyWords))); } #[test] fn os_shell_words_escaped_quotes_are_data() { let words = Words::<32, 2>::parse("say \"a\\\"b\"") .unwrap(); assert_eq!(words.get(0), Some("say")); assert_eq!(words.get(1), Some("a\"b")); assert_eq!(words.get(2), None); let quoted = Words::<16, 1>::parse("\"x\\\"y\"").unwrap(); assert_eq!(quoted.get(0), Some("x\"y")); }