forked from francoispqt/gojay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_string_test.go
253 lines (245 loc) · 6.6 KB
/
encode_string_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package gojay
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEncoderStringEncodeAPI(t *testing.T) {
t.Run("basic", func(t *testing.T) {
builder := &strings.Builder{}
enc := NewEncoder(builder)
err := enc.EncodeString("hello world")
assert.Nil(t, err, "Error should be nil")
assert.Equal(
t,
`"hello world"`,
builder.String(),
"Result of marshalling is different as the one expected")
})
t.Run("utf8", func(t *testing.T) {
builder := &strings.Builder{}
enc := NewEncoder(builder)
err := enc.EncodeString("漢字𩸽")
assert.Nil(t, err, "Error should be nil")
assert.Equal(
t,
`"漢字𩸽"`,
builder.String(),
"Result of marshalling is different as the one expected")
})
t.Run("utf8-multibyte", func(t *testing.T) {
str := "テュールスト マーティン ヤコブ 😁"
builder := &strings.Builder{}
enc := NewEncoder(builder)
err := enc.EncodeString(str)
assert.Nil(t, err, "Error should be nil")
assert.Equal(
t,
`"テュールスト マーティン ヤコブ 😁"`,
builder.String(),
"Result of marshalling is different as the one expected")
})
t.Run("escaped-sequence1", func(t *testing.T) {
str := `テュールスト マ\ーテ
ィン ヤコブ 😁`
builder := &strings.Builder{}
enc := NewEncoder(builder)
err := enc.EncodeString(str)
assert.Nil(t, err, "Error should be nil")
assert.Equal(
t,
`"テュールスト マ\\ーテ\nィン ヤコブ 😁"`,
builder.String(),
"Result of marshalling is different as the one expected")
})
t.Run("escaped-sequence2", func(t *testing.T) {
str := `テュールスト マ\ーテ
ィン ヤコブ 😁 `
builder := &strings.Builder{}
enc := NewEncoder(builder)
err := enc.EncodeString(str)
assert.Nil(t, err, "Error should be nil")
assert.Equal(
t,
`"テュールスト マ\\ーテ\nィン ヤコブ 😁\t"`,
builder.String(),
"Result of marshalling is different as the one expected")
})
t.Run("escaped-sequence3", func(t *testing.T) {
str := "hello \r world 𝄞"
builder := &strings.Builder{}
enc := NewEncoder(builder)
err := enc.EncodeString(str)
assert.Nil(t, err, "Error should be nil")
assert.Equal(
t,
`"hello \r world 𝄞"`,
builder.String(),
"Result of marshalling is different as the one expected")
})
t.Run("escaped-sequence3", func(t *testing.T) {
str := "hello \b world 𝄞"
builder := &strings.Builder{}
enc := NewEncoder(builder)
err := enc.EncodeString(str)
assert.Nil(t, err, "Error should be nil")
assert.Equal(
t,
`"hello \b world 𝄞"`,
builder.String(),
"Result of marshalling is different as the one expected")
})
t.Run("escaped-control-char", func(t *testing.T) {
str := "\u001b"
builder := &strings.Builder{}
enc := NewEncoder(builder)
err := enc.EncodeString(str)
assert.Nil(t, err, "Error should be nil")
assert.Equal(
t,
`"\u001b"`,
builder.String(),
"Result of marshalling is different as the one expected")
})
t.Run("escaped-sequence3", func(t *testing.T) {
str := "hello \f world 𝄞"
builder := &strings.Builder{}
enc := NewEncoder(builder)
err := enc.EncodeString(str)
assert.Nil(t, err, "Error should be nil")
assert.Equal(
t,
"\"hello \\f world 𝄞\"",
builder.String(),
"Result of marshalling is different as the one expected")
})
}
func TestEncoderStringEncodeAPIErrors(t *testing.T) {
t.Run("pool-error", func(t *testing.T) {
v := ""
enc := BorrowEncoder(nil)
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 InvalidUsagePooledDecoderError")
}()
_ = enc.EncodeString(v)
assert.True(t, false, "should not be called as it should have panicked")
})
t.Run("write-error", func(t *testing.T) {
v := "test"
w := TestWriterError("")
enc := BorrowEncoder(w)
defer enc.Release()
err := enc.EncodeString(v)
assert.NotNil(t, err, "err should not be nil")
})
}
func TestEncoderStringMarshalAPI(t *testing.T) {
t.Run("basic", func(t *testing.T) {
r, err := Marshal("string")
assert.Nil(t, err, "Error should be nil")
assert.Equal(
t,
`"string"`,
string(r),
"Result of marshalling is different as the one expected")
})
t.Run("utf8", func(t *testing.T) {
r, err := Marshal("漢字")
assert.Nil(t, err, "Error should be nil")
assert.Equal(
t,
`"漢字"`,
string(r),
"Result of marshalling is different as the one expected")
})
}
func TestEncoderStringNullEmpty(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"`,
},
{
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.StringNullEmpty("")
enc.AddStringNullEmpty("true")
enc.Write()
assert.Equal(t, testCase.expectedJSON, b.String())
})
}
}
func TestEncoderStringNullEmpty2(t *testing.T) {
var testCases = []struct {
name string
baseJSON string
expectedJSON string
}{
{
name: "basic 1st elem",
baseJSON: "[",
expectedJSON: `["test"`,
},
}
for _, testCase := range testCases {
t.Run("true", func(t *testing.T) {
var b strings.Builder
var enc = NewEncoder(&b)
enc.writeString(testCase.baseJSON)
enc.StringNullEmpty("test")
enc.Write()
assert.Equal(t, testCase.expectedJSON, b.String())
})
}
}
func TestEncoderStringNullKeyEmpty(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.StringKeyNullEmpty("foo", "")
enc.AddStringKeyNullEmpty("bar", "true")
enc.Write()
assert.Equal(t, testCase.expectedJSON, b.String())
})
}
}