2014-05-21 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libgo / go / exp / terminal / terminal_test.go
bloba2197210e2a8d4da5720b2c797afccd21156011d
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.
5 package terminal
7 import (
8 "io"
9 "testing"
12 type MockTerminal struct {
13 toSend []byte
14 bytesPerRead int
15 received []byte
18 func (c *MockTerminal) Read(data []byte) (n int, err error) {
19 n = len(data)
20 if n == 0 {
21 return
23 if n > len(c.toSend) {
24 n = len(c.toSend)
26 if n == 0 {
27 return 0, io.EOF
29 if c.bytesPerRead > 0 && n > c.bytesPerRead {
30 n = c.bytesPerRead
32 copy(data, c.toSend[:n])
33 c.toSend = c.toSend[n:]
34 return
37 func (c *MockTerminal) Write(data []byte) (n int, err error) {
38 c.received = append(c.received, data...)
39 return len(data), nil
42 func TestClose(t *testing.T) {
43 c := &MockTerminal{}
44 ss := NewTerminal(c, "> ")
45 line, err := ss.ReadLine()
46 if line != "" {
47 t.Errorf("Expected empty line but got: %s", line)
49 if err != io.EOF {
50 t.Errorf("Error should have been EOF but got: %s", err)
54 var keyPressTests = []struct {
55 in string
56 line string
57 err error
60 "",
61 "",
62 io.EOF,
65 "\r",
66 "",
67 nil,
70 "foo\r",
71 "foo",
72 nil,
75 "a\x1b[Cb\r", // right
76 "ab",
77 nil,
80 "a\x1b[Db\r", // left
81 "ba",
82 nil,
85 "a\177b\r", // backspace
86 "b",
87 nil,
91 func TestKeyPresses(t *testing.T) {
92 for i, test := range keyPressTests {
93 for j := 0; j < len(test.in); j++ {
94 c := &MockTerminal{
95 toSend: []byte(test.in),
96 bytesPerRead: j,
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)
102 break
104 if err != test.err {
105 t.Errorf("Error resulting from test %d (%d bytes per read) was '%v', expected '%v'", i, j, err, test.err)
106 break