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 From bbc4dc995612916a4c0a99396b5259a5fb1dda80 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 29 Jun 2020 17:22:47 +0200 Subject: Update the rest of the tests --- crates/test_utils/src/lib.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (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 e74f3b263..caf847273 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -161,7 +161,7 @@ pub fn add_cursor(text: &str, offset: TextSize) -> String { } /// Extracts `//^ some text` annotations -pub fn extract_annotations(text: &str) -> Vec<(TextSize, String)> { +pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> { let mut res = Vec::new(); let mut prev_line_start: Option = None; let mut line_start: TextSize = 0.into(); @@ -169,7 +169,7 @@ pub fn extract_annotations(text: &str) -> Vec<(TextSize, String)> { 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)) + res.push((TextRange::at(offset, 1.into()), data)) } prev_line_start = Some(line_start); line_start += TextSize::of(line); @@ -179,18 +179,20 @@ pub fn extract_annotations(text: &str) -> Vec<(TextSize, String)> { #[test] fn test_extract_annotations() { - let res = extract_annotations(&trim_indent( + let text = stdx::trim_indent( r#" fn main() { let x = 92; //^ def - - x + 1 + z + 1 } //^ i32 "#, - )); - - assert_eq!(res, vec![(20.into(), "def".into()), (47.into(), "i32".into())]); + ); + let res = extract_annotations(&text) + .into_iter() + .map(|(range, ann)| (&text[range], ann)) + .collect::>(); + assert_eq!(res, vec![("x", "def".into()), ("z", "i32".into()),]); } // Comparison functionality borrowed from cargo: -- cgit v1.2.3