aboutsummaryrefslogtreecommitdiff
path: root/libeditor/tests/test.rs
blob: e1c8aee4bfc79c94b8e175d996110e7165af6091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
extern crate libeditor;
extern crate itertools;

use std::fmt;
use itertools::Itertools;
use libeditor::{File, TextRange};

#[test]
fn test_extend_selection() {
    let file = file(r#"fn foo() {
    1 + 1
}
"#);
    let range = TextRange::offset_len(18.into(), 0.into());
    let range = file.extend_selection(range).unwrap();
    assert_eq!(range, TextRange::from_to(17.into(), 18.into()));
    let range = file.extend_selection(range).unwrap();
    assert_eq!(range, TextRange::from_to(15.into(), 20.into()));
}

#[test]
fn test_highlighting() {
    let file = file(r#"
// comment
fn main() {}
    println!("Hello, {}!", 92);
"#);
    let hls = file.highlight();
    dbg_eq(
        &hls,
        r#"[HighlightedRange { range: [1; 11), tag: "comment" },
            HighlightedRange { range: [12; 14), tag: "keyword" },
            HighlightedRange { range: [15; 19), tag: "function" },
            HighlightedRange { range: [29; 36), tag: "text" },
            HighlightedRange { range: [38; 50), tag: "string" },
            HighlightedRange { range: [52; 54), tag: "literal" }]"#
    );
}

fn file(text: &str) -> File {
    File::new(text)
}

fn dbg_eq(actual: &impl fmt::Debug, expected: &str) {
    let expected = expected.lines().map(|l| l.trim()).join(" ");
    assert_eq!(actual, expected);
}