From e805e8c1d5bf26e9716fb855f97d950395129c20 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 29 Jun 2020 14:21:57 +0200 Subject: (T): make typification tests more data driven --- crates/test_utils/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'crates/test_utils/src/lib.rs') 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::{ }; 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 = 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. -- cgit v1.2.3