This commit is contained in:
Andras Schmelczer 2025-06-22 15:04:24 +01:00
parent 9af06183e7
commit f9ac4d1a2d
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -19,7 +19,6 @@ where
// We don't elongate `equals` as they're needed to maintain cursor positions
// when merging against deletes.
let mut result: Vec<RawOperation<T>> = raw_operations
.into_iter()
.flat_map(|next| match next {
@ -63,3 +62,61 @@ where
result
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokenizer::token::Token;
// Helper constructors for cleaner tests
fn ins(texts: &[&str]) -> RawOperation<String> {
RawOperation::Insert(texts.iter().map(|t| Token::from(*t)).collect())
}
fn del(texts: &[&str]) -> RawOperation<String> {
RawOperation::Delete(texts.iter().map(|t| Token::from(*t)).collect())
}
fn ins_custom(text: &str, lj: bool, rj: bool) -> RawOperation<String> {
RawOperation::Insert(vec![Token::new(text.to_string(), text.to_string(), lj, rj)])
}
#[test]
fn merges_adjacent_joinable_inserts() {
let ops = vec![ins(&["a"]), ins(&["b"]), ins(&["c"])];
let result = elongate_operations(ops);
assert_eq!(result.len(), 1);
match &result[0] {
RawOperation::Insert(tokens) => {
let originals: String = tokens.iter().map(|t| t.original()).collect();
assert_eq!(originals, "abc");
}
_ => panic!("Expected single Insert operation"),
}
}
#[test]
fn does_not_merge_when_not_joinable() {
let ops = vec![
ins_custom("a", true, false), // not right-joinable
ins_custom("b", true, true), // left-joinable but previous isn't right-joinable
];
let result = elongate_operations(ops);
assert_eq!(
result.len(),
2,
"Operations should remain separate when not joinable"
);
}
#[test]
fn merges_interleaved_insert_delete_sequences() {
// Pattern IDID -> II DD
let ops = vec![ins(&["i1"]), del(&["d1"]), ins(&["i2"]), del(&["d2"])];
let result = elongate_operations(ops);
assert_eq!(result.len(), 2);
assert!(matches!(result[0], RawOperation::Delete(_)));
assert!(matches!(result[1], RawOperation::Insert(_)));
}
}