diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs index 04c616ff4c7..c8882c94d29 100644 --- a/crates/libeditor/tests/test.rs +++ b/crates/libeditor/tests/test.rs @@ -3,7 +3,7 @@ #[macro_use] extern crate test_utils; -use test_utils::{assert_eq_dbg}; +use test_utils::{assert_eq_dbg, add_cursor, extract_offset, extract_range}; use libsyntax2::{File, TextUnit, TextRange}; use libeditor::{ ActionResult, @@ -15,7 +15,7 @@ #[test] fn test_extend_selection() { fn do_check(before: &str, afters: &[&str]) { - let (cursor, before) = extract_cursor(before); + let (cursor, before) = extract_offset(before); let file = file(&before); let mut range = TextRange::offset_len(cursor, 0.into()); for &after in afters { @@ -163,7 +163,7 @@ fn test_add_impl() { #[test] fn test_matching_brace() { fn do_check(before: &str, after: &str) { - let (pos, before) = extract_cursor(before); + let (pos, before) = extract_offset(before); let file = file(&before); let new_pos = match matching_brace(&file, pos) { None => pos, @@ -215,9 +215,7 @@ pub fn reparse(&self, edit: &AtomEdit) -> File { #[test] fn test_join_lines_selection() { fn do_check(before: &str, after: &str) { - let (sel_start, before) = extract_cursor(before); - let (sel_end, before) = extract_cursor(&before); - let sel = TextRange::from_to(sel_start, sel_end); + let (sel, before) = extract_range(&before); let file = file(&before); let result = join_lines(&file, sel); let actual = result.edit.apply(&before); @@ -255,7 +253,7 @@ fn check_action Option>( after: &str, f: F, ) { - let (before_cursor_pos, before) = extract_cursor(before); + let (before_cursor_pos, before) = extract_offset(before); let file = file(&before); let result = f(&file, before_cursor_pos).expect("code action is not applicable"); let actual = result.edit.apply(&before); @@ -266,26 +264,3 @@ fn check_action Option>( let actual = add_cursor(&actual, actual_cursor_pos); assert_eq_text!(after, &actual); } - -fn extract_cursor(text: &str) -> (TextUnit, String) { - let cursor = "<|>"; - let cursor_pos = match text.find(cursor) { - None => panic!("text should contain cursor marker"), - Some(pos) => pos, - }; - let mut new_text = String::with_capacity(text.len() - cursor.len()); - new_text.push_str(&text[..cursor_pos]); - new_text.push_str(&text[cursor_pos + cursor.len()..]); - let cursor_pos = TextUnit::from(cursor_pos as u32); - (cursor_pos, new_text) -} - -fn add_cursor(text: &str, offset: TextUnit) -> String { - let offset: u32 = offset.into(); - let offset: usize = offset as usize; - let mut res = String::new(); - res.push_str(&text[..offset]); - res.push_str("<|>"); - res.push_str(&text[offset..]); - res -} diff --git a/crates/test_utils/Cargo.toml b/crates/test_utils/Cargo.toml index 3d336a9b417..41316581ed0 100644 --- a/crates/test_utils/Cargo.toml +++ b/crates/test_utils/Cargo.toml @@ -6,3 +6,4 @@ authors = ["Aleksey Kladov "] [dependencies] difference = "2.0.0" itertools = "0.7.8" +text_unit = "0.1.2" diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 26b9bfb389c..068eb80ce4a 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -1,8 +1,10 @@ extern crate difference; extern crate itertools; +extern crate text_unit; use std::fmt; use itertools::Itertools; +use text_unit::{TextUnit, TextRange}; pub use self::difference::Changeset as __Changeset; @@ -34,3 +36,32 @@ pub fn assert_eq_dbg(expected: &str, actual: &impl fmt::Debug) { let expected = expected.lines().map(|l| l.trim()).join(" "); assert_eq!(expected, actual); } + +pub fn extract_offset(text: &str) -> (TextUnit, String) { + let cursor = "<|>"; + let cursor_pos = match text.find(cursor) { + None => panic!("text should contain cursor marker"), + Some(pos) => pos, + }; + let mut new_text = String::with_capacity(text.len() - cursor.len()); + new_text.push_str(&text[..cursor_pos]); + new_text.push_str(&text[cursor_pos + cursor.len()..]); + let cursor_pos = TextUnit::from(cursor_pos as u32); + (cursor_pos, new_text) +} + +pub fn extract_range(text: &str) -> (TextRange, String) { + let (start, text) = extract_offset(text); + let (end, text) = extract_offset(&text); + (TextRange::from_to(start, end), text) +} + +pub fn add_cursor(text: &str, offset: TextUnit) -> String { + let offset: u32 = offset.into(); + let offset: usize = offset as usize; + let mut res = String::new(); + res.push_str(&text[..offset]); + res.push_str("<|>"); + res.push_str(&text[offset..]); + res +}