(T): make typification tests more data driven

This commit is contained in:
Aleksey Kladov 2020-06-29 14:21:57 +02:00
parent 82ce5792ab
commit e805e8c1d5
5 changed files with 137 additions and 133 deletions

View file

@ -16,6 +16,7 @@ use std::{
};
use serde_json::Value;
use stdx::lines_with_ends;
use text_size::{TextRange, TextSize};
pub use difference::Changeset as __Changeset;
@ -159,6 +160,39 @@ pub fn add_cursor(text: &str, offset: TextSize) -> String {
res
}
/// Extracts `//^ some text` annotations
pub fn extract_annotations(text: &str) -> Vec<(TextSize, String)> {
let mut res = Vec::new();
let mut prev_line_start: Option<TextSize> = None;
let mut line_start: TextSize = 0.into();
for line in lines_with_ends(text) {
if let Some(idx) = line.find("//^") {
let offset = prev_line_start.unwrap() + TextSize::of(&line[..idx + "//".len()]);
let data = line[idx + "//^".len()..].trim().to_string();
res.push((offset, data))
}
prev_line_start = Some(line_start);
line_start += TextSize::of(line);
}
res
}
#[test]
fn test_extract_annotations() {
let res = extract_annotations(&trim_indent(
r#"
fn main() {
let x = 92;
//^ def
x + 1
} //^ i32
"#,
));
assert_eq!(res, vec![(20.into(), "def".into()), (47.into(), "i32".into())]);
}
// Comparison functionality borrowed from cargo:
/// Compare a line with an expected pattern.