Skip to content

Commit cb5628d

Browse files
committed
Adding tests
1 parent 518f25b commit cb5628d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

terminal_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package term
66

77
import (
88
"bytes"
9+
"errors"
10+
"fmt"
911
"io"
1012
"os"
1113
"runtime"
@@ -208,12 +210,24 @@ var keyPressTests = []struct {
208210
line: "efgh",
209211
throwAwayLines: 1,
210212
},
213+
{
214+
// Newline in bracketed paste mode should still work.
215+
in: "abc\x1b[200~d\nefg\x1b[201~h\r",
216+
line: "efgh",
217+
throwAwayLines: 1,
218+
},
211219
{
212220
// Lines consisting entirely of pasted data should be indicated as such.
213221
in: "\x1b[200~a\r",
214222
line: "a",
215223
err: ErrPasteIndicator,
216224
},
225+
{
226+
// Lines consisting entirely of pasted data should be indicated as such (\n paste).
227+
in: "\x1b[200~a\n",
228+
line: "a",
229+
err: ErrPasteIndicator,
230+
},
217231
{
218232
// Ctrl-C terminates readline
219233
in: "\003",
@@ -296,6 +310,32 @@ func TestRender(t *testing.T) {
296310
}
297311
}
298312

313+
func TestCRLF(t *testing.T) {
314+
c := &MockTerminal{
315+
toSend: []byte("line1\rline2\r\nline3\n"),
316+
// bytesPerRead 0 means read all at once - CR+LF need to be in same read which is what terminals would do.
317+
}
318+
319+
ss := NewTerminal(c, "> ")
320+
for i := range 3 {
321+
line, err := ss.ReadLine()
322+
if err != nil {
323+
t.Fatalf("failed to read line %d: %v", i+1, err)
324+
}
325+
expected := fmt.Sprintf("line%d", i+1)
326+
if line != expected {
327+
t.Fatalf("expected '%s', got '%s'", expected, line)
328+
}
329+
}
330+
line, err := ss.ReadLine()
331+
if !errors.Is(err, io.EOF) {
332+
t.Fatalf("expected EOF after 3 lines, got '%s' with error %v", line, err)
333+
}
334+
if line != "" {
335+
t.Fatalf("expected empty line after EOF, got '%s'", line)
336+
}
337+
}
338+
299339
func TestPasswordNotSaved(t *testing.T) {
300340
c := &MockTerminal{
301341
toSend: []byte("password\r\x1b[A\r"),

0 commit comments

Comments
 (0)