mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 07:04:49 +00:00
Fixup tests
This commit is contained in:
parent
27c7ef6d65
commit
ca9e0f5fe9
9 changed files with 71 additions and 43 deletions
|
@ -30,6 +30,10 @@ fn check(assist_id: &str, before: &str, after: &str) {
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
let actual = assist.action.edit.apply(&before);
|
let actual = {
|
||||||
|
let mut actual = before.clone();
|
||||||
|
assist.action.edit.apply(&mut actual);
|
||||||
|
actual
|
||||||
|
};
|
||||||
assert_eq_text!(after, &actual);
|
assert_eq_text!(after, &actual);
|
||||||
}
|
}
|
||||||
|
|
|
@ -199,12 +199,12 @@ mod handlers {
|
||||||
mod helpers {
|
mod helpers {
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use hir::Semantics;
|
||||||
use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
|
use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
|
||||||
use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase};
|
use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase};
|
||||||
use test_utils::{add_cursor, assert_eq_text, extract_range_or_offset, RangeOrOffset};
|
use test_utils::{add_cursor, assert_eq_text, extract_range_or_offset, RangeOrOffset};
|
||||||
|
|
||||||
use crate::{AssistCtx, AssistFile, AssistHandler};
|
use crate::{handlers::Handler, AssistCtx, AssistFile};
|
||||||
use hir::Semantics;
|
|
||||||
|
|
||||||
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
|
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
|
||||||
let (mut db, file_id) = RootDatabase::with_single_file(text);
|
let (mut db, file_id) = RootDatabase::with_single_file(text);
|
||||||
|
@ -214,22 +214,18 @@ mod helpers {
|
||||||
(db, file_id)
|
(db, file_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn check_assist(
|
pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) {
|
||||||
assist: AssistHandler,
|
|
||||||
ra_fixture_before: &str,
|
|
||||||
ra_fixture_after: &str,
|
|
||||||
) {
|
|
||||||
check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after));
|
check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after));
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: instead of having a separate function here, maybe use
|
// FIXME: instead of having a separate function here, maybe use
|
||||||
// `extract_ranges` and mark the target as `<target> </target>` in the
|
// `extract_ranges` and mark the target as `<target> </target>` in the
|
||||||
// fixuture?
|
// fixuture?
|
||||||
pub(crate) fn check_assist_target(assist: AssistHandler, ra_fixture: &str, target: &str) {
|
pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) {
|
||||||
check(assist, ra_fixture, ExpectedResult::Target(target));
|
check(assist, ra_fixture, ExpectedResult::Target(target));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn check_assist_not_applicable(assist: AssistHandler, ra_fixture: &str) {
|
pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) {
|
||||||
check(assist, ra_fixture, ExpectedResult::NotApplicable);
|
check(assist, ra_fixture, ExpectedResult::NotApplicable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,7 +235,7 @@ mod helpers {
|
||||||
Target(&'a str),
|
Target(&'a str),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check(assist: AssistHandler, before: &str, expected: ExpectedResult) {
|
fn check(assist: Handler, before: &str, expected: ExpectedResult) {
|
||||||
let (text_without_caret, file_with_caret_id, range_or_offset, db) =
|
let (text_without_caret, file_with_caret_id, range_or_offset, db) =
|
||||||
if before.contains("//-") {
|
if before.contains("//-") {
|
||||||
let (mut db, position) = RootDatabase::with_position(before);
|
let (mut db, position) = RootDatabase::with_position(before);
|
||||||
|
@ -265,13 +261,13 @@ mod helpers {
|
||||||
(Some(assist), ExpectedResult::After(after)) => {
|
(Some(assist), ExpectedResult::After(after)) => {
|
||||||
let action = assist.0[0].action.clone().unwrap();
|
let action = assist.0[0].action.clone().unwrap();
|
||||||
|
|
||||||
let assisted_file_text = if let AssistFile::TargetFile(file_id) = action.file {
|
let mut actual = if let AssistFile::TargetFile(file_id) = action.file {
|
||||||
db.file_text(file_id).as_ref().to_owned()
|
db.file_text(file_id).as_ref().to_owned()
|
||||||
} else {
|
} else {
|
||||||
text_without_caret
|
text_without_caret
|
||||||
};
|
};
|
||||||
|
action.edit.apply(&mut actual);
|
||||||
|
|
||||||
let mut actual = action.edit.apply(&assisted_file_text);
|
|
||||||
match action.cursor_position {
|
match action.cursor_position {
|
||||||
None => {
|
None => {
|
||||||
if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset {
|
if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset {
|
||||||
|
|
|
@ -241,7 +241,11 @@ mod tests {
|
||||||
diagnostics.pop().unwrap_or_else(|| panic!("no diagnostics for:\n{}\n", before));
|
diagnostics.pop().unwrap_or_else(|| panic!("no diagnostics for:\n{}\n", before));
|
||||||
let mut fix = diagnostic.fix.unwrap();
|
let mut fix = diagnostic.fix.unwrap();
|
||||||
let edit = fix.source_file_edits.pop().unwrap().edit;
|
let edit = fix.source_file_edits.pop().unwrap().edit;
|
||||||
let actual = edit.apply(&before);
|
let actual = {
|
||||||
|
let mut actual = before.to_string();
|
||||||
|
edit.apply(&mut actual);
|
||||||
|
actual
|
||||||
|
};
|
||||||
assert_eq_text!(after, &actual);
|
assert_eq_text!(after, &actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,7 +260,11 @@ mod tests {
|
||||||
let mut fix = diagnostic.fix.unwrap();
|
let mut fix = diagnostic.fix.unwrap();
|
||||||
let edit = fix.source_file_edits.pop().unwrap().edit;
|
let edit = fix.source_file_edits.pop().unwrap().edit;
|
||||||
let target_file_contents = analysis.file_text(file_position.file_id).unwrap();
|
let target_file_contents = analysis.file_text(file_position.file_id).unwrap();
|
||||||
let actual = edit.apply(&target_file_contents);
|
let actual = {
|
||||||
|
let mut actual = target_file_contents.to_string();
|
||||||
|
edit.apply(&mut actual);
|
||||||
|
actual
|
||||||
|
};
|
||||||
|
|
||||||
// Strip indent and empty lines from `after`, to match the behaviour of
|
// Strip indent and empty lines from `after`, to match the behaviour of
|
||||||
// `parse_fixture` called from `analysis_and_position`.
|
// `parse_fixture` called from `analysis_and_position`.
|
||||||
|
@ -288,7 +296,11 @@ mod tests {
|
||||||
let diagnostic = analysis.diagnostics(file_id).unwrap().pop().unwrap();
|
let diagnostic = analysis.diagnostics(file_id).unwrap().pop().unwrap();
|
||||||
let mut fix = diagnostic.fix.unwrap();
|
let mut fix = diagnostic.fix.unwrap();
|
||||||
let edit = fix.source_file_edits.pop().unwrap().edit;
|
let edit = fix.source_file_edits.pop().unwrap().edit;
|
||||||
let actual = edit.apply(&before);
|
let actual = {
|
||||||
|
let mut actual = before.to_string();
|
||||||
|
edit.apply(&mut actual);
|
||||||
|
actual
|
||||||
|
};
|
||||||
assert_eq_text!(after, &actual);
|
assert_eq_text!(after, &actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -662,10 +674,10 @@ mod tests {
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
edit: TextEdit {
|
edit: TextEdit {
|
||||||
atoms: [
|
indels: [
|
||||||
AtomTextEdit {
|
Indel {
|
||||||
delete: 3..9,
|
|
||||||
insert: "{a:42, b: ()}",
|
insert: "{a:42, b: ()}",
|
||||||
|
delete: 3..9,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
|
@ -569,7 +569,11 @@ fn foo() {
|
||||||
let (sel, before) = extract_range(before);
|
let (sel, before) = extract_range(before);
|
||||||
let parse = SourceFile::parse(&before);
|
let parse = SourceFile::parse(&before);
|
||||||
let result = join_lines(&parse.tree(), sel);
|
let result = join_lines(&parse.tree(), sel);
|
||||||
let actual = result.apply(&before);
|
let actual = {
|
||||||
|
let mut actual = before.to_string();
|
||||||
|
result.apply(&mut actual);
|
||||||
|
actual
|
||||||
|
};
|
||||||
assert_eq_text!(after, &actual);
|
assert_eq_text!(after, &actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -537,10 +537,10 @@ mod tests {
|
||||||
2,
|
2,
|
||||||
),
|
),
|
||||||
edit: TextEdit {
|
edit: TextEdit {
|
||||||
atoms: [
|
indels: [
|
||||||
AtomTextEdit {
|
Indel {
|
||||||
delete: 4..7,
|
|
||||||
insert: "foo2",
|
insert: "foo2",
|
||||||
|
delete: 4..7,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -589,10 +589,10 @@ mod tests {
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
edit: TextEdit {
|
edit: TextEdit {
|
||||||
atoms: [
|
indels: [
|
||||||
AtomTextEdit {
|
Indel {
|
||||||
delete: 4..7,
|
|
||||||
insert: "foo2",
|
insert: "foo2",
|
||||||
|
delete: 4..7,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -672,10 +672,10 @@ mod tests {
|
||||||
2,
|
2,
|
||||||
),
|
),
|
||||||
edit: TextEdit {
|
edit: TextEdit {
|
||||||
atoms: [
|
indels: [
|
||||||
AtomTextEdit {
|
Indel {
|
||||||
delete: 8..11,
|
|
||||||
insert: "foo2",
|
insert: "foo2",
|
||||||
|
delete: 8..11,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -685,10 +685,10 @@ mod tests {
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
edit: TextEdit {
|
edit: TextEdit {
|
||||||
atoms: [
|
indels: [
|
||||||
AtomTextEdit {
|
Indel {
|
||||||
delete: 27..30,
|
|
||||||
insert: "foo2",
|
insert: "foo2",
|
||||||
|
delete: 27..30,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -720,13 +720,13 @@ mod tests {
|
||||||
if let Some(change) = source_change {
|
if let Some(change) = source_change {
|
||||||
for edit in change.info.source_file_edits {
|
for edit in change.info.source_file_edits {
|
||||||
file_id = Some(edit.file_id);
|
file_id = Some(edit.file_id);
|
||||||
for atom in edit.edit.as_atoms() {
|
for indel in edit.edit.as_indels() {
|
||||||
text_edit_builder.replace(atom.delete, atom.insert.clone());
|
text_edit_builder.replace(indel.delete, indel.insert.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let result =
|
let mut result = analysis.file_text(file_id.unwrap()).unwrap().to_string();
|
||||||
text_edit_builder.finish().apply(&*analysis.file_text(file_id.unwrap()).unwrap());
|
text_edit_builder.finish().apply(&mut result);
|
||||||
assert_eq_text!(expected, &*result);
|
assert_eq_text!(expected, &*result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -511,7 +511,9 @@ mod tests {
|
||||||
);
|
);
|
||||||
|
|
||||||
let edit = replace(&matches, &query.template);
|
let edit = replace(&matches, &query.template);
|
||||||
assert_eq!(edit.apply(input), "fn main() { bar(1+2); }");
|
let mut after = input.to_string();
|
||||||
|
edit.apply(&mut after);
|
||||||
|
assert_eq!(after, "fn main() { bar(1+2); }");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn assert_ssr_transform(query: &str, input: &str, result: &str) {
|
fn assert_ssr_transform(query: &str, input: &str, result: &str) {
|
||||||
|
@ -519,7 +521,9 @@ mod tests {
|
||||||
let code = SourceFile::parse(input).tree();
|
let code = SourceFile::parse(input).tree();
|
||||||
let matches = find(&query.pattern, code.syntax());
|
let matches = find(&query.pattern, code.syntax());
|
||||||
let edit = replace(&matches, &query.template);
|
let edit = replace(&matches, &query.template);
|
||||||
assert_eq!(edit.apply(input), result);
|
let mut after = input.to_string();
|
||||||
|
edit.apply(&mut after);
|
||||||
|
assert_eq!(after, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -13,7 +13,11 @@ pub fn check_action<F: Fn(&SourceFile, TextSize) -> Option<TextEdit>>(
|
||||||
let (before_cursor_pos, before) = extract_offset(before);
|
let (before_cursor_pos, before) = extract_offset(before);
|
||||||
let file = SourceFile::parse(&before).ok().unwrap();
|
let file = SourceFile::parse(&before).ok().unwrap();
|
||||||
let result = f(&file, before_cursor_pos).expect("code action is not applicable");
|
let result = f(&file, before_cursor_pos).expect("code action is not applicable");
|
||||||
let actual = result.apply(&before);
|
let actual = {
|
||||||
|
let mut actual = before.to_string();
|
||||||
|
result.apply(&mut actual);
|
||||||
|
actual
|
||||||
|
};
|
||||||
let actual_cursor_pos =
|
let actual_cursor_pos =
|
||||||
result.apply_to_offset(before_cursor_pos).expect("cursor position is affected by the edit");
|
result.apply_to_offset(before_cursor_pos).expect("cursor position is affected by the edit");
|
||||||
let actual = add_cursor(&actual, actual_cursor_pos);
|
let actual = add_cursor(&actual, actual_cursor_pos);
|
||||||
|
|
|
@ -142,10 +142,13 @@ mod tests {
|
||||||
fn do_type_char(char_typed: char, before: &str) -> Option<(String, SingleFileChange)> {
|
fn do_type_char(char_typed: char, before: &str) -> Option<(String, SingleFileChange)> {
|
||||||
let (offset, before) = extract_offset(before);
|
let (offset, before) = extract_offset(before);
|
||||||
let edit = TextEdit::insert(offset, char_typed.to_string());
|
let edit = TextEdit::insert(offset, char_typed.to_string());
|
||||||
let before = edit.apply(&before);
|
let mut before = before.to_string();
|
||||||
|
edit.apply(&mut before);
|
||||||
let parse = SourceFile::parse(&before);
|
let parse = SourceFile::parse(&before);
|
||||||
on_char_typed_inner(&parse.tree(), offset, char_typed)
|
on_char_typed_inner(&parse.tree(), offset, char_typed).map(|it| {
|
||||||
.map(|it| (it.edit.apply(&before), it))
|
it.edit.apply(&mut before);
|
||||||
|
(before.to_string(), it)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn type_char(char_typed: char, before: &str, after: &str) {
|
fn type_char(char_typed: char, before: &str, after: &str) {
|
||||||
|
|
|
@ -96,7 +96,8 @@ mod tests {
|
||||||
let result = analysis.on_enter(FilePosition { offset, file_id }).unwrap()?;
|
let result = analysis.on_enter(FilePosition { offset, file_id }).unwrap()?;
|
||||||
|
|
||||||
assert_eq!(result.source_file_edits.len(), 1);
|
assert_eq!(result.source_file_edits.len(), 1);
|
||||||
let actual = result.source_file_edits[0].edit.apply(&before);
|
let mut actual = before.to_string();
|
||||||
|
result.source_file_edits[0].edit.apply(&mut actual);
|
||||||
let actual = add_cursor(&actual, result.cursor_position.unwrap().offset);
|
let actual = add_cursor(&actual, result.cursor_position.unwrap().offset);
|
||||||
Some(actual)
|
Some(actual)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue