clap/tests/subcommands.rs

238 lines
5.8 KiB
Rust
Raw Normal View History

2018-07-02 17:41:01 +00:00
// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>,
// Kevin Knapp (@kbknapp) <kbknapp@gmail.com>, and
// Andrew Hobden (@hoverbear) <andrew@hoverbear.org>
//
2018-02-25 10:22:24 +00:00
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2018-07-02 17:41:01 +00:00
//
// This work was derived from Structopt (https://github.com/TeXitoi/structopt)
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
// MIT/Apache 2.0 license.
2018-05-21 14:54:22 +00:00
#[macro_use]
2018-07-02 19:06:46 +00:00
extern crate clap;
2018-07-02 19:06:46 +00:00
use clap::Clap;
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
enum Opt {
2018-07-02 19:06:46 +00:00
#[clap(name = "fetch", about = "Fetch stuff from GitHub.")]
Fetch {
2018-07-02 19:06:46 +00:00
#[clap(long = "all")]
all: bool,
2018-07-02 19:06:46 +00:00
#[clap(short = "f", long = "force")]
/// Overwrite local branches.
force: bool,
2018-05-21 14:54:22 +00:00
repo: String,
},
2018-05-21 14:54:22 +00:00
2018-07-02 19:06:46 +00:00
#[clap(name = "add")]
Add {
2018-07-02 19:06:46 +00:00
#[clap(short = "i", long = "interactive")]
interactive: bool,
2018-07-02 19:06:46 +00:00
#[clap(short = "v", long = "verbose")]
2018-05-21 14:54:22 +00:00
verbose: bool,
},
}
#[test]
fn test_fetch() {
2018-05-21 14:54:22 +00:00
assert_eq!(
Opt::Fetch {
all: true,
force: false,
repo: "origin".to_string()
},
2018-07-02 19:06:46 +00:00
Opt::from_argmatches(
&Opt::into_app().get_matches_from(&["test", "fetch", "--all", "origin"])
)
2018-05-21 14:54:22 +00:00
);
assert_eq!(
Opt::Fetch {
all: false,
force: true,
repo: "origin".to_string()
},
2018-07-02 19:06:46 +00:00
Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "fetch", "-f", "origin"]))
2018-05-21 14:54:22 +00:00
);
}
#[test]
fn test_add() {
2018-05-21 14:54:22 +00:00
assert_eq!(
Opt::Add {
interactive: false,
verbose: false
},
2018-07-02 19:06:46 +00:00
Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "add"]))
2018-05-21 14:54:22 +00:00
);
assert_eq!(
Opt::Add {
interactive: true,
verbose: true
},
2018-07-02 19:06:46 +00:00
Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "add", "-i", "-v"]))
2018-05-21 14:54:22 +00:00
);
}
#[test]
fn test_no_parse() {
2018-07-02 19:06:46 +00:00
let result = Opt::into_app().get_matches_from_safe(&["test", "badcmd", "-i", "-v"]);
assert!(result.is_err());
2018-07-02 19:06:46 +00:00
let result = Opt::into_app().get_matches_from_safe(&["test", "add", "--badoption"]);
assert!(result.is_err());
2018-07-02 19:06:46 +00:00
let result = Opt::into_app().get_matches_from_safe(&["test"]);
assert!(result.is_err());
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
enum Opt2 {
2018-07-02 19:06:46 +00:00
#[clap(name = "do-something")]
2018-05-21 14:54:22 +00:00
DoSomething { arg: String },
}
#[test]
/// This test is specifically to make sure that hyphenated subcommands get
/// processed correctly.
fn test_hyphenated_subcommands() {
2018-05-21 14:54:22 +00:00
assert_eq!(
Opt2::DoSomething {
arg: "blah".to_string()
},
2018-07-02 19:06:46 +00:00
Opt2::from_argmatches(&Opt2::into_app().get_matches_from(&[
"test",
"do-something",
"blah"
]))
2018-05-21 14:54:22 +00:00
);
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
enum Opt3 {
2018-07-02 19:06:46 +00:00
#[clap(name = "add")]
Add,
2018-07-02 19:06:46 +00:00
#[clap(name = "init")]
Init,
2018-07-02 19:06:46 +00:00
#[clap(name = "fetch")]
2018-05-21 14:54:22 +00:00
Fetch,
}
#[test]
fn test_null_commands() {
2018-05-21 14:54:22 +00:00
assert_eq!(
Opt3::Add,
2018-07-02 19:06:46 +00:00
Opt3::from_argmatches(&Opt3::into_app().get_matches_from(&["test", "add"]))
2018-05-21 14:54:22 +00:00
);
assert_eq!(
Opt3::Init,
2018-07-02 19:06:46 +00:00
Opt3::from_argmatches(&Opt3::into_app().get_matches_from(&["test", "init"]))
2018-05-21 14:54:22 +00:00
);
assert_eq!(
Opt3::Fetch,
2018-07-02 19:06:46 +00:00
Opt3::from_argmatches(&Opt3::into_app().get_matches_from(&["test", "fetch"]))
2018-05-21 14:54:22 +00:00
);
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
#[clap(about = "Not shown")]
struct Add {
file: String,
}
/// Not shown
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
struct Fetch {
remote: String,
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
enum Opt4 {
/// Not shown
2018-07-02 19:06:46 +00:00
#[clap(name = "add", about = "Add a file")]
Add(Add),
2018-07-02 19:06:46 +00:00
#[clap(name = "init")]
Init,
/// download history from remote
2018-07-02 19:06:46 +00:00
#[clap(name = "fetch")]
Fetch(Fetch),
}
#[test]
fn test_tuple_commands() {
assert_eq!(
2018-05-21 14:54:22 +00:00
Opt4::Add(Add {
file: "f".to_string()
}),
2018-07-02 19:06:46 +00:00
Opt4::from_argmatches(&Opt4::into_app().get_matches_from(&["test", "add", "f"]))
);
assert_eq!(
2018-05-21 14:54:22 +00:00
Opt4::Init,
2018-07-02 19:06:46 +00:00
Opt4::from_argmatches(&Opt4::into_app().get_matches_from(&["test", "init"]))
2018-05-21 14:54:22 +00:00
);
assert_eq!(
Opt4::Fetch(Fetch {
remote: "origin".to_string()
}),
2018-07-02 19:06:46 +00:00
Opt4::from_argmatches(&Opt4::into_app().get_matches_from(&["test", "fetch", "origin"]))
);
let mut output = Vec::new();
2018-07-02 19:06:46 +00:00
Opt4::into_app().write_long_help(&mut output).unwrap();
let output = String::from_utf8(output).unwrap();
assert!(output.contains("download history from remote"));
assert!(output.contains("Add a file"));
assert!(!output.contains("Not shown"));
}
2018-02-24 11:11:08 +00:00
#[test]
fn enum_in_enum_subsubcommand() {
2018-07-02 19:06:46 +00:00
#[derive(Clap, Debug, PartialEq)]
2018-02-24 11:11:08 +00:00
pub enum Opt {
2018-07-02 19:06:46 +00:00
#[clap(name = "daemon")]
2018-05-21 14:54:22 +00:00
Daemon(DaemonCommand),
2018-02-24 11:11:08 +00:00
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, Debug, PartialEq)]
2018-02-24 11:11:08 +00:00
pub enum DaemonCommand {
2018-07-02 19:06:46 +00:00
#[clap(name = "start")]
2018-02-24 11:11:08 +00:00
Start,
2018-07-02 19:06:46 +00:00
#[clap(name = "stop")]
2018-02-24 11:11:08 +00:00
Stop,
}
2018-07-02 19:06:46 +00:00
let result = Opt::into_app().get_matches_from_safe(&["test"]);
2018-02-24 11:11:08 +00:00
assert!(result.is_err());
2018-07-02 19:06:46 +00:00
let result = Opt::into_app().get_matches_from_safe(&["test", "daemon"]);
2018-02-24 11:11:08 +00:00
assert!(result.is_err());
2018-07-02 19:06:46 +00:00
let result = Opt::parse_from(&["test", "daemon", "start"]);
2018-02-24 11:11:08 +00:00
assert_eq!(Opt::Daemon(DaemonCommand::Start), result);
}
#[test]
fn flatten_enum() {
2018-07-02 19:06:46 +00:00
#[derive(Clap, Debug, PartialEq)]
struct Opt {
2018-07-02 19:06:46 +00:00
#[clap(flatten)]
sub_cmd: SubCmd,
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, Debug, PartialEq)]
enum SubCmd {
Foo,
Bar,
}
2018-07-02 19:06:46 +00:00
assert!(Opt::try_parse_from(&["test"]).is_err());
2018-05-21 14:54:22 +00:00
assert_eq!(
2018-07-02 19:06:46 +00:00
Opt::parse_from(&["test", "Foo"]),
2018-05-21 14:54:22 +00:00
Opt {
sub_cmd: SubCmd::Foo
}
);
}