From 45da21672ad1e4ec53487c373b1b7e2ca8f944a2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 23 Jan 2019 15:36:29 +0300 Subject: generalize marking infrastructure --- crates/ra_hir/src/marks.rs | 83 +--------------------------------------------- 1 file changed, 1 insertion(+), 82 deletions(-) (limited to 'crates/ra_hir/src/marks.rs') diff --git a/crates/ra_hir/src/marks.rs b/crates/ra_hir/src/marks.rs index 05430b975..6aff2c4e1 100644 --- a/crates/ra_hir/src/marks.rs +++ b/crates/ra_hir/src/marks.rs @@ -1,82 +1 @@ -//! 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 -//! -//! ```no-run -//! #[test] -//! fn test_foo() { -//! covers!(test_foo); -//! } -//! ``` -//! -//! and in the code under test you write -//! -//! ```no-run -//! 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`. - -#[macro_export] -macro_rules! tested_by { - ($ident:ident) => { - #[cfg(test)] - { - crate::marks::marks::$ident.fetch_add(1, std::sync::atomic::Ordering::SeqCst); - } - }; -} - -#[macro_export] -macro_rules! covers { - ($ident:ident) => { - let _checker = crate::marks::marks::MarkChecker::new(&crate::marks::marks::$ident); - }; -} - -#[cfg(test)] -pub(crate) mod marks { - use std::sync::atomic::{AtomicUsize, Ordering}; - - pub(crate) struct MarkChecker { - mark: &'static AtomicUsize, - value_on_entry: usize, - } - - impl MarkChecker { - pub(crate) 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") - } - } - - macro_rules! mark { - ($ident:ident) => { - #[allow(bad_style)] - pub(crate) static $ident: AtomicUsize = AtomicUsize::new(0); - }; - } - - mark!(name_res_works_for_broken_modules); -} +test_utils::mark!(name_res_works_for_broken_modules); -- cgit v1.2.3