-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_test.go
48 lines (44 loc) · 1 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
package zeit
import (
"fmt"
"github.com/stretchr/testify/assert"
"math"
"strconv"
"testing"
)
func TestTime_UnmarshalJSON(t *testing.T) {
a := assert.New(t)
unixTimes := []int64{
0,
123456789,
1000000000,
math.MaxInt32,
math.MaxInt32 + 1,
math.MaxInt16,
math.MaxInt8,
10,
3000000,
}
for _, unixTime := range unixTimes {
t.Run(fmt.Sprintf("testing time %d", unixTime), func(t *testing.T) {
time := Time{}
b := []byte(strconv.FormatInt(unixTime*1000, 10))
err := time.UnmarshalJSON(b)
a.Nil(err, "shouldn't error on correct unix timestamp parsing")
a.Equal(unixTime, time.Unix(), "should be the same as timestamp")
})
}
invalidUnixTimes := []int64{
-1,
10000000000000000,
math.MaxInt64,
}
for _, unixTime := range invalidUnixTimes {
t.Run(fmt.Sprintf("testing time %d", unixTime), func(t *testing.T) {
time := Time{}
b := []byte(strconv.FormatInt(unixTime*1000, 10))
err := time.UnmarshalJSON(b)
a.NotNil(err, "should error on invalid timestamp")
})
}
}