Some tests for add_text

This commit is contained in:
Kovid Goyal 2022-10-05 19:35:15 +05:30
parent 1792c2268a
commit 066465bce7
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 25 additions and 8 deletions

View File

@ -56,9 +56,9 @@ func (self *Readline) add_text(text string) {
}
has_trailing_newline := strings.HasSuffix(text, "\n")
add_line_break := func() {
new_lines = append(new_lines, "")
self.cursor_pos_in_line = 0
add_line_break := func(line string) {
new_lines = append(new_lines, line)
self.cursor_pos_in_line = len(line)
self.cursor_line += 1
}
cline := self.lines[self.cursor_line]
@ -69,17 +69,21 @@ func (self *Readline) add_text(text string) {
}
for i, line := range utils.Splitlines(text) {
if i > 0 {
add_line_break()
new_lines = append(new_lines, line)
self.cursor_pos_in_line = len(line)
add_line_break(line)
} else {
line := before_first_line + line
self.cursor_pos_in_line = len(line)
line += after_first_line
new_lines = append(new_lines, line)
}
}
if has_trailing_newline {
add_line_break()
add_line_break("")
}
if after_first_line != "" {
if len(new_lines) == 0 {
new_lines = append(new_lines, "")
}
new_lines[len(new_lines)-1] += after_first_line
}
if len(lines_after) > 0 {
new_lines = append(new_lines, lines_after...)

View File

@ -37,5 +37,18 @@ func TestAddText(t *testing.T) {
}
dt("test", nil, "test", "", "test")
dt("1234\n", nil, "1234\n", "", "1234\n")
dt("abcd", func(rl *Readline) {
rl.cursor_pos_in_line = 2
rl.add_text("12")
}, "ab12", "cd", "ab12cd")
dt("abcd", func(rl *Readline) {
rl.cursor_pos_in_line = 2
rl.add_text("12\n34")
}, "ab12\n34", "cd", "ab12\n34cd")
dt("abcd\nxyz", func(rl *Readline) {
rl.cursor_pos_in_line = 2
rl.add_text("12\n34")
}, "abcd\nxy12\n34", "z", "abcd\nxy12\n34z")
}