fix: caught textchange diff bug, added tests

This commit is contained in:
əlemi 2023-11-30 00:37:57 +01:00
parent d562e406f2
commit b4b955a39e

View file

@ -37,11 +37,11 @@ impl TextChange {
let mut from_beginning = true; let mut from_beginning = true;
for op in diff.ops() { for op in diff.ops() {
match op { match op {
similar::DiffOp::Equal { .. } => { similar::DiffOp::Equal { len, .. } => {
if from_beginning { if from_beginning {
start += 1 start += len
} else { } else {
end += 1 end += len
} }
}, },
_ => { _ => {
@ -67,3 +67,36 @@ impl TextChange {
RowCol { row, col } RowCol { row, col }
} }
} }
#[cfg(test)]
mod tests {
#[test]
fn textchange_diff_works_for_deletions() {
let change = super::TextChange::from_diff(
"sphinx of black quartz, judge my vow",
"sphinx of quartz, judge my vow"
);
assert_eq!(change.span, 10..16);
assert_eq!(change.content, "");
}
#[test]
fn textchange_diff_works_for_insertions() {
let change = super::TextChange::from_diff(
"sphinx of quartz, judge my vow",
"sphinx of black quartz, judge my vow"
);
assert_eq!(change.span, 10..10);
assert_eq!(change.content, "black ");
}
#[test]
fn textchange_diff_works_for_changes() {
let change = super::TextChange::from_diff(
"sphinx of black quartz, judge my vow",
"sphinx who watches the desert, judge my vow"
);
assert_eq!(change.span, 7..22);
assert_eq!(change.content, "who watches the desert");
}
}