From 6f32f457d97effacb02a56ca557905161d53da41 Mon Sep 17 00:00:00 2001 From: Jay Oster Date: Mon, 19 Feb 2024 12:13:45 -0800 Subject: [PATCH] Add a test for positional escape sequence `--` --- onlyargs_derive/tests/parsing.rs | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/onlyargs_derive/tests/parsing.rs b/onlyargs_derive/tests/parsing.rs index 8388526..46af51f 100644 --- a/onlyargs_derive/tests/parsing.rs +++ b/onlyargs_derive/tests/parsing.rs @@ -107,3 +107,55 @@ fn test_required_positional() -> Result<(), CliError> { Ok(()) } + +#[test] +fn test_positional_escape() -> Result<(), CliError> { + #[derive(Debug, OnlyArgs)] + struct Args { + opt_str: Option, + + #[positional] + rest: Vec, + } + + // All args are optional. + let args = Args::parse(vec![])?; + + assert_eq!(args.opt_str, None); + assert!(args.rest.is_empty()); + + // Captures positional args. + let args = Args::parse( + ["Alice", "--name", "Bob"] + .into_iter() + .map(OsString::from) + .collect(), + )?; + + assert_eq!(args.opt_str, None); + assert_eq!(args.rest, ["Alice", "--name", "Bob"]); + + // Captures the optional string anywhere... + let args = Args::parse( + ["Alice", "--opt-str", "--name", "Bob"] + .into_iter() + .map(OsString::from) + .collect(), + )?; + + assert_eq!(args.opt_str, Some("--name".to_string())); + assert_eq!(args.rest, ["Alice", "Bob"]); + + // ... Unless the `--` escape sequence is encountered. + let args = Args::parse( + ["Alice", "--", "--opt-str", "--name", "Bob"] + .into_iter() + .map(OsString::from) + .collect(), + )?; + + assert_eq!(args.opt_str, None); + assert_eq!(args.rest, ["Alice", "--opt-str", "--name", "Bob"]); + + Ok(()) +}