diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-23 12:40:36 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-23 12:40:36 +0000 |
commit | 86507c0626eb07485b21c61638673acbb3c2a9ad (patch) | |
tree | 450e229240e7ffecd90a6251c1ef61ddb725ef4d | |
parent | b846832b8b2fb221cce42f170e9989c1dac5e468 (diff) | |
parent | 45da21672ad1e4ec53487c373b1b7e2ca8f944a2 (diff) |
Merge #606
606: generalize marking infrastructure r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r-- | crates/ra_hir/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/marks.rs | 83 | ||||
-rw-r--r-- | crates/ra_hir/src/module_tree.rs | 1 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/tests.rs | 2 | ||||
-rw-r--r-- | crates/test_utils/src/lib.rs | 9 | ||||
-rw-r--r-- | crates/test_utils/src/marks.rs | 80 |
6 files changed, 91 insertions, 86 deletions
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 361d39f03..33a9ba605 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -8,7 +8,7 @@ | |||
8 | pub mod db; | 8 | pub mod db; |
9 | #[cfg(test)] | 9 | #[cfg(test)] |
10 | mod mock; | 10 | mod mock; |
11 | #[macro_use] | 11 | #[cfg(test)] |
12 | mod marks; | 12 | mod marks; |
13 | mod query_definitions; | 13 | mod query_definitions; |
14 | mod path; | 14 | mod path; |
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 @@ | |||
1 | //! This module implements manually tracked test coverage, which useful for | test_utils::mark!(name_res_works_for_broken_modules); | |
2 | //! quickly finding a test responsible for testing a particular bit of code. | ||
3 | //! | ||
4 | //! See https://matklad.github.io/2018/06/18/a-trick-for-test-maintenance.html | ||
5 | //! for details, but the TL;DR is that you write your test as | ||
6 | //! | ||
7 | //! ```no-run | ||
8 | //! #[test] | ||
9 | //! fn test_foo() { | ||
10 | //! covers!(test_foo); | ||
11 | //! } | ||
12 | //! ``` | ||
13 | //! | ||
14 | //! and in the code under test you write | ||
15 | //! | ||
16 | //! ```no-run | ||
17 | //! fn foo() { | ||
18 | //! if some_condition() { | ||
19 | //! tested_by!(test_foo); | ||
20 | //! } | ||
21 | //! } | ||
22 | //! ``` | ||
23 | //! | ||
24 | //! This module then checks that executing the test indeed covers the specified | ||
25 | //! function. This is useful if you come back to the `foo` function ten years | ||
26 | //! later and wonder where the test are: now you can grep for `test_foo`. | ||
27 | |||
28 | #[macro_export] | ||
29 | macro_rules! tested_by { | ||
30 | ($ident:ident) => { | ||
31 | #[cfg(test)] | ||
32 | { | ||
33 | crate::marks::marks::$ident.fetch_add(1, std::sync::atomic::Ordering::SeqCst); | ||
34 | } | ||
35 | }; | ||
36 | } | ||
37 | |||
38 | #[macro_export] | ||
39 | macro_rules! covers { | ||
40 | ($ident:ident) => { | ||
41 | let _checker = crate::marks::marks::MarkChecker::new(&crate::marks::marks::$ident); | ||
42 | }; | ||
43 | } | ||
44 | |||
45 | #[cfg(test)] | ||
46 | pub(crate) mod marks { | ||
47 | use std::sync::atomic::{AtomicUsize, Ordering}; | ||
48 | |||
49 | pub(crate) struct MarkChecker { | ||
50 | mark: &'static AtomicUsize, | ||
51 | value_on_entry: usize, | ||
52 | } | ||
53 | |||
54 | impl MarkChecker { | ||
55 | pub(crate) fn new(mark: &'static AtomicUsize) -> MarkChecker { | ||
56 | let value_on_entry = mark.load(Ordering::SeqCst); | ||
57 | MarkChecker { | ||
58 | mark, | ||
59 | value_on_entry, | ||
60 | } | ||
61 | } | ||
62 | } | ||
63 | |||
64 | impl Drop for MarkChecker { | ||
65 | fn drop(&mut self) { | ||
66 | if std::thread::panicking() { | ||
67 | return; | ||
68 | } | ||
69 | let value_on_exit = self.mark.load(Ordering::SeqCst); | ||
70 | assert!(value_on_exit > self.value_on_entry, "mark was not hit") | ||
71 | } | ||
72 | } | ||
73 | |||
74 | macro_rules! mark { | ||
75 | ($ident:ident) => { | ||
76 | #[allow(bad_style)] | ||
77 | pub(crate) static $ident: AtomicUsize = AtomicUsize::new(0); | ||
78 | }; | ||
79 | } | ||
80 | |||
81 | mark!(name_res_works_for_broken_modules); | ||
82 | } | ||
diff --git a/crates/ra_hir/src/module_tree.rs b/crates/ra_hir/src/module_tree.rs index 0256d7996..47c14af35 100644 --- a/crates/ra_hir/src/module_tree.rs +++ b/crates/ra_hir/src/module_tree.rs | |||
@@ -10,6 +10,7 @@ use ra_syntax::{ | |||
10 | ast::{self, AstNode, NameOwner}, | 10 | ast::{self, AstNode, NameOwner}, |
11 | }; | 11 | }; |
12 | use ra_arena::{Arena, RawId, impl_arena_id}; | 12 | use ra_arena::{Arena, RawId, impl_arena_id}; |
13 | use test_utils::tested_by; | ||
13 | 14 | ||
14 | use crate::{Name, AsName, HirDatabase, SourceItemId, HirFileId, Problem, SourceFileItems, ModuleSource}; | 15 | use crate::{Name, AsName, HirDatabase, SourceItemId, HirFileId, Problem, SourceFileItems, ModuleSource}; |
15 | 16 | ||
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 0ec11ec12..e92007453 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs | |||
@@ -2,7 +2,7 @@ use std::sync::Arc; | |||
2 | 2 | ||
3 | use ra_db::{FilesDatabase, CrateGraph, SourceRootId, salsa::Database}; | 3 | use ra_db::{FilesDatabase, CrateGraph, SourceRootId, salsa::Database}; |
4 | use relative_path::RelativePath; | 4 | use relative_path::RelativePath; |
5 | use test_utils::assert_eq_text; | 5 | use test_utils::{assert_eq_text, covers}; |
6 | 6 | ||
7 | use crate::{ | 7 | use crate::{ |
8 | ItemMap, Resolution, | 8 | ItemMap, Resolution, |
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 6489033dd..35a679aea 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs | |||
@@ -1,5 +1,10 @@ | |||
1 | use std::fs; | 1 | #[macro_use] |
2 | use std::path::{Path, PathBuf}; | 2 | pub mod marks; |
3 | |||
4 | use std::{ | ||
5 | fs, | ||
6 | path::{Path, PathBuf} | ||
7 | }; | ||
3 | 8 | ||
4 | use text_unit::{TextRange, TextUnit}; | 9 | use text_unit::{TextRange, TextUnit}; |
5 | use serde_json::Value; | 10 | use serde_json::Value; |
diff --git a/crates/test_utils/src/marks.rs b/crates/test_utils/src/marks.rs new file mode 100644 index 000000000..79ffedf69 --- /dev/null +++ b/crates/test_utils/src/marks.rs | |||
@@ -0,0 +1,80 @@ | |||
1 | //! This module implements manually tracked test coverage, which useful for | ||
2 | //! quickly finding a test responsible for testing a particular bit of code. | ||
3 | //! | ||
4 | //! See https://matklad.github.io/2018/06/18/a-trick-for-test-maintenance.html | ||
5 | //! for details, but the TL;DR is that you write your test as | ||
6 | //! | ||
7 | //! ```no-run | ||
8 | //! #[test] | ||
9 | //! fn test_foo() { | ||
10 | //! covers!(test_foo); | ||
11 | //! } | ||
12 | //! ``` | ||
13 | //! | ||
14 | //! and in the code under test you write | ||
15 | //! | ||
16 | //! ```no-run | ||
17 | //! fn foo() { | ||
18 | //! if some_condition() { | ||
19 | //! tested_by!(test_foo); | ||
20 | //! } | ||
21 | //! } | ||
22 | //! ``` | ||
23 | //! | ||
24 | //! This module then checks that executing the test indeed covers the specified | ||
25 | //! function. This is useful if you come back to the `foo` function ten years | ||
26 | //! later and wonder where the test are: now you can grep for `test_foo`. | ||
27 | use std::sync::atomic::{AtomicUsize, Ordering}; | ||
28 | |||
29 | #[macro_export] | ||
30 | macro_rules! tested_by { | ||
31 | ($ident:ident) => { | ||
32 | #[cfg(test)] | ||
33 | { | ||
34 | // sic! use call-site crate | ||
35 | crate::marks::$ident.fetch_add(1, std::sync::atomic::Ordering::SeqCst); | ||
36 | } | ||
37 | }; | ||
38 | } | ||
39 | |||
40 | #[macro_export] | ||
41 | macro_rules! covers { | ||
42 | ($ident:ident) => { | ||
43 | // sic! use call-site crate | ||
44 | let _checker = $crate::marks::MarkChecker::new(&crate::marks::$ident); | ||
45 | }; | ||
46 | } | ||
47 | |||
48 | #[macro_export] | ||
49 | macro_rules! mark { | ||
50 | ($ident:ident) => { | ||
51 | #[allow(bad_style)] | ||
52 | pub(crate) static $ident: std::sync::atomic::AtomicUsize = | ||
53 | std::sync::atomic::AtomicUsize::new(0); | ||
54 | }; | ||
55 | } | ||
56 | |||
57 | pub struct MarkChecker { | ||
58 | mark: &'static AtomicUsize, | ||
59 | value_on_entry: usize, | ||
60 | } | ||
61 | |||
62 | impl MarkChecker { | ||
63 | pub fn new(mark: &'static AtomicUsize) -> MarkChecker { | ||
64 | let value_on_entry = mark.load(Ordering::SeqCst); | ||
65 | MarkChecker { | ||
66 | mark, | ||
67 | value_on_entry, | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | |||
72 | impl Drop for MarkChecker { | ||
73 | fn drop(&mut self) { | ||
74 | if std::thread::panicking() { | ||
75 | return; | ||
76 | } | ||
77 | let value_on_exit = self.mark.load(Ordering::SeqCst); | ||
78 | assert!(value_on_exit > self.value_on_entry, "mark was not hit") | ||
79 | } | ||
80 | } | ||