aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/tests.rs
diff options
context:
space:
mode:
authorvsrs <[email protected]>2020-05-08 17:34:34 +0100
committervsrs <[email protected]>2020-05-08 17:34:34 +0100
commit0ef17ef1ee9fb0ce7149176d12f4d225f6d01401 (patch)
treefa2f168120f36f8dbef5dc1e85fea0c0071639c2 /crates/ra_assists/src/tests.rs
parent1be6320ea6cf7830195f80681fa0f43cc340da7e (diff)
parentd3eb9d8eafbebca7da95fa8a4813b92eb5080500 (diff)
Merge remote-tracking branch 'upstream/master' into uniformed_debug_lens
# Conflicts: # editors/code/src/commands/runnables.ts
Diffstat (limited to 'crates/ra_assists/src/tests.rs')
-rw-r--r--crates/ra_assists/src/tests.rs55
1 files changed, 28 insertions, 27 deletions
diff --git a/crates/ra_assists/src/tests.rs b/crates/ra_assists/src/tests.rs
index dd9026df6..a3eacb8f1 100644
--- a/crates/ra_assists/src/tests.rs
+++ b/crates/ra_assists/src/tests.rs
@@ -11,7 +11,7 @@ use test_utils::{
11 RangeOrOffset, 11 RangeOrOffset,
12}; 12};
13 13
14use crate::{handlers::Handler, resolved_assists, AssistCtx, AssistFile}; 14use crate::{handlers::Handler, Assist, AssistContext, Assists};
15 15
16pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { 16pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
17 let (mut db, file_id) = RootDatabase::with_single_file(text); 17 let (mut db, file_id) = RootDatabase::with_single_file(text);
@@ -41,24 +41,25 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
41 let (db, file_id) = crate::tests::with_single_file(&before); 41 let (db, file_id) = crate::tests::with_single_file(&before);
42 let frange = FileRange { file_id, range: selection.into() }; 42 let frange = FileRange { file_id, range: selection.into() };
43 43
44 let assist = resolved_assists(&db, frange) 44 let mut assist = Assist::resolved(&db, frange)
45 .into_iter() 45 .into_iter()
46 .find(|assist| assist.label.id.0 == assist_id) 46 .find(|assist| assist.assist.id.0 == assist_id)
47 .unwrap_or_else(|| { 47 .unwrap_or_else(|| {
48 panic!( 48 panic!(
49 "\n\nAssist is not applicable: {}\nAvailable assists: {}", 49 "\n\nAssist is not applicable: {}\nAvailable assists: {}",
50 assist_id, 50 assist_id,
51 resolved_assists(&db, frange) 51 Assist::resolved(&db, frange)
52 .into_iter() 52 .into_iter()
53 .map(|assist| assist.label.id.0) 53 .map(|assist| assist.assist.id.0)
54 .collect::<Vec<_>>() 54 .collect::<Vec<_>>()
55 .join(", ") 55 .join(", ")
56 ) 56 )
57 }); 57 });
58 58
59 let actual = { 59 let actual = {
60 let change = assist.source_change.source_file_edits.pop().unwrap();
60 let mut actual = before.clone(); 61 let mut actual = before.clone();
61 assist.action.edit.apply(&mut actual); 62 change.edit.apply(&mut actual);
62 actual 63 actual
63 }; 64 };
64 assert_eq_text!(after, &actual); 65 assert_eq_text!(after, &actual);
@@ -70,7 +71,7 @@ enum ExpectedResult<'a> {
70 Target(&'a str), 71 Target(&'a str),
71} 72}
72 73
73fn check(assist: Handler, before: &str, expected: ExpectedResult) { 74fn 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 (text_without_caret, file_with_caret_id, range_or_offset, db) = if before.contains("//-") {
75 let (mut db, position) = RootDatabase::with_position(before); 76 let (mut db, position) = RootDatabase::with_position(before);
76 db.set_local_roots(Arc::new(vec![db.file_source_root(position.file_id)])); 77 db.set_local_roots(Arc::new(vec![db.file_source_root(position.file_id)]));
@@ -89,36 +90,36 @@ fn check(assist: Handler, before: &str, expected: ExpectedResult) {
89 let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; 90 let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() };
90 91
91 let sema = Semantics::new(&db); 92 let sema = Semantics::new(&db);
92 let assist_ctx = AssistCtx::new(&sema, frange, true); 93 let ctx = AssistContext::new(sema, frange);
93 94 let mut acc = Assists::new_resolved(&ctx);
94 match (assist(assist_ctx), expected) { 95 handler(&mut acc, &ctx);
96 let mut res = acc.finish_resolved();
97 let assist = res.pop();
98 match (assist, expected) {
95 (Some(assist), ExpectedResult::After(after)) => { 99 (Some(assist), ExpectedResult::After(after)) => {
96 let action = assist.0[0].action.clone().unwrap(); 100 let mut source_change = assist.source_change;
101 let change = source_change.source_file_edits.pop().unwrap();
97 102
98 let mut actual = if let AssistFile::TargetFile(file_id) = action.file { 103 let mut actual = db.file_text(change.file_id).as_ref().to_owned();
99 db.file_text(file_id).as_ref().to_owned() 104 change.edit.apply(&mut actual);
100 } else {
101 text_without_caret
102 };
103 action.edit.apply(&mut actual);
104 105
105 match action.cursor_position { 106 match source_change.cursor_position {
106 None => { 107 None => {
107 if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset { 108 if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset {
108 let off = action 109 let off = change
109 .edit 110 .edit
110 .apply_to_offset(before_cursor_pos) 111 .apply_to_offset(before_cursor_pos)
111 .expect("cursor position is affected by the edit"); 112 .expect("cursor position is affected by the edit");
112 actual = add_cursor(&actual, off) 113 actual = add_cursor(&actual, off)
113 } 114 }
114 } 115 }
115 Some(off) => actual = add_cursor(&actual, off), 116 Some(off) => actual = add_cursor(&actual, off.offset),
116 }; 117 };
117 118
118 assert_eq_text!(after, &actual); 119 assert_eq_text!(after, &actual);
119 } 120 }
120 (Some(assist), ExpectedResult::Target(target)) => { 121 (Some(assist), ExpectedResult::Target(target)) => {
121 let range = assist.0[0].label.target; 122 let range = assist.assist.target;
122 assert_eq_text!(&text_without_caret[range], target); 123 assert_eq_text!(&text_without_caret[range], target);
123 } 124 }
124 (Some(_), ExpectedResult::NotApplicable) => panic!("assist should not be applicable!"), 125 (Some(_), ExpectedResult::NotApplicable) => panic!("assist should not be applicable!"),
@@ -135,14 +136,14 @@ fn assist_order_field_struct() {
135 let (before_cursor_pos, before) = extract_offset(before); 136 let (before_cursor_pos, before) = extract_offset(before);
136 let (db, file_id) = with_single_file(&before); 137 let (db, file_id) = with_single_file(&before);
137 let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; 138 let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) };
138 let assists = resolved_assists(&db, frange); 139 let assists = Assist::resolved(&db, frange);
139 let mut assists = assists.iter(); 140 let mut assists = assists.iter();
140 141
141 assert_eq!( 142 assert_eq!(
142 assists.next().expect("expected assist").label.label, 143 assists.next().expect("expected assist").assist.label,
143 "Change visibility to pub(crate)" 144 "Change visibility to pub(crate)"
144 ); 145 );
145 assert_eq!(assists.next().expect("expected assist").label.label, "Add `#[derive]`"); 146 assert_eq!(assists.next().expect("expected assist").assist.label, "Add `#[derive]`");
146} 147}
147 148
148#[test] 149#[test]
@@ -158,9 +159,9 @@ fn assist_order_if_expr() {
158 let (range, before) = extract_range(before); 159 let (range, before) = extract_range(before);
159 let (db, file_id) = with_single_file(&before); 160 let (db, file_id) = with_single_file(&before);
160 let frange = FileRange { file_id, range }; 161 let frange = FileRange { file_id, range };
161 let assists = resolved_assists(&db, frange); 162 let assists = Assist::resolved(&db, frange);
162 let mut assists = assists.iter(); 163 let mut assists = assists.iter();
163 164
164 assert_eq!(assists.next().expect("expected assist").label.label, "Extract into variable"); 165 assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable");
165 assert_eq!(assists.next().expect("expected assist").label.label, "Replace with match"); 166 assert_eq!(assists.next().expect("expected assist").assist.label, "Replace with match");
166} 167}