diff options
author | Adolfo OchagavĂa <[email protected]> | 2018-10-13 20:33:15 +0100 |
---|---|---|
committer | Adolfo OchagavĂa <[email protected]> | 2018-10-13 20:33:15 +0100 |
commit | 2bc9e9f32711047b06940c335eb5327281f8c555 (patch) | |
tree | 335a8098d74998ef54a0f3afab9f5ae87d5e707a | |
parent | c5069eeef5ba0260f2daed513a28b43ae45445bd (diff) |
Improve tests
-rw-r--r-- | crates/ra_editor/src/folding_ranges.rs | 55 | ||||
-rw-r--r-- | crates/test_utils/src/lib.rs | 38 |
2 files changed, 63 insertions, 30 deletions
diff --git a/crates/ra_editor/src/folding_ranges.rs b/crates/ra_editor/src/folding_ranges.rs index 8aabb5e0b..a1699d449 100644 --- a/crates/ra_editor/src/folding_ranges.rs +++ b/crates/ra_editor/src/folding_ranges.rs | |||
@@ -120,54 +120,65 @@ fn contiguous_range_for_comment<'a>( | |||
120 | #[cfg(test)] | 120 | #[cfg(test)] |
121 | mod tests { | 121 | mod tests { |
122 | use super::*; | 122 | use super::*; |
123 | use test_utils::extract_ranges; | ||
124 | |||
125 | fn do_check(text: &str, fold_kinds: &[FoldKind]) { | ||
126 | let (ranges, text) = extract_ranges(text); | ||
127 | let file = File::parse(&text); | ||
128 | let folds = folding_ranges(&file); | ||
129 | |||
130 | assert_eq!(folds.len(), ranges.len()); | ||
131 | for ((fold, range), fold_kind) in folds.into_iter().zip(ranges.into_iter()).zip(fold_kinds.into_iter()) { | ||
132 | assert_eq!(fold.range.start(), range.start()); | ||
133 | assert_eq!(fold.range.end(), range.end()); | ||
134 | assert_eq!(&fold.kind, fold_kind); | ||
135 | } | ||
136 | } | ||
123 | 137 | ||
124 | #[test] | 138 | #[test] |
125 | fn test_fold_comments() { | 139 | fn test_fold_comments() { |
126 | let text = r#" | 140 | let text = r#" |
127 | // Hello | 141 | <|>// Hello |
128 | // this is a multiline | 142 | // this is a multiline |
129 | // comment | 143 | // comment |
130 | // | 144 | //<|> |
131 | 145 | ||
132 | // But this is not | 146 | // But this is not |
133 | 147 | ||
134 | fn main() { | 148 | fn main() { |
135 | // We should | 149 | <|>// We should |
136 | // also | 150 | // also |
137 | // fold | 151 | // fold |
138 | // this one. | 152 | // this one.<|> |
153 | <|>//! But this one is different | ||
154 | //! because it has another flavor<|> | ||
155 | <|>/* As does this | ||
156 | multiline comment */<|> | ||
139 | }"#; | 157 | }"#; |
140 | 158 | ||
141 | let file = File::parse(&text); | 159 | let fold_kinds = &[ |
142 | let folds = folding_ranges(&file); | 160 | FoldKind::Comment, |
143 | assert_eq!(folds.len(), 2); | 161 | FoldKind::Comment, |
144 | assert_eq!(folds[0].range.start(), 1.into()); | 162 | FoldKind::Comment, |
145 | assert_eq!(folds[0].range.end(), 46.into()); | 163 | FoldKind::Comment, |
146 | assert_eq!(folds[0].kind, FoldKind::Comment); | 164 | ]; |
147 | 165 | do_check(text, fold_kinds); | |
148 | assert_eq!(folds[1].range.start(), 84.into()); | ||
149 | assert_eq!(folds[1].range.end(), 137.into()); | ||
150 | assert_eq!(folds[1].kind, FoldKind::Comment); | ||
151 | } | 166 | } |
152 | 167 | ||
153 | #[test] | 168 | #[test] |
154 | fn test_fold_imports() { | 169 | fn test_fold_imports() { |
155 | let text = r#" | 170 | let text = r#" |
156 | use std::{ | 171 | <|>use std::{ |
157 | str, | 172 | str, |
158 | vec, | 173 | vec, |
159 | io as iop | 174 | io as iop |
160 | }; | 175 | };<|> |
161 | 176 | ||
162 | fn main() { | 177 | fn main() { |
163 | }"#; | 178 | }"#; |
164 | 179 | ||
165 | let file = File::parse(&text); | 180 | let folds = &[FoldKind::Imports]; |
166 | let folds = folding_ranges(&file); | 181 | do_check(text, folds); |
167 | assert_eq!(folds.len(), 1); | ||
168 | assert_eq!(folds[0].range.start(), 1.into()); | ||
169 | assert_eq!(folds[0].range.end(), 46.into()); | ||
170 | assert_eq!(folds[0].kind, FoldKind::Imports); | ||
171 | } | 182 | } |
172 | 183 | ||
173 | 184 | ||
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 068eb80ce..ee73153f0 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs | |||
@@ -38,22 +38,44 @@ pub fn assert_eq_dbg(expected: &str, actual: &impl fmt::Debug) { | |||
38 | } | 38 | } |
39 | 39 | ||
40 | pub fn extract_offset(text: &str) -> (TextUnit, String) { | 40 | pub fn extract_offset(text: &str) -> (TextUnit, String) { |
41 | let cursor = "<|>"; | 41 | match try_extract_offset(text) { |
42 | let cursor_pos = match text.find(cursor) { | ||
43 | None => panic!("text should contain cursor marker"), | 42 | None => panic!("text should contain cursor marker"), |
44 | Some(pos) => pos, | 43 | Some(result) => result, |
45 | }; | 44 | } |
45 | } | ||
46 | |||
47 | pub fn try_extract_offset(text: &str) -> Option<(TextUnit, String)> { | ||
48 | let cursor = "<|>"; | ||
49 | let cursor_pos = text.find(cursor)?; | ||
46 | let mut new_text = String::with_capacity(text.len() - cursor.len()); | 50 | let mut new_text = String::with_capacity(text.len() - cursor.len()); |
47 | new_text.push_str(&text[..cursor_pos]); | 51 | new_text.push_str(&text[..cursor_pos]); |
48 | new_text.push_str(&text[cursor_pos + cursor.len()..]); | 52 | new_text.push_str(&text[cursor_pos + cursor.len()..]); |
49 | let cursor_pos = TextUnit::from(cursor_pos as u32); | 53 | let cursor_pos = TextUnit::from(cursor_pos as u32); |
50 | (cursor_pos, new_text) | 54 | Some((cursor_pos, new_text)) |
51 | } | 55 | } |
52 | 56 | ||
53 | pub fn extract_range(text: &str) -> (TextRange, String) { | 57 | pub fn extract_range(text: &str) -> (TextRange, String) { |
54 | let (start, text) = extract_offset(text); | 58 | match try_extract_range(text) { |
55 | let (end, text) = extract_offset(&text); | 59 | None => panic!("text should contain cursor marker"), |
56 | (TextRange::from_to(start, end), text) | 60 | Some(result) => result, |
61 | } | ||
62 | } | ||
63 | |||
64 | pub fn try_extract_range(text: &str) -> Option<(TextRange, String)> { | ||
65 | let (start, text) = try_extract_offset(text)?; | ||
66 | let (end, text) = try_extract_offset(&text)?; | ||
67 | Some((TextRange::from_to(start, end), text)) | ||
68 | } | ||
69 | |||
70 | pub fn extract_ranges(text: &str) -> (Vec<TextRange>, String) { | ||
71 | let mut ranges = Vec::new(); | ||
72 | let mut text = String::from(text); | ||
73 | while let Some((range, new_text)) = try_extract_range(&text) { | ||
74 | text = new_text; | ||
75 | ranges.push(range); | ||
76 | } | ||
77 | |||
78 | (ranges, text) | ||
57 | } | 79 | } |
58 | 80 | ||
59 | pub fn add_cursor(text: &str, offset: TextUnit) -> String { | 81 | pub fn add_cursor(text: &str, offset: TextUnit) -> String { |