use crate::quote::windows::{first_token_and_rest_wide, first_token_wide, join_wide, split_wide};
fn w(s: &str) -> Vec<u16> {
s.encode_utf16().collect()
}
fn jw(args: &[&str]) -> String {
let wides: Vec<Vec<u16>> = args.iter().map(|a| w(a)).collect();
let refs: Vec<&[u16]> = wides.iter().map(|v| v.as_slice()).collect();
String::from_utf16(&join_wide(&refs)).unwrap()
}
fn sw(s: &str) -> Vec<Vec<u16>> {
split_wide(&w(s)).unwrap()
}
fn sw_strings(s: &str) -> Vec<String> {
sw(s).into_iter().map(|t| String::from_utf16(&t).unwrap()).collect()
}
#[test]
fn simple_args_separated_by_space() {
assert_eq!(jw(&["a", "b"]), "a b");
}
#[test]
fn args_with_space_or_tab_are_quoted() {
assert_eq!(jw(&["a b"]), "\"a b\"");
assert_eq!(jw(&["a\tb"]), "\"a\tb\"");
}
#[test]
fn empty_arg_becomes_empty_quotes() {
assert_eq!(jw(&["a", "", "b"]), "a \"\" b");
}
#[test]
fn embedded_quote_is_backslash_escaped() {
assert_eq!(jw(&["a\"b"]), "a\\\"b");
}
#[test]
fn empty_argv_returns_empty_vec() {
let empty: Vec<&[u16]> = vec![];
assert_eq!(join_wide(&empty), Vec::<u16>::new());
}
#[test]
fn lone_surrogate_passes_through_verbatim() {
let arg: &[u16] = &[b'a' as u16, 0xD800u16, b'b' as u16];
let result = join_wide(&[arg]);
assert_eq!(result, &[b'a' as u16, 0xD800u16, b'b' as u16]);
}
#[test]
fn lone_backslashes_not_before_quote_stay_literal() {
assert_eq!(jw(&["a\\b"]), "a\\b");
assert_eq!(jw(&["a\\"]), "a\\");
}
#[test]
fn multiple_consecutive_backslashes_unquoted_stay_literal() {
assert_eq!(jw(&["a\\\\\\\\b"]), "a\\\\\\\\b");
}
#[test]
fn backslashes_before_quote_are_doubled_plus_one() {
assert_eq!(jw(&["a\\\"b"]), "a\\\\\\\"b");
}
#[test]
fn trailing_backslashes_doubled_before_closing_quote() {
assert_eq!(jw(&["a\\ b"]), "\"a\\ b\""); assert_eq!(jw(&["a b\\"]), "\"a b\\\\\""); }
#[test]
fn split_wide_empty_input_returns_empty_vec() {
assert_eq!(split_wide(&[]).unwrap(), Vec::<Vec<u16>>::new());
}
#[test]
fn split_wide_whitespace_only_input_returns_empty_vec() {
assert_eq!(sw(" \t "), Vec::<Vec<u16>>::new());
}
#[test]
fn split_wide_simple_args() {
assert_eq!(sw_strings("a b c"), vec!["a", "b", "c"]);
}
#[test]
fn split_wide_skips_leading_whitespace_before_argv0() {
assert_eq!(sw_strings(" cmd arg"), vec!["cmd", "arg"]);
}
#[test]
fn split_wide_tab_separates_rest_args() {
assert_eq!(sw_strings("prog\ta\tb"), vec!["prog", "a", "b"]);
}
#[test]
fn split_wide_quoted_arg_with_embedded_space() {
assert_eq!(sw_strings("prog \"a b\""), vec!["prog", "a b"]);
}
#[test]
fn split_wide_empty_quoted_arg_between_args() {
assert_eq!(sw_strings("prog \"\" x"), vec!["prog", "", "x"]);
}
#[test]
fn split_wide_adjacent_empty_quotes_concatenate() {
assert_eq!(sw_strings("prog a\"\"b"), vec!["prog", "ab"]);
}
#[test]
fn split_wide_triple_quote_run_yields_one_literal_quote() {
assert_eq!(sw_strings("prog a\"\"\"b"), vec!["prog", "a\"b"]);
}
#[test]
fn split_wide_double_quote_inside_quoted_region_embeds_literal_quote() {
assert_eq!(sw_strings("prog \"a\"\"b\""), vec!["prog", "a\"b"]);
}
#[test]
fn split_wide_backslash_before_quote_odd_count() {
assert_eq!(sw_strings("prog a\\\"b"), vec!["prog", "a\"b"]);
}
#[test]
fn split_wide_backslash_before_quote_even_count() {
assert_eq!(sw_strings("prog a\\\\\"b"), vec!["prog", "a\\b"]);
}
#[test]
fn split_wide_trailing_backslash_stays_literal() {
assert_eq!(sw_strings("prog a\\"), vec!["prog", "a\\"]);
}
#[test]
fn split_wide_backslashes_before_whitespace_stay_literal() {
assert_eq!(sw_strings("prog a\\\\ b"), vec!["prog", "a\\\\", "b"]);
}
#[test]
fn split_wide_argv0_only_no_further_args() {
assert_eq!(sw_strings("prog"), vec!["prog"]);
}
#[test]
fn split_wide_trailing_whitespace_after_last_arg_yields_no_spurious_empty_token() {
assert_eq!(sw_strings("prog a "), vec!["prog", "a"]);
}
#[test]
fn split_wide_unterminated_quote_in_rest_arg_consumes_to_end() {
assert_eq!(sw_strings("prog \"abc"), vec!["prog", "abc"]);
}
#[test]
fn split_wide_trailing_backslash_run_before_unterminated_opening_quote() {
assert_eq!(sw_strings("prog abc\\\\\""), vec!["prog", "abc\\"]);
}
#[test]
fn split_wide_round_trips_join_wide_for_rest_args() {
let cases: Vec<Vec<&str>> = vec![
vec!["a", "b"],
vec!["a b", "a\"b", "a\\b", "a\\\"b", "trail\\", "", "tab\tx"],
vec!["a\\\\\\\\b"],
];
for rest in cases {
let joined = jw(&rest);
let full = format!("prog {joined}");
let result = sw_strings(&full);
let mut expected = vec!["prog".to_string()];
expected.extend(rest.iter().map(|s| s.to_string()));
assert_eq!(result, expected, "round-trip mismatch for {rest:?}");
}
}
#[test]
fn split_wide_lone_surrogate_passes_through_verbatim() {
let mut cmd: Vec<u16> = w("prog a");
cmd.push(0xD800u16);
cmd.push(b'b' as u16);
let result = split_wide(&cmd).unwrap();
let mut expected_arg = w("a");
expected_arg.push(0xD800u16);
expected_arg.push(b'b' as u16);
assert_eq!(result, vec![w("prog"), expected_arg]);
}
#[cfg(windows)]
mod roundtrip {
use super::*;
#[link(name = "shell32")]
extern "system" {
fn CommandLineToArgvW(lp_cmd_line: *const u16, p_num_args: *mut i32) -> *mut *mut u16;
}
extern "system" {
fn LocalFree(h_mem: *mut core::ffi::c_void) -> *mut core::ffi::c_void;
}
fn os_parse(cmdline: &[u16]) -> Vec<Vec<u16>> {
let mut buf: Vec<u16> = cmdline.to_vec();
buf.push(0); let mut n: i32 = 0;
unsafe {
let argv = CommandLineToArgvW(buf.as_ptr(), &mut n);
assert!(!argv.is_null(), "CommandLineToArgvW failed");
debug_assert!(n >= 0);
let mut out = Vec::with_capacity(n as usize);
for i in 0..n as isize {
let p = *argv.offset(i);
let mut len = 0isize;
while *p.offset(len) != 0 {
len += 1;
}
out.push(std::slice::from_raw_parts(p, len as usize).to_vec());
}
LocalFree(argv as *mut _);
out
}
}
#[test]
fn first_token_deviates_from_os_on_leading_whitespace() {
let input = w(" cmd arg");
let os_argv0 = os_parse(&input).into_iter().next().unwrap_or_default();
assert_eq!(
String::from_utf16(&os_argv0).unwrap(),
"",
"OS should return empty string as argv[0] for whitespace-prefixed input"
);
assert_eq!(
first(" cmd arg").as_deref(),
Some("cmd"),
"our function should skip leading whitespace and return the first token"
);
assert_ne!(
String::from_utf16(&os_argv0).unwrap(),
"cmd",
"OS argv[0] must not equal our result — if this fails the deviation no longer exists"
);
}
#[test]
fn join_wide_round_trips_through_os_parser() {
let cases: Vec<Vec<&str>> = vec![
vec!["plain", "args"],
vec!["has space", "a\"b", "a\\b", "a\\\"b", "trail\\", "", "tab\tx"],
vec!["prefix", "a\\\\\\\\ b"],
];
for case in cases {
let wides: Vec<Vec<u16>> = case.iter().map(|a| w(a)).collect();
let refs: Vec<&[u16]> = wides.iter().map(|v| v.as_slice()).collect();
let line = join_wide(&refs);
let parsed = os_parse(&line);
let expected: Vec<Vec<u16>> = wides.clone();
assert_eq!(parsed, expected, "round-trip mismatch for {:?}", case);
}
}
#[test]
fn split_wide_matches_os_parse_for_representative_cases() {
let cases: Vec<Vec<&str>> = vec![
vec!["plain", "args"],
vec!["has space", "a\"b", "a\\b", "a\\\"b", "trail\\", "", "tab\tx"],
vec!["prefix", "a\\\\\\\\ b"],
];
for case in cases {
let wides: Vec<Vec<u16>> = case.iter().map(|a| w(a)).collect();
let refs: Vec<&[u16]> = wides.iter().map(|v| v.as_slice()).collect();
let line = join_wide(&refs);
let os_result = os_parse(&line);
let our_result = split_wide(&line).unwrap();
assert_eq!(our_result, os_result, "split_wide disagrees with OS for {:?}", case);
assert_eq!(
our_result, wides,
"split_wide disagrees with original argv for {:?}",
case
);
}
}
#[test]
fn split_wide_matches_os_parse_for_adversarial_quote_runs() {
let lines = [
"prog a\"\"\"b",
"prog \"a\"\" b\"",
"prog \"\"\"",
"prog a\"\"\"\"b",
"prog \"\"\"x",
"prog \"abc",
"prog abc\\\\\"",
];
for line in lines {
let cmd = w(line);
let os_result = os_parse(&cmd);
let our_result = split_wide(&cmd).unwrap();
assert_eq!(our_result, os_result, "split_wide disagrees with OS for {line:?}");
}
}
#[test]
fn split_wide_deviates_from_os_like_first_token_on_leading_whitespace() {
let input = w(" cmd arg");
let os_result = os_parse(&input);
let our_result = split_wide(&input).unwrap();
assert_ne!(our_result, os_result, "deviation should still exist for split_wide");
assert_eq!(
our_result,
vec![w("cmd"), w("arg")],
"split_wide should skip leading whitespace and return [\"cmd\", \"arg\"]"
);
}
}
fn first(s: &str) -> Option<String> {
first_token_wide(&w(s)).map(|t| String::from_utf16(&t).unwrap())
}
fn split_first(s: &str) -> Option<(String, String)> {
first_token_and_rest_wide(&w(s)).map(|(a, b)| (String::from_utf16(&a).unwrap(), String::from_utf16(&b).unwrap()))
}
#[test]
fn first_token_and_rest_splits_unquoted() {
assert_eq!(
split_first("git status --short"),
Some(("git".into(), "status --short".into()))
);
}
#[test]
fn first_token_and_rest_splits_quoted_program_with_spaces() {
assert_eq!(
split_first("\"C:\\Program Files\\app.exe\" --flag x"),
Some(("C:\\Program Files\\app.exe".into(), "--flag x".into()))
);
}
#[test]
fn first_token_and_rest_empty_rest() {
assert_eq!(split_first("solo"), Some(("solo".into(), "".into())));
}
#[test]
fn first_token_and_rest_none_for_blank() {
assert_eq!(split_first(" "), None);
}
#[test]
fn first_token_stops_at_whitespace() {
assert_eq!(first("git status --short").as_deref(), Some("git"));
}
#[test]
fn first_token_skips_leading_whitespace_by_design() {
assert_eq!(first(" \t cmd arg").as_deref(), Some("cmd"));
}
#[test]
fn quoted_first_token_spans_to_closing_quote() {
assert_eq!(
first("\"C:\\Program Files\\app.exe\" --flag").as_deref(),
Some("C:\\Program Files\\app.exe")
);
}
#[test]
fn unterminated_opening_quote_consumes_to_end() {
assert_eq!(first("\"C:\\no close").as_deref(), Some("C:\\no close"));
}
#[test]
fn backslashes_are_literal_in_first_token() {
assert_eq!(first("C:\\bin\\tool.exe x").as_deref(), Some("C:\\bin\\tool.exe"));
}
#[test]
fn empty_or_whitespace_only_has_no_first_token() {
assert_eq!(first(""), None);
assert_eq!(first(" \t "), None);
}
#[test]
fn mid_token_embedded_quotes_are_not_terminators() {
assert_eq!(first("a\"b\"c").as_deref(), Some("a\"b\"c"));
}
#[test]
fn bare_empty_quotes_yield_empty_token() {
assert_eq!(first("\"\"").as_deref(), Some(""));
}