Skip to content

Commit

Permalink
refactor test helpers within ide-completions
Browse files Browse the repository at this point in the history
  • Loading branch information
vishruth-thimmaiah committed Jan 8, 2025
1 parent 32b86a8 commit 0b968d7
Show file tree
Hide file tree
Showing 22 changed files with 320 additions and 445 deletions.
106 changes: 48 additions & 58 deletions crates/ide-completion/src/completions/dot.rs

Large diffs are not rendered by default.

15 changes: 5 additions & 10 deletions crates/ide-completion/src/completions/extern_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,13 @@ pub(crate) fn complete_extern_abi(

#[cfg(test)]
mod tests {
use expect_test::{expect, Expect};
use expect_test::expect;

use crate::tests::{check_edit, completion_list_no_kw};

fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list_no_kw(ra_fixture);
expect.assert_eq(&actual);
}
use crate::tests::{check_edit, check_no_kw};

#[test]
fn only_completes_in_string_literals() {
check(
check_no_kw(
r#"
$0 fn foo {}
"#,
Expand All @@ -86,7 +81,7 @@ $0 fn foo {}

#[test]
fn requires_extern_prefix() {
check(
check_no_kw(
r#"
"$0" fn foo {}
"#,
Expand All @@ -96,7 +91,7 @@ $0 fn foo {}

#[test]
fn works() {
check(
check_no_kw(
r#"
extern "$0" fn foo {}
"#,
Expand Down
13 changes: 4 additions & 9 deletions crates/ide-completion/src/completions/format_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,13 @@ pub(crate) fn format_string(

#[cfg(test)]
mod tests {
use expect_test::{expect, Expect};
use expect_test::expect;

use crate::tests::{check_edit, completion_list_no_kw};

fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list_no_kw(ra_fixture);
expect.assert_eq(&actual);
}
use crate::tests::{check_edit, check_no_kw};

#[test]
fn works_when_wrapped() {
check(
check_no_kw(
r#"
//- minicore: fmt
macro_rules! print {
Expand All @@ -89,7 +84,7 @@ fn main() {

#[test]
fn no_completion_without_brace() {
check(
check_no_kw(
r#"
//- minicore: fmt
fn main() {
Expand Down
41 changes: 18 additions & 23 deletions crates/ide-completion/src/completions/item_list/trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,18 +514,13 @@ fn function_declaration(

#[cfg(test)]
mod tests {
use expect_test::{expect, Expect};
use expect_test::expect;

use crate::tests::{check_edit, completion_list_no_kw};

fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list_no_kw(ra_fixture);
expect.assert_eq(&actual)
}
use crate::tests::{check_edit, check_no_kw};

#[test]
fn no_completion_inside_fn() {
check(
check_no_kw(
r"
trait Test { fn test(); fn test2(); }
struct T;
Expand All @@ -544,7 +539,7 @@ impl Test for T {
"#]],
);

check(
check_no_kw(
r"
trait Test { fn test(); fn test2(); }
struct T;
Expand All @@ -558,7 +553,7 @@ impl Test for T {
expect![[""]],
);

check(
check_no_kw(
r"
trait Test { fn test(); fn test2(); }
struct T;
Expand All @@ -573,7 +568,7 @@ impl Test for T {
);

// https://github.com/rust-lang/rust-analyzer/pull/5976#issuecomment-692332191
check(
check_no_kw(
r"
trait Test { fn test(); fn test2(); }
struct T;
Expand All @@ -587,7 +582,7 @@ impl Test for T {
expect![[r#""#]],
);

check(
check_no_kw(
r"
trait Test { fn test(_: i32); fn test2(); }
struct T;
Expand All @@ -606,7 +601,7 @@ impl Test for T {
"#]],
);

check(
check_no_kw(
r"
trait Test { fn test(_: fn()); fn test2(); }
struct T;
Expand All @@ -624,7 +619,7 @@ impl Test for T {

#[test]
fn no_completion_inside_const() {
check(
check_no_kw(
r"
trait Test { const TEST: fn(); const TEST2: u32; type Test; fn test(); }
struct T;
Expand All @@ -636,7 +631,7 @@ impl Test for T {
expect![[r#""#]],
);

check(
check_no_kw(
r"
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
struct T;
Expand All @@ -653,7 +648,7 @@ impl Test for T {
"#]],
);

check(
check_no_kw(
r"
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
struct T;
Expand All @@ -670,7 +665,7 @@ impl Test for T {
"#]],
);

check(
check_no_kw(
r"
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
struct T;
Expand All @@ -689,7 +684,7 @@ impl Test for T {
"#]],
);

check(
check_no_kw(
r"
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
struct T;
Expand All @@ -703,7 +698,7 @@ impl Test for T {
expect![[""]],
);

check(
check_no_kw(
r"
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
struct T;
Expand All @@ -720,7 +715,7 @@ impl Test for T {

#[test]
fn no_completion_inside_type() {
check(
check_no_kw(
r"
trait Test { type Test; type Test2; fn test(); }
struct T;
Expand All @@ -737,7 +732,7 @@ impl Test for T {
"#]],
);

check(
check_no_kw(
r"
trait Test { type Test; type Test2; fn test(); }
struct T;
Expand Down Expand Up @@ -1263,7 +1258,7 @@ impl Foo<u32> for Bar {

#[test]
fn works_directly_in_impl() {
check(
check_no_kw(
r#"
trait Tr {
fn required();
Expand All @@ -1277,7 +1272,7 @@ impl Tr for () {
fn fn required()
"#]],
);
check(
check_no_kw(
r#"
trait Tr {
fn provided() {}
Expand Down
9 changes: 2 additions & 7 deletions crates/ide-completion/src/completions/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,9 @@ pub(crate) fn complete_for_and_where(

#[cfg(test)]
mod tests {
use expect_test::{expect, Expect};
use expect_test::expect;

use crate::tests::{check_edit, completion_list};

fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list(ra_fixture);
expect.assert_eq(&actual)
}
use crate::tests::{check_edit, check};

#[test]
fn test_else_edit_after_if() {
Expand Down
9 changes: 2 additions & 7 deletions crates/ide-completion/src/completions/lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,9 @@ pub(crate) fn complete_label(

#[cfg(test)]
mod tests {
use expect_test::{expect, Expect};
use expect_test::expect;

use crate::tests::{check_edit, completion_list};

fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list(ra_fixture);
expect.assert_eq(&actual);
}
use crate::tests::{check_edit, check};

#[test]
fn check_lifetime_edit() {
Expand Down
9 changes: 2 additions & 7 deletions crates/ide-completion/src/completions/mod_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,9 @@ fn module_chain_to_containing_module_file(

#[cfg(test)]
mod tests {
use expect_test::{expect, Expect};
use expect_test::expect;

use crate::tests::completion_list;

fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list(ra_fixture);
expect.assert_eq(&actual);
}
use crate::tests::check;

#[test]
fn lib_module_completion() {
Expand Down
9 changes: 2 additions & 7 deletions crates/ide-completion/src/completions/postfix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,18 +401,13 @@ fn add_custom_postfix_completions(

#[cfg(test)]
mod tests {
use expect_test::{expect, Expect};
use expect_test::expect;

use crate::{
tests::{check_edit, check_edit_with_config, completion_list, TEST_CONFIG},
tests::{check_edit, check_edit_with_config, check, TEST_CONFIG},
CompletionConfig, Snippet,
};

fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list(ra_fixture);
expect.assert_eq(&actual)
}

#[test]
fn postfix_completion_works_for_trivial_path_expression() {
check(
Expand Down
23 changes: 22 additions & 1 deletion crates/ide-completion/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,32 @@ pub(crate) fn check_edit_with_config(
assert_eq_text!(&ra_fixture_after, &actual)
}

fn check_empty(ra_fixture: &str, expect: Expect) {
pub(crate) fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list(ra_fixture);
expect.assert_eq(&actual);
}

pub(crate) fn check_with_base_items(ra_fixture: &str, expect: Expect) {
check(&format!("{BASE_ITEMS_FIXTURE}{ra_fixture}"), expect)
}

pub(crate) fn check_no_kw(ra_fixture: &str, expect: Expect) {
let actual = completion_list_no_kw(ra_fixture);
expect.assert_eq(&actual)
}

pub(crate) fn check_with_private_editable(ra_fixture: &str, expect: Expect) {
let actual = completion_list_no_kw_with_private_editable(ra_fixture);
expect.assert_eq(&actual);
}


pub(crate) fn check_with_trigger_character(ra_fixture: &str, trigger_character: Option<char>, expect: Expect) {
let actual = completion_list_with_trigger_character(ra_fixture, trigger_character);
expect.assert_eq(&actual)
}


pub(crate) fn get_all_items(
config: CompletionConfig<'_>,
code: &str,
Expand Down
Loading

0 comments on commit 0b968d7

Please sign in to comment.