diff options
Diffstat (limited to 'crates/assists/src')
-rw-r--r-- | crates/assists/src/handlers/ignore_test.rs | 34 | ||||
-rw-r--r-- | crates/assists/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/assists/src/tests/generated.rs | 20 | ||||
-rw-r--r-- | crates/assists/src/utils.rs | 18 |
4 files changed, 74 insertions, 0 deletions
diff --git a/crates/assists/src/handlers/ignore_test.rs b/crates/assists/src/handlers/ignore_test.rs new file mode 100644 index 000000000..d2339184f --- /dev/null +++ b/crates/assists/src/handlers/ignore_test.rs | |||
@@ -0,0 +1,34 @@ | |||
1 | use syntax::{ast, AstNode}; | ||
2 | |||
3 | use crate::{utils::test_related_attribute, AssistContext, AssistId, AssistKind, Assists}; | ||
4 | |||
5 | // Assist: ignore_test | ||
6 | // | ||
7 | // Adds `#[ignore]` attribute to the test. | ||
8 | // | ||
9 | // ``` | ||
10 | // <|>#[test] | ||
11 | // fn arithmetics { | ||
12 | // assert_eq!(2 + 2, 5); | ||
13 | // } | ||
14 | // ``` | ||
15 | // -> | ||
16 | // ``` | ||
17 | // #[test] | ||
18 | // #[ignore] | ||
19 | // fn arithmetics { | ||
20 | // assert_eq!(2 + 2, 5); | ||
21 | // } | ||
22 | // ``` | ||
23 | pub(crate) fn ignore_test(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
24 | let attr: ast::Attr = ctx.find_node_at_offset()?; | ||
25 | let func = attr.syntax().parent().and_then(ast::Fn::cast)?; | ||
26 | let attr = test_related_attribute(&func)?; | ||
27 | |||
28 | acc.add( | ||
29 | AssistId("ignore_test", AssistKind::None), | ||
30 | "Ignore this test", | ||
31 | attr.syntax().text_range(), | ||
32 | |builder| builder.insert(attr.syntax().text_range().end(), &format!("\n#[ignore]")), | ||
33 | ) | ||
34 | } | ||
diff --git a/crates/assists/src/lib.rs b/crates/assists/src/lib.rs index e8d81b33d..17e9312db 100644 --- a/crates/assists/src/lib.rs +++ b/crates/assists/src/lib.rs | |||
@@ -141,6 +141,7 @@ mod handlers { | |||
141 | mod generate_function; | 141 | mod generate_function; |
142 | mod generate_impl; | 142 | mod generate_impl; |
143 | mod generate_new; | 143 | mod generate_new; |
144 | mod ignore_test; | ||
144 | mod infer_function_return_type; | 145 | mod infer_function_return_type; |
145 | mod inline_local_variable; | 146 | mod inline_local_variable; |
146 | mod introduce_named_lifetime; | 147 | mod introduce_named_lifetime; |
@@ -189,6 +190,7 @@ mod handlers { | |||
189 | generate_function::generate_function, | 190 | generate_function::generate_function, |
190 | generate_impl::generate_impl, | 191 | generate_impl::generate_impl, |
191 | generate_new::generate_new, | 192 | generate_new::generate_new, |
193 | ignore_test::ignore_test, | ||
192 | infer_function_return_type::infer_function_return_type, | 194 | infer_function_return_type::infer_function_return_type, |
193 | inline_local_variable::inline_local_variable, | 195 | inline_local_variable::inline_local_variable, |
194 | introduce_named_lifetime::introduce_named_lifetime, | 196 | introduce_named_lifetime::introduce_named_lifetime, |
diff --git a/crates/assists/src/tests/generated.rs b/crates/assists/src/tests/generated.rs index dbf4f21aa..5a9d1a01b 100644 --- a/crates/assists/src/tests/generated.rs +++ b/crates/assists/src/tests/generated.rs | |||
@@ -474,6 +474,26 @@ impl<T: Clone> Ctx<T> { | |||
474 | } | 474 | } |
475 | 475 | ||
476 | #[test] | 476 | #[test] |
477 | fn doctest_ignore_test() { | ||
478 | check_doc_test( | ||
479 | "ignore_test", | ||
480 | r#####" | ||
481 | <|>#[test] | ||
482 | fn arithmetics { | ||
483 | assert_eq!(2 + 2, 5); | ||
484 | } | ||
485 | "#####, | ||
486 | r#####" | ||
487 | #[test] | ||
488 | #[ignore] | ||
489 | fn arithmetics { | ||
490 | assert_eq!(2 + 2, 5); | ||
491 | } | ||
492 | "#####, | ||
493 | ) | ||
494 | } | ||
495 | |||
496 | #[test] | ||
477 | fn doctest_infer_function_return_type() { | 497 | fn doctest_infer_function_return_type() { |
478 | check_doc_test( | 498 | check_doc_test( |
479 | "infer_function_return_type", | 499 | "infer_function_return_type", |
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index 7bd338e99..d1a0a99b1 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs | |||
@@ -9,6 +9,7 @@ use ide_db::RootDatabase; | |||
9 | use itertools::Itertools; | 9 | use itertools::Itertools; |
10 | use syntax::{ | 10 | use syntax::{ |
11 | ast::edit::AstNodeEdit, | 11 | ast::edit::AstNodeEdit, |
12 | ast::AttrsOwner, | ||
12 | ast::NameOwner, | 13 | ast::NameOwner, |
13 | ast::{self, edit, make, ArgListOwner}, | 14 | ast::{self, edit, make, ArgListOwner}, |
14 | AstNode, Direction, | 15 | AstNode, Direction, |
@@ -82,6 +83,23 @@ pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> { | |||
82 | None | 83 | None |
83 | } | 84 | } |
84 | 85 | ||
86 | /// This is a method with a heuristics to support test methods annotated with custom test annotations, such as | ||
87 | /// `#[test_case(...)]`, `#[tokio::test]` and similar. | ||
88 | /// Also a regular `#[test]` annotation is supported. | ||
89 | /// | ||
90 | /// It may produce false positives, for example, `#[wasm_bindgen_test]` requires a different command to run the test, | ||
91 | /// but it's better than not to have the runnables for the tests at all. | ||
92 | pub fn test_related_attribute(fn_def: &ast::Fn) -> Option<ast::Attr> { | ||
93 | fn_def.attrs().find_map(|attr| { | ||
94 | let path = attr.path()?; | ||
95 | if path.syntax().text().to_string().contains("test") { | ||
96 | Some(attr) | ||
97 | } else { | ||
98 | None | ||
99 | } | ||
100 | }) | ||
101 | } | ||
102 | |||
85 | #[derive(Copy, Clone, PartialEq)] | 103 | #[derive(Copy, Clone, PartialEq)] |
86 | pub enum DefaultMethods { | 104 | pub enum DefaultMethods { |
87 | Only, | 105 | Only, |