forked from francoispqt/gojay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_time_test.go
141 lines (134 loc) · 3.13 KB
/
decode_time_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
package gojay
import (
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDecodeTime(t *testing.T) {
testCases := []struct {
name string
json string
format string
err bool
expectedTime string
}{
{
name: "basic",
json: `"2018-02-18"`,
format: `2006-01-02`,
err: false,
expectedTime: "2018-02-18",
},
{
name: "basic",
json: `"2017-01-02T15:04:05Z"`,
format: time.RFC3339,
err: false,
expectedTime: "2017-01-02T15:04:05Z",
},
{
name: "basic",
json: `"2017-01-02T15:04:05ZINVALID"`,
format: time.RFC3339,
err: true,
expectedTime: "",
},
{
name: "basic",
json: `"2017-01-02T15:04:05ZINVALID`,
format: time.RFC1123,
err: true,
expectedTime: "",
},
{
name: "basic",
json: `"2017-01-02T15:04:05ZINVALID"`,
format: time.RFC1123,
err: true,
expectedTime: "",
},
{
name: "basic",
json: `"2017-01-02T15:04:05ZINVALID`,
format: time.RFC3339,
err: true,
expectedTime: "",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
tm := time.Time{}
dec := NewDecoder(strings.NewReader(testCase.json))
err := dec.DecodeTime(&tm, testCase.format)
if !testCase.err {
assert.Nil(t, err)
assert.Equal(t, testCase.expectedTime, tm.Format(testCase.format))
return
}
assert.NotNil(t, err)
})
}
}
func TestDecodeAddTime(t *testing.T) {
testCases := []struct {
name string
json string
format string
err bool
expectedTime string
}{
{
name: "basic",
json: `"2018-02-18"`,
format: `2006-01-02`,
err: false,
expectedTime: "2018-02-18",
},
{
name: "basic",
json: ` "2017-01-02T15:04:05Z"`,
format: time.RFC3339,
err: false,
expectedTime: "2017-01-02T15:04:05Z",
},
{
name: "basic",
json: ` "2017-01-02T15:04:05ZINVALID"`,
format: time.RFC3339,
err: true,
expectedTime: "",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
tm := time.Time{}
dec := NewDecoder(strings.NewReader(testCase.json))
err := dec.AddTime(&tm, testCase.format)
if !testCase.err {
assert.Nil(t, err)
assert.Equal(t, testCase.expectedTime, tm.Format(testCase.format))
return
}
assert.NotNil(t, err)
})
}
}
func TestDecoderTimePoolError(t *testing.T) {
// reset the pool to make sure it's not full
decPool = sync.Pool{
New: func() interface{} {
return NewDecoder(nil)
},
}
dec := NewDecoder(nil)
dec.Release()
defer func() {
err := recover()
assert.NotNil(t, err, "err shouldnt be nil")
assert.IsType(t, InvalidUsagePooledDecoderError(""), err, "err should be of type InvalidUsagePooledDecoderError")
}()
_ = dec.DecodeTime(&time.Time{}, time.RFC3339)
assert.True(t, false, "should not be called as decoder should have panicked")
}