diff options
Diffstat (limited to 'crates/libeditor/src/lib.rs')
-rw-r--r-- | crates/libeditor/src/lib.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs index 34056b3c0..b2e2c4782 100644 --- a/crates/libeditor/src/lib.rs +++ b/crates/libeditor/src/lib.rs | |||
@@ -1,6 +1,9 @@ | |||
1 | extern crate libsyntax2; | 1 | extern crate libsyntax2; |
2 | extern crate superslice; | 2 | extern crate superslice; |
3 | extern crate itertools; | 3 | extern crate itertools; |
4 | #[cfg(test)] | ||
5 | #[macro_use] | ||
6 | extern crate test_utils as _test_utils; | ||
4 | 7 | ||
5 | mod extend_selection; | 8 | mod extend_selection; |
6 | mod symbols; | 9 | mod symbols; |
@@ -10,6 +13,8 @@ mod code_actions; | |||
10 | mod typing; | 13 | mod typing; |
11 | mod completion; | 14 | mod completion; |
12 | mod scope; | 15 | mod scope; |
16 | #[cfg(test)] | ||
17 | mod test_utils; | ||
13 | 18 | ||
14 | use libsyntax2::{ | 19 | use libsyntax2::{ |
15 | File, TextUnit, TextRange, SyntaxNodeRef, | 20 | File, TextUnit, TextRange, SyntaxNodeRef, |
@@ -154,3 +159,68 @@ pub fn find_node_at_offset<'a, N: AstNode<'a>>( | |||
154 | .filter_map(N::cast) | 159 | .filter_map(N::cast) |
155 | .next() | 160 | .next() |
156 | } | 161 | } |
162 | |||
163 | #[cfg(test)] | ||
164 | mod tests { | ||
165 | use super::*; | ||
166 | use test_utils::{assert_eq_dbg, extract_offset, add_cursor}; | ||
167 | |||
168 | #[test] | ||
169 | fn test_highlighting() { | ||
170 | let file = File::parse(r#" | ||
171 | // comment | ||
172 | fn main() {} | ||
173 | println!("Hello, {}!", 92); | ||
174 | "#); | ||
175 | let hls = highlight(&file); | ||
176 | assert_eq_dbg( | ||
177 | r#"[HighlightedRange { range: [1; 11), tag: "comment" }, | ||
178 | HighlightedRange { range: [12; 14), tag: "keyword" }, | ||
179 | HighlightedRange { range: [15; 19), tag: "function" }, | ||
180 | HighlightedRange { range: [29; 36), tag: "text" }, | ||
181 | HighlightedRange { range: [38; 50), tag: "string" }, | ||
182 | HighlightedRange { range: [52; 54), tag: "literal" }]"#, | ||
183 | &hls, | ||
184 | ); | ||
185 | } | ||
186 | |||
187 | #[test] | ||
188 | fn test_runnables() { | ||
189 | let file = File::parse(r#" | ||
190 | fn main() {} | ||
191 | |||
192 | #[test] | ||
193 | fn test_foo() {} | ||
194 | |||
195 | #[test] | ||
196 | #[ignore] | ||
197 | fn test_foo() {} | ||
198 | "#); | ||
199 | let runnables = runnables(&file); | ||
200 | assert_eq_dbg( | ||
201 | r#"[Runnable { range: [1; 13), kind: Bin }, | ||
202 | Runnable { range: [15; 39), kind: Test { name: "test_foo" } }, | ||
203 | Runnable { range: [41; 75), kind: Test { name: "test_foo" } }]"#, | ||
204 | &runnables, | ||
205 | ) | ||
206 | } | ||
207 | |||
208 | #[test] | ||
209 | fn test_matching_brace() { | ||
210 | fn do_check(before: &str, after: &str) { | ||
211 | let (pos, before) = extract_offset(before); | ||
212 | let file = File::parse(&before); | ||
213 | let new_pos = match matching_brace(&file, pos) { | ||
214 | None => pos, | ||
215 | Some(pos) => pos, | ||
216 | }; | ||
217 | let actual = add_cursor(&before, new_pos); | ||
218 | assert_eq_text!(after, &actual); | ||
219 | } | ||
220 | |||
221 | do_check( | ||
222 | "struct Foo { a: i32, }<|>", | ||
223 | "struct Foo <|>{ a: i32, }", | ||
224 | ); | ||
225 | } | ||
226 | } | ||