diff options
author | Aleksey Kladov <[email protected]> | 2018-08-25 12:30:54 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-25 12:30:54 +0100 |
commit | 5211e7d97771aa7f8d7cc99e5131fb3cc71a1627 (patch) | |
tree | c4539fbe335db8ac133386f16645cbc4c86d6bda | |
parent | 2d41bc3e6c1adb8abce5c796cfd24f77fee6f420 (diff) |
move
-rw-r--r-- | crates/libeditor/tests/test.rs | 35 | ||||
-rw-r--r-- | crates/test_utils/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/test_utils/src/lib.rs | 31 |
3 files changed, 37 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] |
4 | extern crate test_utils; | 4 | extern crate test_utils; |
5 | 5 | ||
6 | use test_utils::{assert_eq_dbg}; | 6 | use test_utils::{assert_eq_dbg, add_cursor, extract_offset, extract_range}; |
7 | use libsyntax2::{File, TextUnit, TextRange}; | 7 | use libsyntax2::{File, TextUnit, TextRange}; |
8 | use libeditor::{ | 8 | use libeditor::{ |
9 | ActionResult, | 9 | ActionResult, |
@@ -15,7 +15,7 @@ use libeditor::{ | |||
15 | #[test] | 15 | #[test] |
16 | fn test_extend_selection() { | 16 | fn 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] |
164 | fn test_matching_brace() { | 164 | fn 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] |
216 | fn test_join_lines_selection() { | 216 | fn 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 | |||
270 | fn 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 | |||
283 | fn 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 | } | ||
diff --git a/crates/test_utils/Cargo.toml b/crates/test_utils/Cargo.toml index 3d336a9b4..41316581e 100644 --- a/crates/test_utils/Cargo.toml +++ b/crates/test_utils/Cargo.toml | |||
@@ -6,3 +6,4 @@ authors = ["Aleksey Kladov <[email protected]>"] | |||
6 | [dependencies] | 6 | [dependencies] |
7 | difference = "2.0.0" | 7 | difference = "2.0.0" |
8 | itertools = "0.7.8" | 8 | itertools = "0.7.8" |
9 | text_unit = "0.1.2" | ||
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 26b9bfb38..068eb80ce 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs | |||
@@ -1,8 +1,10 @@ | |||
1 | extern crate difference; | 1 | extern crate difference; |
2 | extern crate itertools; | 2 | extern crate itertools; |
3 | extern crate text_unit; | ||
3 | 4 | ||
4 | use std::fmt; | 5 | use std::fmt; |
5 | use itertools::Itertools; | 6 | use itertools::Itertools; |
7 | use text_unit::{TextUnit, TextRange}; | ||
6 | 8 | ||
7 | pub use self::difference::Changeset as __Changeset; | 9 | pub use self::difference::Changeset as __Changeset; |
8 | 10 | ||
@@ -34,3 +36,32 @@ pub fn assert_eq_dbg(expected: &str, actual: &impl fmt::Debug) { | |||
34 | let expected = expected.lines().map(|l| l.trim()).join(" "); | 36 | let expected = expected.lines().map(|l| l.trim()).join(" "); |
35 | assert_eq!(expected, actual); | 37 | assert_eq!(expected, actual); |
36 | } | 38 | } |
39 | |||
40 | pub fn extract_offset(text: &str) -> (TextUnit, String) { | ||
41 | let cursor = "<|>"; | ||
42 | let cursor_pos = match text.find(cursor) { | ||
43 | None => panic!("text should contain cursor marker"), | ||
44 | Some(pos) => pos, | ||
45 | }; | ||
46 | let mut new_text = String::with_capacity(text.len() - cursor.len()); | ||
47 | new_text.push_str(&text[..cursor_pos]); | ||
48 | new_text.push_str(&text[cursor_pos + cursor.len()..]); | ||
49 | let cursor_pos = TextUnit::from(cursor_pos as u32); | ||
50 | (cursor_pos, new_text) | ||
51 | } | ||
52 | |||
53 | pub fn extract_range(text: &str) -> (TextRange, String) { | ||
54 | let (start, text) = extract_offset(text); | ||
55 | let (end, text) = extract_offset(&text); | ||
56 | (TextRange::from_to(start, end), text) | ||
57 | } | ||
58 | |||
59 | pub fn add_cursor(text: &str, offset: TextUnit) -> String { | ||
60 | let offset: u32 = offset.into(); | ||
61 | let offset: usize = offset as usize; | ||
62 | let mut res = String::new(); | ||
63 | res.push_str(&text[..offset]); | ||
64 | res.push_str("<|>"); | ||
65 | res.push_str(&text[offset..]); | ||
66 | res | ||
67 | } | ||