From 8d7e8a175e6067e4956a23cfd0c1baf003678734 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 20 Dec 2018 22:30:30 +0300 Subject: generalize folding tests By using xml-like tags, we will be able to test nested foldings. --- crates/test_utils/src/lib.rs | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'crates/test_utils') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 0a94adb74..894a22769 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -66,15 +66,39 @@ pub fn try_extract_range(text: &str) -> Option<(TextRange, String)> { Some((TextRange::from_to(start, end), text)) } -pub fn extract_ranges(text: &str) -> (Vec, String) { +/// Extracts ranges, marked with ` ` paris from the `text` +pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec, String) { + let open = format!("<{}>", tag); + let close = format!("", tag); let mut ranges = Vec::new(); - let mut text = String::from(text); - while let Some((range, new_text)) = try_extract_range(&text) { - text = new_text; - ranges.push(range); + let mut res = String::new(); + let mut stack = Vec::new(); + loop { + match text.find('<') { + None => { + res.push_str(text); + break; + } + Some(i) => { + res.push_str(&text[..i]); + text = &text[i..]; + if text.starts_with(&open) { + text = &text[open.len()..]; + let from = TextUnit::of_str(&res); + stack.push(from); + } else if text.starts_with(&close) { + text = &text[close.len()..]; + let from = stack + .pop() + .unwrap_or_else(|| panic!("unmatched ", tag)); + let to = TextUnit::of_str(&res); + ranges.push(TextRange::from_to(from, to)); + } + } + } } - - (ranges, text) + assert!(stack.is_empty(), "unmatched <{}>", tag); + (ranges, res) } pub fn add_cursor(text: &str, offset: TextUnit) -> String { -- cgit v1.2.3