diff options
Diffstat (limited to 'crates/ra_assists/src/tests.rs')
-rw-r--r-- | crates/ra_assists/src/tests.rs | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/crates/ra_assists/src/tests.rs b/crates/ra_assists/src/tests.rs new file mode 100644 index 000000000..17e3ece9f --- /dev/null +++ b/crates/ra_assists/src/tests.rs | |||
@@ -0,0 +1,164 @@ | |||
1 | mod generated; | ||
2 | |||
3 | use std::sync::Arc; | ||
4 | |||
5 | use hir::Semantics; | ||
6 | use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; | ||
7 | use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase}; | ||
8 | use ra_syntax::TextRange; | ||
9 | use test_utils::{ | ||
10 | add_cursor, assert_eq_text, extract_offset, extract_range, extract_range_or_offset, | ||
11 | RangeOrOffset, | ||
12 | }; | ||
13 | |||
14 | use crate::{handlers::Handler, resolved_assists, AssistCtx}; | ||
15 | |||
16 | pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { | ||
17 | let (mut db, file_id) = RootDatabase::with_single_file(text); | ||
18 | // FIXME: ideally, this should be done by the above `RootDatabase::with_single_file`, | ||
19 | // but it looks like this might need specialization? :( | ||
20 | db.set_local_roots(Arc::new(vec![db.file_source_root(file_id)])); | ||
21 | (db, file_id) | ||
22 | } | ||
23 | |||
24 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { | ||
25 | check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after)); | ||
26 | } | ||
27 | |||
28 | // FIXME: instead of having a separate function here, maybe use | ||
29 | // `extract_ranges` and mark the target as `<target> </target>` in the | ||
30 | // fixuture? | ||
31 | pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) { | ||
32 | check(assist, ra_fixture, ExpectedResult::Target(target)); | ||
33 | } | ||
34 | |||
35 | pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { | ||
36 | check(assist, ra_fixture, ExpectedResult::NotApplicable); | ||
37 | } | ||
38 | |||
39 | fn check_doc_test(assist_id: &str, before: &str, after: &str) { | ||
40 | let (selection, before) = extract_range_or_offset(before); | ||
41 | let (db, file_id) = crate::tests::with_single_file(&before); | ||
42 | let frange = FileRange { file_id, range: selection.into() }; | ||
43 | |||
44 | let mut assist = resolved_assists(&db, frange) | ||
45 | .into_iter() | ||
46 | .find(|assist| assist.label.id.0 == assist_id) | ||
47 | .unwrap_or_else(|| { | ||
48 | panic!( | ||
49 | "\n\nAssist is not applicable: {}\nAvailable assists: {}", | ||
50 | assist_id, | ||
51 | resolved_assists(&db, frange) | ||
52 | .into_iter() | ||
53 | .map(|assist| assist.label.id.0) | ||
54 | .collect::<Vec<_>>() | ||
55 | .join(", ") | ||
56 | ) | ||
57 | }); | ||
58 | |||
59 | let actual = { | ||
60 | let change = assist.source_change.source_file_edits.pop().unwrap(); | ||
61 | let mut actual = before.clone(); | ||
62 | change.edit.apply(&mut actual); | ||
63 | actual | ||
64 | }; | ||
65 | assert_eq_text!(after, &actual); | ||
66 | } | ||
67 | |||
68 | enum ExpectedResult<'a> { | ||
69 | NotApplicable, | ||
70 | After(&'a str), | ||
71 | Target(&'a str), | ||
72 | } | ||
73 | |||
74 | fn check(assist: Handler, before: &str, expected: ExpectedResult) { | ||
75 | let (text_without_caret, file_with_caret_id, range_or_offset, db) = if before.contains("//-") { | ||
76 | let (mut db, position) = RootDatabase::with_position(before); | ||
77 | db.set_local_roots(Arc::new(vec![db.file_source_root(position.file_id)])); | ||
78 | ( | ||
79 | db.file_text(position.file_id).as_ref().to_owned(), | ||
80 | position.file_id, | ||
81 | RangeOrOffset::Offset(position.offset), | ||
82 | db, | ||
83 | ) | ||
84 | } else { | ||
85 | let (range_or_offset, text_without_caret) = extract_range_or_offset(before); | ||
86 | let (db, file_id) = with_single_file(&text_without_caret); | ||
87 | (text_without_caret, file_id, range_or_offset, db) | ||
88 | }; | ||
89 | |||
90 | let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; | ||
91 | |||
92 | let sema = Semantics::new(&db); | ||
93 | let assist_ctx = AssistCtx::new(&sema, frange, true); | ||
94 | |||
95 | match (assist(assist_ctx), expected) { | ||
96 | (Some(assist), ExpectedResult::After(after)) => { | ||
97 | let mut action = assist.0[0].source_change.clone().unwrap(); | ||
98 | let change = action.source_file_edits.pop().unwrap(); | ||
99 | |||
100 | let mut actual = db.file_text(change.file_id).as_ref().to_owned(); | ||
101 | change.edit.apply(&mut actual); | ||
102 | |||
103 | match action.cursor_position { | ||
104 | None => { | ||
105 | if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset { | ||
106 | let off = change | ||
107 | .edit | ||
108 | .apply_to_offset(before_cursor_pos) | ||
109 | .expect("cursor position is affected by the edit"); | ||
110 | actual = add_cursor(&actual, off) | ||
111 | } | ||
112 | } | ||
113 | Some(off) => actual = add_cursor(&actual, off.offset), | ||
114 | }; | ||
115 | |||
116 | assert_eq_text!(after, &actual); | ||
117 | } | ||
118 | (Some(assist), ExpectedResult::Target(target)) => { | ||
119 | let range = assist.0[0].label.target; | ||
120 | assert_eq_text!(&text_without_caret[range], target); | ||
121 | } | ||
122 | (Some(_), ExpectedResult::NotApplicable) => panic!("assist should not be applicable!"), | ||
123 | (None, ExpectedResult::After(_)) | (None, ExpectedResult::Target(_)) => { | ||
124 | panic!("code action is not applicable") | ||
125 | } | ||
126 | (None, ExpectedResult::NotApplicable) => (), | ||
127 | }; | ||
128 | } | ||
129 | |||
130 | #[test] | ||
131 | fn assist_order_field_struct() { | ||
132 | let before = "struct Foo { <|>bar: u32 }"; | ||
133 | let (before_cursor_pos, before) = extract_offset(before); | ||
134 | let (db, file_id) = with_single_file(&before); | ||
135 | let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; | ||
136 | let assists = resolved_assists(&db, frange); | ||
137 | let mut assists = assists.iter(); | ||
138 | |||
139 | assert_eq!( | ||
140 | assists.next().expect("expected assist").label.label, | ||
141 | "Change visibility to pub(crate)" | ||
142 | ); | ||
143 | assert_eq!(assists.next().expect("expected assist").label.label, "Add `#[derive]`"); | ||
144 | } | ||
145 | |||
146 | #[test] | ||
147 | fn assist_order_if_expr() { | ||
148 | let before = " | ||
149 | pub fn test_some_range(a: int) -> bool { | ||
150 | if let 2..6 = <|>5<|> { | ||
151 | true | ||
152 | } else { | ||
153 | false | ||
154 | } | ||
155 | }"; | ||
156 | let (range, before) = extract_range(before); | ||
157 | let (db, file_id) = with_single_file(&before); | ||
158 | let frange = FileRange { file_id, range }; | ||
159 | let assists = resolved_assists(&db, frange); | ||
160 | let mut assists = assists.iter(); | ||
161 | |||
162 | assert_eq!(assists.next().expect("expected assist").label.label, "Extract into variable"); | ||
163 | assert_eq!(assists.next().expect("expected assist").label.label, "Replace with match"); | ||
164 | } | ||