-
Notifications
You must be signed in to change notification settings - Fork 114
/
encode_embedded_json_test.go
137 lines (126 loc) · 3.75 KB
/
encode_embedded_json_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
package gojay
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func (r *Request) MarshalJSONObject(enc *Encoder) {
enc.AddStringKey("id", r.id)
enc.AddStringKey("method", r.method)
enc.AddEmbeddedJSONKey("params", &r.params)
params2 := EmbeddedJSON([]byte(``))
enc.AddEmbeddedJSONKeyOmitEmpty("params2", ¶ms2)
params3 := EmbeddedJSON([]byte(`"test"`))
enc.AddEmbeddedJSONKeyOmitEmpty("params3", ¶ms3)
enc.AddIntKey("more", r.more)
}
func (r *Request) IsNil() bool {
return r == nil
}
type EmbeddedJSONArr []EmbeddedJSON
func (ear EmbeddedJSONArr) MarshalJSONArray(enc *Encoder) {
for _, e := range ear {
enc.AddEmbeddedJSON(&e)
}
}
func (ear EmbeddedJSONArr) IsNil() bool {
return len(ear) == 0
}
type EmbeddedJSONOmitEmptyArr []EmbeddedJSON
func (ear EmbeddedJSONOmitEmptyArr) MarshalJSONArray(enc *Encoder) {
for _, e := range ear {
enc.AddEmbeddedJSONOmitEmpty(&e)
}
}
func (ear EmbeddedJSONOmitEmptyArr) IsNil() bool {
return len(ear) == 0
}
func TestEncodingEmbeddedJSON(t *testing.T) {
t.Run("basic-embedded-json", func(t *testing.T) {
ej := EmbeddedJSON([]byte(`"test"`))
b := &strings.Builder{}
enc := BorrowEncoder(b)
err := enc.Encode(&ej)
assert.Nil(t, err, "err should be nil")
assert.Equal(t, b.String(), `"test"`, "b should be equal to content of EmbeddedJSON")
})
t.Run("basic-embedded-json-marshal-api", func(t *testing.T) {
ej := EmbeddedJSON([]byte(`"test"`))
b, err := Marshal(&ej)
assert.Nil(t, err, "err should be nil")
assert.Equal(t, string(b), `"test"`, "b should be equal to content of EmbeddedJSON")
})
t.Run("object-embedded-json", func(t *testing.T) {
req := Request{
id: "test",
method: "GET",
params: EmbeddedJSON([]byte(`"test"`)),
}
b := &strings.Builder{}
enc := BorrowEncoder(b)
err := enc.EncodeObject(&req)
assert.Nil(t, err, "err should be nil")
assert.Equal(
t,
b.String(),
`{"id":"test","method":"GET","params":"test","params3":"test","more":0}`,
"b should be equal to content of EmbeddedJSON",
)
})
t.Run("array-embedded-json", func(t *testing.T) {
ear := EmbeddedJSONArr{
[]byte(`"test"`),
[]byte(`{"test":"test"}`),
}
b := &strings.Builder{}
enc := BorrowEncoder(b)
err := enc.EncodeArray(ear)
assert.Nil(t, err, "err should be nil")
assert.Equal(
t,
b.String(),
`["test",{"test":"test"}]`,
"b should be equal to content of EmbeddedJSON",
)
})
t.Run("array-embedded-json-omit-empty", func(t *testing.T) {
ear := EmbeddedJSONOmitEmptyArr{
[]byte(`"test"`),
[]byte(``),
[]byte(`{"test":"test"}`),
[]byte(``),
[]byte(`{"test":"test"}`),
}
b := &strings.Builder{}
enc := BorrowEncoder(b)
err := enc.EncodeArray(ear)
assert.Nil(t, err, "err should be nil")
assert.Equal(
t,
b.String(),
`["test",{"test":"test"},{"test":"test"}]`,
"b should be equal to content of EmbeddedJSON",
)
})
t.Run("write-error", func(t *testing.T) {
w := TestWriterError("")
v := EmbeddedJSON([]byte(`"test"`))
enc := NewEncoder(w)
err := enc.EncodeEmbeddedJSON(&v)
assert.NotNil(t, err, "Error should not be nil")
assert.Equal(t, "Test Error", err.Error(), "err.Error() should be 'Test Error'")
})
t.Run("pool-error", func(t *testing.T) {
v := EmbeddedJSON([]byte(`"test"`))
enc := BorrowEncoder(nil)
enc.isPooled = 1
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.EncodeEmbeddedJSON(&v)
assert.True(t, false, "should not be called as it should have panicked")
})
}