-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
types_test.go
199 lines (169 loc) · 5.55 KB
/
types_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
package kenall_test
import (
"bytes"
"testing"
"time"
"github.com/osamingo/go-kenall/v2"
)
func TestVersion_UnmarshalJSON(t *testing.T) {
t.Parallel()
cases := map[string]struct {
give string
want time.Time
wantError bool
}{
"Give 2020-11-30": {give: `"2020-11-30"`, want: time.Date(2020, 11, 30, 0, 0, 0, 0, time.UTC), wantError: false},
"Give 20201130": {give: `"20201130"`, want: time.Time{}, wantError: true},
"Give null": {give: `null`, want: time.Time{}, wantError: false},
}
for name, c := range cases {
c := c
t.Run(name, func(t *testing.T) {
t.Parallel()
v := &kenall.Version{}
err := v.UnmarshalJSON([]byte(c.give))
if err == nil == c.wantError {
t.Errorf("give: %v, want: %v", err, c.wantError)
}
if !c.want.Equal(time.Time(*v)) {
t.Errorf("give: %v, want: %v", time.Time(*v), c.want)
}
})
}
}
func TestNullString_UnmarshalJSON(t *testing.T) {
t.Parallel()
cases := map[string]struct {
give string
want string
wantError bool
isValid bool
}{
"Give string": {give: `"123"`, want: "123", wantError: false, isValid: true},
"Give number": {give: `123`, want: "", wantError: true, isValid: true},
"Give empty": {give: `""`, want: "", wantError: false, isValid: true},
"Give null": {give: `null`, want: "", wantError: false, isValid: false},
}
for name, c := range cases {
c := c
t.Run(name, func(t *testing.T) {
t.Parallel()
ns := &kenall.NullString{}
err := ns.UnmarshalJSON([]byte(c.give))
if err == nil == c.wantError {
t.Fatalf("give: %v, want: %v", err, c.wantError)
}
if !c.isValid && ns.Valid {
t.Errorf("give: %v, want: %v", ns.Valid, c.isValid)
} else if c.want != ns.String {
t.Errorf("give: %v, want: %v", ns.String, c.want)
}
})
}
}
func TestRemoteAddress_UnmarshalJSON(t *testing.T) {
t.Parallel()
cases := map[string]struct {
give string
wantError bool
wantNetwork string
wantAddress string
}{
"Give ip4": {give: `{"type":"v4","address":"127.0.0.1"}`, wantError: false, wantNetwork: "ip", wantAddress: "127.0.0.1"},
"Give ip6": {give: `{"type":"v6","address":"::1"}`, wantError: false, wantNetwork: "ip", wantAddress: "::1"},
"Give ip4 wrong object": {give: `{"type":"v4","address":"wrong"}`, wantError: true, wantNetwork: "", wantAddress: ""},
"Give ip6 wrong object": {give: `{"type":"v6","address":"wrong"}`, wantError: true, wantNetwork: "", wantAddress: ""},
"Give undefined type": {give: `{"type":"v8","address":"::1"}`, wantError: true, wantNetwork: "", wantAddress: ""},
"Give empty object": {give: `{}`, wantError: true, wantNetwork: "", wantAddress: ""},
"Give empty": {give: ``, wantError: true, wantNetwork: "", wantAddress: ""},
}
for name, c := range cases {
c := c
t.Run(name, func(t *testing.T) {
t.Parallel()
ra := &kenall.RemoteAddress{}
err := ra.UnmarshalJSON([]byte(c.give))
if c.wantError {
if err == nil {
t.Errorf("an error should not be nil")
}
return
}
if err != nil {
t.Fatalf("an error should be nil, err = %s", err)
}
if ra.Network() != c.wantNetwork {
t.Errorf("give: %s, want: %s", ra.Network(), c.wantNetwork)
}
if ra.String() != c.wantAddress {
t.Errorf("give: %s, want: %s", ra.String(), c.wantAddress)
}
})
}
}
func TestHoliday_UnmarshalJSON(t *testing.T) {
t.Parallel()
cases := map[string]struct {
give string
wantTitle string
wantTime time.Time
wantError bool
}{
"Normal case": {give: `{"title":"元日","date":"2022-01-01","day_of_week":6,"day_of_week_text":"saturday"}`, wantTitle: "元日", wantTime: time.Date(2022, 1, 1, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 9*60*60)), wantError: false},
"Unexpected JSON value": {give: `{"title":2,"date":"2022-01-01","day_of_week":6,"day_of_week_text":"saturday"}`, wantTitle: "", wantTime: time.Time{}, wantError: true},
"Unexpected date format": {give: `{"title":"元日","date":"2022/01/01","day_of_week":6,"day_of_week_text":"saturday"}`, wantTitle: "", wantTime: time.Time{}, wantError: true},
}
for name, c := range cases {
c := c
t.Run(name, func(t *testing.T) {
t.Parallel()
h := &kenall.Holiday{}
err := h.UnmarshalJSON([]byte(c.give))
if c.wantError {
if err == nil {
t.Errorf("an error should not be nil")
}
return
}
if err != nil {
t.Fatalf("an error should be nil, err = %s", err)
}
if h.Title != c.wantTitle {
t.Errorf("give: %s, want: %s", h.Title, c.wantTitle)
}
if !h.Time.Equal(c.wantTime) {
t.Errorf("give: %s, want: %s", h.Time, c.wantTime)
}
})
}
}
func TestHoliday_MarshalJSON(t *testing.T) {
t.Parallel()
cases := map[string]struct {
give *kenall.Holiday
want []byte
wantError bool
}{
"Normal case": {give: &kenall.Holiday{Title: "元日", Time: time.Date(2022, 1, 1, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 9*60*60))}, want: []byte(`{"title":"元日","date":"2022-01-01","day_of_week":6,"day_of_week_text":"saturday"}`), wantError: false},
"Empty case": {give: &kenall.Holiday{}, want: []byte(`{"title":"","date":"0001-01-01","day_of_week":1,"day_of_week_text":"monday"}`), wantError: false},
}
for name, c := range cases {
c := c
t.Run(name, func(t *testing.T) {
t.Parallel()
b, err := c.give.MarshalJSON()
if c.wantError {
if err == nil {
t.Errorf("an error should not be nil")
}
return
}
if err != nil {
t.Fatalf("an error should be nil, err = %s", err)
}
if !bytes.Equal(b, c.want) {
t.Errorf("give: %s, want: %s", b, c.want)
}
})
}
}