| 1 |
// Copyright 2015 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. |
| 4 |
|
| 5 |
package exec |
| 6 |
|
| 7 |
import ( |
| 8 |
"io" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestPrefixSuffixSaver(t *testing.T) { |
| 13 |
tests := []struct { |
| 14 |
N int |
| 15 |
writes []string |
| 16 |
want string |
| 17 |
}{ |
| 18 |
{ |
| 19 |
N: 2, |
| 20 |
writes: nil, |
| 21 |
want: "", |
| 22 |
}, |
| 23 |
{ |
| 24 |
N: 2, |
| 25 |
writes: []string{"a"}, |
| 26 |
want: "a", |
| 27 |
}, |
| 28 |
{ |
| 29 |
N: 2, |
| 30 |
writes: []string{"abc", "d"}, |
| 31 |
want: "abcd", |
| 32 |
}, |
| 33 |
{ |
| 34 |
N: 2, |
| 35 |
writes: []string{"abc", "d", "e"}, |
| 36 |
want: "ab\n... omitting 1 bytes ...\nde", |
| 37 |
}, |
| 38 |
{ |
| 39 |
N: 2, |
| 40 |
writes: []string{"ab______________________yz"}, |
| 41 |
want: "ab\n... omitting 22 bytes ...\nyz", |
| 42 |
}, |
| 43 |
{ |
| 44 |
N: 2, |
| 45 |
writes: []string{"ab_______________________y", "z"}, |
| 46 |
want: "ab\n... omitting 23 bytes ...\nyz", |
| 47 |
}, |
| 48 |
} |
| 49 |
for i, tt := range tests { |
| 50 |
w := &prefixSuffixSaver{N: tt.N} |
| 51 |
for _, s := range tt.writes { |
| 52 |
n, err := io.WriteString(w, s) |
| 53 |
if err != nil || n != len(s) { |
| 54 |
t.Errorf("%d. WriteString(%q) = %v, %v; want %v, %v", i, s, n, err, len(s), nil) |
| 55 |
} |
| 56 |
} |
| 57 |
if got := string(w.Bytes()); got != tt.want { |
| 58 |
t.Errorf("%d. Bytes = %q; want %q", i, got, tt.want) |
| 59 |
} |
| 60 |
} |
| 61 |
} |