Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find ways to better help users #55

Merged
merged 3 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 196 additions & 35 deletions src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ use cmd::output_fmt;
///
/// use std::process::Command;
///
/// Command::main_binary()
/// .unwrap()
/// .assert()
/// .success();
/// let mut cmd = Command::main_binary()
/// .unwrap();
/// cmd.assert()
/// .success();
/// ```
///
/// [`Output`]: https://doc.rust-lang.org/std/process/struct.Output.html
Expand All @@ -40,10 +40,10 @@ pub trait OutputAssertExt {
///
/// use std::process::Command;
///
/// Command::main_binary()
/// .unwrap()
/// .assert()
/// .success();
/// let mut cmd = Command::main_binary()
/// .unwrap();
/// cmd.assert()
/// .success();
/// ```
///
/// [`Output`]: https://doc.rust-lang.org/std/process/struct.Output.html
Expand Down Expand Up @@ -74,10 +74,10 @@ impl<'c> OutputAssertExt for &'c mut process::Command {
///
/// use std::process::Command;
///
/// Command::main_binary()
/// .unwrap()
/// .assert()
/// .success();
/// let mut cmd = Command::main_binary()
/// .unwrap();
/// cmd.assert()
/// .success();
/// ```
///
/// [`Output`]: https://doc.rust-lang.org/std/process/struct.Output.html
Expand Down Expand Up @@ -195,6 +195,7 @@ impl Assert {
///
/// # Examples
///
/// Accepting a predicate:
/// ```rust
/// extern crate assert_cmd;
/// extern crate predicates;
Expand All @@ -211,7 +212,7 @@ impl Assert {
/// .code(predicate::eq(42));
/// ```
///
/// Shortcuts are also provided:
/// Accepting an exit code:
/// ```rust
/// use assert_cmd::prelude::*;
///
Expand All @@ -224,10 +225,23 @@ impl Assert {
/// .code(42);
/// ```
///
/// Accepting multiple exit codes:
/// ```rust
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// Command::main_binary()
/// .unwrap()
/// .env("exit", "42")
/// .assert()
/// .code(&[2, 42] as &[i32]);
/// ```
///
/// - See [`predicates::prelude`] for more predicates.
/// - See [`IntoCodePredicate`] for other built-in conversions.
///
/// [`predicates::prelude`]: https://docs.rs/predicates/0.9.0/predicates/prelude/
/// [`predicates::prelude`]: https://docs.rs/predicates/1.0.0/predicates/prelude/
/// [`IntoCodePredicate`]: trait.IntoCodePredicate.html
pub fn code<I, P>(self, pred: I) -> Self
where
Expand All @@ -251,8 +265,17 @@ impl Assert {

/// Ensure the command wrote the expected data to `stdout`.
///
/// This uses [`IntoOutputPredicate`] to provide short-hands for common cases.
///
/// - See [`predicates::prelude`] for more predicates.
/// - See [`IntoOutputPredicate`] for other built-in conversions.
///
/// [`predicates::prelude`]: https://docs.rs/predicates/1.0.0/predicates/prelude/
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
///
/// # Examples
///
/// Accepting a bytes predicate:
/// ```rust
/// extern crate assert_cmd;
/// extern crate predicates;
Expand All @@ -267,28 +290,54 @@ impl Assert {
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stdout(predicate::str::similar("hello\n").from_utf8());
/// .stdout(predicate::eq(b"hello\n" as &[u8]));
/// ```
///
/// Shortcuts are also provided:
/// Accepting a `str` predicate:
/// ```rust
/// extern crate assert_cmd;
/// extern crate predicates;
///
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
/// use predicates::prelude::*;
///
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stdout("hello\n");
/// .stdout(predicate::str::similar("hello\n"));
/// ```
///
/// - See [`predicates::prelude`] for more predicates.
/// - See [`IntoOutputPredicate`] for other built-in conversions.
/// Accepting bytes:
/// ```rust
/// use assert_cmd::prelude::*;
///
/// [`predicates::prelude`]: https://docs.rs/predicates/0.9.0/predicates/prelude/
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
/// use std::process::Command;
///
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stdout(b"hello\n" as &[u8]);
/// ```
///
/// Accepting a `str`:
/// ```rust
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stdout("hello\n");
/// ```
pub fn stdout<I, P>(self, pred: I) -> Self
where
I: IntoOutputPredicate<P>,
Expand All @@ -309,8 +358,17 @@ impl Assert {

/// Ensure the command wrote the expected data to `stderr`.
///
/// This uses [`IntoOutputPredicate`] to provide short-hands for common cases.
///
/// - See [`predicates::prelude`] for more predicates.
/// - See [`IntoOutputPredicate`] for other built-in conversions.
///
/// [`predicates::prelude`]: https://docs.rs/predicates/1.0.0/predicates/prelude/
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
///
/// # Examples
///
/// Accepting a bytes predicate:
/// ```rust
/// extern crate assert_cmd;
/// extern crate predicates;
Expand All @@ -325,28 +383,54 @@ impl Assert {
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stderr(predicate::str::similar("world\n").from_utf8());
/// .stderr(predicate::eq(b"world\n" as &[u8]));
/// ```
///
/// Shortcuts are also provided:
/// Accepting a `str` predicate:
/// ```rust
/// extern crate assert_cmd;
/// extern crate predicates;
///
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
/// use predicates::prelude::*;
///
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stderr("world\n");
/// .stderr(predicate::str::similar("world\n"));
/// ```
///
/// - See [`predicates::prelude`] for more predicates.
/// - See [`IntoOutputPredicate`] for other built-in conversions.
/// Accepting bytes:
/// ```rust
/// use assert_cmd::prelude::*;
///
/// [`predicates::prelude`]: https://docs.rs/predicates/0.9.0/predicates/prelude/
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
/// use std::process::Command;
///
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stderr(b"world\n" as &[u8]);
/// ```
///
/// Accepting a `str`:
/// ```rust
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stderr("world\n");
/// ```
pub fn stderr<I, P>(self, pred: I) -> Self
where
I: IntoOutputPredicate<P>,
Expand Down Expand Up @@ -412,7 +496,7 @@ impl fmt::Debug for Assert {
/// ```
///
/// [`Assert::code`]: struct.Assert.html#method.code
/// [`Predicate<i32>`]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
/// [`Predicate<i32>`]: https://docs.rs/predicates-core/1.0.0/predicates_core/trait.Predicate.html
pub trait IntoCodePredicate<P>
where
P: predicates_core::Predicate<i32>,
Expand All @@ -438,8 +522,22 @@ where
// Keep `predicates` concrete Predicates out of our public API.
/// [Predicate] used by [`IntoCodePredicate`] for code.
///
/// # Example
///
/// ```rust
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// Command::main_binary()
/// .unwrap()
/// .env("exit", "42")
/// .assert()
/// .code(42);
/// ```
///
/// [`IntoCodePredicate`]: trait.IntoCodePredicate.html
/// [Predicate]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
/// [Predicate]: https://docs.rs/predicates-core/1.0.0/predicates_core/trait.Predicate.html
#[derive(Debug)]
pub struct EqCodePredicate(predicates::ord::EqPredicate<i32>);

Expand Down Expand Up @@ -494,8 +592,22 @@ impl IntoCodePredicate<EqCodePredicate> for i32 {
// Keep `predicates` concrete Predicates out of our public API.
/// [Predicate] used by [`IntoCodePredicate`] for iterables of codes.
///
/// # Example
///
/// ```rust
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// Command::main_binary()
/// .unwrap()
/// .env("exit", "42")
/// .assert()
/// .code(&[2, 42] as &[i32]);
/// ```
///
/// [`IntoCodePredicate`]: trait.IntoCodePredicate.html
/// [Predicate]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
/// [Predicate]: https://docs.rs/predicates-core/1.0.0/predicates_core/trait.Predicate.html
#[derive(Debug)]
pub struct InCodePredicate(predicates::iter::InPredicate<i32>);

Expand Down Expand Up @@ -587,7 +699,7 @@ impl IntoCodePredicate<InCodePredicate> for &'static [i32] {
///
/// [`Assert::stdout`]: struct.Assert.html#method.stdout
/// [`Assert::stderr`]: struct.Assert.html#method.stderr
/// [`Predicate<[u8]>`]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
/// [`Predicate<[u8]>`]: https://docs.rs/predicates-core/1.0.0/predicates_core/trait.Predicate.html
pub trait IntoOutputPredicate<P>
where
P: predicates_core::Predicate<[u8]>,
Expand All @@ -613,8 +725,23 @@ where
// Keep `predicates` concrete Predicates out of our public API.
/// [Predicate] used by [`IntoOutputPredicate`] for bytes.
///
/// # Example
///
/// ```rust
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stderr(b"world\n" as &[u8]);
/// ```
///
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
/// [Predicate]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
/// [Predicate]: https://docs.rs/predicates-core/1.0.0/predicates_core/trait.Predicate.html
#[derive(Debug)]
pub struct BytesContentOutputPredicate(predicates::ord::EqPredicate<&'static [u8]>);

Expand Down Expand Up @@ -669,8 +796,23 @@ impl IntoOutputPredicate<BytesContentOutputPredicate> for &'static [u8] {
// Keep `predicates` concrete Predicates out of our public API.
/// [Predicate] used by [`IntoOutputPredicate`] for [`str`].
///
/// # Example
///
/// ```rust
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stderr("world\n");
/// ```
///
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
/// [Predicate]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
/// [Predicate]: https://docs.rs/predicates-core/1.0.0/predicates_core/trait.Predicate.html
/// [`str`]: https://doc.rust-lang.org/std/primitive.str.html
#[derive(Debug, Clone)]
pub struct StrContentOutputPredicate(
Expand Down Expand Up @@ -728,8 +870,27 @@ impl IntoOutputPredicate<StrContentOutputPredicate> for &'static str {
// Keep `predicates` concrete Predicates out of our public API.
/// [Predicate] used by [`IntoOutputPredicate`] for [`Predicate<str>`].
///
/// # Example
///
/// ```rust
/// extern crate assert_cmd;
/// extern crate predicates;
///
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
/// use predicates::prelude::*;
///
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stderr(predicate::str::similar("world\n"));
/// ```
///
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
/// [Predicate]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
/// [Predicate]: https://docs.rs/predicates-core/1.0.0/predicates_core/trait.Predicate.html
#[derive(Debug, Clone)]
pub struct StrOutputPredicate<P: predicates_core::Predicate<str>>(
predicates::str::Utf8Predicate<P>,
Expand Down
Loading