Skip to content

Commit e6e4872

Browse files
committed
Add tests for stability check in completion
1 parent 0ce71dd commit e6e4872

File tree

9 files changed

+428
-15
lines changed

9 files changed

+428
-15
lines changed

crates/ide-completion/src/completions/dot.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,43 @@ fn foo(s: S) { s.$0 }
172172
);
173173
}
174174

175+
#[test]
176+
fn no_unstable_method_on_stable() {
177+
check(
178+
r#"
179+
//- /main.rs crate:main deps:std
180+
fn foo(s: std::S) { s.$0 }
181+
//- /std.rs crate:std
182+
pub struct S;
183+
impl S {
184+
#[unstable]
185+
pub fn bar(&self) {}
186+
}
187+
"#,
188+
expect![""],
189+
);
190+
}
191+
192+
#[test]
193+
fn unstable_method_on_nightly() {
194+
check(
195+
r#"
196+
//- toolchain:nightly
197+
//- /main.rs crate:main deps:std
198+
fn foo(s: std::S) { s.$0 }
199+
//- /std.rs crate:std
200+
pub struct S;
201+
impl S {
202+
#[unstable]
203+
pub fn bar(&self) {}
204+
}
205+
"#,
206+
expect![[r#"
207+
me bar() fn(&self)
208+
"#]],
209+
);
210+
}
211+
175212
#[test]
176213
fn test_struct_field_completion_self() {
177214
check(

crates/ide-completion/src/tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod type_pos;
2323
mod use_tree;
2424
mod visibility;
2525

26+
use expect_test::Expect;
2627
use hir::PrefixKind;
2728
use ide_db::{
2829
base_db::{fixture::ChangeFixture, FileLoader, FilePosition},
@@ -215,6 +216,11 @@ pub(crate) fn check_edit_with_config(
215216
assert_eq_text!(&ra_fixture_after, &actual)
216217
}
217218

219+
fn check_empty(ra_fixture: &str, expect: Expect) {
220+
let actual = completion_list(ra_fixture);
221+
expect.assert_eq(&actual);
222+
}
223+
218224
pub(crate) fn get_all_items(
219225
config: CompletionConfig,
220226
code: &str,

crates/ide-completion/src/tests/expression.rs

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
//! Completion tests for expressions.
22
use expect_test::{expect, Expect};
33

4-
use crate::tests::{check_edit, completion_list, BASE_ITEMS_FIXTURE};
4+
use crate::tests::{check_edit, check_empty, completion_list, BASE_ITEMS_FIXTURE};
55

66
fn check(ra_fixture: &str, expect: Expect) {
77
let actual = completion_list(&format!("{BASE_ITEMS_FIXTURE}{ra_fixture}"));
88
expect.assert_eq(&actual)
99
}
1010

11-
fn check_empty(ra_fixture: &str, expect: Expect) {
12-
let actual = completion_list(ra_fixture);
13-
expect.assert_eq(&actual);
14-
}
15-
1611
#[test]
1712
fn complete_literal_struct_with_a_private_field() {
1813
// `FooDesc.bar` is private, the completion should not be triggered.
@@ -997,3 +992,105 @@ fn foo() { if foo {} el$0 { let x = 92; } }
997992
"#]],
998993
);
999994
}
995+
996+
#[test]
997+
fn expr_no_unstable_item_on_stable() {
998+
check_empty(
999+
r#"
1000+
//- /main.rs crate:main deps:std
1001+
use std::*;
1002+
fn main() {
1003+
$0
1004+
}
1005+
//- /std.rs crate:std
1006+
#[unstable]
1007+
pub struct UnstableThisShouldNotBeListed;
1008+
"#,
1009+
expect![[r#"
1010+
fn main() fn()
1011+
md std
1012+
bt u32
1013+
kw const
1014+
kw crate::
1015+
kw enum
1016+
kw extern
1017+
kw false
1018+
kw fn
1019+
kw for
1020+
kw if
1021+
kw if let
1022+
kw impl
1023+
kw let
1024+
kw loop
1025+
kw match
1026+
kw mod
1027+
kw return
1028+
kw self::
1029+
kw static
1030+
kw struct
1031+
kw trait
1032+
kw true
1033+
kw type
1034+
kw union
1035+
kw unsafe
1036+
kw use
1037+
kw while
1038+
kw while let
1039+
sn macro_rules
1040+
sn pd
1041+
sn ppd
1042+
"#]],
1043+
);
1044+
}
1045+
1046+
#[test]
1047+
fn expr_unstable_item_on_nightly() {
1048+
check_empty(
1049+
r#"
1050+
//- toolchain:nightly
1051+
//- /main.rs crate:main deps:std
1052+
use std::*;
1053+
fn main() {
1054+
$0
1055+
}
1056+
//- /std.rs crate:std
1057+
#[unstable]
1058+
pub struct UnstableButWeAreOnNightlyAnyway;
1059+
"#,
1060+
expect![[r#"
1061+
fn main() fn()
1062+
md std
1063+
st UnstableButWeAreOnNightlyAnyway
1064+
bt u32
1065+
kw const
1066+
kw crate::
1067+
kw enum
1068+
kw extern
1069+
kw false
1070+
kw fn
1071+
kw for
1072+
kw if
1073+
kw if let
1074+
kw impl
1075+
kw let
1076+
kw loop
1077+
kw match
1078+
kw mod
1079+
kw return
1080+
kw self::
1081+
kw static
1082+
kw struct
1083+
kw trait
1084+
kw true
1085+
kw type
1086+
kw union
1087+
kw unsafe
1088+
kw use
1089+
kw while
1090+
kw while let
1091+
sn macro_rules
1092+
sn pd
1093+
sn ppd
1094+
"#]],
1095+
);
1096+
}

crates/ide-completion/src/tests/flyimport.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,41 @@ fn function() {
11071107
);
11081108
}
11091109

1110+
#[test]
1111+
fn flyimport_pattern_no_unstable_item_on_stable() {
1112+
check(
1113+
r#"
1114+
//- /main.rs crate:main deps:std
1115+
fn function() {
1116+
let foo$0
1117+
}
1118+
//- /std.rs crate:std
1119+
#[unstable]
1120+
pub struct FooStruct {}
1121+
"#,
1122+
expect![""],
1123+
);
1124+
}
1125+
1126+
#[test]
1127+
fn flyimport_pattern_unstable_item_on_nightly() {
1128+
check(
1129+
r#"
1130+
//- toolchain:nightly
1131+
//- /main.rs crate:main deps:std
1132+
fn function() {
1133+
let foo$0
1134+
}
1135+
//- /std.rs crate:std
1136+
#[unstable]
1137+
pub struct FooStruct {}
1138+
"#,
1139+
expect![[r#"
1140+
st FooStruct (use std::FooStruct)
1141+
"#]],
1142+
);
1143+
}
1144+
11101145
#[test]
11111146
fn flyimport_item_name() {
11121147
check(

crates/ide-completion/src/tests/item_list.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Completion tests for item list position.
22
use expect_test::{expect, Expect};
33

4-
use crate::tests::{check_edit, completion_list, BASE_ITEMS_FIXTURE};
4+
use crate::tests::{check_edit, check_empty, completion_list, BASE_ITEMS_FIXTURE};
55

66
fn check(ra_fixture: &str, expect: Expect) {
77
let actual = completion_list(&format!("{BASE_ITEMS_FIXTURE}{ra_fixture}"));
@@ -297,6 +297,58 @@ impl Test for () {
297297
);
298298
}
299299

300+
#[test]
301+
fn in_trait_impl_no_unstable_item_on_stable() {
302+
check_empty(
303+
r#"
304+
trait Test {
305+
#[unstable]
306+
type Type;
307+
#[unstable]
308+
const CONST: ();
309+
#[unstable]
310+
fn function();
311+
}
312+
313+
impl Test for () {
314+
$0
315+
}
316+
"#,
317+
expect![[r#"
318+
kw crate::
319+
kw self::
320+
"#]],
321+
);
322+
}
323+
324+
#[test]
325+
fn in_trait_impl_unstable_item_on_nightly() {
326+
check_empty(
327+
r#"
328+
//- toolchain:nightly
329+
trait Test {
330+
#[unstable]
331+
type Type;
332+
#[unstable]
333+
const CONST: ();
334+
#[unstable]
335+
fn function();
336+
}
337+
338+
impl Test for () {
339+
$0
340+
}
341+
"#,
342+
expect![[r#"
343+
ct const CONST: () =
344+
fn fn function()
345+
ta type Type =
346+
kw crate::
347+
kw self::
348+
"#]],
349+
);
350+
}
351+
300352
#[test]
301353
fn after_unit_struct() {
302354
check(

crates/ide-completion/src/tests/pattern.rs

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
//! Completion tests for pattern position.
22
use expect_test::{expect, Expect};
33

4-
use crate::tests::{check_edit, completion_list, BASE_ITEMS_FIXTURE};
5-
6-
fn check_empty(ra_fixture: &str, expect: Expect) {
7-
let actual = completion_list(ra_fixture);
8-
expect.assert_eq(&actual)
9-
}
4+
use crate::tests::{check_edit, check_empty, completion_list, BASE_ITEMS_FIXTURE};
105

116
fn check(ra_fixture: &str, expect: Expect) {
127
let actual = completion_list(&format!("{BASE_ITEMS_FIXTURE}\n{ra_fixture}"));
@@ -742,3 +737,56 @@ fn f(x: EnumAlias<u8>) {
742737
"#]],
743738
);
744739
}
740+
741+
#[test]
742+
fn pat_no_unstable_item_on_stable() {
743+
check_empty(
744+
r#"
745+
//- /main.rs crate:main deps:std
746+
use std::*;
747+
fn foo() {
748+
let a$0
749+
}
750+
//- /std.rs crate:std
751+
#[unstable]
752+
pub struct S;
753+
#[unstable]
754+
pub enum Enum {
755+
Variant
756+
}
757+
"#,
758+
expect![[r#"
759+
md std
760+
kw mut
761+
kw ref
762+
"#]],
763+
);
764+
}
765+
766+
#[test]
767+
fn pat_unstable_item_on_nightly() {
768+
check_empty(
769+
r#"
770+
//- toolchain:nightly
771+
//- /main.rs crate:main deps:std
772+
use std::*;
773+
fn foo() {
774+
let a$0
775+
}
776+
//- /std.rs crate:std
777+
#[unstable]
778+
pub struct S;
779+
#[unstable]
780+
pub enum Enum {
781+
Variant
782+
}
783+
"#,
784+
expect![[r#"
785+
en Enum
786+
md std
787+
st S
788+
kw mut
789+
kw ref
790+
"#]],
791+
);
792+
}

0 commit comments

Comments
 (0)