From 14028ed2c87dbfac09c82f2c8529bce5e730b657 Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Mon, 17 Nov 2025 21:27:07 +1100 Subject: [PATCH] Add simple argparse test for values --- jaarg/src/argparse.rs | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/jaarg/src/argparse.rs b/jaarg/src/argparse.rs index e550ac9..b627d4a 100644 --- a/jaarg/src/argparse.rs +++ b/jaarg/src/argparse.rs @@ -257,3 +257,48 @@ impl Opts { } } } + +#[cfg(test)] +mod tests { + extern crate alloc; + use alloc::string::String; + use super::*; + + #[test] + fn test() { + enum ArgID { One, Two, Three, Four, Five } + const OPTIONS: Opts = Opts::new(&[ + Opt::positional(ArgID::One, "one"), + Opt::flag(ArgID::Two, &["--two"]), + Opt::value(ArgID::Three, &["--three"], "value"), + Opt::value(ArgID::Four, &["--four"], "value"), + Opt::value(ArgID::Five, &["--five"], "value"), + ]); + const ARGUMENTS: &[&str] = &["one", "--two", "--three=three", "--five=", "--four", "four"]; + + //TODO: currently needs alloc to deal with arguments not being able to escape handler + let mut one: Option = None; + let mut two = false; + let mut three: Option = None; + let mut four: Option = None; + let mut five: Option = None; + assert!(matches!(OPTIONS.parse("", ARGUMENTS.iter(), |ctx| { + match ctx.id { + ArgID::One => { one = Some(ctx.arg.into()); } + ArgID::Two => { two = true; } + ArgID::Three => { three = Some(ctx.arg.into()); } + ArgID::Four => { four = Some(ctx.arg.into()); } + ArgID::Five => { five = Some(ctx.arg.into()); } + } + Ok(ParseControl::Continue) + }, |_, error| { + panic!("unreachable: {error:?}"); + }), ParseResult::ContinueSuccess)); + + assert_eq!(one, Some("one".into())); + assert!(two); + assert_eq!(three, Some("three".into())); + assert_eq!(four, Some("four".into())); + assert_eq!(five, Some("".into())); + } +}