OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package terminal | 5 package terminal |
6 | 6 |
7 import ( | 7 import ( |
8 "io" | 8 "io" |
9 "testing" | 9 "testing" |
10 ) | 10 ) |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 if err != io.EOF { | 49 if err != io.EOF { |
50 t.Errorf("Error should have been EOF but got: %s", err) | 50 t.Errorf("Error should have been EOF but got: %s", err) |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 var keyPressTests = []struct { | 54 var keyPressTests = []struct { |
55 in string | 55 in string |
56 line string | 56 line string |
57 err error | 57 err error |
58 throwAwayLines int | 58 throwAwayLines int |
| 59 handler func(key rune) (string, error, bool) |
59 }{ | 60 }{ |
60 { | 61 { |
61 err: io.EOF, | 62 err: io.EOF, |
62 }, | 63 }, |
63 { | 64 { |
64 in: "\r", | 65 in: "\r", |
65 line: "", | 66 line: "", |
66 }, | 67 }, |
67 { | 68 { |
68 in: "foo\r", | 69 in: "foo\r", |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 { | 157 { |
157 in: "£\r\x1b[A\177\r", // non-ASCII char, enter, up,
backspace. | 158 in: "£\r\x1b[A\177\r", // non-ASCII char, enter, up,
backspace. |
158 line: "", | 159 line: "", |
159 throwAwayLines: 1, | 160 throwAwayLines: 1, |
160 }, | 161 }, |
161 { | 162 { |
162 in: "£\r££\x1b[A\x1b[B\177\r", // non-ASCII char, en
ter, 2x non-ASCII, up, down, backspace, enter. | 163 in: "£\r££\x1b[A\x1b[B\177\r", // non-ASCII char, en
ter, 2x non-ASCII, up, down, backspace, enter. |
163 line: "£", | 164 line: "£", |
164 throwAwayLines: 1, | 165 throwAwayLines: 1, |
165 }, | 166 }, |
| 167 { |
| 168 in: "a\003\r", |
| 169 line: "handled", |
| 170 handler: func(key rune) (string, error, bool) { return "handled"
, io.EOF, true }, |
| 171 err: io.EOF, |
| 172 }, |
| 173 { |
| 174 in: "abc\026\r", |
| 175 line: "abc", |
| 176 handler: func(key rune) (string, error, bool) { return "ignored"
, io.EOF, false }, |
| 177 }, |
166 } | 178 } |
167 | 179 |
168 func TestKeyPresses(t *testing.T) { | 180 func TestKeyPresses(t *testing.T) { |
169 for i, test := range keyPressTests { | 181 for i, test := range keyPressTests { |
170 for j := 1; j < len(test.in); j++ { | 182 for j := 1; j < len(test.in); j++ { |
171 c := &MockTerminal{ | 183 c := &MockTerminal{ |
172 toSend: []byte(test.in), | 184 toSend: []byte(test.in), |
173 bytesPerRead: j, | 185 bytesPerRead: j, |
174 } | 186 } |
175 ss := NewTerminal(c, "> ") | 187 ss := NewTerminal(c, "> ") |
| 188 ss.SignalHandler = test.handler |
176 for k := 0; k < test.throwAwayLines; k++ { | 189 for k := 0; k < test.throwAwayLines; k++ { |
177 _, err := ss.ReadLine() | 190 _, err := ss.ReadLine() |
178 if err != nil { | 191 if err != nil { |
179 t.Errorf("Throwaway line %d from test %d
resulted in error: %s", k, i, err) | 192 t.Errorf("Throwaway line %d from test %d
resulted in error: %s", k, i, err) |
180 } | 193 } |
181 } | 194 } |
182 line, err := ss.ReadLine() | 195 line, err := ss.ReadLine() |
183 if line != test.line { | 196 if line != test.line { |
184 t.Errorf("Line resulting from test %d (%d bytes
per read) was '%s', expected '%s'", i, j, line, test.line) | 197 t.Errorf("Line resulting from test %d (%d bytes
per read) was '%s', expected '%s'", i, j, line, test.line) |
185 break | 198 break |
(...skipping 14 matching lines...) Expand all Loading... |
200 ss := NewTerminal(c, "> ") | 213 ss := NewTerminal(c, "> ") |
201 pw, _ := ss.ReadPassword("> ") | 214 pw, _ := ss.ReadPassword("> ") |
202 if pw != "password" { | 215 if pw != "password" { |
203 t.Fatalf("failed to read password, got %s", pw) | 216 t.Fatalf("failed to read password, got %s", pw) |
204 } | 217 } |
205 line, _ := ss.ReadLine() | 218 line, _ := ss.ReadLine() |
206 if len(line) > 0 { | 219 if len(line) > 0 { |
207 t.Fatalf("password was saved in history") | 220 t.Fatalf("password was saved in history") |
208 } | 221 } |
209 } | 222 } |
OLD | NEW |