forked from gjbae1212/fluent-bit-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formats_test.go
123 lines (119 loc) · 2.24 KB
/
formats_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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestEncodeToJSON(t *testing.T) {
tests := []struct {
name string
input interface{}
expected string
hasErr bool
}{
{
name: "simple map",
input: map[interface{}]interface{}{
"k1": "v1",
"k2": "v2",
},
expected: `{"k1":"v1","k2":"v2"}`,
hasErr: false,
},
{
name: "nested map",
input: map[interface{}]interface{}{
"k1": map[interface{}]interface{}{
"sk1": "sv1",
},
"k2": "v2",
},
expected: `{"k1":{"sk1":"sv1"},"k2":"v2"}`,
hasErr: false,
},
{
name: "invalid key type",
input: map[interface{}]interface{}{
12345: "v1",
},
expected: "",
hasErr: true,
},
{
name: "nested slice",
input: map[interface{}]interface{}{
"k1": []interface{}{
"v1",
map[interface{}]interface{}{
"sk1": "sv1",
},
},
},
expected: `{"k1":["v1",{"sk1":"sv1"}]}`,
hasErr: false,
},
{
name: "byte value",
input: map[interface{}]interface{}{
"k1": []byte("v1"),
},
expected: `{"k1":"v1"}`,
hasErr: false,
},
{
name: "nested byte slice",
input: map[interface{}]interface{}{
"k1": []interface{}{
[]byte("v1"),
map[interface{}]interface{}{
"sk1": []byte("sv1"),
},
},
},
expected: `{"k1":["v1",{"sk1":"sv1"}]}`,
hasErr: false,
},
{
name: "nested interface",
input: map[interface{}]interface{}{
"k1": []interface{}{
interface{}("v1"),
map[interface{}]interface{}{
"sk1": interface{}("sv1"),
},
},
},
expected: `{"k1":["v1",{"sk1":"sv1"}]}`,
hasErr: false,
},
{
name: "deeply nested slice",
input: map[interface{}]interface{}{
"k1": []interface{}{
"v1",
[]interface{}{
"v2",
[]interface{}{
"v3",
map[interface{}]interface{}{
"sk1": "sv1",
},
},
},
},
},
expected: `{"k1":["v1",["v2",["v3",{"sk1":"sv1"}]]]}`,
hasErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := encodeToJSON(tt.input)
if tt.hasErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.JSONEq(t, tt.expected, string(got))
}
})
}
}