9339: minor: Cleanup insert_use tests r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-06-19 20:33:54 +00:00 committed by GitHub
commit 9d60d75e14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 91 deletions

View file

@ -106,7 +106,7 @@ impl ChangeFixture {
let (range_or_offset, text) = extract_range_or_offset(&entry.text); let (range_or_offset, text) = extract_range_or_offset(&entry.text);
assert!(file_position.is_none()); assert!(file_position.is_none());
file_position = Some((file_id, range_or_offset)); file_position = Some((file_id, range_or_offset));
text.to_string() text
} }
} else { } else {
entry.text.clone() entry.text.clone()

View file

@ -992,64 +992,6 @@ mod foo {}
const _: () = { const _: () = {
Foo Foo
}; };
"#,
);
}
#[test]
fn respects_cfg_attr() {
check_assist(
auto_import,
r#"
mod bar {
pub struct Bar;
}
#[cfg(test)]
fn foo() {
Bar$0
}
"#,
r#"
mod bar {
pub struct Bar;
}
#[cfg(test)]
fn foo() {
use bar::Bar;
Bar
}
"#,
);
}
#[test]
fn respects_cfg_attr2() {
check_assist(
auto_import,
r#"
mod bar {
pub struct Bar;
}
#[cfg(test)]
const FOO: Bar = {
Bar$0
}
"#,
r#"
mod bar {
pub struct Bar;
}
#[cfg(test)]
const FOO: Bar = {
use bar::Bar;
Bar
}
"#, "#,
); );
} }

View file

