diff options
Diffstat (limited to 'crates/ra_assists/src/tests.rs')
-rw-r--r-- | crates/ra_assists/src/tests.rs | 153 |
1 files changed, 153 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..62dd3547f --- /dev/null +++ b/crates/ra_assists/src/tests.rs | |||
@@ -0,0 +1,153 @@ | |||
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 | assert_eq_text, extract_offset, extract_range, extract_range_or_offset, RangeOrOffset, | ||
11 | }; | ||
12 | |||
13 | use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, Assists}; | ||
14 | |||
15 | pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { | ||
16 | let (mut db, file_id) = RootDatabase::with_single_file(text); | ||
17 | // FIXME: ideally, this should be done by the above `RootDatabase::with_single_file`, | ||
18 | // but it looks like this might need specialization? :( | ||
19 | db.set_local_roots(Arc::new(vec![db.file_source_root(file_id)])); | ||
20 | (db, file_id) | ||
21 | } | ||
22 | |||
23 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { | ||
24 | check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after)); | ||
25 | } | ||
26 | |||
27 | // FIXME: instead of having a separate function here, maybe use | ||
28 | // `extract_ranges` and mark the target as `<target> </target>` in the | ||
29 | // fixuture? | ||
30 | pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) { | ||
31 | check(assist, ra_fixture, ExpectedResult::Target(target)); | ||
32 | } | ||
33 | |||
34 | pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { | ||
35 | check(assist, ra_fixture, ExpectedResult::NotApplicable); | ||
36 | } | ||
37 | |||
38 | fn check_doc_test(assist_id: &str, before: &str, after: &str) { | ||
39 | let (selection, before) = extract_range_or_offset(before); | ||
40 | let (db, file_id) = crate::tests::with_single_file(&before); | ||
41 | let frange = FileRange { file_id, range: selection.into() }; | ||
42 | |||
43 | let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange) | ||
44 | .into_iter() | ||
45 | .find(|assist| assist.assist.id.0 == assist_id) | ||
46 | .unwrap_or_else(|| { | ||
47 | panic!( | ||
48 | "\n\nAssist is not applicable: {}\nAvailable assists: {}", | ||
49 | assist_id, | ||
50 | Assist::resolved(&db, &AssistConfig::default(), frange) | ||
51 | .into_iter() | ||
52 | .map(|assist| assist.assist.id.0) | ||
53 | .collect::<Vec<_>>() | ||
54 | .join(", ") | ||
55 | ) | ||
56 | }); | ||
57 | |||
58 | let actual = { | ||
59 | let change = assist.source_change.source_file_edits.pop().unwrap(); | ||
60 | let mut actual = before.clone(); | ||
61 | change.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(handler: 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 config = AssistConfig::default(); | ||
93 | let ctx = AssistContext::new(sema, &config, frange); | ||
94 | let mut acc = Assists::new_resolved(&ctx); | ||
95 | handler(&mut acc, &ctx); | ||
96 | let mut res = acc.finish_resolved(); | ||
97 | let assist = res.pop(); | ||
98 | match (assist, expected) { | ||
99 | (Some(assist), ExpectedResult::After(after)) => { | ||
100 | let mut source_change = assist.source_change; | ||
101 | let change = source_change.source_file_edits.pop().unwrap(); | ||
102 | |||
103 | let mut actual = db.file_text(change.file_id).as_ref().to_owned(); | ||
104 | change.edit.apply(&mut actual); | ||
105 | assert_eq_text!(after, &actual); | ||
106 | } | ||
107 | (Some(assist), ExpectedResult::Target(target)) => { | ||
108 | let range = assist.assist.target; | ||
109 | assert_eq_text!(&text_without_caret[range], target); | ||
110 | } | ||
111 | (Some(_), ExpectedResult::NotApplicable) => panic!("assist should not be applicable!"), | ||
112 | (None, ExpectedResult::After(_)) | (None, ExpectedResult::Target(_)) => { | ||
113 | panic!("code action is not applicable") | ||
114 | } | ||
115 | (None, ExpectedResult::NotApplicable) => (), | ||
116 | }; | ||
117 | } | ||
118 | |||
119 | #[test] | ||
120 | fn assist_order_field_struct() { | ||
121 | let before = "struct Foo { <|>bar: u32 }"; | ||
122 | let (before_cursor_pos, before) = extract_offset(before); | ||
123 | let (db, file_id) = with_single_file(&before); | ||
124 | let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; | ||
125 | let assists = Assist::resolved(&db, &AssistConfig::default(), frange); | ||
126 | let mut assists = assists.iter(); | ||
127 | |||
128 | assert_eq!( | ||
129 | assists.next().expect("expected assist").assist.label, | ||
130 | "Change visibility to pub(crate)" | ||
131 | ); | ||
132 | assert_eq!(assists.next().expect("expected assist").assist.label, "Add `#[derive]`"); | ||
133 | } | ||
134 | |||
135 | #[test] | ||
136 | fn assist_order_if_expr() { | ||
137 | let before = " | ||
138 | pub fn test_some_range(a: int) -> bool { | ||
139 | if let 2..6 = <|>5<|> { | ||
140 | true | ||
141 | } else { | ||
142 | false | ||
143 | } | ||
144 | }"; | ||
145 | let (range, before) = extract_range(before); | ||
146 | let (db, file_id) = with_single_file(&before); | ||
147 | let frange = FileRange { file_id, range }; | ||
148 | let assists = Assist::resolved(&db, &AssistConfig::default(), frange); | ||
149 | let mut assists = assists.iter(); | ||
150 | |||
151 | assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable"); | ||
152 | assert_eq!(assists.next().expect("expected assist").assist.label, "Replace with match"); | ||
153 | } | ||