-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocal_time_test.go
162 lines (140 loc) Β· 4.15 KB
/
local_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
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
158
159
160
161
162
package chrono_test
import (
"fmt"
"testing"
"github.com/go-chrono/chrono"
)
func TestLocalTime(t *testing.T) {
time := chrono.LocalTimeOf(12, 30, 59, 12345678)
hour, min, sec := time.Clock()
if hour != 12 {
t.Errorf("time.Clock() hour = %d, want 12", hour)
}
if min != 30 {
t.Errorf("time.Clock() min = %d, want 30", min)
}
if sec != 59 {
t.Errorf("time.Clock() sec = %d, want 59", sec)
}
if nsec := time.Nanosecond(); nsec != 12345678 {
t.Errorf("time.Nanosecond() = %d, want 12345678", nsec)
}
}
func TestLocalTime_String(t *testing.T) {
for _, tt := range []struct {
name string
time chrono.LocalTime
expected string
}{
{"simple", chrono.LocalTimeOf(9, 0, 0, 0), "09:00:00"},
{"nanoseconds", chrono.LocalTimeOf(9, 0, 0, 12345678), "09:00:00.012345678"},
} {
t.Run(tt.name, func(t *testing.T) {
if output := tt.time.String(); output != tt.expected {
t.Errorf("LocalTime.String() = %s, want %s", output, tt.expected)
}
})
}
}
func TestLocalTime_BusinessHour(t *testing.T) {
time := chrono.LocalTimeOf(25, 0, 0, 0)
if hour := time.BusinessHour(); hour != 25 {
t.Errorf("time.Hour() = %d, want 25", hour)
}
if hour, _, _ := time.Clock(); hour != 1 {
t.Errorf("time.Hour() = %d, want 1", hour)
}
}
func TestLocalTime_Sub(t *testing.T) {
for _, tt := range []struct {
t1 chrono.LocalTime
t2 chrono.LocalTime
diff chrono.Extent
}{
{chrono.LocalTimeOf(12, 0, 0, 0), chrono.LocalTimeOf(6, 0, 0, 0), 6 * chrono.Hour},
{chrono.LocalTimeOf(12, 0, 0, 22), chrono.LocalTimeOf(12, 0, 0, 40), -18 * chrono.Nanosecond},
} {
t.Run(fmt.Sprintf("%s - %s", tt.t1, tt.t2), func(t *testing.T) {
if d := tt.t1.Sub(tt.t2); d != tt.diff {
t.Errorf("t1.Sub(t2) = %v, want %v", d, tt.diff)
}
})
}
}
func TestLocalTime_Add(t *testing.T) {
for _, tt := range []struct {
t chrono.LocalTime
e chrono.Extent
expected chrono.LocalTime
}{
{chrono.LocalTimeOf(12, 0, 0, 0), 29 * chrono.Minute, chrono.LocalTimeOf(12, 29, 0, 0)},
{chrono.LocalTimeOf(14, 45, 0, 0), -22 * chrono.Minute, chrono.LocalTimeOf(14, 23, 0, 0)},
{chrono.LocalTimeOf(5, 0, 0, 0), -7 * chrono.Hour, chrono.LocalTimeOf(22, 0, 0, 0)},
{chrono.LocalTimeOf(5, 0, 0, 0), -31 * chrono.Hour, chrono.LocalTimeOf(22, 0, 0, 0)},
} {
t.Run(fmt.Sprintf("%s + %v", tt.t, tt.e), func(t *testing.T) {
if ok := tt.t.CanAdd(tt.e); !ok {
t.Error("t.CanAdd(e) = false, want true")
}
if added := tt.t.Add(tt.e); added.Compare(tt.expected) != 0 {
t.Errorf("t.Add(e) = %s, want %s", added, tt.expected)
}
})
}
for _, tt := range []struct {
name string
t chrono.LocalTime
e chrono.Extent
}{
{"invalid duration", chrono.LocalTimeOf(0, 0, 0, 0), 200 * chrono.Hour},
{"invalid time", chrono.LocalTimeOf(90, 0, 0, 0), 20 * chrono.Hour},
} {
t.Run(tt.name, func(t *testing.T) {
if ok := tt.t.CanAdd(tt.e); ok {
t.Error("t.CanAdd(e) = true, want false")
}
func() {
defer func() {
if r := recover(); r == nil {
t.Error("expecting panic that didn't occur")
}
}()
tt.t.Add(tt.e)
}()
})
}
}
func TestLocalTime_Compare(t *testing.T) {
for _, tt := range []struct {
name string
t chrono.LocalTime
t2 chrono.LocalTime
expected int
}{
{"earlier", chrono.LocalTimeOf(11, 0, 0, 0), chrono.LocalTimeOf(12, 0, 0, 0), -1},
{"later", chrono.LocalTimeOf(13, 30, 0, 0), chrono.LocalTimeOf(13, 29, 55, 0), 1},
{"equal", chrono.LocalTimeOf(15, 0, 0, 1000), chrono.LocalTimeOf(15, 0, 0, 1000), 0},
} {
t.Run(tt.name, func(t *testing.T) {
if v := tt.t.Compare(tt.t2); v != tt.expected {
t.Errorf("t.Compare(t2) = %d, want %d", v, tt.expected)
}
})
}
}
func TestLocalTime_In(t *testing.T) {
time := chrono.LocalTimeOf(9, 0, 0, 0)
output := time.In(chrono.OffsetOf(2, 30))
expected := chrono.OffsetTimeOf(9, 0, 0, 0, 2, 30)
if output.Compare(expected) != 0 {
t.Errorf("time.In = %s, want %s", output, expected)
}
}
func TestLocalTime_UTC(t *testing.T) {
time := chrono.LocalTimeOf(9, 0, 0, 0)
output := time.UTC()
expected := chrono.OffsetTimeOf(9, 0, 0, 0, 0, 0)
if output.Compare(expected) != 0 {
t.Errorf("time.UTC() = %s, want %s", output, expected)
}
}