-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_test.go
71 lines (67 loc) · 1.5 KB
/
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
package jwt
import (
"encoding/json"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestTimeMarshalJSON(t *testing.T) {
now := time.Now()
testCases := []struct {
tt Time
want int64
}{
{Time{}, 0},
{Time{now}, now.Unix()},
{Time{now.Add(24 * time.Hour)}, now.Add(24 * time.Hour).Unix()},
{Time{now.Add(24 * 30 * 12 * time.Hour)}, now.Add(24 * 30 * 12 * time.Hour).Unix()},
}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
b, err := tc.tt.MarshalJSON()
if err != nil {
t.Fatal(err)
}
var n int64
if err = json.Unmarshal(b, &n); err != nil {
t.Fatal(err)
}
if want, got := tc.want, n; got != want {
t.Errorf("Time.Marshal mismatch (-want +got):\n%s", cmp.Diff(want, got))
}
})
}
}
func TestTimeUnmarshalJSON(t *testing.T) {
now := time.Now()
testCases := []struct {
n int64
want Time
isNil bool
}{
{now.Unix(), Time{now}, false},
{Epoch.Unix() - 0xDEAD, Time{Epoch}, false},
{Epoch.Unix(), Time{Epoch}, false},
{Epoch.Unix() + 0xDEAD, Time{Epoch.Add(0xDEAD * time.Second)}, false},
{0, Time{}, true},
}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
var n *int64
if !tc.isNil {
n = &tc.n
}
b, err := json.Marshal(n)
if err != nil {
t.Fatal(err)
}
var tt Time
if err = tt.UnmarshalJSON(b); err != nil {
t.Fatal(err)
}
if want, got := tc.want.Unix(), tt.Unix(); got != want {
t.Errorf("Time.Unmarshal mismatch (-want +got):\n%s", cmp.Diff(want, got))
}
})
}
}