rust-analyzer/crates/ra_editor/src/test_utils.rs
2018-09-16 13:07:39 +03:00

37 lines
1.2 KiB
Rust

use ra_syntax::{File, TextUnit, TextRange};
pub use _test_utils::*;
use LocalEdit;
pub fn check_action<F: Fn(&File, TextUnit) -> Option<LocalEdit>> (
before: &str,
after: &str,
f: F,
) {
let (before_cursor_pos, before) = extract_offset(before);
let file = File::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).unwrap(),
Some(off) => off,
};
let actual = add_cursor(&actual, actual_cursor_pos);
assert_eq_text!(after, &actual);
}
pub fn check_action_range<F: Fn(&File, TextRange) -> Option<LocalEdit>> (
before: &str,
after: &str,
f: F,
) {
let (range, before) = extract_range(before);
let file = File::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);
}