-
Notifications
You must be signed in to change notification settings - Fork 8
/
printer_test.go
91 lines (79 loc) · 1.56 KB
/
printer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"bytes"
"testing"
)
var input = []string{
"xxxYYYzzz\n",
"xxxYYYzzz\n",
"xxxYYYzzz\n",
"xxxYYYzzz\n",
"xxxYYYzzz\n",
}
var printTests = []struct {
lines []string
cols int
rows int
expected string
}{
{ // Normal
input,
999,
999,
"\nxxxYYYzzz\nxxxYYYzzz\nxxxYYYzzz\nxxxYYYzzz\nxxxYYYzzz\n",
},
{ // Too many lines
input,
999,
3,
"\nxxxYYYzzz\nxxxYYYzzz\nxxxYYYzzz",
},
{ // Lines too wide
input,
3,
999,
"\nxxx\nxxx\nxxx\nxxx\nxxx\n",
},
{ // Too many too wide lines
input,
3,
3,
"\nxxx\nxxx\nxxx",
},
}
func TestPrint(t *testing.T) {
for _, tt := range printTests {
target := new(bytes.Buffer)
printer := NewPrinter(target, tt.cols, tt.rows)
for _, line := range tt.lines {
_, err := printer.Print(line)
if err != nil {
t.Errorf("error printing: %s", err)
}
}
actual := target.String()
if actual != tt.expected {
t.Errorf("wrong output written. got=%q, expected=%q", actual, tt.expected)
}
}
}
func TestReset(t *testing.T) {
testTarget := new(bytes.Buffer)
testLine := "foobar\n"
printer := NewPrinter(testTarget, 80, 1)
_, err := printer.Print(testLine)
if err != nil {
t.Errorf("error printing: %s", err)
}
printer.Reset()
_, err = printer.Print(testLine)
if err != nil {
t.Errorf("error printing: %s", err)
}
output := testTarget.String()
// Cut of the newline char on the last line
expected := "\n" + testLine + testLine[:len(testLine)-1]
if output != expected {
t.Errorf("wrong output written. expected: %q, got: %q", expected, output)
}
}