1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
/// Assert a command stdout string is equal to a target string.
///
/// * When true, return `Ok(())`.
///
/// * Otherwise, return [`Err`] with a message and the values of the
///   expressions with their debug representations.
///
/// # Examples
///
/// ```rust
/// # #[macro_use] extern crate assertables;
/// use std::process::Command;
/// 
/// # fn main() {
/// let mut a = Command::new("printf");
/// a.args(["%s", "hello"]);
/// let x = assertable_command_stdout_eq_str!(a, "hello");
/// //-> Ok(())
/// assert_eq!(x.unwrap(), ());
///
/// let mut a = Command::new("printf");
/// a.args(["%s", "hello"]);
/// let x = assertable_command_stdout_eq_str!(a, "world");
/// //-> Err!("…")
/// // assertable failed: `assertable_command_stdout_eq_str!(command, expect)`
/// //  command program: `\"printf\"`,
/// //  stdout: `\"hello\"`,
/// //  expect: `\"world\"`
/// # assert_eq!(x.unwrap_err(), "assertable failed: `assertable_command_stdout_eq_str!(command, expect)`\n command program: `\"printf\"`,\n stdout: `\"hello\"`,\n expect: `\"world\"`");
/// # }
/// ```
///
/// This macro has a second form where a custom message can be provided.
#[macro_export]
macro_rules! assertable_command_stdout_eq_str {
    ($command:expr, $expect:expr $(,)?) => ({
        match $command.output() {
            Ok(output) => {
                let actual = String::from_utf8(output.stdout).unwrap();
                if actual == $expect {
                    Ok(())
                } else {
                    Err(format!("assertable failed: `assertable_command_stdout_eq_str!(command, expect)`\n command program: `{:?}`,\n stdout: `{:?}`,\n expect: `{:?}`", $command.get_program(), actual, $expect))
                }
            }
            Err(err) => {
                Err(format!("assertable failed: `assertable_command_stdout_eq_str!(command, expect)`\n command program: `{:?}`,\n expect: `{:?}`,\n err: {:?}", $command.get_program(), $expect, err))
            }
        }
    });
    ($command:expr, $expect:expr, $($arg:tt)+) => ({
        match $command.output() {
            Ok(output) => {
                let actual = String::from_utf8(output.stdout).unwrap();
                if actual == $expect {
                    Ok(())
                } else {
                    Err($($arg)+)
                }
            }
            Err(_) => {
                Err($($arg)+)
            }
        }
    });
}

#[cfg(test)]
mod tests {

    use std::process::Command;

    #[test]
    fn asserterable_command_stdout_eq_str_x_arity_2_success() {
        let mut a = Command::new("printf");
        a.args(["%s", "alpha"]);
        let expect = "alpha";
        let x = assertable_command_stdout_eq_str!(a, expect);
        assert_eq!(x.unwrap(), ());
    }

    #[test]
    fn asserterable_command_stdout_eq_str_x_arity_2_failure() {
        let mut a = Command::new("printf");
        a.args(["%s", "alpha"]);
        let expect = "bravo";
        let x = assertable_command_stdout_eq_str!(a, expect);
        assert_eq!(x.unwrap_err(), "assertable failed: `assertable_command_stdout_eq_str!(command, expect)`\n command program: `\"printf\"`,\n stdout: `\"alpha\"`,\n expect: `\"bravo\"`");
    }

    #[test]
    fn asserterable_command_stdout_eq_str_x_arity_3_success() {
        let mut a = Command::new("printf");
        a.args(["%s", "alpha"]);
        let expect = "alpha";
        let x = assertable_command_stdout_eq_str!(a, expect, "message");
        assert_eq!(x.unwrap(), ());
    }

    #[test]
    fn asserterable_command_stdout_eq_str_x_arity_3_failure() {
        let mut a = Command::new("printf");
        a.args(["%s", "alpha"]);
        let expect = "bravo";
        let x = assertable_command_stdout_eq_str!(a, expect, "message");
        assert_eq!(x.unwrap_err(), "message");
    }

}