diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-05-06 09:22:10 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-05-06 09:22:10 +0100 |
commit | d81bf3772fa2802dbcb808f64835352519e832a3 (patch) | |
tree | 4797a973b944a831f5567953aa6c991f658d454b /crates/ra_assists/src/tests.rs | |
parent | 30eb458b4fa8adcecd8cbf731bd1cfa9a7a8b88b (diff) | |
parent | eea431180eb5267928c8e0d4b8099ab1042e01d1 (diff) |
Merge #4337
4337: Cleanup assists tests r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/tests.rs')
-rw-r--r-- | crates/ra_assists/src/tests.rs | 167 |
1 files changed, 167 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..483e11931 --- /dev/null +++ b/crates/ra_assists/src/tests.rs | |||
@@ -0,0 +1,167 @@ | |||
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, AssistFile}; | ||
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 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 mut actual = before.clone(); | ||
61 | assist.action.edit.apply(&mut actual); | ||
62 | actual | ||
63 | }; | ||
64 | assert_eq_text!(after, &actual); | ||
65 | } | ||
66 | |||
67 | enum ExpectedResult<'a> { | ||
68 | NotApplicable, | ||
69 | After(&'a str), | ||
70 | Target(&'a str), | ||
71 | } | ||
72 | |||
73 | fn check(assist: Handler, before: &str, expected: ExpectedResult) { | ||
74 | let (text_without_caret, file_with_caret_id, range_or_offset, db) = if before.contains("//-") { | ||
75 | let (mut db, position) = RootDatabase::with_position(before); | ||
76 | db.set_local_roots(Arc::new(vec![db.file_source_root(position.file_id)])); | ||
77 | ( | ||
78 | db.file_text(position.file_id).as_ref().to_owned(), | ||
79 | position.file_id, | ||
80 | RangeOrOffset::Offset(position.offset), | ||
81 | db, | ||
82 | ) | ||
83 | } else { | ||
84 | let (range_or_offset, text_without_caret) = extract_range_or_offset(before); | ||
85 | let (db, file_id) = with_single_file(&text_without_caret); | ||
86 | (text_without_caret, file_id, range_or_offset, db) | ||
87 | }; | ||
88 | |||
89 | let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; | ||
90 | |||
91 | let sema = Semantics::new(&db); | ||
92 | let assist_ctx = AssistCtx::new(&sema, frange, true); | ||
93 | |||
94 | match (assist(assist_ctx), expected) { | ||
95 | (Some(assist), ExpectedResult::After(after)) => { | ||
96 | let action = assist.0[0].action.clone().unwrap(); | ||
97 | |||
98 | let mut actual = if let AssistFile::TargetFile(file_id) = action.file { | ||
99 | db.file_text(file_id).as_ref().to_owned() | ||
100 | } else { | ||
101 | text_without_caret | ||
102 | }; | ||
103 | action.edit.apply(&mut actual); | ||
104 | |||
105 | match action.cursor_position { | ||
106 | None => { | ||
107 | if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset { | ||
108 | let off = action | ||
109 | .edit | ||
110 | .apply_to_offset(before_cursor_pos) | ||
111 | .expect("cursor position is affected by the edit"); | ||
112 | actual = add_cursor(&actual, off) | ||
113 | } | ||
114 | } | ||
115 | Some(off) => actual = add_cursor(&actual, off), | ||
116 | }; | ||
117 | |||
118 | assert_eq_text!(after, &actual); | ||
119 | } | ||
120 | (Some(assist), ExpectedResult::Target(target)) => { | ||
121 | let action = assist.0[0].action.clone().unwrap(); | ||
122 | let range = action.target.expect("expected target on action"); | ||
123 | assert_eq_text!(&text_without_caret[range], target); | ||
124 | } | ||
125 | (Some(_), ExpectedResult::NotApplicable) => panic!("assist should not be applicable!"), | ||
126 | (None, ExpectedResult::After(_)) | (None, ExpectedResult::Target(_)) => { | ||
127 | panic!("code action is not applicable") | ||
128 | } | ||
129 | (None, ExpectedResult::NotApplicable) => (), | ||
130 | }; | ||
131 | } | ||
132 | |||
133 | #[test] | ||
134 | fn assist_order_field_struct() { | ||
135 | let before = "struct Foo { <|>bar: u32 }"; | ||
136 | let (before_cursor_pos, before) = extract_offset(before); | ||
137 | let (db, file_id) = with_single_file(&before); | ||
138 | let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; | ||
139 | let assists = resolved_assists(&db, frange); | ||
140 | let mut assists = assists.iter(); | ||
141 | |||
142 | assert_eq!( | ||
143 | assists.next().expect("expected assist").label.label, | ||
144 | "Change visibility to pub(crate)" | ||
145 | ); | ||
146 | assert_eq!(assists.next().expect("expected assist").label.label, "Add `#[derive]`"); | ||
147 | } | ||
148 | |||
149 | #[test] | ||
150 | fn assist_order_if_expr() { | ||
151 | let before = " | ||
152 | pub fn test_some_range(a: int) -> bool { | ||
153 | if let 2..6 = <|>5<|> { | ||
154 | true | ||
155 | } else { | ||
156 | false | ||
157 | } | ||
158 | }"; | ||
159 | let (range, before) = extract_range(before); | ||
160 | let (db, file_id) = with_single_file(&before); | ||
161 | let frange = FileRange { file_id, range }; | ||
162 | let assists = resolved_assists(&db, frange); | ||
163 | let mut assists = assists.iter(); | ||
164 | |||
165 | assert_eq!(assists.next().expect("expected assist").label.label, "Extract into variable"); | ||
166 | assert_eq!(assists.next().expect("expected assist").label.label, "Replace with match"); | ||
167 | } | ||