-
Notifications
You must be signed in to change notification settings - Fork 4
/
enc_comma_test.go
65 lines (59 loc) · 1.13 KB
/
enc_comma_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
package jx
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestEncoder_comma(t *testing.T) {
t.Run("Array", func(t *testing.T) {
var e Encoder
e.ArrStart()
e.Int(1)
e.ArrStart()
e.Int(2)
e.RawStr(`3`)
e.ArrEnd()
e.ArrEnd()
require.Equal(t, "[1,[2,3]]", e.String())
})
t.Run("Object", func(t *testing.T) {
var e Encoder
e.ObjStart()
e.FieldStart("a")
e.Int(1)
e.FieldStart("b")
e.Int(2)
e.FieldStart("c")
e.ArrStart()
e.Int(1)
e.Raw([]byte{'2'})
e.Float32(3.0)
e.Float64(4.5)
e.Num(Num{'2', '3'})
e.Bool(true)
e.Bool(false)
e.Null()
e.Base64(Raw{1})
e.Bool(true)
e.ArrEnd()
e.ObjEnd()
require.Equal(t, `{"a":1,"b":2,"c":[1,2,3,4.5,23,true,false,null,"AQ==",true]}`, e.String())
})
t.Run("NoPanic", func(t *testing.T) {
var e Encoder
e.ObjEnd()
e.ObjEnd()
e.ArrEnd()
e.ArrEnd()
})
}
func BenchmarkEncoder_comma_overhead(b *testing.B) {
// Measure overhead of ArrStart + comma + resetComma.
// BenchmarkEncoder_comma-32 5.057 ns/op 0 B/op 0 allocs/op.
var e Encoder
b.ReportAllocs()
for i := 0; i < b.N; i++ {
e.ArrStart()
e.comma()
e.Reset()
}
}