This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_test.go
185 lines (175 loc) · 4.08 KB
/
field_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
// MIT License
//
// Copyright (c) 2020 Nobody Night
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package santa
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestElementSerializeJSON(t *testing.T) {
timestamp, _ := time.Parse(time.RFC3339Nano,
"2020-08-13T21:56:30.0719939+08:00")
buffer := make([]byte, 0, 256)
for _, sample := range []struct {
name string
field Field
expected string
} {
{
name: "int",
field: Int("int", 10),
expected: "10",
},
{
name: "uint",
field: Uint("uint", 20),
expected: "20",
},
{
name: "float32",
field: Float32("float32", 3.14),
expected: "3.14",
},
{
name: "float64",
field: Float64("float64", 3.1415),
expected: "3.1415",
},
{
name: "boolean",
field: Boolean("boolean", true),
expected: "true",
},
{
name: "string",
field: String("string", "Hello"),
expected: "\"Hello\"",
},
{
name: "bytes",
field: Bytes("bytes", []byte("Hello")),
expected: "\"Hello\"",
},
{
name: "time",
field: Time("time", timestamp),
expected: "1597326990071993900",
},
{
name: "error",
field: Error("error", errors.New("Error")),
expected: "\"Error\"",
},
{
name: "value",
field: Value("value", 50),
expected: "50",
},
} {
assert.Equal(t, sample.name, sample.field.Name,
"Unexpected field name")
assert.Equal(t, sample.expected, string(
sample.field.SerializeJSON(buffer[ : 0])),
"Unexpected JSON formatted append result",
)
}
fields := []Field {
String("name", "test"),
Int("age", 100),
}
for _, sample := range []struct {
name string
field Field
expected string
} {
{
name: "object",
field: Object("object", fields...),
expected: `{
"name": "test",
"age": 100
}`,
},
{
name: "objects",
field: Objects("objects",
ElementObject(fields),
ElementObject(fields),
),
expected: `[
{
"name": "test",
"age": 100
},
{
"name": "test",
"age": 100
}
]`,
},
{
name: "ints",
field: Ints("ints", []int64 { 10, 20, 30 }),
expected: `[10, 20, 30]`,
},
{
name: "uints",
field: Uints("uints", []uint64 { 40, 50, 60 }),
expected: `[40, 50, 60]`,
},
{
name: "float32s",
field: Float32s("float32s", []float32 { 1.1, 1.2,
1.3 }),
expected: `[1.1, 1.2, 1.3]`,
},
{
name: "float64s",
field: Float64s("float64s", []float64 { 1.4, 1.5, 1.6 }),
expected: `[1.4, 1.5, 1.6]`,
},
{
name: "booleans",
field: Booleans("booleans", []bool { true, false }),
expected: `[true, false]`,
},
{
name: "strings",
field: Strings("strings", []string { "value1", "value2" }),
expected: `["value1", "value2"]`,
},
{
name: "times",
field: Times("times", []time.Time { timestamp,
timestamp }),
expected: `[1597326990071993900, 1597326990071993900]`,
},
} {
assert.Equal(t, sample.name, sample.field.Name,
"Unexpected field name")
assert.JSONEq(t, sample.expected,
string(sample.field.SerializeJSON(buffer[ : 0])),
"Unexpected JSON formatted append result",
)
}
}