diff options
Diffstat (limited to 'crates/assists/src/tests.rs')
-rw-r--r-- | crates/assists/src/tests.rs | 60 |
1 files changed, 48 insertions, 12 deletions
diff --git a/crates/assists/src/tests.rs b/crates/assists/src/tests.rs index 709a34d03..9002040ce 100644 --- a/crates/assists/src/tests.rs +++ b/crates/assists/src/tests.rs | |||
@@ -2,6 +2,7 @@ mod generated; | |||
2 | 2 | ||
3 | use hir::Semantics; | 3 | use hir::Semantics; |
4 | use ide_db::base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; | 4 | use ide_db::base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; |
5 | use ide_db::source_change::FileSystemEdit; | ||
5 | use ide_db::RootDatabase; | 6 | use ide_db::RootDatabase; |
6 | use syntax::TextRange; | 7 | use syntax::TextRange; |
7 | use test_utils::{assert_eq_text, extract_offset, extract_range}; | 8 | use test_utils::{assert_eq_text, extract_offset, extract_range}; |
@@ -47,7 +48,7 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) { | |||
47 | let before = db.file_text(file_id).to_string(); | 48 | let before = db.file_text(file_id).to_string(); |
48 | let frange = FileRange { file_id, range: selection.into() }; | 49 | let frange = FileRange { file_id, range: selection.into() }; |
49 | 50 | ||
50 | let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange) | 51 | let assist = Assist::resolved(&db, &AssistConfig::default(), frange) |
51 | .into_iter() | 52 | .into_iter() |
52 | .find(|assist| assist.assist.id.0 == assist_id) | 53 | .find(|assist| assist.assist.id.0 == assist_id) |
53 | .unwrap_or_else(|| { | 54 | .unwrap_or_else(|| { |
@@ -63,9 +64,12 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) { | |||
63 | }); | 64 | }); |
64 | 65 | ||
65 | let actual = { | 66 | let actual = { |
66 | let change = assist.source_change.source_file_edits.pop().unwrap(); | ||
67 | let mut actual = before; | 67 | let mut actual = before; |
68 | change.edit.apply(&mut actual); | 68 | for source_file_edit in assist.source_change.source_file_edits { |
69 | if source_file_edit.file_id == file_id { | ||
70 | source_file_edit.edit.apply(&mut actual) | ||
71 | } | ||
72 | } | ||
69 | actual | 73 | actual |
70 | }; | 74 | }; |
71 | assert_eq_text!(&after, &actual); | 75 | assert_eq_text!(&after, &actual); |
@@ -99,20 +103,52 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: | |||
99 | (Some(assist), ExpectedResult::After(after)) => { | 103 | (Some(assist), ExpectedResult::After(after)) => { |
100 | let mut source_change = assist.source_change; | 104 | let mut source_change = assist.source_change; |
101 | assert!(!source_change.source_file_edits.is_empty()); | 105 | assert!(!source_change.source_file_edits.is_empty()); |
102 | let skip_header = source_change.source_file_edits.len() == 1; | 106 | let skip_header = source_change.source_file_edits.len() == 1 |
107 | && source_change.file_system_edits.len() == 0; | ||
103 | source_change.source_file_edits.sort_by_key(|it| it.file_id); | 108 | source_change.source_file_edits.sort_by_key(|it| it.file_id); |
104 | 109 | ||
110 | let mut created_file_ids = Vec::new(); | ||
105 | let mut buf = String::new(); | 111 | let mut buf = String::new(); |
112 | for file_system_edit in source_change.file_system_edits.clone() { | ||
113 | match file_system_edit { | ||
114 | FileSystemEdit::CreateFile { dst } => { | ||
115 | created_file_ids.push(dst.anchor); | ||
116 | } | ||
117 | _ => (), | ||
118 | } | ||
119 | } | ||
120 | |||
106 | for source_file_edit in source_change.source_file_edits { | 121 | for source_file_edit in source_change.source_file_edits { |
107 | let mut text = db.file_text(source_file_edit.file_id).as_ref().to_owned(); | 122 | if created_file_ids.contains(&source_file_edit.file_id) { |
108 | source_file_edit.edit.apply(&mut text); | 123 | let target_dst = source_change |
109 | if !skip_header { | 124 | .file_system_edits |
110 | let sr = db.file_source_root(source_file_edit.file_id); | 125 | .iter() |
111 | let sr = db.source_root(sr); | 126 | .find_map(|f| match f { |
112 | let path = sr.path_for_file(&source_file_edit.file_id).unwrap(); | 127 | FileSystemEdit::CreateFile { dst } => { |
113 | format_to!(buf, "//- {}\n", path) | 128 | if dst.anchor == source_file_edit.file_id { |
129 | Some(&dst.path) | ||
130 | } else { | ||
131 | None | ||
132 | } | ||
133 | } | ||
134 | _ => None, | ||
135 | }) | ||
136 | .unwrap(); | ||
137 | format_to!(buf, "//- {}\n", target_dst); | ||
138 | let mut text = String::new(); | ||
139 | source_file_edit.edit.apply(&mut text); | ||
140 | buf.push_str(&text); | ||
141 | } else { | ||
142 | let mut text = db.file_text(source_file_edit.file_id).as_ref().to_owned(); | ||
143 | source_file_edit.edit.apply(&mut text); | ||
144 | if !skip_header { | ||
145 | let sr = db.file_source_root(source_file_edit.file_id); | ||
146 | let sr = db.source_root(sr); | ||
147 | let path = sr.path_for_file(&source_file_edit.file_id).unwrap(); | ||
148 | format_to!(buf, "//- {}\n", path) | ||
149 | } | ||
150 | buf.push_str(&text); | ||
114 | } | 151 | } |
115 | buf.push_str(&text); | ||
116 | } | 152 | } |
117 | 153 | ||
118 | assert_eq_text!(after, &buf); | 154 | assert_eq_text!(after, &buf); |