mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 21:35:20 +00:00
Merge #2899
2899: Provide more runners for potential tests r=matklad a=SomeoneToIgnore Based on the https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Runners.20for.20custom.20test.20annotations discussion. Adds a test runner for every method that has an annotation that contains `test` word in it, allowing to run tests annotated with custom testing annotations such as `#[tokio::test]`, `#[test_case(...)]` and others at costs of potentially emitting some false-positives. Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
This commit is contained in:
commit
adda6dbdf8
1 changed files with 15 additions and 1 deletions
|
@ -43,7 +43,7 @@ fn runnable_fn(fn_def: ast::FnDef) -> Option<Runnable> {
|
||||||
let name = fn_def.name()?.text().clone();
|
let name = fn_def.name()?.text().clone();
|
||||||
let kind = if name == "main" {
|
let kind = if name == "main" {
|
||||||
RunnableKind::Bin
|
RunnableKind::Bin
|
||||||
} else if fn_def.has_atom_attr("test") {
|
} else if has_test_related_attribute(&fn_def) {
|
||||||
RunnableKind::Test { name: name.to_string() }
|
RunnableKind::Test { name: name.to_string() }
|
||||||
} else if fn_def.has_atom_attr("bench") {
|
} else if fn_def.has_atom_attr("bench") {
|
||||||
RunnableKind::Bench { name: name.to_string() }
|
RunnableKind::Bench { name: name.to_string() }
|
||||||
|
@ -53,6 +53,20 @@ fn runnable_fn(fn_def: ast::FnDef) -> Option<Runnable> {
|
||||||
Some(Runnable { range: fn_def.syntax().text_range(), kind })
|
Some(Runnable { range: fn_def.syntax().text_range(), kind })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This is a method with a heuristics to support test methods annotated with custom test annotations, such as
|
||||||
|
/// `#[test_case(...)]`, `#[tokio::test]` and similar.
|
||||||
|
/// Also a regular `#[test]` annotation is supported.
|
||||||
|
///
|
||||||
|
/// It may produce false positives, for example, `#[wasm_bindgen_test]` requires a different command to run the test,
|
||||||
|
/// but it's better than not to have the runnables for the tests at all.
|
||||||
|
fn has_test_related_attribute(fn_def: &ast::FnDef) -> bool {
|
||||||
|
fn_def
|
||||||
|
.attrs()
|
||||||
|
.filter_map(|attr| attr.path())
|
||||||
|
.map(|path| path.syntax().to_string().to_lowercase())
|
||||||
|
.any(|attribute_text| attribute_text.contains("test"))
|
||||||
|
}
|
||||||
|
|
||||||
fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Option<Runnable> {
|
fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Option<Runnable> {
|
||||||
let has_test_function = module
|
let has_test_function = module
|
||||||
.item_list()?
|
.item_list()?
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue