Auto merge of #381 - sru:tests, r=sru

Tests

#166

Almost everything from run_tests.py except the suggestion...
This commit is contained in:
Homu 2016-01-14 17:11:25 -08:00
commit 113ed0284e
7 changed files with 419 additions and 102 deletions

View file

@ -1,6 +1,30 @@
extern crate clap;
use clap::App;
use clap::{App, ClapErrorType};
#[test]
fn help_short() {
let m = App::new("test")
.author("Kevin K.")
.about("tests stuff")
.version("1.3")
.get_matches_from_safe(vec!["", "-h"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().error_type, ClapErrorType::HelpDisplayed);
}
#[test]
fn help_long() {
let m = App::new("test")
.author("Kevin K.")
.about("tests stuff")
.version("1.3")
.get_matches_from_safe(vec!["", "--help"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().error_type, ClapErrorType::HelpDisplayed);
}
#[test]
fn print_app_help() {

View file

@ -3,7 +3,7 @@ extern crate clap;
use clap::{App, Arg, ClapErrorType};
#[test]
fn multiple_values_of_long_multiple() {
fn multiple_values_of_option_long() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.long("option")
@ -26,29 +26,7 @@ fn multiple_values_of_long_multiple() {
}
#[test]
fn multiple_values_of_long_single() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.long("option")
.help("multiple options")
.takes_value(true)
.multiple(true))
.get_matches_from_safe(vec![
"",
"--option", "val1",
"--option", "val2",
]);
assert!(m.is_ok());
let m = m.unwrap();
assert!(m.is_present("option"));
assert_eq!(m.occurrences_of("option"), 2);
assert_eq!(m.values_of("option"), Some(vec!["val1", "val2"]));
}
#[test]
fn multiple_values_of_short_multiple() {
fn multiple_values_of_option_short() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.short("o")
@ -71,29 +49,7 @@ fn multiple_values_of_short_multiple() {
}
#[test]
fn multiple_values_of_short_single() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.short("o")
.help("multiple options")
.takes_value(true)
.multiple(true))
.get_matches_from_safe(vec![
"",
"-o", "val1",
"-o", "val2",
]);
assert!(m.is_ok());
let m = m.unwrap();
assert!(m.is_present("option"));
assert_eq!(m.occurrences_of("option"), 2);
assert_eq!(m.values_of("option"), Some(vec!["val1", "val2"]));
}
#[test]
fn multiple_values_of_mixed() {
fn multiple_values_of_option_mixed() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.long("option")
@ -118,7 +74,7 @@ fn multiple_values_of_mixed() {
}
#[test]
fn multiple_values_of_exact_exact() {
fn multiple_values_of_option_exact_exact() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.short("o")
@ -142,7 +98,7 @@ fn multiple_values_of_exact_exact() {
}
#[test]
fn multiple_values_of_exact_less() {
fn multiple_values_of_option_exact_less() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.short("o")
@ -161,7 +117,7 @@ fn multiple_values_of_exact_less() {
}
#[test]
fn multiple_values_of_exact_more() {
fn multiple_values_of_option_exact_more() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.short("o")
@ -182,7 +138,7 @@ fn multiple_values_of_exact_more() {
}
#[test]
fn multiple_values_of_min_exact() {
fn multiple_values_of_option_min_exact() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.short("o")
@ -205,7 +161,7 @@ fn multiple_values_of_min_exact() {
}
#[test]
fn multiple_values_of_min_less() {
fn multiple_values_of_option_min_less() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.short("o")
@ -223,7 +179,7 @@ fn multiple_values_of_min_less() {
}
#[test]
fn multiple_values_of_min_more() {
fn multiple_values_of_option_min_more() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.short("o")
@ -247,7 +203,7 @@ fn multiple_values_of_min_more() {
}
#[test]
fn multiple_values_of_max_exact() {
fn multiple_values_of_option_max_exact() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.short("o")
@ -270,7 +226,7 @@ fn multiple_values_of_max_exact() {
}
#[test]
fn multiple_values_of_max_less() {
fn multiple_values_of_option_max_less() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.short("o")
@ -292,7 +248,7 @@ fn multiple_values_of_max_less() {
}
#[test]
fn multiple_values_of_max_more() {
fn multiple_values_of_option_max_more() {
let m = App::new("multiple_values")
.arg(Arg::with_name("option")
.short("o")
@ -310,3 +266,160 @@ fn multiple_values_of_max_more() {
assert!(m.is_err());
assert_eq!(m.unwrap_err().error_type, ClapErrorType::TooManyValues);
}
#[test]
fn multiple_values_of_positional() {
let m = App::new("multiple_values")
.arg(Arg::with_name("pos")
.help("multiple positionals")
.index(1)
.multiple(true))
.get_matches_from_safe(vec!["", "val1", "val2", "val3"]);
assert!(m.is_ok());
let m = m.unwrap();
assert!(m.is_present("pos"));
assert_eq!(m.occurrences_of("pos"), 3);
assert_eq!(m.values_of("pos"), Some(vec!["val1", "val2", "val3"]));
}
#[test]
fn multiple_values_of_positional_exact_exact() {
let m = App::new("multiple_values")
.arg(Arg::with_name("pos")
.help("multiple positionals")
.index(1)
.multiple(true)
.number_of_values(3))
.get_matches_from_safe(vec!["", "val1", "val2", "val3"]);
assert!(m.is_ok());
let m = m.unwrap();
assert!(m.is_present("pos"));
assert_eq!(m.occurrences_of("pos"), 3);
assert_eq!(m.values_of("pos"), Some(vec!["val1", "val2", "val3"]));
}
#[test]
fn multiple_values_of_positional_exact_less() {
let m = App::new("multiple_values")
.arg(Arg::with_name("pos")
.help("multiple positionals")
.index(1)
.multiple(true)
.number_of_values(3))
.get_matches_from_safe(vec!["", "val1", "val2"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().error_type, ClapErrorType::WrongNumValues);
}
#[test]
fn multiple_values_of_positional_exact_more() {
let m = App::new("multiple_values")
.arg(Arg::with_name("pos")
.help("multiple positionals")
.index(1)
.multiple(true)
.number_of_values(3))
.get_matches_from_safe(vec!["", "val1", "val2", "val3", "val4"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().error_type, ClapErrorType::WrongNumValues);
}
#[test]
fn multiple_values_of_positional_min_exact() {
let m = App::new("multiple_values")
.arg(Arg::with_name("pos")
.help("multiple positionals")
.index(1)
.min_values(3))
.get_matches_from_safe(vec!["", "val1", "val2", "val3"]);
assert!(m.is_ok());
let m = m.unwrap();
assert!(m.is_present("pos"));
assert_eq!(m.occurrences_of("pos"), 3);
assert_eq!(m.values_of("pos"), Some(vec!["val1", "val2", "val3"]));
}
#[test]
fn multiple_values_of_positional_min_less() {
let m = App::new("multiple_values")
.arg(Arg::with_name("pos")
.help("multiple positionals")
.index(1)
.min_values(3))
.get_matches_from_safe(vec!["", "val1", "val2"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().error_type, ClapErrorType::TooFewValues);
}
#[test]
fn multiple_values_of_positional_min_more() {
let m = App::new("multiple_values")
.arg(Arg::with_name("pos")
.help("multiple positionals")
.index(1)
.min_values(3))
.get_matches_from_safe(vec!["", "val1", "val2", "val3", "val4"]);
assert!(m.is_ok());
let m = m.unwrap();
assert!(m.is_present("pos"));
assert_eq!(m.occurrences_of("pos"), 4);
assert_eq!(m.values_of("pos"), Some(vec!["val1", "val2", "val3", "val4"]));
}
#[test]
fn multiple_values_of_positional_max_exact() {
let m = App::new("multiple_values")
.arg(Arg::with_name("pos")
.help("multiple positionals")
.index(1)
.max_values(3))
.get_matches_from_safe(vec!["", "val1", "val2", "val3"]);
assert!(m.is_ok());
let m = m.unwrap();
assert!(m.is_present("pos"));
assert_eq!(m.occurrences_of("pos"), 3);
assert_eq!(m.values_of("pos"), Some(vec!["val1", "val2", "val3"]));
}
#[test]
fn multiple_values_of_positional_max_less() {
let m = App::new("multiple_values")
.arg(Arg::with_name("pos")
.help("multiple positionals")
.index(1)
.max_values(3))
.get_matches_from_safe(vec!["", "val1", "val2"]);
assert!(m.is_ok());
let m = m.unwrap();
assert!(m.is_present("pos"));
assert_eq!(m.occurrences_of("pos"), 2);
assert_eq!(m.values_of("pos"), Some(vec!["val1", "val2"]));
}
#[test]
fn multiple_values_of_positional_max_more() {
let m = App::new("multiple_values")
.arg(Arg::with_name("pos")
.help("multiple positionals")
.index(1)
.max_values(3))
.get_matches_from_safe(vec!["", "val1", "val2", "val3", "val4"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().error_type, ClapErrorType::TooManyValues);
}

View file

@ -67,18 +67,3 @@ fn positional_multiple_2() {
let err = result.err().unwrap();
assert_eq!(err.error_type, ClapErrorType::UnexpectedArgument);
}
#[test]
fn positional_possible_values() {
let m = App::new("positional_possible_values")
.args(vec![
Arg::from_usage("-f, --flag 'some flag'"),
Arg::with_name("positional")
.index(1)
.possible_value("test123")
])
.get_matches_from(vec!["", "-f", "test123"]);
assert!(m.is_present("positional"));
assert!(m.is_present("flag"));
assert_eq!(m.values_of("positional").unwrap(), vec!["test123"]);
}

135
tests/possible_values.rs Normal file
View file

@ -0,0 +1,135 @@
extern crate clap;
use clap::{App, Arg, ClapErrorType};
#[test]
fn possible_values_of_positional() {
let m = App::new("possible_values")
.arg(Arg::with_name("positional")
.index(1)
.possible_value("test123"))
.get_matches_from_safe(vec!["", "test123"]);
assert!(m.is_ok());
let m = m.unwrap();
assert!(m.is_present("positional"));
assert_eq!(m.value_of("positional"), Some("test123"));
}
#[test]
fn possible_values_of_positional_fail() {
let m = App::new("possible_values")
.arg(Arg::with_name("positional")
.index(1)
.possible_value("test123"))
.get_matches_from_safe(vec!["", "notest"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().error_type, ClapErrorType::InvalidValue);
}
#[test]
fn possible_values_of_positional_multiple() {
let m = App::new("possible_values")
.arg(Arg::with_name("positional")
.index(1)
.possible_value("test123")
.possible_value("test321")
.multiple(true))
.get_matches_from_safe(vec!["", "test123", "test321"]);
assert!(m.is_ok());
let m = m.unwrap();
assert!(m.is_present("positional"));
assert_eq!(m.values_of("positional"), Some(vec!["test123", "test321"]));
}
#[test]
fn possible_values_of_positional_multiple_fail() {
let m = App::new("possible_values")
.arg(Arg::with_name("positional")
.index(1)
.possible_value("test123")
.possible_value("test321")
.multiple(true))
.get_matches_from_safe(vec!["", "test123", "notest"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().error_type, ClapErrorType::InvalidValue);
}
#[test]
fn possible_values_of_option() {
let m = App::new("possible_values")
.arg(Arg::with_name("option")
.short("-o")
.long("--option")
.takes_value(true)
.possible_value("test123"))
.get_matches_from_safe(vec!["", "--option", "test123"]);
assert!(m.is_ok());
let m = m.unwrap();
assert!(m.is_present("option"));
assert_eq!(m.value_of("option"), Some("test123"));
}
#[test]
fn possible_values_of_option_fail() {
let m = App::new("possible_values")
.arg(Arg::with_name("option")
.short("-o")
.long("--option")
.takes_value(true)
.possible_value("test123"))
.get_matches_from_safe(vec!["", "--option", "notest"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().error_type, ClapErrorType::InvalidValue);
}
#[test]
fn possible_values_of_option_multiple() {
let m = App::new("possible_values")
.arg(Arg::with_name("option")
.short("-o")
.long("--option")
.takes_value(true)
.possible_value("test123")
.possible_value("test321")
.multiple(true))
.get_matches_from_safe(vec![
"",
"--option", "test123",
"--option", "test321",
]);
assert!(m.is_ok());
let m = m.unwrap();
assert!(m.is_present("option"));
assert_eq!(m.values_of("option"), Some(vec!["test123", "test321"]));
}
#[test]
fn possible_values_of_option_multiple_fail() {
let m = App::new("possible_values")
.arg(Arg::with_name("option")
.short("-o")
.long("--option")
.takes_value(true)
.possible_value("test123")
.possible_value("test321")
.multiple(true))
.get_matches_from_safe(vec![
"",
"--option", "test123",
"--option", "notest",
]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().error_type, ClapErrorType::InvalidValue);
}

61
tests/subcommands.rs Normal file
View file

@ -0,0 +1,61 @@
extern crate clap;
use clap::{App, Arg, SubCommand};
#[test]
fn subcommand() {
let m = App::new("test")
.subcommand(SubCommand::with_name("some")
.arg(Arg::with_name("test")
.short("t")
.long("test")
.takes_value(true)
.help("testing testing")))
.arg(Arg::with_name("other").long("other"))
.get_matches_from(vec!["", "some", "--test", "testing"]);
assert_eq!(m.subcommand_name().unwrap(), "some");
let sub_m = m.subcommand_matches("some").unwrap();
assert!(sub_m.is_present("test"));
assert_eq!(sub_m.value_of("test").unwrap(), "testing");
}
#[test]
fn subcommand_none_given() {
let m = App::new("test")
.subcommand(SubCommand::with_name("some")
.arg(Arg::with_name("test")
.short("t")
.long("test")
.takes_value(true)
.help("testing testing")))
.arg(Arg::with_name("other").long("other"))
.get_matches_from(vec![""]);
assert!(m.subcommand_name().is_none());
}
#[test]
fn subcommand_multiple() {
let m = App::new("test")
.subcommands(vec![
SubCommand::with_name("some")
.arg(Arg::with_name("test")
.short("t")
.long("test")
.takes_value(true)
.help("testing testing")),
SubCommand::with_name("add")
.arg(Arg::with_name("roster").short("r"))
])
.arg(Arg::with_name("other").long("other"))
.get_matches_from(vec!["", "some", "--test", "testing"]);
assert!(m.subcommand_matches("some").is_some());
assert!(m.subcommand_matches("add").is_none());
assert_eq!(m.subcommand_name().unwrap(), "some");
let sub_m = m.subcommand_matches("some").unwrap();
assert!(sub_m.is_present("test"));
assert_eq!(sub_m.value_of("test").unwrap(), "testing");
}

View file

@ -3,7 +3,7 @@ extern crate clap;
use std::collections::HashSet;
use clap::{App, Arg, ArgGroup, SubCommand};
use clap::{App, Arg, ArgGroup};
use std::vec::Vec;
arg_enum!{
@ -885,34 +885,6 @@ fn create_option_with_vals() {
assert_eq!(d.num_vals.unwrap(), 2);
}
#[test]
fn create_subcommand() {
let _ = App::new("test")
.subcommand(SubCommand::with_name("some")
.arg(Arg::with_name("test")
.short("t")
.long("test")
.takes_value(true)
.help("testing testing")))
.arg(Arg::with_name("other").long("other"))
.get_matches();
}
#[test]
fn create_multiple_subcommands() {
let _ = App::new("test")
.subcommands(vec![ SubCommand::with_name("some")
.arg(Arg::with_name("test")
.short("t")
.long("test")
.takes_value(true)
.help("testing testing")),
SubCommand::with_name("add")
.arg(Arg::with_name("roster").short("r"))])
.arg(Arg::with_name("other").long("other"))
.get_matches();
}
#[test]
#[should_panic]
fn empty_group() {
@ -943,4 +915,4 @@ fn errous_group() {
.add("vers")
.required(true))
.get_matches();
}
}

27
tests/version.rs Normal file
View file

@ -0,0 +1,27 @@
extern crate clap;
use clap::{App, ClapErrorType};
#[test]
fn version_short() {
let m = App::new("test")
.author("Kevin K.")
.about("tests stuff")
.version("1.3")
.get_matches_from_safe(vec!["", "-V"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().error_type, ClapErrorType::VersionDisplayed);
}
#[test]
fn version_long() {
let m = App::new("test")
.author("Kevin K.")
.about("tests stuff")
.version("1.3")
.get_matches_from_safe(vec!["", "--version"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().error_type, ClapErrorType::VersionDisplayed);
}