6676: Minor cleanup r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-11-30 10:48:05 +00:00 committed by GitHub
commit 13025ae2a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 36 deletions

View file

@ -5,7 +5,7 @@ use syntax::{
use crate::{utils::test_related_attribute, AssistContext, AssistId, AssistKind, Assists};
// Assist: ignore_test
// Assist: toggle_ignore
//
// Adds `#[ignore]` attribute to the test.
//
@ -23,20 +23,20 @@ use crate::{utils::test_related_attribute, AssistContext, AssistId, AssistKind,
// assert_eq!(2 + 2, 5);
// }
// ```
pub(crate) fn ignore_test(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
pub(crate) fn toggle_ignore(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let attr: ast::Attr = ctx.find_node_at_offset()?;
let func = attr.syntax().parent().and_then(ast::Fn::cast)?;
let attr = test_related_attribute(&func)?;
match has_ignore_attribute(&func) {
None => acc.add(
AssistId("ignore_test", AssistKind::None),
AssistId("toggle_ignore", AssistKind::None),
"Ignore this test",
attr.syntax().text_range(),
|builder| builder.insert(attr.syntax().text_range().end(), &format!("\n#[ignore]")),
),
Some(ignore_attr) => acc.add(
AssistId("unignore_test", AssistKind::None),
AssistId("toggle_ignore", AssistKind::None),
"Re-enable this test",
ignore_attr.syntax().text_range(),
|builder| {
@ -55,24 +55,19 @@ pub(crate) fn ignore_test(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
}
fn has_ignore_attribute(fn_def: &ast::Fn) -> Option<ast::Attr> {
fn_def.attrs().find_map(|attr| {
if attr.path()?.syntax().text() == "ignore" {
Some(attr)
} else {
None
}
})
fn_def.attrs().find(|attr| attr.path().map(|it| it.syntax().text() == "ignore") == Some(true))
}
#[cfg(test)]
mod tests {
use super::ignore_test;
use crate::tests::check_assist;
use super::*;
#[test]
fn test_base_case() {
check_assist(
ignore_test,
toggle_ignore,
r#"
#[test<|>]
fn test() {}
@ -88,7 +83,7 @@ mod tests {
#[test]
fn test_unignore() {
check_assist(
ignore_test,
toggle_ignore,
r#"
#[test<|>]
#[ignore]

View file

@ -141,7 +141,6 @@ mod handlers {
mod generate_function;
mod generate_impl;
mod generate_new;
mod ignore_test;
mod infer_function_return_type;
mod inline_local_variable;
mod introduce_named_lifetime;
@ -164,6 +163,7 @@ mod handlers {
mod replace_string_with_char;
mod replace_unwrap_with_match;
mod split_import;
mod toggle_ignore;
mod unwrap_block;
mod wrap_return_type_in_result;
@ -190,7 +190,6 @@ mod handlers {
generate_function::generate_function,
generate_impl::generate_impl,
generate_new::generate_new,
ignore_test::ignore_test,
infer_function_return_type::infer_function_return_type,
inline_local_variable::inline_local_variable,
introduce_named_lifetime::introduce_named_lifetime,
@ -215,6 +214,7 @@ mod handlers {
replace_qualified_name_with_use::replace_qualified_name_with_use,
replace_unwrap_with_match::replace_unwrap_with_match,
split_import::split_import,
toggle_ignore::toggle_ignore,
unwrap_block::unwrap_block,
wrap_return_type_in_result::wrap_return_type_in_result,
// These are manually sorted for better priorities

View file

@ -473,26 +473,6 @@ impl<T: Clone> Ctx<T> {
)
}
#[test]
fn doctest_ignore_test() {
check_doc_test(
"ignore_test",
r#####"
<|>#[test]
fn arithmetics {
assert_eq!(2 + 2, 5);
}
"#####,
r#####"
#[test]
#[ignore]
fn arithmetics {
assert_eq!(2 + 2, 5);
}
"#####,
)
}
#[test]
fn doctest_infer_function_return_type() {
check_doc_test(
@ -978,6 +958,26 @@ use std::{collections::HashMap};
)
}
#[test]
fn doctest_toggle_ignore() {
check_doc_test(
"toggle_ignore",
r#####"
<|>#[test]
fn arithmetics {
assert_eq!(2 + 2, 5);
}
"#####,
r#####"
#[test]
#[ignore]
fn arithmetics {
assert_eq!(2 + 2, 5);
}
"#####,
)
}
#[test]
fn doctest_unwrap_block() {
check_doc_test(