diff options
Diffstat (limited to 'crates/ra_assists/src/tests.rs')
-rw-r--r-- | crates/ra_assists/src/tests.rs | 32 |
1 files changed, 11 insertions, 21 deletions
diff --git a/crates/ra_assists/src/tests.rs b/crates/ra_assists/src/tests.rs index 55576813f..858f5ca80 100644 --- a/crates/ra_assists/src/tests.rs +++ b/crates/ra_assists/src/tests.rs | |||
@@ -4,18 +4,18 @@ use hir::Semantics; | |||
4 | use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; | 4 | use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; |
5 | use ra_ide_db::RootDatabase; | 5 | use ra_ide_db::RootDatabase; |
6 | use ra_syntax::TextRange; | 6 | use ra_syntax::TextRange; |
7 | use test_utils::{ | 7 | use test_utils::{assert_eq_text, extract_offset, extract_range}; |
8 | assert_eq_text, extract_offset, extract_range, extract_range_or_offset, RangeOrOffset, | ||
9 | }; | ||
10 | 8 | ||
11 | use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, Assists}; | 9 | use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, Assists}; |
10 | use stdx::trim_indent; | ||
12 | 11 | ||
13 | pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { | 12 | pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { |
14 | RootDatabase::with_single_file(text) | 13 | RootDatabase::with_single_file(text) |
15 | } | 14 | } |
16 | 15 | ||
17 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { | 16 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { |
18 | check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after)); | 17 | let ra_fixture_after = trim_indent(ra_fixture_after); |
18 | check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after)); | ||
19 | } | 19 | } |
20 | 20 | ||
21 | // FIXME: instead of having a separate function here, maybe use | 21 | // FIXME: instead of having a separate function here, maybe use |
@@ -30,8 +30,9 @@ pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { | |||
30 | } | 30 | } |
31 | 31 | ||
32 | fn check_doc_test(assist_id: &str, before: &str, after: &str) { | 32 | fn check_doc_test(assist_id: &str, before: &str, after: &str) { |
33 | let (selection, before) = extract_range_or_offset(before); | 33 | let after = trim_indent(after); |
34 | let (db, file_id) = crate::tests::with_single_file(&before); | 34 | let (db, file_id, selection) = RootDatabase::with_range_or_offset(&before); |
35 | let before = db.file_text(file_id).to_string(); | ||
35 | let frange = FileRange { file_id, range: selection.into() }; | 36 | let frange = FileRange { file_id, range: selection.into() }; |
36 | 37 | ||
37 | let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange) | 38 | let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange) |
@@ -51,11 +52,11 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) { | |||
51 | 52 | ||
52 | let actual = { | 53 | let actual = { |
53 | let change = assist.source_change.source_file_edits.pop().unwrap(); | 54 | let change = assist.source_change.source_file_edits.pop().unwrap(); |
54 | let mut actual = before.clone(); | 55 | let mut actual = before; |
55 | change.edit.apply(&mut actual); | 56 | change.edit.apply(&mut actual); |
56 | actual | 57 | actual |
57 | }; | 58 | }; |
58 | assert_eq_text!(after, &actual); | 59 | assert_eq_text!(&after, &actual); |
59 | } | 60 | } |
60 | 61 | ||
61 | enum ExpectedResult<'a> { | 62 | enum ExpectedResult<'a> { |
@@ -65,19 +66,8 @@ enum ExpectedResult<'a> { | |||
65 | } | 66 | } |
66 | 67 | ||
67 | fn check(handler: Handler, before: &str, expected: ExpectedResult) { | 68 | fn check(handler: Handler, before: &str, expected: ExpectedResult) { |
68 | let (text_without_caret, file_with_caret_id, range_or_offset, db) = if before.contains("//-") { | 69 | let (db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before); |
69 | let (db, position) = RootDatabase::with_position(before); | 70 | let text_without_caret = db.file_text(file_with_caret_id).to_string(); |
70 | ( | ||
71 | db.file_text(position.file_id).as_ref().to_owned(), | ||
72 | position.file_id, | ||
73 | RangeOrOffset::Offset(position.offset), | ||
74 | db, | ||
75 | ) | ||
76 | } else { | ||
77 | let (range_or_offset, text_without_caret) = extract_range_or_offset(before); | ||
78 | let (db, file_id) = with_single_file(&text_without_caret); | ||
79 | (text_without_caret, file_id, range_or_offset, db) | ||
80 | }; | ||
81 | 71 | ||
82 | let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; | 72 | let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; |
83 | 73 | ||