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/src/lib.rs') 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 From 23b040962ff299feeef1f967bc2d5ba92b01c2bc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 20 Dec 2018 22:13:16 +0300 Subject: fold curly blocks --- crates/test_utils/src/lib.rs | 1 + 1 file changed, 1 insertion(+) (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 894a22769..1ae800d7c 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -98,6 +98,7 @@ pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec, String) { } } assert!(stack.is_empty(), "unmatched <{}>", tag); + ranges.sort_by_key(|r| (r.start(), r.end())); (ranges, res) } -- cgit v1.2.3