aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor/src/lib.rs')
-rw-r--r--crates/libeditor/src/lib.rs70
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 @@
1extern crate libsyntax2; 1extern crate libsyntax2;
2extern crate superslice; 2extern crate superslice;
3extern crate itertools; 3extern crate itertools;
4#[cfg(test)]
5#[macro_use]
6extern crate test_utils as _test_utils;
4 7
5mod extend_selection; 8mod extend_selection;
6mod symbols; 9mod symbols;
@@ -10,6 +13,8 @@ mod code_actions;
10mod typing; 13mod typing;
11mod completion; 14mod completion;
12mod scope; 15mod scope;
16#[cfg(test)]
17mod test_utils;
13 18
14use libsyntax2::{ 19use 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)]
164mod 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
172fn 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#"
190fn main() {}
191
192#[test]
193fn test_foo() {}
194
195#[test]
196#[ignore]
197fn 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}