aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils/src/marks.rs
blob: 1c268b49cd75b6f22bd6fd02efdc4512ceca6ba8 (plain)
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
//! This module implements manually tracked test coverage, which useful for
//! quickly finding a test responsible for testing a particular bit of code.
//!
//! See <https://matklad.github.io/2018/06/18/a-trick-for-test-maintenance.html>
//! for details, but the TL;DR is that you write your test as
//!
//! ```rust,no_run
//! #[test]
//! fn test_foo() {
//!     covers!(test_foo);
//! }
//! ```
//!
//! and in the code under test you write
//!
//! ```rust,no_run
//! # use test_utils::tested_by;
//! # fn some_condition() -> bool { true }
//! fn foo() {
//!     if some_condition() {
//!         tested_by!(test_foo);
//!     }
//! }
//! ```
//!
//! This module then checks that executing the test indeed covers the specified
//! function. This is useful if you come back to the `foo` function ten years
//! later and wonder where the test are: now you can grep for `test_foo`.
use std::sync::atomic::{AtomicUsize, Ordering};

#[macro_export]
macro_rules! tested_by {
    ($ident:ident) => {{
        #[cfg(test)]
        {
            // sic! use call-site crate
            crate::marks::$ident.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
    }};
}

#[macro_export]
macro_rules! covers {
    ($ident:ident) => {
        // sic! use call-site crate
        let _checker = $crate::marks::MarkChecker::new(&crate::marks::$ident);
    };
}

#[macro_export]
macro_rules! marks {
    ($($ident:ident)*) => {
        $(
        #[allow(bad_style)]
        pub(crate) static $ident: std::sync::atomic::AtomicUsize =
            std::sync::atomic::AtomicUsize::new(0);
        )*
    };
}

pub struct MarkChecker {
    mark: &'static AtomicUsize,
    value_on_entry: usize,
}

impl MarkChecker {
    pub fn new(mark: &'static AtomicUsize) -> MarkChecker {
        let value_on_entry = mark.load(Ordering::SeqCst);
        MarkChecker { mark, value_on_entry }
    }
}

impl Drop for MarkChecker {
    fn drop(&mut self) {
        if std::thread::panicking() {
            return;
        }
        let value_on_exit = self.mark.load(Ordering::SeqCst);
        assert!(value_on_exit > self.value_on_entry, "mark was not hit")
    }
}