From f9ac4d1a2d950d70a15e0d4c465aa889eea885ea Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sun, 22 Jun 2025 15:04:24 +0100 Subject: [PATCH] Add test --- .../utils/elongate_operations.rs | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/operation_transformation/utils/elongate_operations.rs b/src/operation_transformation/utils/elongate_operations.rs index 1368346..1a218c6 100644 --- a/src/operation_transformation/utils/elongate_operations.rs +++ b/src/operation_transformation/utils/elongate_operations.rs @@ -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> = 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 { + RawOperation::Insert(texts.iter().map(|t| Token::from(*t)).collect()) + } + + fn del(texts: &[&str]) -> RawOperation { + RawOperation::Delete(texts.iter().map(|t| Token::from(*t)).collect()) + } + + fn ins_custom(text: &str, lj: bool, rj: bool) -> RawOperation { + 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(_))); + } +}