Using a helper macro vs helper function in tests



examples/macros/test-failure-report/src/lib.rs
pub fn calc(a: i32, b: i32) -> String {
    format!(
        r#"
    <html>
    <head>
    <title>Calc</title>
    </head>
    <body>
    <h1>Results</h1>
    <div id="add">{}</div>
    <div id="multiply">{}</div>
    </body>
    </html>
    "#,
        a * b,
        a + b
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_good() {
        let html = calc(2, 2);
        check_html_function(&html, "title", "Calc");
        check_html_function(&html, "h1", "Results");
        check_html_function(&html, "#add", "4");
        check_html_function(&html, "#multiply", "4");
        check_html_macro!(&html, "title", "Calc");
        check_html_macro!(&html, "h1", "Results");
        check_html_macro!(&html, "#add", "4");
        check_html_macro!(&html, "#multiply", "4");
    }

    #[test]
    fn test_bad_add_func() {
        let html = calc(3, 3);
        check_html_function(&html, "title", "Calc");
        check_html_function(&html, "#add", "6");
        check_html_function(&html, "h1", "Results");
    }

    #[test]
    fn test_bad_multiply_func() {
        let html = calc(3, 3);
        check_html_function(&html, "title", "Calc");
        check_html_function(&html, "#multiply", "9");
        check_html_function(&html, "h1", "Results");
    }

    #[test]
    fn test_bad_add_macro() {
        let html = calc(3, 3);
        check_html_macro!(&html, "title", "Calc");
        check_html_macro!(&html, "#add", "6");
        check_html_macro!(&html, "h1", "Results");
    }

    #[test]
    fn test_bad_multiply_macro() {
        let html = calc(3, 3);
        check_html_macro!(&html, "title", "Calc");
        check_html_macro!(&html, "#multiply", "9");
        check_html_macro!(&html, "h1", "Results");
    }

    fn check_html_function(html: &str, css_selector: &str, expected: &str) {
        let document = scraper::Html::parse_document(html);
        let selector = scraper::Selector::parse(css_selector).unwrap();
        assert_eq!(
            &document.select(&selector).next().unwrap().inner_html(),
            expected
        );
    }

    macro_rules! check_html_macro {
        ($html: expr, $selectors: expr, $text: expr) => {{
            let document = scraper::Html::parse_document($html);
            let selector = scraper::Selector::parse($selectors).unwrap();
            assert_eq!(
                &document.select(&selector).next().unwrap().inner_html(),
                $text
            );
        }};
    }
    pub(crate) use check_html_macro;
}


running 5 tests
test tests::test_bad_add_func ... FAILED
test tests::test_bad_add_macro ... FAILED
test tests::test_bad_multiply_macro ... FAILED
test tests::test_bad_multiply_func ... FAILED
test tests::test_good ... ok

failures:

---- tests::test_bad_add_func stdout ----
thread 'tests::test_bad_add_func' panicked at src/lib.rs:69:13:
assertion `left == right` failed
  left: "9"
 right: "6"

---- tests::test_bad_add_macro stdout ----
thread 'tests::test_bad_add_macro' panicked at src/lib.rs:54:9:
assertion `left == right` failed
  left: "9"
 right: "6"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- tests::test_bad_multiply_macro stdout ----
thread 'tests::test_bad_multiply_macro' panicked at src/lib.rs:62:9:
assertion `left == right` failed
  left: "6"
 right: "9"

---- tests::test_bad_multiply_func stdout ----
thread 'tests::test_bad_multiply_func' panicked at src/lib.rs:69:13:
assertion `left == right` failed
  left: "6"
 right: "9"


failures:
    tests::test_bad_add_func
    tests::test_bad_add_macro
    tests::test_bad_multiply_func
    tests::test_bad_multiply_macro

test result: FAILED. 1 passed; 4 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s