Fixing some of the failing test cases

Still more to finish, however...
This commit is contained in:
Zach Dziura 2015-06-24 16:04:50 -04:00
parent 75c277db3a
commit cc6f53979a
4 changed files with 48 additions and 40 deletions

View file

@ -21,6 +21,6 @@ mod token;
mod usage; mod usage;
mod vars; mod vars;
pub use matches::{Matches, MatchesTrait, matches}; pub use matches::{Matches, Match, matches};
pub use vars::{Vars, vars}; pub use vars::{Vars, vars};
pub use usage::usage; pub use usage::usage;

View file

@ -76,17 +76,20 @@ pub fn matches(vars: &mut Vars, env_args: &[String]) -> Result<Matches, Error> {
} }
} }
pub trait MatchesTrait { pub trait Match {
fn get(&self, arg: &str) -> Option<&String>; fn get(&self, arg: &str) -> Option<String>;
fn has_match(&self, arg: &str) -> bool; fn has_match(&self, arg: &str) -> bool;
fn matches(&self) -> Keys<String, String>; fn matches(&self) -> Keys<String, String>;
} }
impl MatchesTrait for Matches { impl Match for Matches {
fn get(&self, arg: &str) -> Option<&String> { fn get(&self, arg: &str) -> Option<String> {
self.get(arg) match self.get(arg) {
Some(s) => Some(s.clone()),
None => None
}
} }
fn has_match(&self, arg: &str) -> bool { fn has_match(&self, arg: &str) -> bool {
@ -101,20 +104,20 @@ impl MatchesTrait for Matches {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::Matches; use super::matches;
use super::super::errors::{Error, ErrorKind}; use super::super::vars::vars;
use super::super::vars::Vars;
#[test] #[test]
fn test_matches() { 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 env_args = vec![String::from("test"), String::from("-a"), String::from("Test")];
let mut vars = Vars::new("Test", &opts).unwrap(); let mut vars = vars("Test", &opts).unwrap();
let matches = match Matches::new(&mut vars, &env_args) { let matches = match matches(&mut vars, &env_args) {
Ok(m) => m, Ok(m) => m,
Err(why) => panic!("An error occurred: {}", why) 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"));
} }
} }

View file

@ -191,12 +191,12 @@ enum AnalysisStage {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::Token; use super::{Token, token};
#[test] #[test]
fn test_new_token() { fn test_new_token() {
let opt = "h/help(Display the program usage)"; let opt = "h/help(Display the program usage)";
let token = match Token::new(opt) { let token = match token(opt) {
Ok(t) => t, Ok(t) => t,
Err(why) => panic!("Received error: {}", why) Err(why) => panic!("Received error: {}", why)
}; };
@ -216,7 +216,7 @@ mod tests {
#[test] #[test]
fn test_new_group() { fn test_new_group() {
let opt = "(This is a group)"; let opt = "(This is a group)";
let token = match Token::new(opt) { let token = match token(opt) {
Ok(t) => t, Ok(t) => t,
Err(why) => panic!("Received error: {}", why) Err(why) => panic!("Received error: {}", why)
}; };
@ -236,7 +236,7 @@ mod tests {
#[test] #[test]
fn test_new_token_with_arg() { fn test_new_token_with_arg() {
let opt = "o/option(An option with an argument):"; let opt = "o/option(An option with an argument):";
let token = match Token::new(opt) { let token = match token(opt) {
Ok(t) => t, Ok(t) => t,
Err(why) => panic!("Received error: {}", why) Err(why) => panic!("Received error: {}", why)
}; };
@ -256,7 +256,7 @@ mod tests {
#[test] #[test]
fn test_new_token_as_arg() { fn test_new_token_as_arg() {
let opt = ":a/arg(An argument)"; let opt = ":a/arg(An argument)";
let token = match Token::new(opt) { let token = match token(opt) {
Ok(t) => t, Ok(t) => t,
Err(why) => panic!("Received error: {}", why) Err(why) => panic!("Received error: {}", why)
}; };
@ -277,7 +277,7 @@ mod tests {
#[should_panic] #[should_panic]
fn test_invalid_token_format() { fn test_invalid_token_format() {
let input = ":w/wrong(Wrong format):"; let input = ":w/wrong(Wrong format):";
match Token::new(input) { match token(input) {
Ok(t) => t, Ok(t) => t,
Err(why) => panic!("Received error: {}", why) Err(why) => panic!("Received error: {}", why)
}; };
@ -289,12 +289,12 @@ mod tests {
let long_name = "o/out"; let long_name = "o/out";
let group = "(Output)"; let group = "(Output)";
let short_token = Token::new(short_name).unwrap(); let short_token = token(short_name).unwrap();
let long_token = Token::new(long_name).unwrap(); let long_token = token(long_name).unwrap();
let group_token = Token::new(group).unwrap(); let group_token = token(group).unwrap();
assert_eq!(short_token.name(), "o"); assert_eq!(short_token.name(), "o");
assert_eq!(long_token.name(), "out"); assert_eq!(long_token.name(), "out");
assert_eq!(group_token.name(), ""); assert_eq!(group_token.name(), "Output");
} }
} }

View file

@ -1,33 +1,38 @@
extern crate pirate; extern crate pirate;
use std::env; use pirate::{Matches, Match, matches, usage, vars};
use pirate::{Matches, MatchesTrait, matches, usage, vars};
#[test]
fn main() { fn main() {
let env_args: Vec<String> = env::args().collect(); let env_args: Vec<String> = vec![String::from("test"), String::from("-a"), String::from("2"), String::from("3")];
let opts = vec!["o/opt(An option)", "a(An Argument):"]; 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 mut vars = vars("test", &opts).unwrap();
let matches: Matches = match matches(&mut vars, &env_args) { let matches: Matches = match matches(&mut vars, &env_args) {
Ok(m) => m, Ok(m) => m,
Err(why) => panic!("An error occurred: {}", why) Err(why) => {
println!("Error: {}", why);
usage(&vars);
return;
}
}; };
if matches.has_match("help") { if matches.has_match("help") {
usage(&vars); usage(&vars);
return; 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);
} }