aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/test_utils.rs
blob: 48c8fd1f4662d38e8d4ebd6fc7e68cf99fcfe219 (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
//! FIXME: write short doc here

use ra_syntax::{SourceFile, TextSize};
use ra_text_edit::TextEdit;

pub use test_utils::*;

pub fn check_action<F: Fn(&SourceFile, TextSize) -> Option<TextEdit>>(
    before: &str,
    after: &str,
    f: F,
) {
    let (before_cursor_pos, before) = extract_offset(before);
    let file = SourceFile::parse(&before).ok().unwrap();
    let result = f(&file, before_cursor_pos).expect("code action is not applicable");
    let actual = {
        let mut actual = before.to_string();
        result.apply(&mut actual);
        actual
    };
    let actual_cursor_pos =
        result.apply_to_offset(before_cursor_pos).expect("cursor position is affected by the edit");
    let actual = add_cursor(&actual, actual_cursor_pos);
    assert_eq_text!(after, &actual);
}