aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-06-29 13:21:57 +0100
committerAleksey Kladov <[email protected]>2020-06-29 16:23:01 +0100
commite805e8c1d5bf26e9716fb855f97d950395129c20 (patch)
treed42f406a04551626fba261e75edd752ffb1894c2 /crates/test_utils
parent82ce5792ab70ab8d20a1afde72c5400c27b9c190 (diff)
(T): make typification tests more data driven
Diffstat (limited to 'crates/test_utils')
-rw-r--r--crates/test_utils/src/lib.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index eaeeeb97b..e74f3b263 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,39 @@ 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<(TextSize, 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((offset, 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 res = extract_annotations(&trim_indent(
183 r#"
184fn main() {
185 let x = 92;
186 //^ def
187
188 x + 1
189} //^ i32
190 "#,
191 ));
192
193 assert_eq!(res, vec![(20.into(), "def".into()), (47.into(), "i32".into())]);
194}
195
162// Comparison functionality borrowed from cargo: 196// Comparison functionality borrowed from cargo:
163 197
164/// Compare a line with an expected pattern. 198/// Compare a line with an expected pattern.