Skip to content

Commit

Permalink
tests: removes old python tests and replaces with rust tests
Browse files Browse the repository at this point in the history
Closes #166
  • Loading branch information
kbknapp committed May 9, 2016
1 parent a0a5631 commit 852e581
Show file tree
Hide file tree
Showing 18 changed files with 586 additions and 503 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# Generated by Cargo
/target/
/clap-test/target/

# Cargo files
Cargo.lock
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ clippy = { version = "=0.0.64", optional = true }
unicode-width = { version = "~0.1.3", optional = true }

[dev-dependencies]
regex = "~0.1.69"
clap-test = { path = "clap-test/" }

[features]
default = ["suggestions", "color", "wrap_help"]
Expand Down
10 changes: 10 additions & 0 deletions clap-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "clap-test"
version = "0.1.0"
authors = ["Kevin K. <[email protected]>"]
description = "functions and macros to assist in testing clap"
license = "MIT"

[dependencies]
regex = "~0.1.69"
clap = { path = "../" }
164 changes: 164 additions & 0 deletions clap-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
extern crate clap;
extern crate regex;

use std::str;
use std::io::Write;

use regex::Regex;

use clap::{App, Arg, SubCommand};

pub fn check_err_output(a: App, args: &str, out: &str, use_stderr: bool) {
let res = a.get_matches_from_safe(args.split(' ').collect::<Vec<_>>());
let re = Regex::new("\x1b[^m]*m").unwrap();

let mut w = vec![];
let err = res.unwrap_err();
err.write_to(&mut w).unwrap();
let err_s = str::from_utf8(&w).unwrap();
assert_eq!(re.replace_all(err_s, ""), out);
assert_eq!(use_stderr, err.use_stderr());
}

pub fn check_help(mut a: App, out: &str) {
// We call a get_matches method to cause --help and --version to be built
let _ = a.get_matches_from_safe_borrow(vec![""]);

// Now we check the output of print_help()
let mut help = vec![];
a.write_help(&mut help).ok().expect("failed to print help");
assert_eq!(str::from_utf8(&help).unwrap(), out);
}

pub fn complex_app() -> App<'static, 'static> {
let args = "-o --option=[opt]... 'tests options'
[positional] 'tests positionals'";
let opt3_vals = ["fast", "slow"];
let pos3_vals = ["vi", "emacs"];
App::new("clap-test")
.version("v1.4.8")
.about("tests clap library")
.author("Kevin K. <[email protected]>")
.args_from_usage(args)
.arg(Arg::from_usage("-f --flag... 'tests flags'")
.global(true))
.args(&[
Arg::from_usage("[flag2] -F 'tests flags with exclusions'").conflicts_with("flag").requires("long-option-2"),
Arg::from_usage("--long-option-2 [option2] 'tests long options with exclusions'").conflicts_with("option").requires("positional2"),
Arg::from_usage("[positional2] 'tests positionals with exclusions'"),
Arg::from_usage("-O --Option [option3] 'specific vals'").possible_values(&opt3_vals),
Arg::from_usage("[positional3]... 'tests specific values'").possible_values(&pos3_vals),
Arg::from_usage("--multvals [one] [two] 'Tests mutliple values, not mult occs'"),
Arg::from_usage("--multvalsmo... [one] [two] 'Tests mutliple values, and mult occs'"),
Arg::from_usage("--minvals2 [minvals]... 'Tests 2 min vals'").min_values(2),
Arg::from_usage("--maxvals3 [maxvals]... 'Tests 3 max vals'").max_values(3)
])
.subcommand(SubCommand::with_name("subcmd")
.about("tests subcommands")
.version("0.1")
.author("Kevin K. <[email protected]>")
.arg_from_usage("-o --option [scoption]... 'tests options'")
.arg_from_usage("[scpositional] 'tests positionals'"))
}

