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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
// 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.
mod utils;
use structopt::StructOpt;
use utils::*;
#[derive(StructOpt, PartialEq, Debug)]
enum Opt {
/// Fetch stuff from GitHub
Fetch {
#[structopt(long)]
all: bool,
#[structopt(short, long)]
/// Overwrite local branches.
force: bool,
repo: String,
},
Add {
#[structopt(short, long)]
interactive: bool,
#[structopt(short, long)]
verbose: bool,
},
}
#[test]
fn test_fetch() {
assert_eq!(
Opt::Fetch {
all: true,
force: false,
repo: "origin".to_string()
},
Opt::from_clap(&Opt::clap().get_matches_from(&["test", "fetch", "--all", "origin"]))
);
assert_eq!(
Opt::Fetch {
all: false,
force: true,
repo: "origin".to_string()
},
Opt::from_clap(&Opt::clap().get_matches_from(&["test", "fetch", "-f", "origin"]))
);
}
#[test]
fn test_add() {
assert_eq!(
Opt::Add {
interactive: false,
verbose: false
},
Opt::from_clap(&Opt::clap().get_matches_from(&["test", "add"]))
);
assert_eq!(
Opt::Add {
interactive: true,
verbose: true
},
Opt::from_clap(&Opt::clap().get_matches_from(&["test", "add", "-i", "-v"]))
);
}
#[test]
fn test_no_parse() {
let result = Opt::clap().get_matches_from_safe(&["test", "badcmd", "-i", "-v"]);
assert!(result.is_err());
let result = Opt::clap().get_matches_from_safe(&["test", "add", "--badoption"]);
assert!(result.is_err());
let result = Opt::clap().get_matches_from_safe(&["test"]);
assert!(result.is_err());
}
#[derive(StructOpt, PartialEq, Debug)]
enum Opt2 {
DoSomething { arg: String },
}
#[test]
/// This test is specifically to make sure that hyphenated subcommands get
/// processed correctly.
fn test_hyphenated_subcommands() {
assert_eq!(
Opt2::DoSomething {
arg: "blah".to_string()
},
Opt2::from_clap(&Opt2::clap().get_matches_from(&["test", "do-something", "blah"]))
);
}
#[derive(StructOpt, PartialEq, Debug)]
enum Opt3 {
Add,
Init,
Fetch,
}
#[test]
fn test_null_commands() {
assert_eq!(
Opt3::Add,
Opt3::from_clap(&Opt3::clap().get_matches_from(&["test", "add"]))
);
assert_eq!(
Opt3::Init,
Opt3::from_clap(&Opt3::clap().get_matches_from(&["test", "init"]))
);
assert_eq!(
Opt3::Fetch,
Opt3::from_clap(&Opt3::clap().get_matches_from(&["test", "fetch"]))
);
}
#[derive(StructOpt, PartialEq, Debug)]
#[structopt(about = "Not shown")]
struct Add {
file: String,
}
/// Not shown
#[derive(StructOpt, PartialEq, Debug)]
struct Fetch {
remote: String,
}
#[derive(StructOpt, PartialEq, Debug)]
enum Opt4 {
// Not shown
/// Add a file
Add(Add),
Init,
/// download history from remote
Fetch(Fetch),
}
#[test]
fn test_tuple_commands() {
assert_eq!(
Opt4::Add(Add {
file: "f".to_string()
}),
Opt4::from_clap(&Opt4::clap().get_matches_from(&["test", "add", "f"]))
);
assert_eq!(
Opt4::Init,
Opt4::from_clap(&Opt4::clap().get_matches_from(&["test", "init"]))
);
assert_eq!(
Opt4::Fetch(Fetch {
remote: "origin".to_string()
}),
Opt4::from_clap(&Opt4::clap().get_matches_from(&["test", "fetch", "origin"]))
);
let output = get_long_help::<Opt4>();
assert!(output.contains("download history from remote"));
assert!(output.contains("Add a file"));
assert!(!output.contains("Not shown"));
}
#[test]
fn enum_in_enum_subsubcommand() {
#[derive(StructOpt, Debug, PartialEq)]
pub enum Opt {
Daemon(DaemonCommand),
}
#[derive(StructOpt, Debug, PartialEq)]
pub enum DaemonCommand {
Start,
Stop,
}
let result = Opt::clap().get_matches_from_safe(&["test"]);
assert!(result.is_err());
let result = Opt::clap().get_matches_from_safe(&["test", "daemon"]);
assert!(result.is_err());
let result = Opt::from_iter(&["test", "daemon", "start"]);
assert_eq!(Opt::Daemon(DaemonCommand::Start), result);
}
#[test]
fn flatten_enum() {
#[derive(StructOpt, Debug, PartialEq)]
struct Opt {
#[structopt(flatten)]
sub_cmd: SubCmd,
}
#[derive(StructOpt, Debug, PartialEq)]
enum SubCmd {
Foo,
Bar,
}
assert!(Opt::from_iter_safe(&["test"]).is_err());
assert_eq!(
Opt::from_iter(&["test", "foo"]),
Opt {
sub_cmd: SubCmd::Foo
}
);
}
|