aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor')
-rw-r--r--crates/libeditor/tests/test.rs35
1 files changed, 5 insertions, 30 deletions
diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs
index 04c616ff4..c8882c94d 100644
--- a/crates/libeditor/tests/test.rs
+++ b/crates/libeditor/tests/test.rs
@@ -3,7 +3,7 @@ extern crate libsyntax2;
3#[macro_use] 3#[macro_use]
4extern crate test_utils; 4extern crate test_utils;
5 5
6use test_utils::{assert_eq_dbg}; 6use test_utils::{assert_eq_dbg, add_cursor, extract_offset, extract_range};
7use libsyntax2::{File, TextUnit, TextRange}; 7use libsyntax2::{File, TextUnit, TextRange};
8use libeditor::{ 8use libeditor::{
9 ActionResult, 9 ActionResult,
@@ -15,7 +15,7 @@ use libeditor::{
15#[test] 15#[test]
16fn test_extend_selection() { 16fn test_extend_selection() {
17 fn do_check(before: &str, afters: &[&str]) { 17 fn do_check(before: &str, afters: &[&str]) {
18 let (cursor, before) = extract_cursor(before); 18 let (cursor, before) = extract_offset(before);
19 let file = file(&before); 19 let file = file(&before);
20 let mut range = TextRange::offset_len(cursor, 0.into()); 20 let mut range = TextRange::offset_len(cursor, 0.into());
21 for &after in afters { 21 for &after in afters {
@@ -163,7 +163,7 @@ fn test_add_impl() {
163#[test] 163#[test]
164fn test_matching_brace() { 164fn test_matching_brace() {
165 fn do_check(before: &str, after: &str) { 165 fn do_check(before: &str, after: &str) {
166 let (pos, before) = extract_cursor(before); 166 let (pos, before) = extract_offset(before);
167 let file = file(&before); 167 let file = file(&before);
168 let new_pos = match matching_brace(&file, pos) { 168 let new_pos = match matching_brace(&file, pos) {
169 None => pos, 169 None => pos,
@@ -215,9 +215,7 @@ pub fn reparse(&self, edit: &AtomEdit) -> File {
215#[test] 215#[test]
216fn test_join_lines_selection() { 216fn test_join_lines_selection() {
217 fn do_check(before: &str, after: &str) { 217 fn do_check(before: &str, after: &str) {
218 let (sel_start, before) = extract_cursor(before); 218 let (sel, before) = extract_range(&before);
219 let (sel_end, before) = extract_cursor(&before);
220 let sel = TextRange::from_to(sel_start, sel_end);
221 let file = file(&before); 219 let file = file(&before);
222 let result = join_lines(&file, sel); 220 let result = join_lines(&file, sel);
223 let actual = result.edit.apply(&before); 221 let actual = result.edit.apply(&before);
@@ -255,7 +253,7 @@ fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>>(
255 after: &str, 253 after: &str,
256 f: F, 254 f: F,
257) { 255) {
258 let (before_cursor_pos, before) = extract_cursor(before); 256 let (before_cursor_pos, before) = extract_offset(before);
259 let file = file(&before); 257 let file = file(&before);
260 let result = f(&file, before_cursor_pos).expect("code action is not applicable"); 258 let result = f(&file, before_cursor_pos).expect("code action is not applicable");
261 let actual = result.edit.apply(&before); 259 let actual = result.edit.apply(&before);
@@ -266,26 +264,3 @@ fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>>(
266 let actual = add_cursor(&actual, actual_cursor_pos); 264 let actual = add_cursor(&actual, actual_cursor_pos);
267 assert_eq_text!(after, &actual); 265 assert_eq_text!(after, &actual);
268} 266}
269
270fn extract_cursor(text: &str) -> (TextUnit, String) {
271 let cursor = "<|>";
272 let cursor_pos = match text.find(cursor) {
273 None => panic!("text should contain cursor marker"),
274 Some(pos) => pos,
275 };
276 let mut new_text = String::with_capacity(text.len() - cursor.len());
277 new_text.push_str(&text[..cursor_pos]);
278 new_text.push_str(&text[cursor_pos + cursor.len()..]);
279 let cursor_pos = TextUnit::from(cursor_pos as u32);
280 (cursor_pos, new_text)
281}
282
283fn add_cursor(text: &str, offset: TextUnit) -> String {
284 let offset: u32 = offset.into();
285 let offset: usize = offset as usize;
286 let mut res = String::new();
287 res.push_str(&text[..offset]);
288 res.push_str("<|>");
289 res.push_str(&text[offset..]);
290 res
291}