Skip to content
This repository was archived by the owner on Dec 29, 2021. It is now read-only.

Commit 9900f41

Browse files
committed
More docs
1 parent ff3bc75 commit 9900f41

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ after_success:
2727
notifications:
2828
email:
2929
on_success: never
30+
branches:
31+
only:
32+
- master
3033
env:
3134
global:
3235
- TRAVIS_CARGO_NIGHTLY_FEATURE=

src/lib.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,52 +114,151 @@ impl Assert {
114114
}
115115

116116
/// 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+
/// ```
117127
pub fn command(cmd: &[&str]) -> Self {
118128
Assert {
119129
cmd: cmd.into_iter().cloned().map(String::from).collect(),
120130
..Self::default()
121131
}
122132
}
123133

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+
124152
/// 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+
/// ```
125163
pub fn and(self) -> Self {
126164
self
127165
}
128166

129167
/// 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+
/// ```
130178
pub fn succeeds(mut self) -> Self {
131179
self.expect_success = true;
132180
self
133181
}
134182

135183
/// 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+
/// ```
136194
pub fn fails(mut self) -> Self {
137195
self.expect_success = false;
138196
self
139197
}
140198

141199
/// 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+
/// ```
142210
pub fn fails_with(mut self, expect_exit_code: i32) -> Self {
143211
self.expect_success = false;
144212
self.expect_exit_code = Some(expect_exit_code);
145213
self
146214
}
147215

148216
/// 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+
/// ```
149227
pub fn prints<O: Into<String>>(mut self, output: O) -> Self {
150228
self.expect_output = Some(output.into());
151229
self.fuzzy = true;
152230
self
153231
}
154232

155233
/// 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+
/// ```
156244
pub fn prints_exactly<O: Into<String>>(mut self, output: O) -> Self {
157245
self.expect_output = Some(output.into());
158246
self.fuzzy = false;
159247
self
160248
}
161249

162250
/// 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+
/// ```
163262
pub fn execute(self) -> Result<()> {
164263
let cmd = &self.cmd[0];
165264
let args: Vec<_> = self.cmd.iter().skip(1).collect();
@@ -206,6 +305,16 @@ impl Assert {
206305
}
207306

208307
/// 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+
/// ```
209318
pub fn unwrap(self) {
210319
if let Err(err) = self.execute() {
211320
panic!("Assert CLI failure:\n{}", err);
@@ -238,6 +347,10 @@ impl Assert {
238347
/// .unwrap();
239348
/// # }
240349
/// ```
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"`.
241354
#[macro_export]
242355
macro_rules! assert_cmd {
243356
($($x:tt)+) => {{

tests/skeptic.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
#![allow(toplevel_ref_arg, useless_format)]
2-
31
include!(concat!(env!("OUT_DIR"), "/skeptic-tests.rs"));

0 commit comments

Comments
 (0)