aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_assists/src/tests.rs')
-rw-r--r--crates/ide_assists/src/tests.rs46
1 files changed, 1 insertions, 45 deletions
diff --git a/crates/ide_assists/src/tests.rs b/crates/ide_assists/src/tests.rs
index 66820058b..b7f616760 100644
--- a/crates/ide_assists/src/tests.rs
+++ b/crates/ide_assists/src/tests.rs
@@ -12,7 +12,7 @@ use ide_db::{
12 RootDatabase, 12 RootDatabase,
13}; 13};
14use stdx::{format_to, trim_indent}; 14use stdx::{format_to, trim_indent};
15use syntax::{ast, AstNode, TextRange}; 15use syntax::TextRange;
16use test_utils::{assert_eq_text, extract_offset}; 16use test_utils::{assert_eq_text, extract_offset};
17 17
18use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, Assists}; 18use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, Assists};
@@ -180,50 +180,6 @@ fn labels(assists: &[Assist]) -> String {
180 labels.into_iter().collect::<String>() 180 labels.into_iter().collect::<String>()
181} 181}
182 182
183pub(crate) type NameSuggestion = fn(&ast::Expr, &Semantics<'_, RootDatabase>) -> Option<String>;
184
185#[track_caller]
186pub(crate) fn check_name_suggestion(
187 suggestion: NameSuggestion,
188 ra_fixture: &str,
189 suggested_name: &str,
190) {
191 check_name(suggestion, ra_fixture, Some(suggested_name));
192}
193
194#[track_caller]
195pub(crate) fn check_name_suggestion_not_applicable(suggestion: NameSuggestion, ra_fixture: &str) {
196 check_name(suggestion, ra_fixture, None);
197}
198
199#[track_caller]
200fn check_name(suggestion: NameSuggestion, ra_fixture: &str, expected: Option<&str>) {
201 let (db, file_with_carret_id, range_or_offset) = RootDatabase::with_range_or_offset(ra_fixture);
202 let frange = FileRange { file_id: file_with_carret_id, range: range_or_offset.into() };
203
204 let sema = Semantics::new(&db);
205 let source_file = sema.parse(frange.file_id);
206 let element = source_file.syntax().covering_element(frange.range);
207 let expr =
208 element.ancestors().find_map(ast::Expr::cast).expect("selection is not an expression");
209 assert_eq!(
210 expr.syntax().text_range(),
211 frange.range,
212 "selection is not an expression(yet contained in one)"
213 );
214
215 let name = suggestion(&expr, &sema);
216
217 match (name, expected) {
218 (Some(name), Some(expected_name)) => {
219 assert_eq_text!(&name, expected_name);
220 }
221 (Some(_), None) => panic!("name suggestion should not be applicable"),
222 (None, Some(_)) => panic!("name suggestion is not applicable"),
223 (None, None) => (),
224 }
225}
226
227#[test] 183#[test]
228fn assist_order_field_struct() { 184fn assist_order_field_struct() {
229 let before = "struct Foo { $0bar: u32 }"; 185 let before = "struct Foo { $0bar: u32 }";