diff --git a/src/lib.rs b/src/lib.rs index e1e2ca2..970da74 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/matches.rs b/src/matches.rs index cc4c873..41ac4a6 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -76,17 +76,20 @@ pub fn matches(vars: &mut Vars, env_args: &[String]) -> Result { } } -pub trait MatchesTrait { - fn get(&self, arg: &str) -> Option<&String>; +pub trait Match { + fn get(&self, arg: &str) -> Option; fn has_match(&self, arg: &str) -> bool; fn matches(&self) -> Keys; } -impl MatchesTrait for Matches { - fn get(&self, arg: &str) -> Option<&String> { - self.get(arg) +impl Match for Matches { + fn get(&self, arg: &str) -> Option { + 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")); } } diff --git a/src/token.rs b/src/token.rs index e1ca886..ad6d2dd 100644 --- a/src/token.rs +++ b/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"); } } diff --git a/tests/main.rs b/tests/main.rs index 9752730..191cdd2 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -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 = env::args().collect(); - let opts = vec!["o/opt(An option)", "a(An Argument):"]; + let env_args: Vec = 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::().unwrap(); + + let addend: i32 = match matches.get("addend") { + Some(a) => a.parse::().unwrap(), + None => 1 + }; + + let sum = augend + addend; + + assert_eq!(augend, 3); + assert_eq!(addend, 2); + assert_eq!(sum, 5); }