Creating test cases for each module
This commit is contained in:
parent
0bab8dc9ca
commit
87675e409a
4 changed files with 97 additions and 49 deletions
11
src/lib.rs
11
src/lib.rs
|
@ -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;
|
||||
|
|
74
src/token.rs
74
src/token.rs
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -134,7 +134,7 @@ impl Display for Token {
|
|||
let repr = if self.is_group {
|
||||
format!("{}:", self.description)
|
||||
} else {
|
||||
format!(" -{}, --{}{} {}", self.short_name, self.long_name, spacing, self.description)
|
||||
format!(" -{}, --{}{} {}", self.short_name, self.long_name, spacing, self.description)
|
||||
};
|
||||
|
||||
write!(f, "{}", repr)
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::VecDeque;
|
||||
use std::slice::Iter;
|
||||
|
||||
use errors::Error;
|
||||
use token::Token;
|
||||
|
|
|
@ -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();
|
||||
let mut vars = match Vars::new(&opts) {
|
||||
Ok(v) => v,
|
||||
Err(why) => {
|
||||
println!("{}", why);
|
||||
return;
|
||||
},
|
||||
Ok(m) => m
|
||||
}
|
||||
};
|
||||
let matches = match Matches::new(&mut vars) {
|
||||
Ok(m) => m,
|
||||
Err(why) => {
|
||||
println!("{}", why);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Print the program help if necessary
|
||||
if matches.has_arg("h") || matches.has_arg("help") {
|
||||
help();
|
||||
if matches.has_arg("-h") || matches.has_arg("--help") {
|
||||
usage(&vars);
|
||||
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!!");
|
||||
}
|
||||
}
|
||||
|
||||
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!");
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue