-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathage_test.go
More file actions
157 lines (141 loc) · 3.4 KB
/
age_test.go
File metadata and controls
157 lines (141 loc) · 3.4 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"strings"
"testing"
"time"
)
func TestAgeSet(t *testing.T) {
testCases := []struct {
in string
err string // Contained in error text
out int64 // Only checked if no error (natch)
}{
{"", "1-5 digits", 0}, // # 0
{"123456s", "1-5 digits", 0},
{"01s", "octal", 0},
{"1S", "invalid unit", 0},
{"1t", "invalid unit", 0},
{"1xs", "invalid syntax", 0},
{"1234xs", "invalid syntax", 0},
{"x1234s", "invalid syntax", 0},
{"3000Y", "exceeds", 0},
{"16000M", "exceeds", 0}, // # 9
{"0s", "", 0 * second}, // # 10
{"1s", "", 1 * second},
{"59s", "", 59 * second},
{"60s", "", 1 * minute},
{"61s", "", 61 * second},
{"1m", "", 1 * minute},
{"59m", "", 59 * minute},
{"1h", "", 1 * hour},
{"23h", "", 23 * hour},
{"1D", "", 1 * day},
{"7D", "", 1 * week},
{"1W", "", 1 * week},
{"1Y", "", 1 * year},
}
for ix, tc := range testCases {
var a age
err := a.Set(tc.in)
if err != nil {
if len(tc.err) == 0 {
t.Error(ix, "Unexpected error", err)
continue
}
if !strings.Contains(err.Error(), tc.err) {
t.Errorf("%d Wrong error returned. Want '%s' got '%s'\n", ix, tc.err, err.Error())
continue
}
continue
}
if len(tc.err) > 0 {
t.Error(ix, "Expected error", tc.err)
continue
}
if tc.out != a.seconds {
t.Error(ix, "Wrong value parsed. Expect", tc.out, "got", a.seconds)
continue
}
if a.String() != tc.in {
t.Error(ix, "Set value of", a.String(), "differs from input", tc.in)
continue
}
}
}
func TestAgeGreaterThan(t *testing.T) {
var baby, twin, granny age
baby.seconds = -3
twin.seconds = -3 // Equal ages are not greater than each other
granny.seconds = 1
if !granny.gt(baby, false) {
t.Error("Granny should be GT baby")
}
if baby.gt(twin, false) {
t.Error("Baby should not be greater than twin")
}
if twin.gt(baby, false) {
t.Error("Twin should not be greater than baby")
}
baby.seconds = -3*day + 11*hour
twin.seconds = -3 * day
baby.multiplier = day
twin.multiplier = day
if baby.gt(twin, true) {
t.Error("Truncated baby should not be greater than twin")
}
if twin.gt(baby, true) {
t.Error("Truncated twin should not be greater than baby")
}
}
func TestAgeLessThan(t *testing.T) {
var baby, twin, granny age
baby.seconds = -3
twin.seconds = -3 // Equal ages are not younger than each other
granny.seconds = 1
if !baby.lt(granny) {
t.Error("Baby should be younger than granny")
}
if baby.lt(twin) {
t.Error("Baby should not be younger than twin")
}
if twin.lt(baby) {
t.Error("Twin should not be younger than baby")
}
}
func TestAgeSetFromTime(t *testing.T) {
var baby, granny age
base := time.Date(1000, 1, 1, 0, 0, 0, 0, time.UTC)
year2000 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
year3000 := time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)
granny.setFromTime(base, year2000)
baby.setFromTime(base, year3000)
if granny.lt(baby) {
t.Error("Granny", granny, "should not be younger than baby", baby)
}
}
func TestAgeCompactString(t *testing.T) {
testCases := []struct {
seconds int64
expect string
}{
{86401 * 366, "1Y"},
{86401 * 99, "3M"},
{86401 * 7 * 2, "2W"},
{86401, "1D"},
{60*60 + 1, "1h"},
{121, "2m"},
{61, "1m"},
{59, "59s"},
{1, "1s"},
{0, "0s"},
{-5, "fut"},
}
var a age
for ix, tc := range testCases {
a.seconds = tc.seconds
s := a.compactString()
if s != tc.expect {
t.Error(ix, "Expect", tc.expect, "got", s)
}
}
}