Skip to content

Commit 9d60d75

Browse files
bors[bot]Veykril
andauthored
Merge #9339
9339: minor: Cleanup insert_use tests r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents c2aa778 + 2113c46 commit 9d60d75

File tree

3 files changed

+78
-91
lines changed

3 files changed

+78
-91
lines changed

crates/base_db/src/fixture.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl ChangeFixture {
106106
let (range_or_offset, text) = extract_range_or_offset(&entry.text);
107107
assert!(file_position.is_none());
108108
file_position = Some((file_id, range_or_offset));
109-
text.to_string()
109+
text
110110
}
111111
} else {
112112
entry.text.clone()

crates/ide_assists/src/handlers/auto_import.rs

-58
Original file line numberDiff line numberDiff line change
@@ -992,64 +992,6 @@ mod foo {}
992992
const _: () = {
993993
Foo
994994
};
995-
"#,
996-
);
997-
}
998-
999-
#[test]
1000-
fn respects_cfg_attr() {
1001-
check_assist(
1002-
auto_import,
1003-
r#"
1004-
mod bar {
1005-
pub struct Bar;
1006-
}
1007-
1008-
#[cfg(test)]
1009-
fn foo() {
1010-
Bar$0
1011-
}
1012-
"#,
1013-
r#"
1014-
mod bar {
1015-
pub struct Bar;
1016-
}
1017-
1018-
#[cfg(test)]
1019-
fn foo() {
1020-
use bar::Bar;
1021-
1022-
Bar
1023-
}
1024-
"#,
1025-
);
1026-
}
1027-
1028-
#[test]
1029-
fn respects_cfg_attr2() {
1030-
check_assist(
1031-
auto_import,
1032-
r#"
1033-
mod bar {
1034-
pub struct Bar;
1035-
}
1036-
1037-
#[cfg(test)]
1038-
const FOO: Bar = {
1039-
Bar$0
1040-
}
1041-
"#,
1042-
r#"
1043-
mod bar {
1044-
pub struct Bar;
1045-
}
1046-
1047-
#[cfg(test)]
1048-
const FOO: Bar = {
1049-
use bar::Bar;
1050-
1051-
Bar
1052-
}
1053995
"#,
1054996
);
1055997
}

crates/ide_db/src/helpers/insert_use/tests.rs

+77-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
11
use super::*;
22

33
use hir::PrefixKind;
4-
use test_utils::assert_eq_text;
4+
use test_utils::{assert_eq_text, extract_range_or_offset, CURSOR_MARKER};
5+
6+
#[test]
7+
fn respects_cfg_attr_fn() {
8+
check(
9+
r"bar::Bar",
10+
r#"
11+
#[cfg(test)]
12+
fn foo() {$0}
13+
"#,
14+
r#"
15+
#[cfg(test)]
16+
fn foo() {
17+
use bar::Bar;
18+
}
19+
"#,
20+
ImportGranularity::Crate,
21+
);
22+
}
23+
24+
#[test]
25+
fn respects_cfg_attr_const() {
26+
check(
27+
r"bar::Bar",
28+
r#"
29+
#[cfg(test)]
30+
const FOO: Bar = {$0};
31+
"#,
32+
r#"
33+
#[cfg(test)]
34+
const FOO: Bar = {
35+
use bar::Bar;
36+
};
37+
"#,
38+
ImportGranularity::Crate,
39+
);
40+
}
541

642
#[test]
743
fn insert_skips_lone_glob_imports() {
@@ -15,15 +51,13 @@ use foo::bar::*;
1551
use foo::baz::A;
1652
",
1753
ImportGranularity::Crate,
18-
false,
19-
false,
2054
);
2155
}
2256

