Added test application

This commit is contained in:
Kevin K 2015-03-24 12:16:59 -04:00
parent b0a47a5004
commit c7f22728f1
6 changed files with 222 additions and 98 deletions

View file

@ -3,7 +3,7 @@
name = "clap" name = "clap"
version = "0.4.14" version = "0.4.14"
authors = ["Kevin K. <kbknapp@gmail.com>"] authors = ["Kevin K. <kbknapp@gmail.com>"]
exclude = ["docs/*", "examples/*", "tests/*"] exclude = ["docs/*", "examples/*", "claptests/*"]
description = "A Command Line Argument Parser written in Rust" description = "A Command Line Argument Parser written in Rust"
repository = "https://github.com/kbknapp/clap-rs.git" repository = "https://github.com/kbknapp/clap-rs.git"
documentation = "http://kbknapp.github.io/clap-rs/docs/clap/index.html" documentation = "http://kbknapp.github.io/clap-rs/docs/clap/index.html"

11
claptests/.gitignore vendored Normal file
View file

@ -0,0 +1,11 @@
# Compiled files
*.o
*.so
*.rlib
*.dll
# Executables
*.exe
# Generated by Cargo
/target/

8
claptests/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "claptests"
version = "0.0.1"
authors = ["Kevin K. <kbknapp@gmail.com>"]
[dependencies.clap]
git = "file:/home/kevin/Projects/clap-rs"

101
claptests/src/main.rs Normal file
View file

@ -0,0 +1,101 @@
extern crate clap;
use clap::{App, Arg, SubCommand};
fn main() {
let version = format!("{}.{}.{}{}",
env!("CARGO_PKG_VERSION_MAJOR"),
env!("CARGO_PKG_VERSION_MINOR"),
env!("CARGO_PKG_VERSION_PATCH"),
option_env!("CARGO_PKG_VERSION_PRE").unwrap_or(""));
let matches = App::new("claptests")
.version(&version[..])
.about("tests clap library")
.author("Kevin K. <kbknapp@gmail.com>")
.arg(Arg::new("flag")
.short("f")
.long("flag")
.help("tests flags")
.multiple(true))
.arg(Arg::new("option")
.short("o")
.long("option")
.help("tests options")
.takes_value(true)
.multiple(true))
.arg(Arg::new("positional")
.index(1)
.help("tests positionals"))
.subcommand(SubCommand::new("subcmd")
.about("tests subcommands")
.version("0.1")
.author("Kevin K. <kbknapp@gmail.com>")
.arg(Arg::new("scflag")
.short("f")
.long("flag")
.help("tests flags")
.multiple(true))
.arg(Arg::new("scoption")
.short("o")
.long("option")
.help("tests options")
.takes_value(true)
.multiple(true))
.arg(Arg::new("scpositional")
.index(1)
.help("tests positionals")))
.get_matches();
if matches.is_present("flag") {
println!("flag present {} times", matches.occurrences_of("flag"));
} else {
println!("flag NOT present");
}
if matches.is_present("option") {
if let Some(v) = matches.value_of("option") {
println!("option present with value: {}", v);
}
if let Some(ref ov) = matches.values_of("option") {
for o in ov {
println!("An option: {}", o);
}
}
} else {
println!("option NOT present");
}
if let Some(p) = matches.value_of("positional") {
println!("positional present with value: {}", p);
}
if matches.is_present("subcmd") {
println!("subcmd present");
if let Some(matches) = matches.subcommand_matches("subcmd") {
if matches.is_present("scflag") {
println!("scflag present {} times", matches.occurrences_of("scflag"));
} else {
println!("scflag NOT present");
}
if matches.is_present("scoption") {
if let Some(v) = matches.value_of("scoption") {
println!("scoption present with value: {}", v);
}
if let Some(ref ov) = matches.values_of("scoption") {
for o in ov {
println!("An scoption: {}", o);
}
}
} else {
println!("scoption NOT present");
}
if let Some(p) = matches.value_of("scpositional") {
println!("scpositional present with value: {}", p);
}
}
} else {
println!("subcmd NOT present");
}
}

View file

