2018-11-07 15:32:33 +00:00
|
|
|
use ra_syntax::{SourceFileNode, TextRange, TextUnit};
|
2018-08-28 11:47:12 +00:00
|
|
|
|
2018-12-06 18:16:37 +00:00
|
|
|
use crate::LocalEdit;
|
|
|
|
pub use test_utils::*;
|
|
|
|
|
2018-11-07 15:32:33 +00:00
|
|
|
pub fn check_action<F: Fn(&SourceFileNode, TextUnit) -> Option<LocalEdit>>(
|
|
|
|
before: &str,
|
|
|
|
after: &str,
|
|
|
|
f: F,
|
|
|
|
) {
|
2018-08-28 11:47:12 +00:00
|
|
|
let (before_cursor_pos, before) = extract_offset(before);
|
2018-11-07 15:32:33 +00:00
|
|
|
let file = SourceFileNode::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 {
|
|
|
|
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);
|
|
|
|
}
|
2018-09-05 21:59:07 +00:00
|
|
|
|
2018-11-07 15:32:33 +00:00
|
|
|
pub fn check_action_range<F: Fn(&SourceFileNode, TextRange) -> Option<LocalEdit>>(
|
2018-09-05 21:59:07 +00:00
|
|
|
before: &str,
|
|
|
|
after: &str,
|
|
|
|
f: F,
|
|
|
|
) {
|
|
|
|
let (range, before) = extract_range(before);
|
2018-11-07 15:32:33 +00:00
|
|
|
let file = SourceFileNode::parse(&before);
|
2018-09-05 21:59:07 +00:00
|
|
|
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);
|
|
|
|
}
|