Skip to content

Commit df840d4

Browse files
committed
docs: Give examples with predicates
1 parent e089a32 commit df840d4

File tree

2 files changed

+110
-9
lines changed

2 files changed

+110
-9
lines changed

src/assert.rs

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,29 @@ impl Assert {
204204
///
205205
/// # Examples
206206
///
207-
/// ```rust
207+
/// ```rust,ignore
208208
/// use assert_cmd::prelude::*;
209209
///
210210
/// use std::process::Command;
211+
/// use predicates::prelude::*;
212+
///
213+
/// Command::main_binary()
214+
/// .unwrap()
215+
/// .env("exit", "42")
216+
/// .assert()
217+
/// .code(predicate::eq(42));
211218
///
219+
/// // which can be shortened to:
212220
/// Command::main_binary()
213221
/// .unwrap()
214222
/// .env("exit", "42")
215223
/// .assert()
216224
/// .code(42);
217225
/// ```
226+
///
227+
/// See [`IntoCodePredicate`] for other built-in conversions.
228+
///
229+
/// [IntoCodePredicate]: trait.IntoCodePredicate.html
218230
pub fn code<I, P>(self, pred: I) -> Self
219231
where
220232
I: IntoCodePredicate<P>,
@@ -241,18 +253,31 @@ impl Assert {
241253
///
242254
/// # Examples
243255
///
244-
/// ```rust
256+
/// ```rust,ignore
245257
/// use assert_cmd::prelude::*;
246258
///
247259
/// use std::process::Command;
260+
/// use predicates::prelude::*;
248261
///
249262
/// Command::main_binary()
250263
/// .unwrap()
251264
/// .env("stdout", "hello")
252265
/// .env("stderr", "world")
253266
/// .assert()
267+
/// .stdout(predicate::str::similar("hello\n").from_utf8());
268+
///
269+
/// // which can be shortened to:
270+
/// Command::main_binary()
271+
/// .unwrap()
272+
/// .env("stdout", "hello")
273+
/// .env("stderr", "world")
274+
/// .assert()
254275
/// .stdout("hello\n");
255276
/// ```
277+
///
278+
/// See [`IntoOutputPredicate`] for other built-in conversions.
279+
///
280+
/// [IntoOutputPredicate]: trait.IntoOutputPredicate.html
256281
pub fn stdout<I, P>(self, pred: I) -> Self
257282
where
258283
I: IntoOutputPredicate<P>,
@@ -275,18 +300,31 @@ impl Assert {
275300
///
276301
/// # Examples
277302
///
278-
/// ```rust
303+
/// ```rust,ignore
279304
/// use assert_cmd::prelude::*;
280305
///
281306
/// use std::process::Command;
307+
/// use predicates::prelude::*;
282308
///
283309
/// Command::main_binary()
284310
/// .unwrap()
285311
/// .env("stdout", "hello")
286312
/// .env("stderr", "world")
287313
/// .assert()
314+
/// .stderr(predicate::str::similar("world\n").from_utf8());
315+
///
316+
/// // which can be shortened to:
317+
/// Command::main_binary()
318+
/// .unwrap()
319+
/// .env("stdout", "hello")
320+
/// .env("stderr", "world")
321+
/// .assert()
288322
/// .stderr("world\n");
289323
/// ```
324+
///
325+
/// See [`IntoOutputPredicate`] for other built-in conversions.
326+
///
327+
/// [IntoOutputPredicate]: trait.IntoOutputPredicate.html
290328
pub fn stderr<I, P>(self, pred: I) -> Self
291329
where
292330
I: IntoOutputPredicate<P>,
@@ -332,18 +370,20 @@ impl fmt::Debug for Assert {
332370
/// use assert_cmd::prelude::*;
333371
///
334372
/// use std::process::Command;
373+
/// use predicates::prelude::*;
335374
///
336375
/// Command::main_binary()
337376
/// .unwrap()
338377
/// .env("exit", "42")
339378
/// .assert()
340-
/// .code(42);
341-
/// // which is equivalent to
379+
/// .code(predicate::eq(42));
380+
///
381+
/// // which can be shortened to:
342382
/// Command::main_binary()
343383
/// .unwrap()
344384
/// .env("exit", "42")
345385
/// .assert()
346-
/// .code(predicates::ord::eq(42));
386+
/// .code(42);
347387
/// ```
348388
///
349389
/// [`Assert::code`]: struct.Assert.html#method.code
@@ -397,6 +437,30 @@ impl IntoCodePredicate<predicates::iter::InPredicate<i32>> for &'static [i32] {
397437
/// Used by [`Assert::stdout`] and [`Assert::stderr`] to convert Self
398438
/// into the needed [`Predicate<[u8]>`].
399439
///
440+
/// # Examples
441+
///
442+
/// ```rust,ignore
443+
/// use assert_cmd::prelude::*;
444+
///
445+
/// use std::process::Command;
446+
/// use predicates::prelude::*;
447+
///
448+
/// Command::main_binary()
449+
/// .unwrap()
450+
/// .env("stdout", "hello")
451+
/// .env("stderr", "world")
452+
/// .assert()
453+
/// .stdout(predicate::str::similar("hello\n").from_utf8());
454+
///
455+
/// // which can be shortened to:
456+
/// Command::main_binary()
457+
/// .unwrap()
458+
/// .env("stdout", "hello")
459+
/// .env("stderr", "world")
460+
/// .assert()
461+
/// .stdout("hello\n");
462+
/// ```
463+
///
400464
/// [`Assert::stdout`]: struct.Assert.html#method.stdout
401465
/// [`Assert::stderr`]: struct.Assert.html#method.stderr
402466
/// [`Predicate<[u8]>`]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html

tests/assert.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,48 @@ fn code_example() {
1212
.unwrap()
1313
.env("exit", "42")
1414
.assert()
15-
.code(42);
16-
// which is equivalent to
15+
.code(predicate::eq(42));
16+
17+
// which can be shortened to:
1718
Command::main_binary()
1819
.unwrap()
1920
.env("exit", "42")
2021
.assert()
21-
.code(predicate::eq(42));
22+
.code(42);
23+
}
24+
25+
#[test]
26+
fn stdout_example() {
27+
Command::main_binary()
28+
.unwrap()
29+
.env("stdout", "hello")
30+
.env("stderr", "world")
31+
.assert()
32+
.stdout(predicate::str::similar("hello\n").from_utf8());
33+
34+
// which can be shortened to:
35+
Command::main_binary()
36+
.unwrap()
37+
.env("stdout", "hello")
38+
.env("stderr", "world")
39+
.assert()
40+
.stdout("hello\n");
41+
}
42+
43+
#[test]
44+
fn stderr_example() {
45+
Command::main_binary()
46+
.unwrap()
47+
.env("stdout", "hello")
48+
.env("stderr", "world")
49+
.assert()
50+
.stderr(predicate::str::similar("world\n").from_utf8());
51+
52+
// which can be shortened to:
53+
Command::main_binary()
54+
.unwrap()
55+
.env("stdout", "hello")
56+
.env("stderr", "world")
57+
.assert()
58+
.stderr("world\n");
2259
}

0 commit comments

Comments
 (0)