Fixing some of the failing test cases
Still more to finish, however...
This commit is contained in:
parent
75c277db3a
commit
cc6f53979a
4 changed files with 48 additions and 40 deletions
|
@ -21,6 +21,6 @@ mod token;
|
|||
mod usage;
|
||||
mod vars;
|
||||
|
||||
pub use matches::{Matches, MatchesTrait, matches};
|
||||
pub use matches::{Matches, Match, matches};
|
||||
pub use vars::{Vars, vars};
|
||||
pub use usage::usage;
|
||||
|
|
|
@ -76,17 +76,20 @@ pub fn matches(vars: &mut Vars, env_args: &[String]) -> Result<Matches, Error> {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait MatchesTrait {
|
||||
fn get(&self, arg: &str) -> Option<&String>;
|
||||
pub trait Match {
|
||||
fn get(&self, arg: &str) -> Option<String>;
|
||||
|
||||
fn has_match(&self, arg: &str) -> bool;
|
||||
|
||||
fn matches(&self) -> Keys<String, String>;
|
||||
}
|
||||
|
||||
impl MatchesTrait for Matches {
|
||||
fn get(&self, arg: &str) -> Option<&String> {
|
||||
self.get(arg)
|
||||
impl Match for Matches {
|
||||
fn get(&self, arg: &str) -> Option<String> {
|
||||
match self.get(arg) {
|
||||
Some(s) => Some(s.clone()),
|
||||
None => None
|
||||
}
|
||||
}
|
||||
|
||||
fn has_match(&self, arg: &str) -> bool {
|
||||
|
@ -101,20 +104,20 @@ impl MatchesTrait for Matches {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Matches;
|
||||
use super::super::errors::{Error, ErrorKind};
|
||||
use super::super::vars::Vars;
|
||||
use super::matches;
|
||||
use super::super::vars::vars;
|
||||
|
||||
#[test]
|
||||
fn test_matches() {
|
||||
let opts = vec!["o/opt(An option)", "a(Argument):"];
|
||||
let opts = vec!["o/opt(An option)", "a(An argument):"];
|
||||
let env_args = vec![String::from("test"), String::from("-a"), String::from("Test")];
|
||||
let mut vars = Vars::new("Test", &opts).unwrap();
|
||||
let matches = match Matches::new(&mut vars, &env_args) {
|
||||
let mut vars = vars("Test", &opts).unwrap();
|
||||
let matches = match matches(&mut vars, &env_args) {
|
||||
Ok(m) => m,
|
||||
Err(why) => panic!("An error occurred: {}", why)
|
||||
};
|
||||
|
||||
assert_eq!(matches.get("opt").unwrap(), &String::from("opt"));
|
||||
let argument = matches.get("a").unwrap();
|
||||
assert_eq!(argument, String::from("Test"));
|
||||
}
|
||||
}
|
||||
|
|
20
src/token.rs
20
src/token.rs
|
@ -191,12 +191,12 @@ enum AnalysisStage {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Token;
|
||||
use super::{Token, token};
|
||||
|
||||
#[test]
|
||||
fn test_new_token() {
|
||||
let opt = "h/help(Display the program usage)";
|
||||
let token = match Token::new(opt) {
|
||||
let token = match token(opt) {
|
||||
Ok(t) => t,
|
||||
Err(why) => panic!("Received error: {}", why)
|
||||
};
|
||||
|
@ -216,7 +216,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_new_group() {
|
||||
let opt = "(This is a group)";
|
||||
let token = match Token::new(opt) {
|
||||
let token = match token(opt) {
|
||||
Ok(t) => t,
|
||||
Err(why) => panic!("Received error: {}", why)
|
||||
};
|
||||
|
@ -236,7 +236,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_new_token_with_arg() {
|
||||
let opt = "o/option(An option with an argument):";
|
||||
let token = match Token::new(opt) {
|
||||
let token = match token(opt) {
|
||||
Ok(t) => t,
|
||||
Err(why) => panic!("Received error: {}", why)
|
||||
};
|
||||
|
@ -256,7 +256,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_new_token_as_arg() {
|
||||
let opt = ":a/arg(An argument)";
|
||||
let token = match Token::new(opt) {
|
||||
let token = match token(opt) {
|
||||
Ok(t) => t,
|
||||
Err(why) => panic!("Received error: {}", why)
|
||||
};
|
||||
|
@ -277,7 +277,7 @@ mod tests {
|
|||
#[should_panic]
|
||||
fn test_invalid_token_format() {
|
||||
let input = ":w/wrong(Wrong format):";
|
||||
match Token::new(input) {
|
||||
match token(input) {
|
||||
Ok(t) => t,
|
||||
Err(why) => panic!("Received error: {}", why)
|
||||
};
|
||||
|
@ -289,12 +289,12 @@ mod tests {
|
|||
let long_name = "o/out";
|
||||
let group = "(Output)";
|
||||
|
||||
let short_token = Token::new(short_name).unwrap();
|
||||
let long_token = Token::new(long_name).unwrap();
|
||||
let group_token = Token::new(group).unwrap();
|
||||
let short_token = token(short_name).unwrap();
|
||||
let long_token = token(long_name).unwrap();
|
||||
let group_token = token(group).unwrap();
|
||||
|
||||
assert_eq!(short_token.name(), "o");
|
||||
assert_eq!(long_token.name(), "out");
|
||||
assert_eq!(group_token.name(), "");
|
||||
assert_eq!(group_token.name(), "Output");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,33 +1,38 @@
|
|||
extern crate pirate;
|
||||
|
||||
use std::env;
|
||||
|
||||
use pirate::{Matches, MatchesTrait, matches, usage, vars};
|
||||
use pirate::{Matches, Match, matches, usage, vars};
|
||||
|
||||
#[test]
|
||||
fn main() {
|
||||
let env_args: Vec<String> = env::args().collect();
|
||||
let opts = vec!["o/opt(An option)", "a(An Argument):"];
|
||||
let env_args: Vec<String> = vec![String::from("test"), String::from("-a"), String::from("2"), String::from("3")];
|
||||
let opts = vec!["a/addend(The right side of the addition equation; default=1):", "(Required Arguments)",
|
||||
":augend(The left side of an addition equation)"];
|
||||
let mut vars = vars("test", &opts).unwrap();
|
||||
|
||||
let matches: Matches = match matches(&mut vars, &env_args) {
|
||||
Ok(m) => m,
|
||||
Err(why) => panic!("An error occurred: {}", why)
|
||||
Err(why) => {
|
||||
println!("Error: {}", why);
|
||||
usage(&vars);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if matches.has_match("help") {
|
||||
usage(&vars);
|
||||
return;
|
||||
}
|
||||
|
||||
let arg = match matches.get("a") {
|
||||
Some(a) => a.clone(),
|
||||
None => String::from("Pickle")
|
||||
};
|
||||
|
||||
match matches.get("opt") {
|
||||
Some(_) => println!("Opt was passed to the program"),
|
||||
None => println!("Opt was not passed to the program")
|
||||
}
|
||||
|
||||
println!("{}", arg);
|
||||
let augend: i32 = matches.get("augend").unwrap().parse::<i32>().unwrap();
|
||||
|
||||
let addend: i32 = match matches.get("addend") {
|
||||
Some(a) => a.parse::<i32>().unwrap(),
|
||||
None => 1
|
||||
};
|
||||
|
||||
let sum = augend + addend;
|
||||
|
||||
assert_eq!(augend, 3);
|
||||
assert_eq!(addend, 2);
|
||||
assert_eq!(sum, 5);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue