aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-12-20 19:30:30 +0000
committerAleksey Kladov <[email protected]>2018-12-20 19:30:30 +0000
commit8d7e8a175e6067e4956a23cfd0c1baf003678734 (patch)
tree8c30485c8652cc0c8a7d442744eaead61706c9e6 /crates/test_utils
parent61dcaa6addcb3fca522b298bdba12239772eb81e (diff)
generalize folding tests
By using xml-like tags, we will be able to test nested foldings.
Diffstat (limited to 'crates/test_utils')
-rw-r--r--crates/test_utils/src/lib.rs38
1 files changed, 31 insertions, 7 deletions
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)> {
66 Some((TextRange::from_to(start, end), text)) 66 Some((TextRange::from_to(start, end), text))
67} 67}
68 68
69pub fn extract_ranges(text: &str) -> (Vec<TextRange>, String) { 69/// Extracts ranges, marked with `<tag> </tag>` paris from the `text`
70pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec<TextRange>, String) {
71 let open = format!("<{}>", tag);
72 let close = format!("</{}>", tag);
70 let mut ranges = Vec::new(); 73 let mut ranges = Vec::new();
71 let mut text = String::from(text); 74 let mut res = String::new();
72 while let Some((range, new_text)) = try_extract_range(&text) { 75 let mut stack = Vec::new();
73 text = new_text; 76 loop {
74 ranges.push(range); 77 match text.find('<') {
78 None => {
79 res.push_str(text);
80 break;
81 }
82 Some(i) => {
83 res.push_str(&text[..i]);
84 text = &text[i..];
85 if text.starts_with(&open) {
86 text = &text[open.len()..];
87 let from = TextUnit::of_str(&res);
88 stack.push(from);
89 } else if text.starts_with(&close) {
90 text = &text[close.len()..];
91 let from = stack
92 .pop()
93 .unwrap_or_else(|| panic!("unmatched </{}>", tag));
94 let to = TextUnit::of_str(&res);
95 ranges.push(TextRange::from_to(from, to));
96 }
97 }
98 }
75 } 99 }
76 100 assert!(stack.is_empty(), "unmatched <{}>", tag);
77 (ranges, text) 101 (ranges, res)
78} 102}
79 103
80pub fn add_cursor(text: &str, offset: TextUnit) -> String { 104pub fn add_cursor(text: &str, offset: TextUnit) -> String {