rust/crates/ra_ide/src/test_utils.rs

22 lines
715 B
Rust
Raw Normal View History

//! FIXME: write short doc here
2019-02-03 12:26:35 -06:00
use ra_syntax::{SourceFile, TextUnit};
2019-03-22 11:10:20 -05:00
use ra_text_edit::TextEdit;
2018-08-28 06:47:12 -05:00
2018-12-06 12:16:37 -06:00
pub use test_utils::*;
2019-03-22 11:10:20 -05:00
pub fn check_action<F: Fn(&SourceFile, TextUnit) -> Option<TextEdit>>(
2018-11-07 09:32:33 -06:00
before: &str,
after: &str,
f: F,
) {
2018-08-28 06:47:12 -05:00
let (before_cursor_pos, before) = extract_offset(before);
2019-05-28 10:46:11 -05:00
let file = SourceFile::parse(&before).ok().unwrap();
2018-08-28 06:47:12 -05:00
let result = f(&file, before_cursor_pos).expect("code action is not applicable");
2019-03-22 11:10:20 -05:00
let actual = result.apply(&before);
let actual_cursor_pos =
result.apply_to_offset(before_cursor_pos).expect("cursor position is affected by the edit");
2018-08-28 06:47:12 -05:00
let actual = add_cursor(&actual, actual_cursor_pos);
assert_eq_text!(after, &actual);
}