Test adding strings at end of TextBuffer

This commit is contained in:
Matthew Gordon 2025-11-10 12:07:01 -04:00
parent 4ca32decc9
commit 01b1ed1591
1 changed files with 35 additions and 0 deletions

View File

@ -45,6 +45,16 @@ impl TextBuffer {
} }
} }
pub fn insert_text(&mut self, text: impl Into<String>, 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) { pub fn insert_char(&mut self, c: char, point: Point) {
match point { match point {
Point::End => { Point::End => {
@ -154,4 +164,29 @@ mod tests {
assert_eq!(16, target.num_chars()); assert_eq!(16, target.num_chars());
assert_eq!(5, target.num_lines()); 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());
}
} }