-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_test.go
82 lines (73 loc) · 1.85 KB
/
buffer_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
package tsbuffer_test
import (
"github.com/stretchr/testify/require"
"github.com/weisbartb/tsbuffer"
"testing"
)
func TestNew(t *testing.T) {
b := tsbuffer.New()
require.NotEmpty(t, b)
require.Empty(t, b.String())
require.NoError(t, b.Close())
}
func TestSyncBuffer_Bytes(t *testing.T) {
b := tsbuffer.New()
n, err := b.Write([]byte("Test"))
require.Equal(t, 4, n)
require.NoError(t, err)
require.NotEmpty(t, b.Bytes())
require.Equal(t, []byte("Test"), b.Bytes())
}
func TestSyncBuffer_Close(t *testing.T) {
b := tsbuffer.New()
require.NoError(t, b.Close())
n, err := b.WriteString("test")
require.Error(t, err)
require.Equal(t, 0, n)
}
func TestSyncBuffer_Len(t *testing.T) {
b := tsbuffer.New()
_, err := b.WriteString("test")
require.NoError(t, err)
require.Equal(t, 4, b.Len())
}
func TestSyncBuffer_Read(t *testing.T) {
b := tsbuffer.New()
_, err := b.WriteString("test1test2test3")
require.NoError(t, err)
var buf = make([]byte, 5)
n, err := b.Read(buf)
require.NoError(t, err)
require.Equal(t, 5, n)
require.Equal(t, "test1", string(buf))
n, err = b.Read(buf)
require.NoError(t, err)
require.Equal(t, 5, n)
require.Equal(t, "test2", string(buf))
n, err = b.Read(buf)
require.NoError(t, err)
require.Equal(t, 5, n)
require.Equal(t, "test3", string(buf))
n, err = b.Read(buf)
require.Error(t, err)
require.Equal(t, 0, n)
}
func TestSyncBuffer_String(t *testing.T) {
b := tsbuffer.New()
n, err := b.Write([]byte("Test"))
require.Equal(t, 4, n)
require.NoError(t, err)
require.NotEmpty(t, b.Bytes())
require.Equal(t, "Test", b.String())
}
func TestSyncBuffer_Truncate(t *testing.T) {
b := tsbuffer.New()
_, err := b.WriteString("test1test2test3")
require.NoError(t, err)
var buf = make([]byte, 5)
b.Truncate(4)
n, err := b.Read(buf)
require.NoError(t, err)
require.Equal(t, 4, n)
require.Equal(t, "test\x00", string(buf))
}