-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson_test.go
172 lines (142 loc) · 4.16 KB
/
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
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
package magicjson_test
import (
"encoding/json"
"fmt"
"reflect"
"testing"
"time"
"github.com/parinpan/magicjson"
)
type Location struct {
Latitude float64
Longitude float64
}
func (l Location) MarshalJSON() ([]byte, error) {
s := fmt.Sprintf(`"lat:%f,lon:%f"`, l.Latitude, l.Longitude)
return []byte(s), nil
}
type student struct {
name string
age uint8
height float32
joinedAt *time.Time
}
type teacher struct {
name string
age uint8
height float32
joinedAt *time.Time
}
type classRoom struct {
name string
capacity uint
homeRoomTeacher teacher
students []student
}
type school struct {
name string
establishedDate *time.Time
classRooms []classRoom
descriptionUnexported []byte
Location Location
DescriptionExported []byte
}
func TestMarshal(t *testing.T) {
t.Run("should generate null when data is nil", func(t *testing.T) {
expected := []byte(`null`)
actual, err := magicjson.Marshal(nil)
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("got: %s, expected: %s", string(actual), string(expected))
}
if err != nil {
t.Fatal("should not raise an error")
}
})
t.Run("should successfully marshal on data type", func(t *testing.T) {
expected, _ := json.Marshal([]byte(`1234`))
actual, err := magicjson.Marshal([]byte(`1234`))
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("got: %s, expected: %s", string(actual), string(expected))
}
if err != nil {
t.Fatal("should not raise an error")
}
})
t.Run("should successfully marshal on primitive slice", func(t *testing.T) {
expected := []byte(`[1,2,3,4]`)
actual, err := magicjson.Marshal([]int{1, 2, 3, 4})
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("got: %s, expected: %s", string(actual), string(expected))
}
if err != nil {
t.Fatal("should not raise an error")
}
})
t.Run("should successfully marshal on primitive array", func(t *testing.T) {
expected := []byte(`[1,2,3,4]`)
actual, err := magicjson.Marshal([4]int{1, 2, 3, 4})
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("got: %s, expected: %s", string(actual), string(expected))
}
if err != nil {
t.Fatal("should not raise an error")
}
})
t.Run("should successfully marshal on an empty slice", func(t *testing.T) {
expected, _ := json.Marshal([]school{})
actual, err := magicjson.Marshal([]school{})
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("got: %s, expected: %s", string(actual), string(expected))
}
if err != nil {
t.Fatal("should not raise an error")
}
})
t.Run("should successfully marshal given data", func(t *testing.T) {
joinedAt := toPtr(time.Unix(1704537759, 0).UTC())
establishedAt := toPtr(time.Unix(1504537759, 0).UTC())
schools := []school{
{
name: "School A",
establishedDate: establishedAt,
classRooms: []classRoom{
{
name: "Class A",
capacity: 100,
homeRoomTeacher: teacher{
name: "Mrs. Diana",
age: 30,
height: 180.5,
joinedAt: joinedAt,
},
students: []student{
{
name: "Brian",
age: 14,
height: 170.5,
joinedAt: joinedAt,
},
},
},
},
descriptionUnexported: []byte(`unexported description`),
Location: Location{
Latitude: 1.1,
Longitude: 2.2,
},
DescriptionExported: []byte(`exported description`),
},
}
expected := []byte(`[{"name":"School A","establishedDate":"2017-09-04T15:09:19Z","classRooms":[{"name":"Class A","capacity":100,"homeRoomTeacher":{"name":"Mrs. Diana","age":30,"height":180.5,"joinedAt":"2024-01-06T10:42:39Z"},"students":[{"name":"Brian","age":14,"height":170.5,"joinedAt":"2024-01-06T10:42:39Z"}]}],"descriptionUnexported":"dW5leHBvcnRlZCBkZXNjcmlwdGlvbg==","Location":"lat:1.100000,lon:2.200000","DescriptionExported":"ZXhwb3J0ZWQgZGVzY3JpcHRpb24="}]`)
actual, err := magicjson.Marshal(schools)
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("got: %s, expected: %s", string(actual), string(expected))
}
if err != nil {
t.Fatal("should not raise an error")
}
})
}
func toPtr[T any](v T) *T {
return &v
}