aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/tests/test.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor/tests/test.rs')
-rw-r--r--crates/libeditor/tests/test.rs69
1 files changed, 69 insertions, 0 deletions
diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs
new file mode 100644
index 000000000..2a84c5080
--- /dev/null
+++ b/crates/libeditor/tests/test.rs
@@ -0,0 +1,69 @@
1extern crate libeditor;
2extern crate itertools;
3
4use std::fmt;
5use itertools::Itertools;
6use libeditor::{ast, highlight, runnables, extend_selection, TextRange};
7
8#[test]
9fn test_extend_selection() {
10 let file = file(r#"fn foo() {
11 1 + 1
12}
13"#);
14 let range = TextRange::offset_len(18.into(), 0.into());
15 let range = extend_selection(&file, range).unwrap();
16 assert_eq!(range, TextRange::from_to(17.into(), 18.into()));
17 let range = extend_selection(&file, range).unwrap();
18 assert_eq!(range, TextRange::from_to(15.into(), 20.into()));
19}
20
21#[test]
22fn test_highlighting() {
23 let file = file(r#"
24// comment
25fn main() {}
26 println!("Hello, {}!", 92);
27"#);
28 let hls = highlight(&file);
29 dbg_eq(
30 &hls,
31 r#"[HighlightedRange { range: [1; 11), tag: "comment" },
32 HighlightedRange { range: [12; 14), tag: "keyword" },
33 HighlightedRange { range: [15; 19), tag: "function" },
34 HighlightedRange { range: [29; 36), tag: "text" },
35 HighlightedRange { range: [38; 50), tag: "string" },
36 HighlightedRange { range: [52; 54), tag: "literal" }]"#
37 );
38}
39
40#[test]
41fn test_runnables() {
42 let file = file(r#"
43fn main() {}
44
45#[test]
46fn test_foo() {}
47
48#[test]
49#[ignore]
50fn test_foo() {}
51"#);
52 let runnables = runnables(&file);
53 dbg_eq(
54 &runnables,
55 r#"[Runnable { range: [1; 13), kind: Bin },
56 Runnable { range: [15; 39), kind: Test { name: "test_foo" } },
57 Runnable { range: [41; 75), kind: Test { name: "test_foo" } }]"#,
58 )
59}
60
61fn file(text: &str) -> ast::File {
62 ast::File::parse(text)
63}
64
65fn dbg_eq(actual: &impl fmt::Debug, expected: &str) {
66 let actual = format!("{:?}", actual);
67 let expected = expected.lines().map(|l| l.trim()).join(" ");
68 assert_eq!(actual, expected);
69}