1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 type MockTerminal struct {
18 func (c *MockTerminal) Read(data []byte) (n int, err error) {
23 if n > len(c.toSend) {
29 if c.bytesPerRead > 0 && n > c.bytesPerRead {
32 copy(data, c.toSend[:n])
33 c.toSend = c.toSend[n:]
37 func (c *MockTerminal) Write(data []byte) (n int, err error) {
38 c.received = append(c.received, data...)
42 func TestClose(t *testing.T) {
44 ss := NewTerminal(c, "> ")
45 line, err := ss.ReadLine()
47 t.Errorf("Expected empty line but got: %s", line)
50 t.Errorf("Error should have been EOF but got: %s", err)
54 var keyPressTests = []struct {
75 "a\x1b[Cb\r", // right
85 "a\177b\r", // backspace
91 func TestKeyPresses(t *testing.T) {
92 for i, test := range keyPressTests {
93 for j := 0; j < len(test.in); j++ {
95 toSend: []byte(test.in),
98 ss := NewTerminal(c, "> ")
99 line, err := ss.ReadLine()
100 if line != test.line {
101 t.Errorf("Line resulting from test %d (%d bytes per read) was '%s', expected '%s'", i, j, line, test.line)
105 t.Errorf("Error resulting from test %d (%d bytes per read) was '%v', expected '%v'", i, j, err, test.err)