diff options
Diffstat (limited to 'crates/assists/src/tests.rs')
-rw-r--r-- | crates/assists/src/tests.rs | 236 |
1 files changed, 0 insertions, 236 deletions
diff --git a/crates/assists/src/tests.rs b/crates/assists/src/tests.rs deleted file mode 100644 index 32bd8698b..000000000 --- a/crates/assists/src/tests.rs +++ /dev/null | |||
@@ -1,236 +0,0 @@ | |||
1 | mod generated; | ||
2 | |||
3 | use hir::Semantics; | ||
4 | use ide_db::{ | ||
5 | base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}, | ||
6 | helpers::{ | ||
7 | insert_use::{InsertUseConfig, MergeBehavior}, | ||
8 | SnippetCap, | ||
9 | }, | ||
10 | source_change::FileSystemEdit, | ||
11 | RootDatabase, | ||
12 | }; | ||
13 | use syntax::TextRange; | ||
14 | use test_utils::{assert_eq_text, extract_offset, extract_range}; | ||
15 | |||
16 | use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, Assists}; | ||
17 | use stdx::{format_to, trim_indent}; | ||
18 | |||
19 | pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig { | ||
20 | snippet_cap: SnippetCap::new(true), | ||
21 | allowed: None, | ||
22 | insert_use: InsertUseConfig { | ||
23 | merge: Some(MergeBehavior::Full), | ||
24 | prefix_kind: hir::PrefixKind::Plain, | ||
25 | }, | ||
26 | }; | ||
27 | |||
28 | pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { | ||
29 | RootDatabase::with_single_file(text) | ||
30 | } | ||
31 | |||
32 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { | ||
33 | let ra_fixture_after = trim_indent(ra_fixture_after); | ||
34 | check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), None); | ||
35 | } | ||
36 | |||
37 | // There is no way to choose what assist within a group you want to test against, | ||
38 | // so this is here to allow you choose. | ||
39 | pub(crate) fn check_assist_by_label( | ||
40 | assist: Handler, | ||
41 | ra_fixture_before: &str, | ||
42 | ra_fixture_after: &str, | ||
43 | label: &str, | ||
44 | ) { | ||
45 | let ra_fixture_after = trim_indent(ra_fixture_after); | ||
46 | check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), Some(label)); | ||
47 | } | ||
48 | |||
49 | // FIXME: instead of having a separate function here, maybe use | ||
50 | // `extract_ranges` and mark the target as `<target> </target>` in the | ||
51 | // fixture? | ||
52 | pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) { | ||
53 | check(assist, ra_fixture, ExpectedResult::Target(target), None); | ||
54 | } | ||
55 | |||
56 | pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { | ||
57 | check(assist, ra_fixture, ExpectedResult::NotApplicable, None); | ||
58 | } | ||
59 | |||
60 | fn check_doc_test(assist_id: &str, before: &str, after: &str) { | ||
61 | let after = trim_indent(after); | ||
62 | let (db, file_id, selection) = RootDatabase::with_range_or_offset(&before); | ||
63 | let before = db.file_text(file_id).to_string(); | ||
64 | let frange = FileRange { file_id, range: selection.into() }; | ||
65 | |||
66 | let assist = Assist::get(&db, &TEST_CONFIG, true, frange) | ||
67 | .into_iter() | ||
68 | .find(|assist| assist.id.0 == assist_id) | ||
69 | .unwrap_or_else(|| { | ||
70 | panic!( | ||
71 | "\n\nAssist is not applicable: {}\nAvailable assists: {}", | ||
72 | assist_id, | ||
73 | Assist::get(&db, &TEST_CONFIG, false, frange) | ||
74 | .into_iter() | ||
75 | .map(|assist| assist.id.0) | ||
76 | .collect::<Vec<_>>() | ||
77 | .join(", ") | ||
78 | ) | ||
79 | }); | ||
80 | |||
81 | let actual = { | ||
82 | let source_change = assist.source_change.unwrap(); | ||
83 | let mut actual = before; | ||
84 | if let Some(source_file_edit) = source_change.get_source_edit(file_id) { | ||
85 | source_file_edit.apply(&mut actual); | ||
86 | } | ||
87 | actual | ||
88 | }; | ||
89 | assert_eq_text!(&after, &actual); | ||
90 | } | ||
91 | |||
92 | enum ExpectedResult<'a> { | ||
93 | NotApplicable, | ||
94 | After(&'a str), | ||
95 | Target(&'a str), | ||
96 | } | ||
97 | |||
98 | fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: Option<&str>) { | ||
99 | let (db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before); | ||
100 | let text_without_caret = db.file_text(file_with_caret_id).to_string(); | ||
101 | |||
102 | let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; | ||
103 | |||
104 | let sema = Semantics::new(&db); | ||
105 | let config = TEST_CONFIG; | ||
106 | let ctx = AssistContext::new(sema, &config, frange); | ||
107 | let mut acc = Assists::new(&ctx, true); | ||
108 | handler(&mut acc, &ctx); | ||
109 | let mut res = acc.finish(); | ||
110 | |||
111 | let assist = match assist_label { | ||
112 | Some(label) => res.into_iter().find(|resolved| resolved.label == label), | ||
113 | None => res.pop(), | ||
114 | }; | ||
115 | |||
116 | match (assist, expected) { | ||
117 | (Some(assist), ExpectedResult::After(after)) => { | ||
118 | let source_change = assist.source_change.unwrap(); | ||
119 | assert!(!source_change.source_file_edits.is_empty()); | ||
120 | let skip_header = source_change.source_file_edits.len() == 1 | ||
121 | && source_change.file_system_edits.len() == 0; | ||
122 | |||
123 | let mut buf = String::new(); | ||
124 | for (file_id, edit) in source_change.source_file_edits { | ||
125 | let mut text = db.file_text(file_id).as_ref().to_owned(); | ||
126 | edit.apply(&mut text); | ||
127 | if !skip_header { | ||
128 | let sr = db.file_source_root(file_id); | ||
129 | let sr = db.source_root(sr); | ||
130 | let path = sr.path_for_file(&file_id).unwrap(); | ||
131 | format_to!(buf, "//- {}\n", path) | ||
132 | } | ||
133 | buf.push_str(&text); | ||
134 | } | ||
135 | |||
136 | for file_system_edit in source_change.file_system_edits { | ||
137 | if let FileSystemEdit::CreateFile { dst, initial_contents } = file_system_edit { | ||
138 | let sr = db.file_source_root(dst.anchor); | ||
139 | let sr = db.source_root(sr); | ||
140 | let mut base = sr.path_for_file(&dst.anchor).unwrap().clone(); | ||
141 | base.pop(); | ||
142 | let created_file_path = format!("{}{}", base.to_string(), &dst.path[1..]); | ||
143 | format_to!(buf, "//- {}\n", created_file_path); | ||
144 | buf.push_str(&initial_contents); | ||
145 | } | ||
146 | } | ||
147 | |||
148 | assert_eq_text!(after, &buf); | ||
149 | } | ||
150 | (Some(assist), ExpectedResult::Target(target)) => { | ||
151 | let range = assist.target; | ||
152 | assert_eq_text!(&text_without_caret[range], target); | ||
153 | } | ||
154 | (Some(_), ExpectedResult::NotApplicable) => panic!("assist should not be applicable!"), | ||
155 | (None, ExpectedResult::After(_)) | (None, ExpectedResult::Target(_)) => { | ||
156 | panic!("code action is not applicable") | ||
157 | } | ||
158 | (None, ExpectedResult::NotApplicable) => (), | ||
159 | }; | ||
160 | } | ||
161 | |||
162 | #[test] | ||
163 | fn assist_order_field_struct() { | ||
164 | let before = "struct Foo { $0bar: u32 }"; | ||
165 | let (before_cursor_pos, before) = extract_offset(before); | ||
166 | let (db, file_id) = with_single_file(&before); | ||
167 | let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; | ||
168 | let assists = Assist::get(&db, &TEST_CONFIG, false, frange); | ||
169 | let mut assists = assists.iter(); | ||
170 | |||
171 | assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)"); | ||
172 | assert_eq!(assists.next().expect("expected assist").label, "Add `#[derive]`"); | ||
173 | } | ||
174 | |||
175 | #[test] | ||
176 | fn assist_order_if_expr() { | ||
177 | let before = " | ||
178 | pub fn test_some_range(a: int) -> bool { | ||
179 | if let 2..6 = $05$0 { | ||
180 | true | ||
181 | } else { | ||
182 | false | ||
183 | } | ||
184 | }"; | ||
185 | let (range, before) = extract_range(before); | ||
186 | let (db, file_id) = with_single_file(&before); | ||
187 | let frange = FileRange { file_id, range }; | ||
188 | let assists = Assist::get(&db, &TEST_CONFIG, false, frange); | ||
189 | let mut assists = assists.iter(); | ||
190 | |||
191 | assert_eq!(assists.next().expect("expected assist").label, "Extract into variable"); | ||
192 | assert_eq!(assists.next().expect("expected assist").label, "Replace with match"); | ||
193 | } | ||
194 | |||
195 | #[test] | ||
196 | fn assist_filter_works() { | ||
197 | let before = " | ||
198 | pub fn test_some_range(a: int) -> bool { | ||
199 | if let 2..6 = $05$0 { | ||
200 | true | ||
201 | } else { | ||
202 | false | ||
203 | } | ||
204 | }"; | ||
205 | let (range, before) = extract_range(before); | ||
206 | let (db, file_id) = with_single_file(&before); | ||
207 | let frange = FileRange { file_id, range }; | ||
208 | |||
209 | { | ||
210 | let mut cfg = TEST_CONFIG; | ||
211 | cfg.allowed = Some(vec![AssistKind::Refactor]); | ||
212 | |||
213 | let assists = Assist::get(&db, &cfg, false, frange); | ||
214 | let mut assists = assists.iter(); | ||
215 | |||
216 | assert_eq!(assists.next().expect("expected assist").label, "Extract into variable"); | ||
217 | assert_eq!(assists.next().expect("expected assist").label, "Replace with match"); | ||
218 | } | ||
219 | |||
220 | { | ||
221 | let mut cfg = TEST_CONFIG; | ||
222 | cfg.allowed = Some(vec![AssistKind::RefactorExtract]); | ||
223 | let assists = Assist::get(&db, &cfg, false, frange); | ||
224 | assert_eq!(assists.len(), 1); | ||
225 | |||
226 | let mut assists = assists.iter(); | ||
227 | assert_eq!(assists.next().expect("expected assist").label, "Extract into variable"); | ||
228 | } | ||
229 | |||
230 | { | ||
231 | let mut cfg = TEST_CONFIG; | ||
232 | cfg.allowed = Some(vec![AssistKind::QuickFix]); | ||
233 | let assists = Assist::get(&db, &cfg, false, frange); | ||
234 | assert!(assists.is_empty(), "All asserts but quickfixes should be filtered out"); | ||
235 | } | ||
236 | } | ||