forked from francoispqt/gojay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_bool_test.go
124 lines (117 loc) · 3.27 KB
/
encode_bool_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package gojay
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEncoderBoolMarshalAPI(t *testing.T) {
t.Run("true", func(t *testing.T) {
b, err := Marshal(true)
assert.Nil(t, err, "err must be nil")
assert.Equal(t, "true", string(b), "string(b) must be equal to 'true'")
})
t.Run("false", func(t *testing.T) {
b, err := Marshal(false)
assert.Nil(t, err, "err must be nil")
assert.Equal(t, "false", string(b), "string(b) must be equal to 'false'")
})
}
func TestEncoderBoolEncodeAPI(t *testing.T) {
t.Run("true", func(t *testing.T) {
builder := &strings.Builder{}
enc := BorrowEncoder(builder)
defer enc.Release()
err := enc.EncodeBool(true)
assert.Nil(t, err, "err must be nil")
assert.Equal(t, "true", builder.String(), "string(b) must be equal to 'true'")
})
t.Run("false", func(t *testing.T) {
builder := &strings.Builder{}
enc := BorrowEncoder(builder)
defer enc.Release()
err := enc.EncodeBool(false)
assert.Nil(t, err, "err must be nil")
assert.Equal(t, "false", builder.String(), "string(b) must be equal to 'false'")
})
}
func TestEncoderBoolErrors(t *testing.T) {
t.Run("pool-error", func(t *testing.T) {
builder := &strings.Builder{}
enc := BorrowEncoder(builder)
enc.Release()
defer func() {
err := recover()
assert.NotNil(t, err, "err shouldnt be nil")
assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledEncoderError")
}()
_ = enc.EncodeBool(false)
assert.True(t, false, "should not be called as it should have panicked")
})
t.Run("encode-api-write-error", func(t *testing.T) {
v := true
w := TestWriterError("")
enc := BorrowEncoder(w)
defer enc.Release()
err := enc.EncodeBool(v)
assert.NotNil(t, err, "err should not be nil")
})
}
func TestEncoderBoolNullEmpty(t *testing.T) {
var testCases = []struct {
name string
baseJSON string
expectedJSON string
}{
{
name: "basic 1st elem",
baseJSON: "[",
expectedJSON: "[null,true",
},
{
name: "basic 2nd elem",
baseJSON: `["test"`,
expectedJSON: `["test",null,true`,
},
}
for _, testCase := range testCases {
t.Run("true", func(t *testing.T) {
var b strings.Builder
var enc = NewEncoder(&b)
enc.writeString(testCase.baseJSON)
enc.BoolNullEmpty(false)
enc.AddBoolNullEmpty(true)
enc.Write()
assert.Equal(t, testCase.expectedJSON, b.String())
})
}
}
func TestEncoderBoolNullKeyEmpty(t *testing.T) {
var testCases = []struct {
name string
baseJSON string
expectedJSON string
}{
{
name: "basic 1st elem",
baseJSON: "{",
expectedJSON: `{"foo":null,"bar":true`,
},
{
name: "basic 2nd elem",
baseJSON: `{"test":"test"`,
expectedJSON: `{"test":"test","foo":null,"bar":true`,
},
}
for _, testCase := range testCases {
t.Run("true", func(t *testing.T) {
var b strings.Builder
var enc = NewEncoder(&b)
enc.writeString(testCase.baseJSON)
enc.BoolKeyNullEmpty("foo", false)
enc.AddBoolKeyNullEmpty("bar", true)
enc.Write()
assert.Equal(t, testCase.expectedJSON, b.String())
})
}
}