rust/crates/ra_ide_api/src/test_utils.rs

25 lines
784 B
Rust
Raw Normal View History

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