...
1 package jsoniter
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/require"
7 )
8
9 func Test_writeByte_should_grow_buffer(t *testing.T) {
10 should := require.New(t)
11 stream := NewStream(ConfigDefault, nil, 1)
12 stream.writeByte('1')
13 should.Equal("1", string(stream.Buffer()))
14 should.Equal(1, len(stream.buf))
15 stream.writeByte('2')
16 should.Equal("12", string(stream.Buffer()))
17 should.Equal(2, len(stream.buf))
18 stream.writeThreeBytes('3', '4', '5')
19 should.Equal("12345", string(stream.Buffer()))
20 }
21
22 func Test_writeBytes_should_grow_buffer(t *testing.T) {
23 should := require.New(t)
24 stream := NewStream(ConfigDefault, nil, 1)
25 stream.Write([]byte{'1', '2'})
26 should.Equal("12", string(stream.Buffer()))
27 should.Equal(2, len(stream.buf))
28 stream.Write([]byte{'3', '4', '5', '6', '7'})
29 should.Equal("1234567", string(stream.Buffer()))
30 should.Equal(7, len(stream.buf))
31 }
32
33 func Test_writeIndention_should_grow_buffer(t *testing.T) {
34 should := require.New(t)
35 stream := NewStream(Config{IndentionStep: 2}.Froze(), nil, 1)
36 stream.WriteVal([]int{1, 2, 3})
37 should.Equal("[\n 1,\n 2,\n 3\n]", string(stream.Buffer()))
38 }
39
40 func Test_writeRaw_should_grow_buffer(t *testing.T) {
41 should := require.New(t)
42 stream := NewStream(ConfigDefault, nil, 1)
43 stream.WriteRaw("123")
44 should.Nil(stream.Error)
45 should.Equal("123", string(stream.Buffer()))
46 }
47
48 func Test_writeString_should_grow_buffer(t *testing.T) {
49 should := require.New(t)
50 stream := NewStream(ConfigDefault, nil, 0)
51 stream.WriteString("123")
52 should.Nil(stream.Error)
53 should.Equal(`"123"`, string(stream.Buffer()))
54 }
55
56 type NopWriter struct {
57 bufferSize int
58 }
59
60 func (w *NopWriter) Write(p []byte) (n int, err error) {
61 w.bufferSize = cap(p)
62 return len(p), nil
63 }
64
65 func Test_flush_buffer_should_stop_grow_buffer(t *testing.T) {
66
67 writer := new(NopWriter)
68 stream := NewStream(ConfigDefault, writer, 512)
69 stream.WriteArrayStart()
70 for i := 0; i < 10000000; i++ {
71 stream.WriteInt(0)
72 stream.WriteMore()
73 stream.Flush()
74 }
75 stream.WriteInt(0)
76 stream.WriteArrayEnd()
77
78
79 should := require.New(t)
80
81
82
83
84
85 should.Equal(512, writer.bufferSize)
86 }
87
View as plain text