-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.go
81 lines (74 loc) · 1.82 KB
/
utils_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
package myradio
import (
"testing"
"time"
)
func TestParseShortTime(t *testing.T) {
tests := []struct {
expected time.Time
time string
}{
{
time.Date(1970, time.January, 1, 0, 0, 0, 0, time.Local),
"01/01/1970 00:00",
},
{
time.Date(2009, time.April, 13, 11, 11, 0, 0, time.Local),
"13/04/2009 11:11",
},
}
for _, test := range tests {
got, err := parseShortTime(test.time)
if err != nil {
t.Error("unexpected error:", err)
}
if !got.Equal(test.expected) {
t.Error("expected:", test.expected, "got:", got)
}
}
}
func TestParseDuration(t *testing.T) {
tests := []struct {
expectedStr string
time string
}{
{"2h", "02:00:00"},
{"30m", "00:30:00"},
{"30h", "30:00:00"},
{"-5s", "-0:00:05"},
}
for _, test := range tests {
// We can safely leave testing the time lib to the stl
expected, _ := time.ParseDuration(test.expectedStr)
got, err := parseDuration(test.time)
if err != nil {
t.Error("unexpected error:", err)
}
if got != expected {
t.Error("expected:", expected, "got:", got)
}
}
}
func TestParseDurationError(t *testing.T) {
tests := []struct {
errStr string
time string
}{
{"parseDuration: duration string empty", ""},
{"parseDuration: '01' has 1 sections but should have 3", "01"},
{"parseDuration: '02:05' has 2 sections but should have 3", "02:05"},
{"parseDuration: '01:02:03:04' has 4 sections but should have 3", "01:02:03:04"},
{"parseDuration: expected 0-59, got -1", "10:-1:00"},
{"parseDuration: expected 0-59, got 60", "10:10:60"},
{`strconv.ParseInt: parsing "a": invalid syntax`, "a:b:c"},
}
for _, test := range tests {
_, err := parseDuration(test.time)
if err == nil {
t.Error("no error, was expecting one")
}
if err.Error() != test.errStr {
t.Error("expected:", test.errStr, "got:", err.Error())
}
}
}