aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils/src/lib.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-06-29 16:25:12 +0100
committerGitHub <[email protected]>2020-06-29 16:25:12 +0100
commit32ee06032785ea1792144f7263cbce93a4de467b (patch)
treefac101a18e111a2a05bce14fbb68981c5bef3bfd /crates/test_utils/src/lib.rs
parent82ce5792ab70ab8d20a1afde72c5400c27b9c190 (diff)
parentbbc4dc995612916a4c0a99396b5259a5fb1dda80 (diff)
Merge #5127
5127: Update the rest of the tests r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/test_utils/src/lib.rs')
-rw-r--r--crates/test_utils/src/lib.rs36
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
18use serde_json::Value; 18use serde_json::Value;
19use stdx::lines_with_ends;
19use text_size::{TextRange, TextSize}; 20use text_size::{TextRange, TextSize};
20 21
21pub use difference::Changeset as __Changeset; 22pub 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
164pub 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]
181fn test_extract_annotations() {
182 let text = stdx::trim_indent(
183 r#"
184fn 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.