1
0

test(windows): add tests for Windows command line argument parsing

Add comprehensive test cases for Windows command line argument parsing, including edge cases with quotes and backslashes. Tests cover both single argument and multiple argument scenarios.
This commit is contained in:
2025-10-22 16:51:57 +08:00
parent ffd58ff677
commit 563ab483fc
2 changed files with 67 additions and 0 deletions

View File

View File

@ -0,0 +1,67 @@
use wfassoc::extra::windows::*;
fn test_cmd_args_ex(s: &str, probe: &[&'static str]) {
let rv = CmdArgs::new(s);
let inner = rv.get_inner();
assert_eq!(inner.len(), probe.len());
let n = inner.len();
for i in 0..n {
assert_eq!(inner[i].get_inner(), probe[i]);
}
}
#[test]
fn test_cmd_args() {
// Normal cases
test_cmd_args_ex(
"MyApp.exe --config ppic.toml",
&["MyApp.exe", "--config", "ppic.toml"],
);
test_cmd_args_ex(
r#""C:\Program Files\MyApp\MyApp.exe" --config ppic.toml"#,
&[
r#"C:\Program Files\MyApp\MyApp.exe"#,
"--config",
"ppic.toml",
],
);
// Microsoft shitty cases.
test_cmd_args_ex(r#""abc" d e"#, &[r#"abc"#, r#"d"#, r#"e"#]);
test_cmd_args_ex(r#"a\\b d"e f"g h"#, &[r#"a\\b"#, r#"de fg"#, r#"h"#]);
test_cmd_args_ex(r#"a\\\"b c d"#, &[r#"a\"b"#, r#"c"#, r#"d"#]);
test_cmd_args_ex(r#"a\\\\"b c" d e"#, &[r#"a\\b c"#, r#"d"#, r#"e"#]);
test_cmd_args_ex(r#"a"b"" c d"#, &[r#"ab" c d"#]);
}
#[test]
fn test_cmd_arg() {
let rv = CmdArg::new(r#""C:\Program Files\MyApp\MyApp.exe""#);
assert!(rv.is_ok());
assert_eq!(rv.unwrap().get_inner(), r#"C:\Program Files\MyApp\MyApp.exe"#);
let rv = CmdArg::new("MyApp.exe --config ppic.toml");
assert!(rv.is_err());
let rv = CmdArg::new("");
assert!(rv.is_err());
}
#[test]
fn test_quote_cmd_arg() {
let rv = CmdArg::with_inner(r#"C:\Program Files\MyApp\MyApp.exe"#).to_quoted_string(true);
assert_eq!(rv, r#""C:\Program Files\MyApp\MyApp.exe""#)
}
#[test]
fn test_quote_cmd_args() {
let args = [
r#"C:\Program Files\MyApp\MyApp.exe"#,
"--config",
"ppic.toml",
];
let rv = CmdArgs::with_inner(args.iter().map(|s| CmdArg::with_inner(s))).to_quoted_string();
assert_eq!(rv, r#""C:\Program Files\MyApp\MyApp.exe" --config ppic.toml"#);
}