@ -1,7 +1,43 @@
use super::*; use super::*;
use hir::PrefixKind; use hir::PrefixKind;
use test_utils::assert_eq_text; use test_utils::{assert_eq_text, extract_range_or_offset, CURSOR_MARKER};
#[test]
fn respects_cfg_attr_fn() {
check(
r"bar::Bar",
r#"
#[cfg(test)]
fn foo() {$0}
"#,
r#"
#[cfg(test)]
fn foo() {
use bar::Bar;
}
"#,
ImportGranularity::Crate,
);
}
#[test]
fn respects_cfg_attr_const() {
check(
r"bar::Bar",
r#"
#[cfg(test)]
const FOO: Bar = {$0};
"#,
r#"
#[cfg(test)]
const FOO: Bar = {
use bar::Bar;
};
"#,
ImportGranularity::Crate,
);
}
#[test] #[test]
fn insert_skips_lone_glob_imports() { fn insert_skips_lone_glob_imports() {
@ -15,15 +51,13 @@ use foo::bar::*;
use foo::baz::A; use foo::baz::A;
", ",
ImportGranularity::Crate, ImportGranularity::Crate,
false,
false,
); );
} }
#[test] #[test]
fn insert_not_group() { fn insert_not_group() {
cov_mark::check!(insert_no_grouping_last); cov_mark::check!(insert_no_grouping_last);
check( check_with_config(
"use external_crate2::bar::A", "use external_crate2::bar::A",
r" r"
use std::bar::B; use std::bar::B;
@ -38,24 +72,32 @@ use crate::bar::A;
use self::bar::A; use self::bar::A;
use super::bar::A; use super::bar::A;
use external_crate2::bar::A;", use external_crate2::bar::A;",
ImportGranularity::Item, &InsertUseConfig {
false, granularity: ImportGranularity::Item,
false, enforce_granularity: true,
prefix_kind: PrefixKind::Plain,
group: false,
skip_glob_imports: true,
},
); );
} }
#[test] #[test]
fn insert_not_group_empty() { fn insert_not_group_empty() {
cov_mark::check!(insert_no_grouping_last2); cov_mark::check!(insert_no_grouping_last2);
check( check_with_config(
"use external_crate2::bar::A", "use external_crate2::bar::A",
r"", r"",
r"use external_crate2::bar::A; r"use external_crate2::bar::A;
", ",
ImportGranularity::Item, &InsertUseConfig {
false, granularity: ImportGranularity::Item,
false, enforce_granularity: true,
prefix_kind: PrefixKind::Plain,
group: false,
skip_glob_imports: true,
},
); );
} }
@ -294,13 +336,15 @@ fn insert_empty_module() {
cov_mark::check!(insert_group_empty_module); cov_mark::check!(insert_group_empty_module);
check( check(
"foo::bar", "foo::bar",
"mod x {}", r"
r"{ mod x {$0}
",
r"
mod x {
use foo::bar; use foo::bar;
}", }
",
ImportGranularity::Item, ImportGranularity::Item,
true,
true,
) )
} }
@ -555,7 +599,6 @@ fn merge_mod_into_glob() {
"token::TokenKind", "token::TokenKind",
r"use token::TokenKind::*;", r"use token::TokenKind::*;",
r"use token::TokenKind::{*, self};", r"use token::TokenKind::{*, self};",
false,
&InsertUseConfig { &InsertUseConfig {
granularity: ImportGranularity::Crate, granularity: ImportGranularity::Crate,
enforce_granularity: true, enforce_granularity: true,
@ -573,7 +616,6 @@ fn merge_self_glob() {
"self", "self",
r"use self::*;", r"use self::*;",
r"use self::{*, self};", r"use self::{*, self};",
false,
&InsertUseConfig { &InsertUseConfig {
granularity: ImportGranularity::Crate, granularity: ImportGranularity::Crate,
enforce_granularity: true, enforce_granularity: true,
@ -798,14 +840,20 @@ fn check_with_config(
path: &str, path: &str,
ra_fixture_before: &str, ra_fixture_before: &str,
ra_fixture_after: &str, ra_fixture_after: &str,
module: bool,
config: &InsertUseConfig, config: &InsertUseConfig,
) { ) {
let mut syntax = ast::SourceFile::parse(ra_fixture_before).tree().syntax().clone(); let (text, pos) = if ra_fixture_before.contains(CURSOR_MARKER) {
if module { let (range_or_offset, text) = extract_range_or_offset(ra_fixture_before);
syntax = syntax.descendants().find_map(ast::Module::cast).unwrap().syntax().clone(); (text, Some(range_or_offset))
} } else {
let file = super::ImportScope::from(syntax.clone_for_update()).unwrap(); (ra_fixture_before.to_owned(), None)
};
let syntax = ast::SourceFile::parse(&text).tree().syntax().clone_for_update();
let file = pos
.and_then(|pos| syntax.token_at_offset(pos.expect_offset()).next()?.parent())
.and_then(|it| super::ImportScope::find_insert_use_container(&it))
.or_else(|| super::ImportScope::from(syntax))
.unwrap();
let path = ast::SourceFile::parse(&format!("use {};", path)) let path = ast::SourceFile::parse(&format!("use {};", path))
.tree() .tree()
.syntax() .syntax()
@ -814,7 +862,7 @@ fn check_with_config(
.unwrap(); .unwrap();
insert_use(&file, path, config); insert_use(&file, path, config);
let result = file.as_syntax_node().to_string(); let result = file.as_syntax_node().ancestors().last().unwrap().to_string();
assert_eq_text!(ra_fixture_after, &result); assert_eq_text!(ra_fixture_after, &result);
} }
@ -823,34 +871,31 @@ fn check(
ra_fixture_before: &str, ra_fixture_before: &str,
ra_fixture_after: &str, ra_fixture_after: &str,
granularity: ImportGranularity, granularity: ImportGranularity,
module: bool,
group: bool,
) { ) {
check_with_config( check_with_config(
path, path,
ra_fixture_before, ra_fixture_before,
ra_fixture_after, ra_fixture_after,
module,
&InsertUseConfig { &InsertUseConfig {
granularity, granularity,
enforce_granularity: true, enforce_granularity: true,
prefix_kind: PrefixKind::Plain, prefix_kind: PrefixKind::Plain,
group, group: true,
skip_glob_imports: true, skip_glob_imports: true,
}, },
) )
} }
fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate, false, true) check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate)
} }
fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module, false, true) check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module)
} }
fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item, false, true) check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item)
} }
fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) { fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) {