-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter_test.go
80 lines (64 loc) · 1.52 KB
/
writer_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
package textbuffer_test
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/ykhrustalev/textbuffer"
"testing"
)
func TestWriter(t *testing.T) {
var buf bytes.Buffer
w := textbuffer.NewWriter(&buf, 3)
n, err := w.Write([]byte("foo"))
assert.NoError(t, err)
assert.Equal(t, 3, n)
assert.Equal(t, "", buf.String())
n, err = w.WriteString("hello")
assert.NoError(t, err)
assert.Equal(t, 5, n)
assert.Equal(t, "", buf.String())
n, err = w.WriteString("")
assert.NoError(t, err)
assert.Equal(t, 0, n)
assert.Equal(t, "foohello", buf.String())
n, err = w.WriteString("1")
assert.NoError(t, err)
assert.Equal(t, 1, n)
assert.Equal(t, "foohello", buf.String())
n, err = w.WriteString("2")
assert.NoError(t, err)
assert.Equal(t, 1, n)
assert.Equal(t, "foohello", buf.String())
n, err = w.WriteString("3")
assert.NoError(t, err)
assert.Equal(t, 1, n)
assert.Equal(t, "foohello123", buf.String())
n, err = w.WriteString("bar")
assert.NoError(t, err)
assert.Equal(t, 3, n)
assert.NoError(t, w.Flush())
assert.Equal(t, "foohello123bar", buf.String())
}
func Example_basic() {
var buf bytes.Buffer
w := textbuffer.NewWriter(&buf, 2)
_, _ = w.Write([]byte("first\n"))
fmt.Println("Action#1")
fmt.Println(buf.String())
_, _ = w.WriteString("second\n")
fmt.Println("Action#2")
fmt.Println(buf.String())
_, _ = w.Write([]byte("third\n"))
fmt.Println("Action#3")
fmt.Println(buf.String())
// Output:
// Action#1
//
// Action#2
// first
// second
//
// Action#3
// first
// second
}