@@ -114,52 +114,151 @@ impl Assert {
114
114
}
115
115
116
116
/// Use custom command
117
+ ///
118
+ /// # Examples
119
+ ///
120
+ /// ```rust
121
+ /// extern crate assert_cli;
122
+ ///
123
+ /// assert_cli::Assert::command(&["echo", "1337"])
124
+ /// .succeeds()
125
+ /// .unwrap();
126
+ /// ```
117
127
pub fn command ( cmd : & [ & str ] ) -> Self {
118
128
Assert {
119
129
cmd : cmd. into_iter ( ) . cloned ( ) . map ( String :: from) . collect ( ) ,
120
130
..Self :: default ( )
121
131
}
122
132
}
123
133
134
+ /// Add arguments to the command
135
+ ///
136
+ /// # Examples
137
+ ///
138
+ /// ```rust
139
+ /// extern crate assert_cli;
140
+ ///
141
+ /// assert_cli::Assert::command(&["echo"])
142
+ /// .with_args(&["42"])
143
+ /// .succeeds()
144
+ /// .prints("42")
145
+ /// .unwrap();
146
+ /// ```
147
+ pub fn with_args ( mut self , args : & [ & str ] ) -> Self {
148
+ self . cmd . extend ( args. into_iter ( ) . cloned ( ) . map ( String :: from) ) ;
149
+ self
150
+ }
151
+
124
152
/// Small helper to make chains more readable
153
+ ///
154
+ /// # Examples
155
+ ///
156
+ /// ```rust
157
+ /// extern crate assert_cli;
158
+ ///
159
+ /// assert_cli::Assert::command(&["echo", "42"])
160
+ /// .succeeds().and().prints("42")
161
+ /// .unwrap();
162
+ /// ```
125
163
pub fn and ( self ) -> Self {
126
164
self
127
165
}
128
166
129
167
/// Expect the command to be executed successfully
168
+ ///
169
+ /// # Examples
170
+ ///
171
+ /// ```rust
172
+ /// extern crate assert_cli;
173
+ ///
174
+ /// assert_cli::Assert::command(&["echo", "42"])
175
+ /// .succeeds()
176
+ /// .unwrap();
177
+ /// ```
130
178
pub fn succeeds ( mut self ) -> Self {
131
179
self . expect_success = true ;
132
180
self
133
181
}
134
182
135
183
/// Expect the command to fail
184
+ ///
185
+ /// # Examples
186
+ ///
187
+ /// ```rust
188
+ /// extern crate assert_cli;
189
+ ///
190
+ /// assert_cli::Assert::command(&["cat", "non-exisiting-file"])
191
+ /// .fails()
192
+ /// .unwrap();
193
+ /// ```
136
194
pub fn fails ( mut self ) -> Self {
137
195
self . expect_success = false ;
138
196
self
139
197
}
140
198
141
199
/// Expect the command to fail and return a specific error code
200
+ ///
201
+ /// # Examples
202
+ ///
203
+ /// ```rust
204
+ /// extern crate assert_cli;
205
+ ///
206
+ /// assert_cli::Assert::command(&["cat", "non-exisiting-file"])
207
+ /// .fails_with(1)
208
+ /// .unwrap();
209
+ /// ```
142
210
pub fn fails_with ( mut self , expect_exit_code : i32 ) -> Self {
143
211
self . expect_success = false ;
144
212
self . expect_exit_code = Some ( expect_exit_code) ;
145
213
self
146
214
}
147
215
148
216
/// Expect the command's output to contain `output`
217
+ ///
218
+ /// # Examples
219
+ ///
220
+ /// ```rust
221
+ /// extern crate assert_cli;
222
+ ///
223
+ /// assert_cli::Assert::command(&["echo", "42"])
224
+ /// .prints("42")
225
+ /// .unwrap();
226
+ /// ```
149
227
pub fn prints < O : Into < String > > ( mut self , output : O ) -> Self {
150
228
self . expect_output = Some ( output. into ( ) ) ;
151
229
self . fuzzy = true ;
152
230
self
153
231
}
154
232
155
233
/// Expect the command to output exactly this `output`
234
+ ///
235
+ /// # Examples
236
+ ///
237
+ /// ```rust
238
+ /// extern crate assert_cli;
239
+ ///
240
+ /// assert_cli::Assert::command(&["echo", "42"])
241
+ /// .prints_exactly("42")
242
+ /// .unwrap();
243
+ /// ```
156
244
pub fn prints_exactly < O : Into < String > > ( mut self , output : O ) -> Self {
157
245
self . expect_output = Some ( output. into ( ) ) ;
158
246
self . fuzzy = false ;
159
247
self
160
248
}
161
249
162
250
/// Execute the command and check the assertions
251
+ ///
252
+ /// # Examples
253
+ ///
254
+ /// ```rust
255
+ /// extern crate assert_cli;
256
+ ///
257
+ /// let test = assert_cli::Assert::command(&["echo", "42"])
258
+ /// .succeeds()
259
+ /// .execute();
260
+ /// assert!(test.is_ok());
261
+ /// ```
163
262
pub fn execute ( self ) -> Result < ( ) > {
164
263
let cmd = & self . cmd [ 0 ] ;
165
264
let args: Vec < _ > = self . cmd . iter ( ) . skip ( 1 ) . collect ( ) ;
@@ -206,6 +305,16 @@ impl Assert {
206
305
}
207
306
208
307
/// Execute the command, check the assertions, and panic when they fail
308
+ ///
309
+ /// # Examples
310
+ ///
311
+ /// ```rust,should_panic="Assert CLI failure"
312
+ /// extern crate assert_cli;
313
+ ///
314
+ /// assert_cli::Assert::command(&["echo", "42"])
315
+ /// .fails()
316
+ /// .unwrap(); // panics
317
+ /// ```
209
318
pub fn unwrap ( self ) {
210
319
if let Err ( err) = self . execute ( ) {
211
320
panic ! ( "Assert CLI failure:\n {}" , err) ;
@@ -238,6 +347,10 @@ impl Assert {
238
347
/// .unwrap();
239
348
/// # }
240
349
/// ```
350
+ ///
351
+ /// The macro will try to convert its arguments as strings, but is limited by
352
+ /// Rust's default tokenizer, e.g., you always need to quote CLI arguments
353
+ /// like `"--verbose"`.
241
354
#[ macro_export]
242
355
macro_rules! assert_cmd {
243
356
( $( $x: tt) +) => { {
0 commit comments