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

Commit 0d31bed

Browse files
committed
assert_cli! Macro
1 parent a8650cf commit 0d31bed

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/lib.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
//! Here's a trivial example:
99
//!
1010
//! ```rust
11-
//! extern crate assert_cli;
11+
//! # extern crate assert_cli;
12+
//!
1213
//! assert_cli::assert_cli_output("echo", &["42"], "42").unwrap();
1314
//! ```
1415
//!
1516
//! And here is one that will fail:
1617
//!
1718
//! ```rust,should_panic
18-
//! extern crate assert_cli;
1919
//! assert_cli::assert_cli_output("echo", &["42"], "1337").unwrap();
2020
//! ```
2121
//!
@@ -25,6 +25,15 @@
2525
//! -1337
2626
//! +42
2727
//! ```
28+
//!
29+
//! Alternatively, you can use the `assert_cli!` macro:
30+
//!
31+
//! ```rust,ignore
32+
//! assert_cli!("echo 42" => Success, "42").unwrap();
33+
//! ```
34+
//!
35+
//! Make sure to include the crate as `#[macro_use] extern crate assert_cli;`.
36+
2837

2938
#![cfg_attr(feature = "dev", feature(plugin))]
3039
#![cfg_attr(feature = "dev", plugin(clippy))]
@@ -127,3 +136,25 @@ pub fn assert_cli_output_error<S>(cmd: &str,
127136
})
128137
.map_err(From::from)
129138
}
139+
140+
/// The `assert_cli!` macro combines the functionality of the other functions in this crate in one
141+
/// short macro.
142+
///
143+
/// ```rust,ignore
144+
/// assert_cli!("echo 42" => Success, "42").unwrap();
145+
/// assert_cli!("exit 11" => Error 11, "").unwrap();
146+
/// ```
147+
///
148+
/// Make sure to include the crate as `#[macro_use] extern crate assert_cli;`.
149+
#[macro_export]
150+
macro_rules! assert_cli {
151+
($cmd:expr, $args:expr => Success, $output:expr) => {{
152+
$crate::assert_cli_output($cmd, $args, $output)
153+
}};
154+
($cmd:expr, $args:expr => Error, $output:expr) => {{
155+
$crate::assert_cli_output_error($cmd, $args, None, $output)
156+
}};
157+
($cmd:expr, $args:expr => Error $err:expr, $output:expr) => {{
158+
$crate::assert_cli_output_error($cmd, $args, Some($err), $output)
159+
}};
160+
}

tests/macro.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#[macro_use]
2+
extern crate assert_cli;
3+
4+
fn test_helper(exit_code: i32, output: &str) -> Vec<String> {
5+
vec!["-c".into(),
6+
format!(r#"function test_helper() {{ >&2 echo $1; return {}; }}; test_helper "{}""#,
7+
exit_code,
8+
output)]
9+
}
10+
11+
#[test]
12+
fn assert_success() {
13+
assert_cli!("echo", &["42"] => Success, "42").unwrap();
14+
assert!(assert_cli!("echo", &["1"] => Success, "42").is_err());
15+
}
16+
17+
#[test]
18+
fn assert_failure() {
19+
assert_cli!("bash", &test_helper(66, "sorry, my bad") => Error, "sorry, my bad\n").unwrap();
20+
assert_cli!("bash", &test_helper(42, "error no 42") => Error 42, "error no 42\n").unwrap();
21+
22+
assert!(assert_cli!("echo", &["good"] => Error, "").is_err());
23+
assert!(assert_cli!("echo", &["good"] => Error 11, "").is_err());
24+
}

0 commit comments

Comments
 (0)