]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | // Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu> |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
6 | // option. This file may not be copied, modified, or distributed | |
7 | // except according to those terms. | |
8 | ||
9 | use structopt::StructOpt; | |
10 | ||
11 | mod utils; | |
12 | ||
13 | #[test] | |
14 | fn flatten() { | |
15 | #[derive(StructOpt, PartialEq, Debug)] | |
16 | struct Common { | |
17 | arg: i32, | |
18 | } | |
19 | ||
20 | #[derive(StructOpt, PartialEq, Debug)] | |
21 | struct Opt { | |
22 | #[structopt(flatten)] | |
23 | common: Common, | |
24 | } | |
25 | assert_eq!( | |
26 | Opt { | |
27 | common: Common { arg: 42 } | |
28 | }, | |
29 | Opt::from_iter(&["test", "42"]) | |
30 | ); | |
31 | assert!(Opt::clap().get_matches_from_safe(&["test"]).is_err()); | |
32 | assert!(Opt::clap() | |
33 | .get_matches_from_safe(&["test", "42", "24"]) | |
34 | .is_err()); | |
35 | } | |
36 | ||
37 | #[test] | |
38 | #[should_panic] | |
39 | fn flatten_twice() { | |
40 | #[derive(StructOpt, PartialEq, Debug)] | |
41 | struct Common { | |
42 | arg: i32, | |
43 | } | |
44 | ||
45 | #[derive(StructOpt, PartialEq, Debug)] | |
46 | struct Opt { | |
47 | #[structopt(flatten)] | |
48 | c1: Common, | |
49 | // Defines "arg" twice, so this should not work. | |
50 | #[structopt(flatten)] | |
51 | c2: Common, | |
52 | } | |
53 | Opt::from_iter(&["test", "42", "43"]); | |
54 | } | |
55 | ||
56 | #[test] | |
57 | fn flatten_in_subcommand() { | |
58 | #[derive(StructOpt, PartialEq, Debug)] | |
59 | struct Common { | |
60 | arg: i32, | |
61 | } | |
62 | ||
63 | #[derive(StructOpt, PartialEq, Debug)] | |
64 | struct Add { | |
65 | #[structopt(short)] | |
66 | interactive: bool, | |
67 | #[structopt(flatten)] | |
68 | common: Common, | |
69 | } | |
70 | ||
71 | #[derive(StructOpt, PartialEq, Debug)] | |
72 | enum Opt { | |
73 | Fetch { | |
74 | #[structopt(short)] | |
75 | all: bool, | |
76 | #[structopt(flatten)] | |
77 | common: Common, | |
78 | }, | |
79 | ||
80 | Add(Add), | |
81 | } | |
82 | ||
83 | assert_eq!( | |
84 | Opt::Fetch { | |
85 | all: false, | |
86 | common: Common { arg: 42 } | |
87 | }, | |
88 | Opt::from_iter(&["test", "fetch", "42"]) | |
89 | ); | |
90 | assert_eq!( | |
91 | Opt::Add(Add { | |
92 | interactive: true, | |
93 | common: Common { arg: 43 } | |
94 | }), | |
95 | Opt::from_iter(&["test", "add", "-i", "43"]) | |
96 | ); | |
97 | } | |
98 | ||
99 | #[test] | |
100 | fn merge_subcommands_with_flatten() { | |
101 | #[derive(StructOpt, PartialEq, Debug)] | |
102 | enum BaseCli { | |
103 | Command1(Command1), | |
104 | } | |
105 | ||
106 | #[derive(StructOpt, PartialEq, Debug)] | |
107 | struct Command1 { | |
108 | arg1: i32, | |
109 | } | |
110 | ||
111 | #[derive(StructOpt, PartialEq, Debug)] | |
112 | struct Command2 { | |
113 | arg2: i32, | |
114 | } | |
115 | ||
116 | #[derive(StructOpt, PartialEq, Debug)] | |
117 | enum Opt { | |
118 | #[structopt(flatten)] | |
119 | BaseCli(BaseCli), | |
120 | Command2(Command2), | |
121 | } | |
122 | ||
123 | assert_eq!( | |
124 | Opt::BaseCli(BaseCli::Command1(Command1 { arg1: 42 })), | |
125 | Opt::from_iter(&["test", "command1", "42"]) | |
126 | ); | |
127 | assert_eq!( | |
128 | Opt::Command2(Command2 { arg2: 43 }), | |
129 | Opt::from_iter(&["test", "command2", "43"]) | |
130 | ); | |
131 | } | |
132 | ||
133 | #[test] | |
134 | #[should_panic = "structopt misuse: You likely tried to #[flatten] a struct \ | |
135 | that contains #[subcommand]. This is forbidden."] | |
136 | fn subcommand_in_flatten() { | |
137 | #[derive(Debug, StructOpt)] | |
138 | pub enum Struct1 { | |
139 | #[structopt(flatten)] | |
140 | Struct1(Struct2), | |
141 | } | |
142 | ||
143 | #[derive(Debug, StructOpt)] | |
144 | pub struct Struct2 { | |
145 | #[structopt(subcommand)] | |
146 | command_type: Enum3, | |
147 | } | |
148 | ||
149 | #[derive(Debug, StructOpt)] | |
150 | pub enum Enum3 { | |
151 | Command { args: Vec<String> }, | |
152 | } | |
153 | ||
154 | Struct1::from_iter(&["test", "command", "foo"]); | |
155 | } | |
156 | ||
157 | #[test] | |
158 | fn flatten_doc_comment() { | |
159 | #[derive(StructOpt, PartialEq, Debug)] | |
160 | struct Common { | |
161 | /// This is an arg. Arg means "argument". Command line argument. | |
162 | arg: i32, | |
163 | } | |
164 | ||
165 | #[derive(StructOpt, PartialEq, Debug)] | |
166 | struct Opt { | |
167 | /// The very important comment that clippy had me put here. | |
168 | /// It knows better. | |
169 | #[structopt(flatten)] | |
170 | common: Common, | |
171 | } | |
172 | assert_eq!( | |
173 | Opt { | |
174 | common: Common { arg: 42 } | |
175 | }, | |
176 | Opt::from_iter(&["test", "42"]) | |
177 | ); | |
178 | ||
179 | let help = utils::get_help::<Opt>(); | |
180 | assert!(help.contains("This is an arg.")); | |
181 | assert!(!help.contains("The very important")); | |
182 | } |