1
0

refactor(tests): restructure windows commandline tests for better readability

- Extract test helpers into nested functions to reduce duplication
- Rename test functions to be more descriptive
- Group related test cases together
This commit is contained in:
2025-10-23 21:48:48 +08:00
parent 217d3d5fd3
commit e908e775ed
2 changed files with 66 additions and 43 deletions

View File

@ -205,7 +205,7 @@ impl Drop for Icon {
// region: Windows Commandline // region: Windows Commandline
// region Cmd Lexer // region: Cmd Lexer
/// The lexer for Windows commandline argument split. /// The lexer for Windows commandline argument split.
/// ///

View File

@ -1,6 +1,9 @@
use wfassoc::extra::windows::*; use wfassoc::extra::windows::*;
fn test_cmd_args_ex(s: &str, probe: &[&'static str]) { #[test]
fn test_cmd_args() {
// Declare tester
fn tester(s: &str, probe: &[&'static str]) {
let rv = CmdArgs::new(s); let rv = CmdArgs::new(s);
let inner = rv.get_inner(); let inner = rv.get_inner();
assert_eq!(inner.len(), probe.len()); assert_eq!(inner.len(), probe.len());
@ -11,14 +14,12 @@ fn test_cmd_args_ex(s: &str, probe: &[&'static str]) {
} }
} }
#[test]
fn test_cmd_args() {
// Normal cases // Normal cases
test_cmd_args_ex( tester(
"MyApp.exe --config ppic.toml", "MyApp.exe --config ppic.toml",
&["MyApp.exe", "--config", "ppic.toml"], &["MyApp.exe", "--config", "ppic.toml"],
); );
test_cmd_args_ex( tester(
r#""C:\Program Files\MyApp\MyApp.exe" --config ppic.toml"#, r#""C:\Program Files\MyApp\MyApp.exe" --config ppic.toml"#,
&[ &[
r#"C:\Program Files\MyApp\MyApp.exe"#, r#"C:\Program Files\MyApp\MyApp.exe"#,
@ -28,40 +29,62 @@ fn test_cmd_args() {
); );
// Microsoft shitty cases. // Microsoft shitty cases.
test_cmd_args_ex(r#""abc" d e"#, &[r#"abc"#, r#"d"#, r#"e"#]); tester(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"#]); tester(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"#]); tester(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"#]); tester(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"#]); tester(r#"a"b"" c d"#, &[r#"ab" c d"#]);
} }
#[test] #[test]
fn test_cmd_arg() { fn test_cmd_arg() {
let rv = CmdArg::new(r#""C:\Program Files\MyApp\MyApp.exe""#); // Declare tester
fn ok_tester(s: &str, probe: &str) {
let rv = CmdArg::new(s);
assert!(rv.is_ok()); assert!(rv.is_ok());
assert_eq!(rv.unwrap().get_inner(), r#"C:\Program Files\MyApp\MyApp.exe"#); let rv = rv.unwrap();
assert_eq!(rv.get_inner(), probe);
let rv = CmdArg::new("MyApp.exe --config ppic.toml"); }
assert!(rv.is_err()); fn err_tester(s: &str) {
let rv = CmdArg::new(s);
let rv = CmdArg::new("");
assert!(rv.is_err()); assert!(rv.is_err());
} }
#[test] // Normal test
fn test_quote_cmd_arg() { ok_tester(
let rv = CmdArg::with_inner(r#"C:\Program Files\MyApp\MyApp.exe"#).to_quoted_string(true); r#""C:\Program Files\MyApp\MyApp.exe""#,
assert_eq!(rv, r#""C:\Program Files\MyApp\MyApp.exe""#) r#"C:\Program Files\MyApp\MyApp.exe"#,
);
err_tester("MyApp.exe --config ppic.toml");
err_tester("");
} }
#[test] #[test]
fn test_quote_cmd_args() { fn test_cmd_arg_quoted_string() {
let args = [ fn tester(s: &str, probe: &str) {
let rv = CmdArg::with_inner(s);
assert_eq!(rv.to_quoted_string(true), probe);
}
tester(
r#"C:\Program Files\MyApp\MyApp.exe"#,
r#""C:\Program Files\MyApp\MyApp.exe""#,
);
}
#[test]
fn test_cmd_args_quoted_string() {
fn tester(args: &[&str], probe: &str) {
let rv = CmdArgs::with_inner(args.iter().map(|s| CmdArg::with_inner(s)));
assert_eq!(rv.to_quoted_string(), probe);
}
tester(
&[
r#"C:\Program Files\MyApp\MyApp.exe"#, r#"C:\Program Files\MyApp\MyApp.exe"#,
"--config", "--config",
"ppic.toml", "ppic.toml",
]; ],
let rv = CmdArgs::with_inner(args.iter().map(|s| CmdArg::with_inner(s))).to_quoted_string(); r#""C:\Program Files\MyApp\MyApp.exe" --config ppic.toml"#,
assert_eq!(rv, r#""C:\Program Files\MyApp\MyApp.exe" --config ppic.toml"#); );
} }