use serde_json;
use std::borrow::Cow;
#[macro_export]
macro_rules! assert_cmd {
($($x:tt)+) => {{
$(__assert_single_token_expression!(@CHECK $x);)*
$crate::Assert::command(
&[$(
$crate::flatten_escaped_string(stringify!($x)).as_ref()
),*]
)
}}
}
#[doc(hidden)]
fn deserialize_json_string(x: &str) -> String {
serde_json::from_str(x).expect(&format!("Unable to deserialize `{:?}` as string.", x))
}
#[doc(hidden)]
pub fn flatten_escaped_string(x: &str) -> Cow<str> {
if x.starts_with('"') && x.ends_with('"') {
Cow::Owned(deserialize_json_string(x))
} else {
Cow::Borrowed(x)
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! __assert_single_token_expression {
(@CHECK {$( $x:tt )*}) => { assert_cmd!(@DENY {$( $x )*}) };
(@CHECK ($( $x:tt )*)) => { assert_cmd!(@DENY {$( $x )*}) };
(@CHECK [$( $x:tt )*]) => { assert_cmd!(@DENY {$( $x )*}) };
(@CHECK $x:expr) => { };
(@DENY) => { };
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn flatten_unquoted() {
assert_eq!(flatten_escaped_string("hello world"), "hello world");
}
#[test]
fn flatten_quoted() {
assert_eq!(flatten_escaped_string(r#""hello world""#), "hello world");
}
#[test]
fn flatten_escaped() {
assert_eq!(
flatten_escaped_string(r#""hello world \u0042 A""#),
"hello world B A"
);
}
}