libpt_core/
macros.rs

1//! # common macros for `libpt`
2//!
3//! This module implements macros for use with `libpt`.
4
5pub use crate::get_stdout_for;
6
7/// ## catches what the expression would write to the `stdout`
8///
9/// This macro takes an expression, executes it, and catches what it would write to the stdout.
10/// The buffer of the stdout will then be returned for further use.
11///
12/// This is especially useful when testing loggers or other frontend CLI functions.
13#[macro_export]
14macro_rules! get_stdout_for {
15    ($test:expr) => {{
16        use gag::BufferRedirect;
17        use std::io::Read;
18
19        let mut buf = BufferRedirect::stdout().unwrap();
20
21        $test;
22
23        let mut output = String::new();
24        buf.read_to_string(&mut output).unwrap();
25        drop(buf);
26
27        output
28    }};
29}