-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
time_test.go
118 lines (102 loc) · 2.52 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
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
package toki_test
import (
"testing"
"time"
"github.com/guregu/toki"
)
var parseTable = map[string]string{
"12:34:56:7890": "12:34:56", // extra bits are ignored
"12:34:56": "12:34:56",
"12:34:01": "12:34:01",
"12:34": "12:34",
"12": "12:00",
"1": "1:00",
}
func TestParseTime(t *testing.T) {
for given, expected := range parseTable {
time, err := toki.ParseTime(given)
if err != nil {
t.Errorf("ParseTime: %s → expected %s, got error: %v", given, expected, time)
}
result := time.String()
if result != expected {
t.Errorf("ParseTime: %s → expected %s, got: %s", given, expected, result)
}
}
_, err := toki.ParseTime("12:abcdef")
if err == nil {
t.Errorf("ParseTime: expected error, got: nil")
}
}
func TestMustParseTime(t *testing.T) {
panicked := false
defer func() {
if r := recover(); r != nil {
panicked = true
}
if !panicked {
t.Error("MustParseTime: didn't panic")
}
}()
for given, expected := range parseTable {
time := toki.MustParseTime(given)
result := time.String()
if result != expected {
t.Errorf("MustParseTime: %s → expected %s, got: %s", given, expected, result)
}
}
toki.MustParseTime("invalid input")
}
func TestScan(t *testing.T) {
expected := toki.MustParseTime("12:34")
strTime := toki.Time{}
err := strTime.Scan("12:34")
if err != nil {
t.Errorf("Scan: error: %v", err)
}
if strTime != expected {
t.Errorf("Scan: %#v ≠ %#v", strTime, expected)
}
byteTime := toki.Time{}
err = byteTime.Scan([]byte("12:34:00"))
if err != nil {
t.Errorf("Scan: error: %v", err)
}
if byteTime != expected {
t.Errorf("Scan: %#v ≠ %#v", byteTime, expected)
}
timeTime := toki.Time{}
err = timeTime.Scan(time.Date(1992, 2, 23, 12, 34, 0, 0, time.UTC))
if err != nil {
t.Errorf("Scan: error: %v", err)
}
if timeTime != expected {
t.Errorf("Scan: %#v ≠ %#v", timeTime, expected)
}
errTime := toki.Time{}
err = errTime.Scan(42)
if err == nil {
t.Errorf("Scan: expected error, got: nil")
}
}
func TestValue(t *testing.T) {
time := toki.MustParseTime("12:34")
v, err := time.Value()
if string(v.([]byte)) != "12:34" {
t.Errorf("Value: %v ≠ 12:34", v)
}
if err != nil {
t.Errorf("Value: error: %v", err)
}
}
func TestEquals(t *testing.T) {
same1 := toki.MustParseTime("12:34")
same2 := toki.MustParseTime("12:34")
if !same1.Equals(same2) {
t.Error(same1.String(), "≠", same2.String())
}
diff := toki.MustParseTime("12:35")
if same1.Equals(diff) {
t.Error(same1.String(), "=", diff.String())
}
}