2357
#[test]
2458
fn insert_not_group() {
2559
cov_mark::check!(insert_no_grouping_last);
26-
check(
60+
check_with_config(
2761
"use external_crate2::bar::A",
2862
r"
2963
use std::bar::B;
@@ -38,24 +72,32 @@ use crate::bar::A;
3872
use self::bar::A;
3973
use super::bar::A;
4074
use external_crate2::bar::A;",
41-
ImportGranularity::Item,
42-
false,
43-
false,
75+
&InsertUseConfig {
76+
granularity: ImportGranularity::Item,
77+
enforce_granularity: true,
78+
prefix_kind: PrefixKind::Plain,
79+
group: false,
80+
skip_glob_imports: true,
81+
},
4482
);
4583
}
4684

4785
#[test]
4886
fn insert_not_group_empty() {
4987
cov_mark::check!(insert_no_grouping_last2);
50-
check(
88+
check_with_config(
5189
"use external_crate2::bar::A",
5290
r"",
5391
r"use external_crate2::bar::A;
5492
5593
",
56-
ImportGranularity::Item,
57-
false,
58-
false,
94+
&InsertUseConfig {
95+
granularity: ImportGranularity::Item,
96+
enforce_granularity: true,
97+
prefix_kind: PrefixKind::Plain,
98+
group: false,
99+
skip_glob_imports: true,
100+
},
59101
);
60102
}
61103

@@ -294,13 +336,15 @@ fn insert_empty_module() {
294336
cov_mark::check!(insert_group_empty_module);
295337
check(
296338
"foo::bar",
297-
"mod x {}",
298-
r"{
339+
r"
340+
mod x {$0}
341+
",
342+
r"
343+
mod x {
299344
use foo::bar;
300-
}",
345+
}
346+
",
301347
ImportGranularity::Item,
302-
true,
303-
true,
304348
)
305349
}
306350

@@ -555,7 +599,6 @@ fn merge_mod_into_glob() {
555599
"token::TokenKind",
556600
r"use token::TokenKind::*;",
557601
r"use token::TokenKind::{*, self};",
558-
false,
559602
&InsertUseConfig {
560603
granularity: ImportGranularity::Crate,
561604
enforce_granularity: true,
@@ -573,7 +616,6 @@ fn merge_self_glob() {
573616
"self",
574617
r"use self::*;",
575618
r"use self::{*, self};",
576-
false,
577619
&InsertUseConfig {
578620
granularity: ImportGranularity::Crate,
579621
enforce_granularity: true,
@@ -798,14 +840,20 @@ fn check_with_config(
798840
path: &str,
799841
ra_fixture_before: &str,
800842
ra_fixture_after: &str,
801-
module: bool,
802843
config: &InsertUseConfig,
803844
) {
804-
let mut syntax = ast::SourceFile::parse(ra_fixture_before).tree().syntax().clone();
805-
if module {
806-
syntax = syntax.descendants().find_map(ast::Module::cast).unwrap().syntax().clone();
807-
}
808-
let file = super::ImportScope::from(syntax.clone_for_update()).unwrap();
845+
let (text, pos) = if ra_fixture_before.contains(CURSOR_MARKER) {
846+
let (range_or_offset, text) = extract_range_or_offset(ra_fixture_before);
847+
(text, Some(range_or_offset))
848+
} else {
849+
(ra_fixture_before.to_owned(), None)
850+
};
851+
let syntax = ast::SourceFile::parse(&text).tree().syntax().clone_for_update();
852+
let file = pos
853+
.and_then(|pos| syntax.token_at_offset(pos.expect_offset()).next()?.parent())
854+
.and_then(|it| super::ImportScope::find_insert_use_container(&it))
855+
.or_else(|| super::ImportScope::from(syntax))
856+
.unwrap();
809857
let path = ast::SourceFile::parse(&format!("use {};", path))
810858
.tree()
811859
.syntax()
@@ -814,7 +862,7 @@ fn check_with_config(
814862
.unwrap();
815863

816864
insert_use(&file, path, config);
817-
let result = file.as_syntax_node().to_string();
865+
let result = file.as_syntax_node().ancestors().last().unwrap().to_string();
818866
assert_eq_text!(ra_fixture_after, &result);
819867
}
820868

@@ -823,34 +871,31 @@ fn check(
823871
ra_fixture_before: &str,
824872
ra_fixture_after: &str,
825873
granularity: ImportGranularity,
826-
module: bool,
827-
group: bool,
828874
) {
829875
check_with_config(
830876
path,
831877
ra_fixture_before,
832878
ra_fixture_after,
833-
module,
834879
&InsertUseConfig {
835880
granularity,
836881
enforce_granularity: true,
837882
prefix_kind: PrefixKind::Plain,
838-
group,
883+
group: true,
839884
skip_glob_imports: true,
840885
},
841886
)
842887
}
843888

844889
fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
845-
check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate, false, true)
890+
check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate)
846891
}
847892

848893
fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
849-
check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module, false, true)
894+
check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module)
850895
}
851896

852897
fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
853-
check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item, false, true)
898+
check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item)
854899
}
855900

856901
fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) {

0 commit comments

Comments
 (0)