From 01b1ed15915365f3a863a9605650e8dbce339def Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Mon, 10 Nov 2025 12:07:01 -0400 Subject: [PATCH] Test adding strings at end of TextBuffer --- core/src/text_buffer/mod.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/core/src/text_buffer/mod.rs b/core/src/text_buffer/mod.rs index 5f81c90..7e7c764 100644 --- a/core/src/text_buffer/mod.rs +++ b/core/src/text_buffer/mod.rs @@ -45,6 +45,16 @@ impl TextBuffer { } } + pub fn insert_text(&mut self, text: impl Into, point: Point) { + match point { + Point::End => { + self.contents = self + .contents + .insert_at_char_index(self.contents.total_chars(), text); + } + } + } + pub fn insert_char(&mut self, c: char, point: Point) { match point { Point::End => { @@ -154,4 +164,29 @@ mod tests { assert_eq!(16, target.num_chars()); assert_eq!(5, target.num_lines()); } + + #[test] + fn insert_text_at_end_increases_counts_as_expected() { + let mut target = TextBuffer::new(); + target.insert_text("The", Point::End); + assert_eq!(3, target.num_bytes()); + assert_eq!(3, target.num_chars()); + assert_eq!(1, target.num_lines()); + target.insert_text(" ", Point::End); + assert_eq!(4, target.num_bytes()); + assert_eq!(4, target.num_chars()); + assert_eq!(1, target.num_lines()); + target.insert_text("quick brown", Point::End); + assert_eq!(15, target.num_bytes()); + assert_eq!(15, target.num_chars()); + assert_eq!(1, target.num_lines()); + target.insert_text(" fox\n", Point::End); + assert_eq!(20, target.num_bytes()); + assert_eq!(20, target.num_chars()); + assert_eq!(1, target.num_lines()); + target.insert_text("jumps over the lazy 猫.", Point::End); + assert_eq!(44, target.num_bytes()); + assert_eq!(42, target.num_chars()); + assert_eq!(2, target.num_lines()); + } }