use super::{TextEdit, apply_text_edits, apply_text_edits_checked, check_text_edits};
#[test]
fn a_malformed_plan_is_refused_whole() {
let source = "αβγ";
let inside_a_character = vec![TextEdit {
start: 1,
end: 3,
replacement: "x".to_owned(),
}];
assert!(check_text_edits(source, &inside_a_character).is_err());
assert_eq!(apply_text_edits(source, &inside_a_character), source);
assert!(apply_text_edits_checked(source, &inside_a_character).is_err());
let past_the_end = vec![TextEdit {
start: 0,
end: 99,
replacement: "x".to_owned(),
}];
assert!(check_text_edits(source, &past_the_end).is_err());
assert_eq!(apply_text_edits(source, &past_the_end), source);
let inverted = vec![TextEdit {
start: 4,
end: 2,
replacement: "x".to_owned(),
}];
assert!(check_text_edits(source, &inverted).is_err());
let overlapping = vec![
TextEdit {
start: 0,
end: 3,
replacement: "x".to_owned(),
},
TextEdit {
start: 2,
end: 6,
replacement: "y".to_owned(),
},
];
assert!(check_text_edits(source, &overlapping).is_err());
assert_eq!(apply_text_edits(source, &overlapping), source);
let disjoint = vec![
TextEdit {
start: 0,
end: 2,
replacement: "A".to_owned(),
},
TextEdit {
start: 2,
end: 4,
replacement: "B".to_owned(),
},
];
assert_eq!(check_text_edits(source, &disjoint), Ok(()));
assert_eq!(apply_text_edits(source, &disjoint), "ABγ");
}
#[test]
fn multi_byte_edits_land_on_their_boundaries() {
let source = "中::文";
let edits = vec![
TextEdit {
start: 0,
end: 3,
replacement: "甲".to_owned(),
},
TextEdit {
start: 5,
end: 8,
replacement: "乙".to_owned(),
},
];
assert_eq!(apply_text_edits(source, &edits), "甲::乙");
}