From 4f4a61d493547660260a5aa1db1af8a6ec42bbb6 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 16 Jul 2026 19:47:01 +0800 Subject: refactor: extract `seek_end_of_options` into utils and add test helpers Deduplicate the `seek_end_of_options` function by moving it from both `arg_matcher` and `flag_matcher` into a single shared implementation in utils. Add `make_masked` and `make_args` test helpers to the test crate --- mingling_picker/test/src/lib.rs | 15 +++++++++++ mingling_picker/test/src/test/arg_matcher_test.rs | 31 ++--------------------- mingling_picker/test/src/test/style_test.rs | 10 +++----- 3 files changed, 21 insertions(+), 35 deletions(-) (limited to 'mingling_picker/test/src') diff --git a/mingling_picker/test/src/lib.rs b/mingling_picker/test/src/lib.rs index 9bc0560..eac6cad 100644 --- a/mingling_picker/test/src/lib.rs +++ b/mingling_picker/test/src/lib.rs @@ -6,3 +6,18 @@ #[cfg(test)] mod test; + +use mingling_picker::parselib::MaskedArg; + +/// Create a single `MaskedArg` from a raw string and its original index. +pub fn make_masked(raw: &str, idx: usize) -> MaskedArg<'_> { + MaskedArg { raw, raw_idx: idx } +} + +/// Create a `Vec` from an array of `(raw, raw_idx)` pairs. +pub fn make_args<'a>(pairs: &'a [(&'a str, usize)]) -> Vec> { + pairs + .iter() + .map(|&(raw, idx)| MaskedArg { raw, raw_idx: idx }) + .collect() +} diff --git a/mingling_picker/test/src/test/arg_matcher_test.rs b/mingling_picker/test/src/test/arg_matcher_test.rs index 3249079..947e20f 100644 --- a/mingling_picker/test/src/test/arg_matcher_test.rs +++ b/mingling_picker/test/src/test/arg_matcher_test.rs @@ -1,16 +1,9 @@ use mingling_picker::PickerArgInfo; -use mingling_picker::parselib::{ArgMatcher, MaskedArg, Matcher, POWERSHELL_STYLE, UNIX_STYLE}; +use mingling_picker::parselib::{ArgMatcher, Matcher, POWERSHELL_STYLE, UNIX_STYLE}; -fn make_args<'a>(pairs: &'a [(&'a str, usize)]) -> Vec> { - pairs - .iter() - .map(|&(raw, idx)| MaskedArg { raw, raw_idx: idx }) - .collect() -} +use crate::make_args; -// ============================================================ // on_match_one — Named -// ============================================================ #[test] fn test_match_one_named_basic() { @@ -59,9 +52,7 @@ fn test_match_one_named_after_end_of_options() { assert_eq!(result, None); } -// ============================================================ // on_match_one — Positional -// ============================================================ #[test] fn test_match_one_positional_basic() { @@ -81,9 +72,7 @@ fn test_match_one_positional_takes_first() { assert_eq!(result, Some(0)); } -// ============================================================ // on_match_all — Named, single occurrence -// ============================================================ #[test] fn test_match_all_named_flag_plus_value() { @@ -125,9 +114,7 @@ fn test_match_all_named_value_looks_like_flag() { assert_eq!(result, vec![0, 1]); } -// ============================================================ // on_match_all — Named, multiple occurrences (Single per flag) -// ============================================================ #[test] fn test_match_all_named_two_occurrences() { @@ -149,9 +136,7 @@ fn test_match_all_named_skips_non_matching_args() { assert_eq!(result, vec![0, 1, 3, 4]); } -// ============================================================ // on_match_all — Named, short flag -// ============================================================ #[test] fn test_match_all_named_short_flag() { @@ -163,9 +148,7 @@ fn test_match_all_named_short_flag() { assert_eq!(result, vec![0, 1]); } -// ============================================================ // on_match_all — Named, eq + non-eq mixed -// ============================================================ #[test] fn test_match_all_named_mixed_eq_and_regular() { @@ -176,9 +159,7 @@ fn test_match_all_named_mixed_eq_and_regular() { assert_eq!(result, vec![0, 1, 2]); } -// ============================================================ // on_match_all — Named, case insensitive (PowerShell) -// ============================================================ #[test] fn test_match_all_named_powershell_case_insensitive() { @@ -189,9 +170,7 @@ fn test_match_all_named_powershell_case_insensitive() { assert_eq!(result, vec![0, 1]); } -// ============================================================ // on_match_all — Positional -// ============================================================ #[test] fn test_match_all_positional_single() { @@ -211,9 +190,7 @@ fn test_match_all_positional_multiple() { assert_eq!(result, vec![0, 1]); } -// ============================================================ // End-of-options marker (`--`) -// ============================================================ #[test] fn test_match_all_named_stops_at_end_of_options() { @@ -240,9 +217,7 @@ fn test_match_all_positional_stops_at_end_of_options() { assert_eq!(result, vec![0]); } -// ============================================================ // Empty args -// ============================================================ #[test] fn test_match_one_empty() { @@ -262,9 +237,7 @@ fn test_match_all_empty() { assert!(result.is_empty()); } -// ============================================================ // Verify that -- itself is never matched as a flag -// ============================================================ #[test] fn test_match_all_end_of_options_not_matched() { diff --git a/mingling_picker/test/src/test/style_test.rs b/mingling_picker/test/src/test/style_test.rs index efb2185..3c337b9 100644 --- a/mingling_picker/test/src/test/style_test.rs +++ b/mingling_picker/test/src/test/style_test.rs @@ -1,9 +1,11 @@ use mingling_picker::PickerArgInfo; use mingling_picker::parselib::{ - FlagMatcher, MaskedArg, Matcher, POWERSHELL_STYLE, ParserStyle, ParserStyleNamingCase, - UNIX_STYLE, WINDOWS_STYLE, build_possible_flags, + FlagMatcher, Matcher, POWERSHELL_STYLE, ParserStyle, ParserStyleNamingCase, UNIX_STYLE, + WINDOWS_STYLE, build_possible_flags, }; +use crate::make_masked; + // Style: formatting utilities #[test] @@ -45,10 +47,6 @@ fn test_build_possible_flags_with_short_and_alias() { // Style: matching with different styles via Matcher trait -fn make_masked(raw: &str, idx: usize) -> MaskedArg<'_> { - MaskedArg { raw, raw_idx: idx } -} - #[test] fn test_windows_style_match() { // Windows style: /verbose (case insensitive) -- cgit