-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenc_test.go
69 lines (61 loc) · 1.43 KB
/
enc_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
package jx
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestEncoder_byte_should_grow_buffer(t *testing.T) {
should := require.New(t)
e := GetEncoder()
e.byte('1')
should.Equal("1", string(e.Bytes()))
should.Equal(1, len(e.buf))
e.byte('2')
should.Equal("12", string(e.Bytes()))
should.Equal(2, len(e.buf))
e.threeBytes('3', '4', '5')
should.Equal("12345", string(e.Bytes()))
}
func TestEncoder(t *testing.T) {
data := `"hello world"`
buf := []byte(data)
var e Encoder
t.Run("Write", func(t *testing.T) {
e.Reset()
n, err := e.Write(buf)
require.NoError(t, err)
require.Equal(t, n, len(buf))
require.Equal(t, data, e.String())
})
t.Run("RawBytes", func(t *testing.T) {
e.Reset()
e.RawBytes(buf)
require.Equal(t, data, e.String())
})
t.Run("SetBytes", func(t *testing.T) {
e.Reset()
e.SetBytes(buf)
require.Equal(t, data, e.String())
})
}
func TestEncoder_Raw_should_grow_buffer(t *testing.T) {
should := require.New(t)
e := GetEncoder()
e.Raw("123")
should.Equal("123", string(e.Bytes()))
}
func TestEncoder_Str_should_grow_buffer(t *testing.T) {
should := require.New(t)
e := GetEncoder()
e.Str("123")
should.Equal(`"123"`, string(e.Bytes()))
}
func TestEncoder_ArrEmpty(t *testing.T) {
e := GetEncoder()
e.ArrEmpty()
require.Equal(t, "[]", string(e.Bytes()))
}
func TestEncoder_ObjEmpty(t *testing.T) {
e := GetEncoder()
e.ObjEmpty()
require.Equal(t, "{}", string(e.Bytes()))
}