mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-27 12:25:05 +00:00
generalize marking infrastructure
This commit is contained in:
parent
b846832b8b
commit
45da21672a
6 changed files with 91 additions and 86 deletions
|
@ -8,7 +8,7 @@
|
||||||
pub mod db;
|
pub mod db;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod mock;
|
mod mock;
|
||||||
#[macro_use]
|
#[cfg(test)]
|
||||||
mod marks;
|
mod marks;
|
||||||
mod query_definitions;
|
mod query_definitions;
|
||||||
mod path;
|
mod path;
|
||||||
|
|
|
@ -1,82 +1 @@
|
||||||
//! This module implements manually tracked test coverage, which useful for
|
test_utils::mark!(name_res_works_for_broken_modules);
|
||||||
//! 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);
|
|
||||||
}
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ use ra_syntax::{
|
||||||
ast::{self, AstNode, NameOwner},
|
ast::{self, AstNode, NameOwner},
|
||||||
};
|
};
|
||||||
use ra_arena::{Arena, RawId, impl_arena_id};
|
use ra_arena::{Arena, RawId, impl_arena_id};
|
||||||
|
use test_utils::tested_by;
|
||||||
|
|
||||||
use crate::{Name, AsName, HirDatabase, SourceItemId, HirFileId, Problem, SourceFileItems, ModuleSource};
|
use crate::{Name, AsName, HirDatabase, SourceItemId, HirFileId, Problem, SourceFileItems, ModuleSource};
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use ra_db::{FilesDatabase, CrateGraph, SourceRootId, salsa::Database};
|
use ra_db::{FilesDatabase, CrateGraph, SourceRootId, salsa::Database};
|
||||||
use relative_path::RelativePath;
|
use relative_path::RelativePath;
|
||||||
use test_utils::assert_eq_text;
|
use test_utils::{assert_eq_text, covers};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ItemMap, Resolution,
|
ItemMap, Resolution,
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
use std::fs;
|
#[macro_use]
|
||||||
use std::path::{Path, PathBuf};
|
pub mod marks;
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
fs,
|
||||||
|
path::{Path, PathBuf}
|
||||||
|
};
|
||||||
|
|
||||||
use text_unit::{TextRange, TextUnit};
|
use text_unit::{TextRange, TextUnit};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
80
crates/test_utils/src/marks.rs
Normal file
80
crates/test_utils/src/marks.rs
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
//! 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`.
|
||||||
|
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! mark {
|
||||||
|
($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")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue