Creating test cases for each module
This commit is contained in:
parent
0bab8dc9ca
commit
87675e409a
4 changed files with 97 additions and 49 deletions
13
src/lib.rs
13
src/lib.rs
|
@ -15,7 +15,12 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pub mod errors;
|
mod errors;
|
||||||
pub mod matches;
|
mod matches;
|
||||||
pub mod vars;
|
mod token;
|
||||||
mod token;
|
mod usage;
|
||||||
|
mod vars;
|
||||||
|
|
||||||
|
pub use matches::Matches;
|
||||||
|
pub use vars::Vars;
|
||||||
|
pub use usage::usage;
|
||||||
|
|
76
src/token.rs
76
src/token.rs
|
@ -19,14 +19,14 @@ use std::fmt::{self, Display, Formatter};
|
||||||
|
|
||||||
use errors::{Error, ErrorKind};
|
use errors::{Error, ErrorKind};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct Token {
|
pub struct Token {
|
||||||
short_name: String,
|
short_name: String,
|
||||||
long_name: String,
|
long_name: String,
|
||||||
|
description: String,
|
||||||
pub is_arg: bool,
|
pub is_arg: bool,
|
||||||
pub has_arg: bool,
|
pub has_arg: bool,
|
||||||
pub is_group: bool,
|
pub is_group: bool,
|
||||||
description: String,
|
|
||||||
padding: usize
|
padding: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,7 +134,7 @@ impl Display for Token {
|
||||||
let repr = if self.is_group {
|
let repr = if self.is_group {
|
||||||
format!("{}:", self.description)
|
format!("{}:", self.description)
|
||||||
} else {
|
} else {
|
||||||
format!(" -{}, --{}{} {}", self.short_name, self.long_name, spacing, self.description)
|
format!(" -{}, --{}{} {}", self.short_name, self.long_name, spacing, self.description)
|
||||||
};
|
};
|
||||||
|
|
||||||
write!(f, "{}", repr)
|
write!(f, "{}", repr)
|
||||||
|
@ -145,4 +145,72 @@ enum AnalysisStage {
|
||||||
ShortName,
|
ShortName,
|
||||||
LongName,
|
LongName,
|
||||||
Description
|
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::HashMap;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::slice::Iter;
|
|
||||||
|
|
||||||
use errors::Error;
|
use errors::Error;
|
||||||
use token::Token;
|
use token::Token;
|
||||||
|
@ -87,4 +86,4 @@ impl Vars {
|
||||||
pub fn arg_len(&self) -> usize {
|
pub fn arg_len(&self) -> usize {
|
||||||
self.args.len()
|
self.args.len()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,50 +1,26 @@
|
||||||
extern crate pirate;
|
extern crate pirate;
|
||||||
|
|
||||||
use std::env;
|
use pirate::{Matches, usage, Vars};
|
||||||
|
|
||||||
use pirate::Matches;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let opts = vec!["n:", "b/boop", ":input"];
|
let opts = vec!["n:", "b/boop", ":input"];
|
||||||
|
let mut vars = match Vars::new(&opts) {
|
||||||
let matches: Matches = match pirate::parse(env::args(), &opts) {
|
Ok(v) => v,
|
||||||
Err(ref e) => {
|
Err(why) => {
|
||||||
println!("Error: {}", e);
|
println!("{}", why);
|
||||||
help();
|
|
||||||
return;
|
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") {
|
||||||
if matches.has_arg("h") || matches.has_arg("help") {
|
usage(&vars);
|
||||||
help();
|
|
||||||
return;
|
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