diff options
Diffstat (limited to 'crates/test_utils')
-rw-r--r-- | crates/test_utils/src/lib.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index eaeeeb97b..caf847273 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs | |||
@@ -16,6 +16,7 @@ use std::{ | |||
16 | }; | 16 | }; |
17 | 17 | ||
18 | use serde_json::Value; | 18 | use serde_json::Value; |
19 | use stdx::lines_with_ends; | ||
19 | use text_size::{TextRange, TextSize}; | 20 | use text_size::{TextRange, TextSize}; |
20 | 21 | ||
21 | pub use difference::Changeset as __Changeset; | 22 | pub use difference::Changeset as __Changeset; |
@@ -159,6 +160,41 @@ pub fn add_cursor(text: &str, offset: TextSize) -> String { | |||
159 | res | 160 | res |
160 | } | 161 | } |
161 | 162 | ||
163 | /// Extracts `//^ some text` annotations | ||
164 | pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> { | ||
165 | let mut res = Vec::new(); | ||
166 | let mut prev_line_start: Option<TextSize> = None; | ||
167 | let mut line_start: TextSize = 0.into(); | ||
168 | for line in lines_with_ends(text) { | ||
169 | if let Some(idx) = line.find("//^") { | ||
170 | let offset = prev_line_start.unwrap() + TextSize::of(&line[..idx + "//".len()]); | ||
171 | let data = line[idx + "//^".len()..].trim().to_string(); | ||
172 | res.push((TextRange::at(offset, 1.into()), data)) | ||
173 | } | ||
174 | prev_line_start = Some(line_start); | ||
175 | line_start += TextSize::of(line); | ||
176 | } | ||
177 | res | ||
178 | } | ||
179 | |||
180 | #[test] | ||
181 | fn test_extract_annotations() { | ||
182 | let text = stdx::trim_indent( | ||
183 | r#" | ||
184 | fn main() { | ||
185 | let x = 92; | ||
186 | //^ def | ||
187 | z + 1 | ||
188 | } //^ i32 | ||
189 | "#, | ||
190 | ); | ||
191 | let res = extract_annotations(&text) | ||
192 | .into_iter() | ||
193 | .map(|(range, ann)| (&text[range], ann)) | ||
194 | .collect::<Vec<_>>(); | ||
195 | assert_eq!(res, vec![("x", "def".into()), ("z", "i32".into()),]); | ||
196 | } | ||
197 | |||
162 | // Comparison functionality borrowed from cargo: | 198 | // Comparison functionality borrowed from cargo: |
163 | 199 | ||
164 | /// Compare a line with an expected pattern. | 200 | /// Compare a line with an expected pattern. |