-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_types_test.go
87 lines (70 loc) · 2 KB
/
sub_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
package slk
import (
"encoding/json"
"fmt"
"strings"
"testing"
"time"
)
func TestTime(t *testing.T) {
vals := [][]string{
{"1401383885.000061", "2014-05-29 13:18:05.000000061 -0400 EDT"},
{`"1401383885.000061"`, "2014-05-29 13:18:05.000000061 -0400 EDT"},
{"1401383885", "2014-05-29 13:18:05 -0400 EDT"},
{`"1401383885"`, "2014-05-29 13:18:05 -0400 EDT"},
{"2012-04-23T18:25:43.511Z", "2012-04-23 18:25:43.511 +0000 UTC"},
{`"2012-04-23T18:25:43.511Z"`, "2012-04-23 18:25:43.511 +0000 UTC"},
}
for _, val := range vals {
tv := Time{}
if err := tv.UnmarshalJSON([]byte(val[0])); err != nil {
t.Error(err)
}
if tv.String() != val[1] {
t.Errorf("Expected %q, got %q", val[1], tv.String())
}
}
}
func TestSlackTime(t *testing.T) {
vals := []string{
`1401383884.000061`,
`1401383885.000062`,
`1401383885.000063`,
`1401383886.111164`,
}
for _, val := range vals {
tv := Time{}
if err := tv.UnmarshalJSON([]byte(val)); err != nil {
t.Error(err)
}
if tv.SlackString() != val {
t.Errorf("Expected %q, got %q", val, tv.SlackString())
}
}
}
func TestUnmarshal(t *testing.T) {
const valjson = `{"ok":true,"snooze_endtime":1450373897,"snooze_remaining":60}`
type testStruct struct {
OK bool `json:"ok"`
SnoozeEndTime Time `json:"snooze_endtime"`
SnoozeRemaining Duration `json:"snooze_remaining"`
}
var val testStruct
if err := json.NewDecoder(strings.NewReader(valjson)).Decode(&val); err != nil {
fmt.Println(err)
}
var expected = testStruct{
OK: true,
SnoozeEndTime: Time{time.Unix(1450373897, 0)},
SnoozeRemaining: Duration{60 * time.Second},
}
if val.OK != expected.OK {
t.Errorf("Expected: %#v, got: %#v\n", expected.OK, val.OK)
}
if val.SnoozeEndTime != expected.SnoozeEndTime {
t.Errorf("Expected: %#v, got: %#v\n", expected.SnoozeEndTime, val.SnoozeEndTime)
}
if val.SnoozeRemaining != expected.SnoozeRemaining {
t.Errorf("Expected: %#v, got: %#v\n", expected.SnoozeRemaining, val.SnoozeRemaining)
}
}