aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light/src/test_utils.rs
blob: 22ded243553b9a49bec97d4dbd260f4505a42313 (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
48
49
50
51
52
53
use ra_syntax::{SourceFile, TextRange, TextUnit};

use crate::LocalEdit;
pub use test_utils::*;

pub fn check_action<F: Fn(&SourceFile, TextUnit) -> Option<LocalEdit>>(
    before: &str,
    after: &str,
    f: F,
) {
    let (before_cursor_pos, before) = extract_offset(before);
    let file = SourceFile::parse(&before);
    let result = f(&file, before_cursor_pos).expect("code action is not applicable");
    let actual = result.edit.apply(&before);
    let actual_cursor_pos = match result.cursor_position {
        None => result
            .edit
            .apply_to_offset(before_cursor_pos)
            .expect("cursor position is affected by the edit"),
        Some(off) => off,
    };
    let actual = add_cursor(&actual, actual_cursor_pos);
    assert_eq_text!(after, &actual);
}

pub fn check_action_not_applicable<F: Fn(&SourceFile, TextUnit) -> Option<LocalEdit>>(
    text: &str,
    f: F,
) {
    let (text_cursor_pos, text) = extract_offset(text);
    let file = SourceFile::parse(&text);
    assert!(
        f(&file, text_cursor_pos).is_none(),
        "code action is applicable but it shouldn't"
    );
}

pub fn check_action_range<F: Fn(&SourceFile, TextRange) -> Option<LocalEdit>>(
    before: &str,
    after: &str,
    f: F,
) {
    let (range, before) = extract_range(before);
    let file = SourceFile::parse(&before);
    let result = f(&file, range).expect("code action is not applicable");
    let actual = result.edit.apply(&before);
    let actual_cursor_pos = match result.cursor_position {
        None => result.edit.apply_to_offset(range.start()).unwrap(),
        Some(off) => off,
    };
    let actual = add_cursor(&actual, actual_cursor_pos);
    assert_eq_text!(after, &actual);
}