Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-completion/src/context/tests.rs')
-rw-r--r--crates/ide-completion/src/context/tests.rs101
1 files changed, 97 insertions, 4 deletions
diff --git a/crates/ide-completion/src/context/tests.rs b/crates/ide-completion/src/context/tests.rs
index 445afa75f3..41f0db3c52 100644
--- a/crates/ide-completion/src/context/tests.rs
+++ b/crates/ide-completion/src/context/tests.rs
@@ -1,4 +1,3 @@
-use base_db::salsa;
use expect_test::{Expect, expect};
use hir::HirDisplay;
@@ -11,12 +10,12 @@ fn check_expected_type_and_name(#[rust_analyzer::rust_fixture] ra_fixture: &str,
let (db, pos) = position(ra_fixture);
let config = TEST_CONFIG;
let (completion_context, _analysis) =
- salsa::attach(&db, || CompletionContext::new(&db, pos, &config).unwrap());
+ hir::attach_db(&db, || CompletionContext::new(&db, pos, &config, None).unwrap());
let ty = completion_context
.expected_type
.map(|t| {
- salsa::attach(&db, || {
+ hir::attach_db(&db, || {
t.display_test(&db, completion_context.krate.to_display_target(&db)).to_string()
})
})
@@ -91,6 +90,20 @@ fn bar(x: u32) {}
"#,
expect![[r#"ty: u32, name: x"#]],
);
+ check_expected_type_and_name(
+ r#"
+fn foo() { bar(, $0); }
+fn bar(x: u32, y: i32) {}
+"#,
+ expect![[r#"ty: i32, name: y"#]],
+ );
+ check_expected_type_and_name(
+ r#"
+fn foo() { bar(, c$0); }
+fn bar(x: u32, y: i32) {}
+"#,
+ expect![[r#"ty: i32, name: y"#]],
+ );
}
#[test]
@@ -279,6 +292,62 @@ fn foo() {
}
#[test]
+fn expected_type_if_let_chain_bool() {
+ check_expected_type_and_name(
+ r#"
+fn foo() {
+ let f = Foo::Quux;
+ if let c = f && $0 { }
+}
+"#,
+ expect![[r#"ty: bool, name: ?"#]],
+ );
+}
+
+#[test]
+fn expected_type_if_condition() {
+ check_expected_type_and_name(
+ r#"
+fn foo() {
+ if a$0 { }
+}
+"#,
+ expect![[r#"ty: bool, name: ?"#]],
+ );
+}
+
+#[test]
+fn expected_type_if_body() {
+ check_expected_type_and_name(
+ r#"
+enum Foo { Bar, Baz, Quux }
+
+fn foo() {
+ let _: Foo = if true {
+ $0
+ };
+}
+"#,
+ expect![[r#"ty: Foo, name: ?"#]],
+ );
+
+ check_expected_type_and_name(
+ r#"
+enum Foo { Bar, Baz, Quux }
+
+fn foo() {
+ let _: Foo = if true {
+ Foo::Bar
+ } else {
+ $0
+ };
+}
+"#,
+ expect![[r#"ty: Foo, name: ?"#]],
+ );
+}
+
+#[test]
fn expected_type_fn_ret_without_leading_char() {
cov_mark::check!(expected_type_fn_ret_without_leading_char);
check_expected_type_and_name(
@@ -318,7 +387,6 @@ fn foo() -> u32 {
#[test]
fn expected_type_closure_param_return() {
- // FIXME: make this work with `|| $0`
check_expected_type_and_name(
r#"
//- minicore: fn
@@ -330,6 +398,18 @@ fn bar(f: impl FnOnce() -> u32) {}
"#,
expect![[r#"ty: u32, name: ?"#]],
);
+
+ check_expected_type_and_name(
+ r#"
+//- minicore: fn
+fn foo() {
+ bar(|| $0);
+}
+
+fn bar(f: impl FnOnce() -> u32) {}
+"#,
+ expect![[r#"ty: u32, name: ?"#]],
+ );
}
#[test]
@@ -526,3 +606,16 @@ fn foo() {
expect![[r#"ty: State, name: ?"#]],
);
}
+
+#[test]
+fn expected_type_logic_op() {
+ check_expected_type_and_name(
+ r#"
+enum State { Stop }
+fn foo() {
+ true && $0;
+}
+"#,
+ expect![[r#"ty: bool, name: ?"#]],
+ );
+}