Creating test cases for each module

This commit is contained in:
Zach Dziura 2015-06-10 18:14:11 +00:00
parent 0bab8dc9ca
commit 87675e409a
4 changed files with 97 additions and 49 deletions

View file

@ -15,7 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
pub mod errors;
pub mod matches;
pub mod vars;
mod errors;
mod matches;
mod token;
mod usage;
mod vars;
pub use matches::Matches;
pub use vars::Vars;
pub use usage::usage;

View file

@ -19,14 +19,14 @@ use std::fmt::{self, Display, Formatter};
use errors::{Error, ErrorKind};
#[derive(Clone)]
#[derive(Clone, Debug, PartialEq)]
pub struct Token {
short_name: String,
long_name: String,
description: String,
pub is_arg: bool,
pub has_arg: bool,
pub is_group: bool,
description: String,
padding: usize
}
@ -146,3 +146,71 @@ enum AnalysisStage {
LongName,
Description
}
#[cfg(test)]
mod tests {
use super::Token;
#[test]
fn test_new_token() {
let opt = "h/help(Display the program usage)";
let token = match Token::new(opt) {
Ok(t) => t,
Err(why) => panic!("Received error: {}", why)
};
let control_token = Token {
short_name: String::from("h"),
long_name: String::from("help"),
description: String::from("Display the program usage"),
is_arg: false,
has_arg: false,
is_group: false,
padding: 0
};
println!("{}", token);
assert_eq!(token, control_token);
}
#[test]
fn test_new_group() {
let opt = "(This is a group)";
let token = match Token::new(opt) {
Ok(t) => t,
Err(why) => panic!("Received error: {}", why)
};
let control_token = Token {
short_name: String::new(),
long_name: String::new(),
description: String::from("This is a group"),
is_arg: false,
has_arg: false,
is_group: true,
padding: 0
};
println!("{}", token);
assert_eq!(token, control_token);
}
#[test]
fn control_token_with_arg() {
let opt = "o/option(An option with an argument):";
let token = match Token::new(opt) {
Ok(t) => t,
Err(why) => panic!("Received error: {}", why)
};
let control_token = Token {
short_name: String::from("o"),
long_name: String::from("option"),
description: String::from("An option with an argument"),
is_arg: false,
has_arg: true,
is_group: false,
padding: 0
};
println!("{}", token);
assert_eq!(token, control_token);
}
}

View file

@ -17,7 +17,6 @@
use std::collections::HashMap;
use std::collections::VecDeque;
use std::slice::Iter;
use errors::Error;
use token::Token;

View file

@ -1,50 +1,26 @@
extern crate pirate;
use std::env;
use pirate::Matches;
use pirate::{Matches, usage, Vars};
fn main() {
let opts = vec!["n:", "b/boop", ":input"];
let matches: Matches = match pirate::parse(env::args(), &opts) {
Err(ref e) => {
println!("Error: {}", e);
help();
return;
},
Ok(m) => m
};
// Print the program help if necessary
if matches.has_arg("h") || matches.has_arg("help") {
help();
let mut vars = match Vars::new(&opts) {
Ok(v) => v,
Err(why) => {
println!("{}", why);
return;
}
};
let matches = match Matches::new(&mut vars) {
Ok(m) => m,
Err(why) => {
println!("{}", why);
return;
}
let input = matches.get("input").unwrap().parse::<i32>().unwrap();
let num = match matches.get("n") {
Some(n) => n.parse::<i32>().unwrap(),
None => 1
};
let sum = input + num;
println!("{} + {} = {}", input, num, sum);
if matches.has_arg("b") || matches.has_arg("boop") {
println!("Boop!!");
if matches.has_arg("-h") || matches.has_arg("--help") {
usage(&vars);
return;
}
}
fn help() {
println!("usage: pirate-test [-n NUM] [-b|--boop] INPUT\n");
println!("Options:");
println!(" -n NUM\tChange the default number that's added to the input");
println!(" -b, --boop\tIt's a surprise!");
println!("\nRequired arguments:");
println!(" INPUT\tWe're gonna manipulate this somehow, you'll see!");
}