2016-04-10 00:35:24 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
|
|
use clap::{App, SubCommand};
|
|
|
|
|
|
|
|
static EXAMPLE1_TMPL_S : &'static str = include_str!("example1_tmpl_simple.txt");
|
|
|
|
static EXAMPLE1_TMPS_F : &'static str = include_str!("example1_tmpl_full.txt");
|
|
|
|
|
2016-05-30 09:39:54 +00:00
|
|
|
static CUSTOM_TEMPL_HELP: &'static str = "MyApp 1.0
|
|
|
|
Kevin K. <kbknapp@gmail.com>
|
|
|
|
Does awesome things
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
MyApp [FLAGS] [OPTIONS] <output> [SUBCOMMAND]
|
|
|
|
|
|
|
|
FLAGS:
|
2016-10-20 23:37:19 +00:00
|
|
|
-d Turn debugging information on
|
2016-05-30 09:39:54 +00:00
|
|
|
OPTIONS:
|
|
|
|
-c, --config <FILE> Sets a custom config file
|
|
|
|
ARGS:
|
|
|
|
<output> Sets an optional output file
|
|
|
|
SUBCOMMANDS:
|
|
|
|
test does testing things";
|
|
|
|
|
|
|
|
static SIMPLE_TEMPLATE: &'static str = "MyApp 1.0
|
|
|
|
Kevin K. <kbknapp@gmail.com>
|
|
|
|
Does awesome things
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
MyApp [FLAGS] [OPTIONS] <output> [SUBCOMMAND]
|
|
|
|
|
|
|
|
FLAGS:
|
2016-10-20 23:37:19 +00:00
|
|
|
-d Turn debugging information on
|
2016-05-30 09:39:54 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
-c, --config <FILE> Sets a custom config file
|
|
|
|
|
|
|
|
ARGS:
|
|
|
|
<output> Sets an optional output file
|
|
|
|
|
|
|
|
SUBCOMMANDS:
|
|
|
|
test does testing things";
|
|
|
|
|
2016-04-10 00:35:24 +00:00
|
|
|
fn build_new_help(app: &App) -> String {
|
|
|
|
let mut buf = Cursor::new(Vec::with_capacity(50));
|
|
|
|
app.write_help(&mut buf).unwrap();
|
|
|
|
let content = buf.into_inner();
|
|
|
|
String::from_utf8(content).unwrap()
|
|
|
|
}
|
|
|
|
|
2016-05-30 09:39:54 +00:00
|
|
|
fn compare_app_str(l: &App, right: &str) -> bool {
|
|
|
|
let left = build_new_help(&l);
|
2016-07-16 21:10:26 +00:00
|
|
|
// Strip out any mismatching \r character on windows that might sneak in on either side
|
|
|
|
let b = left.trim().replace("\r", "") == right.replace("\r", "");
|
2016-04-10 00:35:24 +00:00
|
|
|
if !b {
|
|
|
|
println!("");
|
2016-05-30 09:39:54 +00:00
|
|
|
println!("--> left");
|
|
|
|
println!("{}", left);
|
|
|
|
println!("--> right");
|
|
|
|
println!("{}", right);
|
2016-04-10 00:35:24 +00:00
|
|
|
println!("--")
|
|
|
|
}
|
|
|
|
b
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-05-30 09:39:54 +00:00
|
|
|
fn with_template() {
|
|
|
|
assert!(compare_app_str(&app_example1().template(EXAMPLE1_TMPL_S), SIMPLE_TEMPLATE));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_template() {
|
|
|
|
let app = app_example1().template(EXAMPLE1_TMPS_F);
|
|
|
|
assert!(compare_app_str(&app, CUSTOM_TEMPL_HELP));
|
2016-04-10 00:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn template_empty() {
|
|
|
|
let app = App::new("MyApp")
|
|
|
|
.version("1.0")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
|
|
|
.about("Does awesome things")
|
|
|
|
.template("");
|
2016-05-30 09:39:54 +00:00
|
|
|
assert!(compare_app_str(&app, ""));
|
2016-04-10 00:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn template_notag() {
|
|
|
|
let app = App::new("MyApp")
|
|
|
|
.version("1.0")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
|
|
|
.about("Does awesome things")
|
2016-05-30 09:39:54 +00:00
|
|
|
.template("test no tag test");
|
|
|
|
assert!(compare_app_str(&app, "test no tag test"));
|
2016-04-10 00:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn template_unknowntag() {
|
|
|
|
let app = App::new("MyApp")
|
|
|
|
.version("1.0")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
|
|
|
.about("Does awesome things")
|
2016-05-30 09:39:54 +00:00
|
|
|
.template("test {unknown_tag} test");
|
|
|
|
assert!(compare_app_str(&app, "test {unknown_tag} test"));
|
2016-04-10 00:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn template_author_version() {
|
|
|
|
let app = App::new("MyApp")
|
|
|
|
.version("1.0")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
|
|
|
.about("Does awesome things")
|
|
|
|
.template("{author}\n{version}\n{about}\n{bin}");
|
2016-05-30 09:39:54 +00:00
|
|
|
assert!(compare_app_str(&app, "Kevin K. <kbknapp@gmail.com>\n1.0\nDoes awesome things\nMyApp"));
|
2016-04-10 00:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn app_example1<'b, 'c>() -> App<'b, 'c> {
|
|
|
|
App::new("MyApp")
|
|
|
|
.version("1.0")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
|
|
|
.about("Does awesome things")
|
|
|
|
.args_from_usage("-c, --config=[FILE] 'Sets a custom config file'
|
2016-05-30 09:39:54 +00:00
|
|
|
<output> 'Sets an optional output file'
|
|
|
|
-d... 'Turn debugging information on'")
|
2016-04-10 00:35:24 +00:00
|
|
|
.subcommand(SubCommand::with_name("test")
|
|
|
|
.about("does testing things")
|
|
|
|
.arg_from_usage("-l, --list 'lists test values'"))
|
|
|
|
}
|