diff options
Diffstat (limited to 'crates/test_utils')
-rw-r--r-- | crates/test_utils/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/test_utils/src/lib.rs | 31 |
2 files changed, 32 insertions, 0 deletions
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 | } | ||