@ -13,5 +13,104 @@ mod args;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::{App, Arg, SubCommand};
}
#[test]
fn create_app() {
let _ = App::new("test").version("1.0").author("kevin").about("does awesome things").get_matches();
}
#[test]
fn add_multiple_arg() {
let _ = App::new("test")
.args( vec![
Arg::new("test").short("s"),
Arg::new("test2").short("l")])
.get_matches();
}
#[test]
fn create_flag() {
let _ = App::new("test")
.arg(Arg::new("test")
.short("t")
.long("test")
.help("testing testing"))
.get_matches();
}
#[test]
fn create_positional() {
let _ = App::new("test")
.arg(Arg::new("test")
.index(1)
.help("testing testing"))
.get_matches();
}
#[test]
fn create_option() {
let _ = App::new("test")
.arg(Arg::new("test")
.short("t")
.long("test")
.takes_value(true)
.help("testing testing"))
.get_matches();
}
#[test]
fn create_subcommand() {
let _ = App::new("test")
.subcommand(SubCommand::new("some")
.arg(Arg::new("test")
.short("t")
.long("test")
.takes_value(true)
.help("testing testing")))
.arg(Arg::new("other").long("other"))
.get_matches();
}
#[test]
fn create_multiple_subcommands() {
let _ = App::new("test")
.subcommands(vec![ SubCommand::new("some")
.arg(Arg::new("test")
.short("t")
.long("test")
.takes_value(true)
.help("testing testing")),
SubCommand::new("add")
.arg(Arg::new("roster").short("r"))])
.arg(Arg::new("other").long("other"))
.get_matches();
}
#[test]
#[should_panic]
fn unique_arg_names(){
App::new("some").args(vec![
Arg::new("arg").short("a"),
Arg::new("arg").short("b")
]);
}
#[test]
#[should_panic]
fn unique_arg_shorts(){
App::new("some").args(vec![
Arg::new("arg1").short("a"),
Arg::new("arg2").short("a")
]);
}
#[test]
#[should_panic]
fn unique_arg_longs(){
App::new("some").args(vec![
Arg::new("arg1").long("long"),
Arg::new("arg2").long("long")
]);
}
}

View file

@ -1,95 +0,0 @@
extern crate clap;
use clap::{App, Arg, SubCommand};
#[test]
fn create_app() {
let _ = App::new("test").version("1.0").author("kevin").about("does awesome things").get_matches();
}
#[test]
fn add_multiple_arg() {
let _ = App::new("test")
.args( vec![
Arg::new("test").short("s"),
Arg::new("test2").short("l")])
.get_matches();
}
#[test]
fn create_flag() {
let _ = App::new("test")
.arg(Arg::new("test")
.short("t")
.long("test")
.help("testing testing"))
.get_matches();
}
#[test]
fn create_positional() {
let _ = App::new("test")
.arg(Arg::new("test")
.index(1)
.help("testing testing"))
.get_matches();
}
#[test]
fn create_option() {
let _ = App::new("test")
.arg(Arg::new("test")
.short("t")
.long("test")
.takes_value(true)
.help("testing testing"))
.get_matches();
}
#[test]
fn create_subcommand() {
let _ = App::new("test")
.subcommand(SubCommand::new("some")
.arg(Arg::new("test")
.short("t")
.long("test")
.takes_value(true)
.help("testing testing")))
.arg(Arg::new("other").long("other"))
.get_matches();
}
#[test]
fn create_multiple_subcommands() {
let _ = App::new("test")
.subcommands(vec![ SubCommand::new("some")
.arg(Arg::new("test")
.short("t")
.long("test")
.takes_value(true)
.help("testing testing")),
SubCommand::new("add")
.arg(Arg::new("roster").short("r"))])
.arg(Arg::new("other").long("other"))
.get_matches();
}
#[test]
#[should_panic]
fn unique_arg_names(){
App::new("some").args(vec![
Arg::new("arg").short("a"),
Arg::new("arg").short("b")
]);
}
#[test]
#[should_panic]
fn unique_arg_shorts(){
App::new("some").args(vec![
Arg::new("arg1").short("a"),
Arg::new("arg2").short("a")
]);
}
#[test]
#[should_panic]
fn unique_arg_longs(){
App::new("some").args(vec![
Arg::new("arg1").long("long"),
Arg::new("arg2").long("long")
]);
}