aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-07-16 01:12:59 +0800
committer魏曹先生 <1992414357@qq.com>2026-07-16 01:12:59 +0800
commit9231c1de40f6f66d59ab86bea113ac3ac323c62d (patch)
tree4e9187eb8b45b6b3cbb5fafc53dd8e74df14fcf2
parent904f78d506240516ace2305e570d542c3b18440f (diff)
feat(test): add comprehensive bool flag and route tests
-rw-r--r--mingling_picker/test/src/test.rs413
1 files changed, 412 insertions, 1 deletions
diff --git a/mingling_picker/test/src/test.rs b/mingling_picker/test/src/test.rs
index 20b07ab..d5349d0 100644
--- a/mingling_picker/test/src/test.rs
+++ b/mingling_picker/test/src/test.rs
@@ -1,11 +1,422 @@
use mingling_picker::{IntoPicker, macros::flag};
+// Basic bool flag — present / absent
+
+#[test]
+fn test_bool_flag_present() {
+ let args = vec!["--verbose"];
+ let parsed = args
+ .to_picker()
+ .pick(&flag![verbose: bool])
+ .or_default()
+ .unwrap();
+ assert_eq!(parsed, true);
+}
+
+#[test]
+fn test_bool_flag_absent() {
+ let args: Vec<&str> = vec![];
+ let parsed = args
+ .to_picker()
+ .pick(&flag![verbose: bool])
+ .or_default()
+ .unwrap();
+ assert_eq!(parsed, false);
+}
+
+// Short flag — '-v'
+
+#[test]
+fn test_bool_short_flag_present() {
+ let args = vec!["-v"];
+ let parsed = args
+ .to_picker()
+ .pick(&flag![verbose: bool, 'v'])
+ .or_default()
+ .unwrap();
+ assert_eq!(parsed, true);
+}
+
+// Multiple bool flags at once
+
+#[test]
+fn test_two_bool_flags_both_present() {
+ // The `flag!` macro expands the identifier `flag_a` into the string "flag_a" → --flag_a
+ let args = vec!["--flag_a", "--flag_b"];
+ let (a, b) = args
+ .to_picker()
+ .pick(&flag![flag_a: bool])
+ .or_default()
+ .pick(&flag![flag_b: bool])
+ .or_default()
+ .unwrap();
+ assert_eq!(a, true);
+ assert_eq!(b, true);
+}
+
+#[test]
+fn test_two_bool_flags_one_present() {
+ let args = vec!["--flag_a"];
+ let (a, b) = args
+ .to_picker()
+ .pick(&flag![flag_a: bool])
+ .or_default()
+ .pick(&flag![flag_b: bool])
+ .or_default()
+ .unwrap();
+ assert_eq!(a, true);
+ assert_eq!(b, false);
+}
+
+#[test]
+fn test_two_bool_flags_neither_present() {
+ let args: Vec<&str> = vec![];
+ let (a, b) = args
+ .to_picker()
+ .pick(&flag![flag_a: bool])
+ .or_default()
+ .pick(&flag![flag_b: bool])
+ .or_default()
+ .unwrap();
+ assert_eq!(a, false);
+ assert_eq!(b, false);
+}
+
+// Mixed short and long flags
+
+#[test]
+fn test_short_and_long_flags() {
+ let args = vec!["-a", "--long_b"];
+ let (a, b) = args
+ .to_picker()
+ .pick(&flag![flag_a: bool, 'a'])
+ .or_default()
+ .pick(&flag![long_b: bool])
+ .or_default()
+ .unwrap();
+ assert_eq!(a, true);
+ assert_eq!(b, true);
+}
+
+// Flags after `--` (end-of-options marker) should not be parsed.
+
+#[test]
+fn test_flag_after_end_of_options() {
+ let args = vec!["--", "--verbose"];
+ let parsed = args
+ .to_picker()
+ .pick(&flag![verbose: bool])
+ .or_default()
+ .unwrap();
+ assert_eq!(parsed, false);
+}
+
+// Alias matching for bool flags
+
#[test]
-fn test_picker_pipeline() {
+fn test_bool_flag_with_alias() {
+ let args = vec!["--cfg"];
+ let parsed = args
+ .to_picker()
+ .pick(&flag![config: bool, "cfg"])
+ .or_default()
+ .unwrap();
+ assert_eq!(parsed, true);
+}
+
+#[test]
+fn test_bool_flag_primary_name() {
+ let args = vec!["--config"];
+ let parsed = args
+ .to_picker()
+ .pick(&flag![config: bool, "cfg"])
+ .or_default()
+ .unwrap();
+ assert_eq!(parsed, true);
+}
+
+// Short flag + alias for bool flag
+
+#[test]
+fn test_bool_flag_short_and_alias() {
+ let args = vec!["-v"];
+ let parsed = args
+ .to_picker()
+ .pick(&flag![verbose: bool, 'v', "cfg"])
+ .or_default()
+ .unwrap();
+ assert_eq!(parsed, true);
+}
+
+// Default values: .or() / .or_default()
+
+#[test]
+fn test_or_default_without_args() {
+ let args: Vec<&str> = vec![];
+ let parsed = args
+ .to_picker()
+ .pick(&flag![verbose: bool])
+ .or_default()
+ .unwrap();
+ assert_eq!(parsed, false);
+}
+
+#[test]
+fn test_or_custom_default() {
+ let args: Vec<&str> = vec![];
+ let parsed = args
+ .to_picker()
+ .pick(&flag![verbose: bool])
+ .or(|| true)
+ .unwrap();
+ assert_eq!(parsed, true);
+}
+
+// to_result / to_option interface
+
+#[test]
+fn test_to_result_ok() {
+ let args = vec!["--verbose"];
+ let result = args
+ .to_picker()
+ .pick(&flag![verbose: bool])
+ .or_default()
+ .to_result();
+ assert_eq!(result, Ok(true));
+}
+
+#[test]
+fn test_to_option_some() {
+ let args = vec!["--verbose"];
+ let opt = args
+ .to_picker()
+ .pick(&flag![verbose: bool])
+ .or_default()
+ .to_option();
+ assert_eq!(opt, Some(true));
+}
+
+// Chain with_route passthrough
+
+#[test]
+fn test_with_route_chain() {
let args = vec!["--flag"];
let parsed = args
.with_route::<String>()
.pick(&flag![flag: bool])
+ .or_default()
.unwrap();
assert_eq!(parsed, true);
}
+
+// Unrelated flag should not match
+
+#[test]
+fn test_unrelated_flag_does_not_match() {
+ let args = vec!["--other"];
+ let parsed = args
+ .to_picker()
+ .pick(&flag![verbose: bool])
+ .or_default()
+ .unwrap();
+ assert_eq!(parsed, false);
+}
+
+// Route mechanism — or_route
+
+#[test]
+fn test_or_route_triggered_to_result() {
+ // flag not present and no default value → route triggered → Err
+ let args: Vec<&str> = vec![];
+ let result: Result<bool, &'static str> = args
+ .with_route::<&'static str>()
+ .pick(&flag![verbose: bool])
+ .or_route(|| "missing_verbose")
+ .to_result();
+ assert_eq!(result, Err("missing_verbose"));
+}
+
+#[test]
+fn test_or_route_not_triggered_when_flag_present() {
+ // flag present → route not triggered → Ok
+ let args = vec!["--verbose"];
+ let result: Result<bool, &'static str> = args
+ .with_route::<&'static str>()
+ .pick(&flag![verbose: bool])
+ .or_route(|| "missing_verbose")
+ .to_result();
+ assert_eq!(result, Ok(true));
+}
+
+#[test]
+fn test_or_default_priority_over_or_route() {
+ // or_default takes priority over or_route: even if the flag is absent,
+ // having a default prevents the route from being triggered
+ let args: Vec<&str> = vec![];
+ let result: Result<bool, &'static str> = args
+ .with_route::<&'static str>()
+ .pick(&flag![verbose: bool])
+ .or_default()
+ .or_route(|| "should_not_reach")
+ .to_result();
+ assert_eq!(result, Ok(false));
+}
+
+#[test]
+fn test_or_route_to_option_returns_none() {
+ // When route is triggered, to_option returns None
+ let args: Vec<&str> = vec![];
+ let opt: Option<bool> = args
+ .with_route::<&'static str>()
+ .pick(&flag![verbose: bool])
+ .or_route(|| "missing")
+ .to_option();
+ assert_eq!(opt, None);
+}
+
+#[test]
+#[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
+fn test_or_route_unwrap_panics() {
+ let args: Vec<&str> = vec![];
+ args.with_route::<&'static str>()
+ .pick(&flag![verbose: bool])
+ .or_route(|| "missing")
+ .unwrap();
+}
+
+#[test]
+fn test_or_route_with_string_route() {
+ // Route type is String
+ let args: Vec<&str> = vec![];
+ let result: Result<bool, String> = args
+ .with_route::<String>()
+ .pick(&flag![verbose: bool])
+ .or_route(|| "route_hit".to_string())
+ .to_result();
+ assert_eq!(result, Err("route_hit".to_string()));
+}
+
+#[test]
+fn test_or_route_with_custom_enum() {
+ // Route type is a custom enum
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ #[allow(dead_code)]
+ enum Redirect {
+ Help,
+ Version,
+ }
+
+ let args: Vec<&str> = vec![];
+ let result: Result<bool, Redirect> = args
+ .with_route::<Redirect>()
+ .pick(&flag![verbose: bool])
+ .or_route(|| Redirect::Help)
+ .to_result();
+ assert_eq!(result, Err(Redirect::Help));
+}
+
+// Route mechanism — multiple flags
+
+#[test]
+fn test_two_flags_first_missing_triggers_route() {
+ // Both flags missing; first has or_route → Err("first");
+ // first route wins (first-checked, first-triggered)
+ let args: Vec<&str> = vec![];
+ let result: Result<(bool, bool), &'static str> = args
+ .with_route::<&'static str>()
+ .pick(&flag![flag_a: bool])
+ .or_route(|| "first_missing")
+ .pick(&flag![flag_b: bool])
+ .or_route(|| "second_missing")
+ .to_result();
+ assert_eq!(result, Err("first_missing"));
+}
+
+#[test]
+fn test_two_flags_second_missing_triggers_route() {
+ // First has or_default (no route triggered), second missing with or_route → Err("second")
+ let args: Vec<&str> = vec![];
+ let result: Result<(bool, bool), &'static str> = args
+ .with_route::<&'static str>()
+ .pick(&flag![flag_a: bool])
+ .or_default()
+ .pick(&flag![flag_b: bool])
+ .or_route(|| "second_missing")
+ .to_result();
+ assert_eq!(result, Err("second_missing"));
+}
+
+#[test]
+fn test_two_flags_both_present_route_not_triggered() {
+ // Both flags present → route not triggered → Ok((true, true))
+ let args = vec!["--flag_a", "--flag_b"];
+ let result: Result<(bool, bool), &'static str> = args
+ .with_route::<&'static str>()
+ .pick(&flag![flag_a: bool])
+ .or_route(|| "first_missing")
+ .pick(&flag![flag_b: bool])
+ .or_route(|| "second_missing")
+ .to_result();
+ assert_eq!(result, Ok((true, true)));
+}
+
+#[test]
+fn test_two_flags_first_missing_no_route_second_has_route() {
+ // First missing but has no or_route, second missing with or_route → Err("second")
+ // Note: the first has neither route nor default, so pick failure does not modify error_route
+ let args: Vec<&str> = vec![];
+ let result: Result<(bool, bool), &'static str> = args
+ .with_route::<&'static str>()
+ .pick(&flag![flag_a: bool])
+ // No or_default, no or_route either
+ .pick(&flag![flag_b: bool])
+ .or_route(|| "second_missing")
+ .to_result();
+ assert_eq!(result, Err("second_missing"));
+}
+
+#[test]
+fn test_two_flags_only_second_has_default() {
+ // First is missing with no default/route (v1=NotFound), second is missing but has or_default
+ // Use unpack to check both results
+ let args: Vec<&str> = vec![];
+ let (a, b) = args
+ .to_picker()
+ .pick(&flag![flag_a: bool])
+ .pick(&flag![flag_b: bool])
+ .or_default()
+ .unpack();
+ assert_eq!(a, None);
+ assert_eq!(b, Some(false));
+}
+
+// Route with with_route
+
+#[test]
+fn test_with_route_and_or_route() {
+ // with_route::&lt;String&gt;() sets the Route type + or_route triggers
+ let args: Vec<&str> = vec![];
+ let result: Result<bool, String> = args
+ .with_route::<String>()
+ .pick(&flag![verbose: bool])
+ .or_route(|| "redirected".to_string())
+ .to_result();
+ assert_eq!(result, Err("redirected".to_string()));
+}
+
+#[test]
+fn test_with_route_type_switch() {
+ // with_route switching Route type clears old route_$ and error_route
+ let args: Vec<&str> = vec![];
+ let result: Result<(bool, bool), i32> = args
+ .with_route::<String>()
+ .pick(&flag![verbose: bool])
+ .or_route(|| "string_route".to_string())
+ .with_route::<i32>()
+ .pick(&flag![other: bool])
+ .or_route(|| -1)
+ .to_result();
+ // The first flag is missing, but its or_route is cleared when with_route switches (route_$ = None)
+ // The second flag is missing and has or_route → Err(-1)
+ assert_eq!(result, Err(-1));
+}