|
| 1 | +use std::borrow::Cow; |
| 2 | +use rustc_serialize::json::Json; |
| 3 | + |
| 4 | +/// Easily construct an `Assert` with a custom command. |
| 5 | +/// |
| 6 | +/// Make sure to include the crate as `#[macro_use] extern crate assert_cli;` if |
| 7 | +/// you want to use this macro. |
| 8 | +/// |
| 9 | +/// # Examples |
| 10 | +/// |
| 11 | +/// To test that our very complex cli applications succeeds and prints some |
| 12 | +/// text to stdout that contains |
| 13 | +/// |
| 14 | +/// ```plain |
| 15 | +/// No errors whatsoever |
| 16 | +/// ``` |
| 17 | +/// |
| 18 | +/// ..., you would call it like this: |
| 19 | +/// |
| 20 | +/// ```rust |
| 21 | +/// #[macro_use] extern crate assert_cli; |
| 22 | +/// # fn main() { |
| 23 | +/// assert_cmd!(echo "Launch sequence initiated.\nNo errors whatsoever!\n") |
| 24 | +/// .succeeds() |
| 25 | +/// .prints("No errors whatsoever") |
| 26 | +/// .unwrap(); |
| 27 | +/// # } |
| 28 | +/// ``` |
| 29 | +/// |
| 30 | +/// The macro will try to convert its arguments as strings, but is limited by |
| 31 | +/// Rust's default tokenizer, e.g., you always need to quote CLI arguments |
| 32 | +/// like `"--verbose"`. |
| 33 | +#[macro_export] |
| 34 | +macro_rules! assert_cmd { |
| 35 | + ($($x:tt)+) => {{ |
| 36 | + $(__assert_single_token_expression!(@CHECK $x);)* |
| 37 | + |
| 38 | + $crate::Assert::command( |
| 39 | + &[$( |
| 40 | + $crate::flatten_escaped_string(stringify!($x)).as_ref() |
| 41 | + ),*] |
| 42 | + ) |
| 43 | + }} |
| 44 | +} |
| 45 | + |
| 46 | +/// Deserialize a JSON-encoded `String`. |
| 47 | +/// |
| 48 | +/// # Panics |
| 49 | +/// |
| 50 | +/// If `x` can not be decoded as `String`. |
| 51 | +#[doc(hidden)] |
| 52 | +fn deserialize_json_string(x: &str) -> String { |
| 53 | + match Json::from_str(x).expect(&format!("Unable to deserialize `{:?}` as string.", x)) { |
| 54 | + Json::String(deserialized) => deserialized, |
| 55 | + _ => panic!("Unable to deserialize `{:?}` as string.", x), |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// Deserialize a JSON-encoded `String`. |
| 60 | +/// |
| 61 | +/// # Panics |
| 62 | +/// |
| 63 | +/// If `x` can not be decoded as `String`. |
| 64 | +#[doc(hidden)] |
| 65 | +pub fn flatten_escaped_string(x: &str) -> Cow<str> { |
| 66 | + if x.starts_with('"') && x.ends_with('"') { |
| 67 | + Cow::Owned(deserialize_json_string(x)) |
| 68 | + } else { |
| 69 | + Cow::Borrowed(x) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// Inspect a single token and decide if it is safe to `stringify!`, without loosing |
| 74 | +/// information about whitespaces, to address https://github.com/killercup/assert_cli/issues/22. |
| 75 | +/// |
| 76 | +/// Call like `__assert_single_token_expression!(@CHECK x)`, where `x` can be any token to check. |
| 77 | +/// |
| 78 | +/// This macro will only accept single tokens, which parse as expressions, e.g. |
| 79 | +/// - strings "foo", r#"foo" |
| 80 | +/// - idents `foo`, `foo42` |
| 81 | +/// - numbers `42` |
| 82 | +/// - chars `'a'` |
| 83 | +/// |
| 84 | +/// Delimited token trees `{...}` and the like are rejected. Everything thats not an expression |
| 85 | +/// will also be rejected. |
| 86 | +#[doc(hidden)] |
| 87 | +#[macro_export] |
| 88 | +macro_rules! __assert_single_token_expression { |
| 89 | + // deny `{...}` |
| 90 | + (@CHECK {$( $x:tt )*}) => { assert_cmd!(@DENY {$( $x )*}) }; |
| 91 | + // deny `(...)` |
| 92 | + (@CHECK ($( $x:tt )*)) => { assert_cmd!(@DENY {$( $x )*}) }; |
| 93 | + // deny `[...]` |
| 94 | + (@CHECK [$( $x:tt )*]) => { assert_cmd!(@DENY {$( $x )*}) }; |
| 95 | + // only allow tokens that parse as expression |
| 96 | + (@CHECK $x:expr) => { }; |
| 97 | + // little helper |
| 98 | + (@DENY) => { }; |
| 99 | +} |
0 commit comments