Test adding strings at end of TextBuffer
This commit is contained in:
parent
4ca32decc9
commit
01b1ed1591
|
|
@ -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) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue