mirror of
https://github.com/gay-pizza/jaarg.git
synced 2025-12-18 23:10:17 +00:00
Add simple argparse test for values
This commit is contained in:
@@ -257,3 +257,48 @@ impl<ID: 'static> Opts<ID> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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<ArgID> = 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<String> = None;
|
||||
let mut two = false;
|
||||
let mut three: Option<String> = None;
|
||||
let mut four: Option<String> = None;
|
||||
let mut five: Option<String> = 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()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user