aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/test_utils/src/lib.rs')
-rw-r--r--crates/test_utils/src/lib.rs17
1 files changed, 9 insertions, 8 deletions
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index 656dd2072..84c1d7ebb 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -3,7 +3,7 @@
3//! Most notable things are: 3//! Most notable things are:
4//! 4//!
5//! * Rich text comparison, which outputs a diff. 5//! * Rich text comparison, which outputs a diff.
6//! * Extracting markup (mainly, `<|>` markers) out of fixture strings. 6//! * Extracting markup (mainly, `$0` markers) out of fixture strings.
7//! * marks (see the eponymous module). 7//! * marks (see the eponymous module).
8 8
9#[macro_use] 9#[macro_use]
@@ -25,7 +25,8 @@ pub use rustc_hash::FxHashMap;
25 25
26pub use crate::fixture::Fixture; 26pub use crate::fixture::Fixture;
27 27
28pub const CURSOR_MARKER: &str = "<|>"; 28pub const CURSOR_MARKER: &str = "$0";
29pub const ESCAPED_CURSOR_MARKER: &str = "\\$0";
29 30
30/// Asserts that two strings are equal, otherwise displays a rich diff between them. 31/// Asserts that two strings are equal, otherwise displays a rich diff between them.
31/// 32///
@@ -62,7 +63,7 @@ pub fn extract_offset(text: &str) -> (TextSize, String) {
62 } 63 }
63} 64}
64 65
65/// Returns the offset of the first occurence of `<|>` marker and the copy of `text` 66/// Returns the offset of the first occurence of `$0` marker and the copy of `text`
66/// without the marker. 67/// without the marker.
67fn try_extract_offset(text: &str) -> Option<(TextSize, String)> { 68fn try_extract_offset(text: &str) -> Option<(TextSize, String)> {
68 let cursor_pos = text.find(CURSOR_MARKER)?; 69 let cursor_pos = text.find(CURSOR_MARKER)?;
@@ -81,7 +82,7 @@ pub fn extract_range(text: &str) -> (TextRange, String) {
81 } 82 }
82} 83}
83 84
84/// Returns `TextRange` between the first two markers `<|>...<|>` and the copy 85/// Returns `TextRange` between the first two markers `$0...$0` and the copy
85/// of `text` without both of these markers. 86/// of `text` without both of these markers.
86fn try_extract_range(text: &str) -> Option<(TextRange, String)> { 87fn try_extract_range(text: &str) -> Option<(TextRange, String)> {
87 let (start, text) = try_extract_offset(text)?; 88 let (start, text) = try_extract_offset(text)?;
@@ -104,11 +105,11 @@ impl From<RangeOrOffset> for TextRange {
104 } 105 }
105} 106}
106 107
107/// Extracts `TextRange` or `TextSize` depending on the amount of `<|>` markers 108/// Extracts `TextRange` or `TextSize` depending on the amount of `$0` markers
108/// found in `text`. 109/// found in `text`.
109/// 110///
110/// # Panics 111/// # Panics
111/// Panics if no `<|>` marker is present in the `text`. 112/// Panics if no `$0` marker is present in the `text`.
112pub fn extract_range_or_offset(text: &str) -> (RangeOrOffset, String) { 113pub fn extract_range_or_offset(text: &str) -> (RangeOrOffset, String) {
113 if let Some((range, text)) = try_extract_range(text) { 114 if let Some((range, text)) = try_extract_range(text) {
114 return (RangeOrOffset::Range(range), text); 115 return (RangeOrOffset::Range(range), text);
@@ -164,12 +165,12 @@ fn test_extract_tags() {
164 assert_eq!(actual, vec![("fn main() {}", Some("fn".into())), ("main", None),]); 165 assert_eq!(actual, vec![("fn main() {}", Some("fn".into())), ("main", None),]);
165} 166}
166 167
167/// Inserts `<|>` marker into the `text` at `offset`. 168/// Inserts `$0` marker into the `text` at `offset`.
168pub fn add_cursor(text: &str, offset: TextSize) -> String { 169pub fn add_cursor(text: &str, offset: TextSize) -> String {
169 let offset: usize = offset.into(); 170 let offset: usize = offset.into();
170 let mut res = String::new(); 171 let mut res = String::new();
171 res.push_str(&text[..offset]); 172 res.push_str(&text[..offset]);
172 res.push_str("<|>"); 173 res.push_str("$0");
173 res.push_str(&text[offset..]); 174 res.push_str(&text[offset..]);
174 res 175 res
175} 176}