1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>
//
// 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.
use structopt::StructOpt;
#[derive(StructOpt, PartialEq, Debug)]
struct Opt {
#[structopt(short, long)]
force: bool,
#[structopt(short, long, parse(from_occurrences))]
verbose: u64,
#[structopt(subcommand)]
cmd: Sub,
}
#[derive(StructOpt, PartialEq, Debug)]
enum Sub {
Fetch {},
Add {},
}
#[derive(StructOpt, PartialEq, Debug)]
struct Opt2 {
#[structopt(short, long)]
force: bool,
#[structopt(short, long, parse(from_occurrences))]
verbose: u64,
#[structopt(subcommand)]
cmd: Option<Sub>,
}
#[test]
fn test_no_cmd() {
let result = Opt::clap().get_matches_from_safe(&["test"]);
assert!(result.is_err());
assert_eq!(
Opt2 {
force: false,
verbose: 0,
cmd: None
},
Opt2::from_clap(&Opt2::clap().get_matches_from(&["test"]))
);
}
#[test]
fn test_fetch() {
assert_eq!(
Opt {
force: false,
verbose: 3,
cmd: Sub::Fetch {}
},
Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-vvv", "fetch"]))
);
assert_eq!(
Opt {
force: true,
verbose: 0,
cmd: Sub::Fetch {}
},
Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--force", "fetch"]))
);
}
#[test]
fn test_add() {
assert_eq!(
Opt {
force: false,
verbose: 0,
cmd: Sub::Add {}
},
Opt::from_clap(&Opt::clap().get_matches_from(&["test", "add"]))
);
assert_eq!(
Opt {
force: false,
verbose: 2,
cmd: Sub::Add {}
},
Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-vv", "add"]))
);
}
#[test]
fn test_badinput() {
let result = Opt::clap().get_matches_from_safe(&["test", "badcmd"]);
assert!(result.is_err());
let result = Opt::clap().get_matches_from_safe(&["test", "add", "--verbose"]);
assert!(result.is_err());
let result = Opt::clap().get_matches_from_safe(&["test", "--badopt", "add"]);
assert!(result.is_err());
let result = Opt::clap().get_matches_from_safe(&["test", "add", "--badopt"]);
assert!(result.is_err());
}
#[derive(StructOpt, PartialEq, Debug)]
struct Opt3 {
#[structopt(short, long)]
all: bool,
#[structopt(subcommand)]
cmd: Sub2,
}
#[derive(StructOpt, PartialEq, Debug)]
enum Sub2 {
Foo {
file: String,
#[structopt(subcommand)]
cmd: Sub3,
},
Bar {},
}
#[derive(StructOpt, PartialEq, Debug)]
enum Sub3 {
Baz {},
Quux {},
}
#[test]
fn test_subsubcommand() {
assert_eq!(
Opt3 {
all: true,
cmd: Sub2::Foo {
file: "lib.rs".to_string(),
cmd: Sub3::Quux {}
}
},
Opt3::from_clap(
&Opt3::clap().get_matches_from(&["test", "--all", "foo", "lib.rs", "quux"])
)
);
}
#[derive(StructOpt, PartialEq, Debug)]
enum SubSubCmdWithOption {
Remote {
#[structopt(subcommand)]
cmd: Option<Remote>,
},
Stash {
#[structopt(subcommand)]
cmd: Stash,
},
}
#[derive(StructOpt, PartialEq, Debug)]
enum Remote {
Add { name: String, url: String },
Remove { name: String },
}
#[derive(StructOpt, PartialEq, Debug)]
enum Stash {
Save,
Pop,
}
#[test]
fn sub_sub_cmd_with_option() {
fn make(args: &[&str]) -> Option<SubSubCmdWithOption> {
SubSubCmdWithOption::clap()
.get_matches_from_safe(args)
.ok()
.map(|m| SubSubCmdWithOption::from_clap(&m))
}
assert_eq!(
Some(SubSubCmdWithOption::Remote { cmd: None }),
make(&["", "remote"])
);
assert_eq!(
Some(SubSubCmdWithOption::Remote {
cmd: Some(Remote::Add {
name: "origin".into(),
url: "http".into()
})
}),
make(&["", "remote", "add", "origin", "http"])
);
assert_eq!(
Some(SubSubCmdWithOption::Stash { cmd: Stash::Save }),
make(&["", "stash", "save"])
);
assert_eq!(None, make(&["", "stash"]));
}
|