rust-analyzer/crates/ra_ide_api_light/src/test_utils.rs

25 lines
784 B
Rust
Raw Normal View History

2019-02-03 18:26:35 +00:00
use ra_syntax::{SourceFile, TextUnit};
2018-08-28 11:47:12 +00:00
2018-12-06 18:16:37 +00:00
use crate::LocalEdit;
pub use test_utils::*;
2019-01-07 13:53:24 +00:00
pub fn check_action<F: Fn(&SourceFile, TextUnit) -> Option<LocalEdit>>(
2018-11-07 15:32:33 +00:00
before: &str,
after: &str,
f: F,
) {
2018-08-28 11:47:12 +00:00
let (before_cursor_pos, before) = extract_offset(before);
2019-01-07 13:53:24 +00:00
let file = SourceFile::parse(&before);
2018-08-28 11:47:12 +00:00
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 {
2019-01-08 11:21:29 +00:00
None => result
.edit
.apply_to_offset(before_cursor_pos)
.expect("cursor position is affected by the edit"),
2018-08-28 11:47:12 +00:00
Some(off) => off,
};
let actual = add_cursor(&actual, actual_cursor_pos);
assert_eq_text!(after, &actual);
}