pub fn check_complex_output(args: &str, out: &str) {
let mut w = vec![];
let matches = complex_app().get_matches_from(args.split(' ').collect::<Vec<_>>());
if matches.is_present("flag") {
writeln!(w, "flag present {} times", matches.occurrences_of("flag")).unwrap();
} else {
writeln!(w, "flag NOT present").unwrap();
}

if matches.is_present("option") {
if let Some(v) = matches.value_of("option") {
writeln!(w, "option present {} times with value: {}",matches.occurrences_of("option"), v).unwrap();
}
if let Some(ov) = matches.values_of("option") {
for o in ov {
writeln!(w, "An option: {}", o).unwrap();
}
}
} else {
writeln!(w, "option NOT present").unwrap();
}

if let Some(p) = matches.value_of("positional") {
writeln!(w, "positional present with value: {}", p).unwrap();
} else {
writeln!(w, "positional NOT present").unwrap();
}

if matches.is_present("flag2") {
writeln!(w, "flag2 present").unwrap();
writeln!(w, "option2 present with value of: {}", matches.value_of("long-option-2").unwrap()).unwrap();
writeln!(w, "positional2 present with value of: {}", matches.value_of("positional2").unwrap()).unwrap();
} else {
writeln!(w, "flag2 NOT present").unwrap();
writeln!(w, "option2 maybe present with value of: {}", matches.value_of("long-option-2").unwrap_or("Nothing")).unwrap();
writeln!(w, "positional2 maybe present with value of: {}", matches.value_of("positional2").unwrap_or("Nothing")).unwrap();
}

let _ = match matches.value_of("Option3").unwrap_or("") {
"fast" => writeln!(w, "option3 present quickly"),
"slow" => writeln!(w, "option3 present slowly"),
_ => writeln!(w, "option3 NOT present")
};

let _ = match matches.value_of("positional3").unwrap_or("") {
"vi" => writeln!(w, "positional3 present in vi mode"),
"emacs" => writeln!(w, "positional3 present in emacs mode"),
_ => writeln!(w, "positional3 NOT present")
};

if matches.is_present("option") {
if let Some(v) = matches.value_of("option") {
writeln!(w, "option present {} times with value: {}",matches.occurrences_of("option"), v).unwrap();
}
if let Some(ov) = matches.values_of("option") {
for o in ov {
writeln!(w, "An option: {}", o).unwrap();
}
}
} else {
writeln!(w, "option NOT present").unwrap();
}

if let Some(p) = matches.value_of("positional") {
writeln!(w, "positional present with value: {}", p).unwrap();
} else {
writeln!(w, "positional NOT present").unwrap();
}
if matches.is_present("subcmd") {
writeln!(w, "subcmd present").unwrap();
if let Some(matches) = matches.subcommand_matches("subcmd") {
if matches.is_present("flag") {
writeln!(w, "flag present {} times", matches.occurrences_of("flag")).unwrap();
} else {
writeln!(w, "flag NOT present").unwrap();
}

if matches.is_present("option") {
if let Some(v) = matches.value_of("option") {
writeln!(w, "scoption present with value: {}", v).unwrap();
}
if let Some(ov) = matches.values_of("option") {
for o in ov {
writeln!(w, "An scoption: {}", o).unwrap();
}
}
} else {
writeln!(w, "scoption NOT present").unwrap();
}

if let Some(p) = matches.value_of("scpositional") {
writeln!(w, "scpositional present with value: {}", p).unwrap();
}
}
} else {
writeln!(w, "subcmd NOT present").unwrap();
}

let res = str::from_utf8(&w).unwrap();
assert_eq!(res, out);
}
11 changes: 0 additions & 11 deletions clap-tests/.gitignore

This file was deleted.

8 changes: 0 additions & 8 deletions clap-tests/Cargo.toml

This file was deleted.

14 changes: 0 additions & 14 deletions clap-tests/Makefile

This file was deleted.

Loading

0 comments on commit 852e581

Please sign in to comment.