-
Notifications
You must be signed in to change notification settings - Fork 3
/
timestepper_test.go
130 lines (111 loc) · 4.2 KB
/
timestepper_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
package timekit
import (
"reflect"
"testing"
"time"
)
func TestNewTimeStepper(t *testing.T) {
loc := time.UTC // closure can be used if necessary
start := time.Date(2022, 1, 7, 1, 0, 0, 0, loc) // Jan 7th 2022
end := time.Date(2022, 1, 10, 1, 0, 0, 0, loc) // Jan 10th 2022
actual := NewTimeStepper(start, end, 0, 0, 1, 0, 0, 0)
expected := &TimeStepper{
tz: loc,
curr: start,
start: start,
end: end,
yearStep: 0,
monthStep: 0,
dayStep: 1,
hourStep: 0,
minuteStep: 0,
secondStep: 0,
}
if reflect.DeepEqual(actual, expected) == false {
t.Errorf("Incorrect struct, got %v but was expecting %v", actual, expected)
}
}
func TestNext(t *testing.T) {
loc := time.UTC // closure can be used if necessary
start := time.Date(2022, 1, 7, 1, 0, 0, 0, loc) // Jan 7th 2022
end := time.Date(2022, 1, 10, 1, 0, 0, 0, loc) // Jan 10th 2022
ts := NewTimeStepper(start, end, 0, 0, 1, 0, 0, 0)
// Perform our step operation and verify it works.
isFinished := ts.Next()
if isFinished == false {
t.Errorf("Incorrect value, got %v but was expecting %v", isFinished, true)
}
}
func TestDone(t *testing.T) {
loc := time.UTC // closure can be used if necessary
start := time.Date(2022, 1, 7, 1, 0, 0, 0, loc) // Jan 7th 2022
end := time.Date(2022, 1, 10, 1, 0, 0, 0, loc) // Jan 10th 2022
ts := NewTimeStepper(start, end, 0, 0, 1, 0, 0, 0)
// Perform our step operation and verify it works.
isFinished := ts.Done()
if isFinished == true {
t.Errorf("Incorrect value, got %v but was expecting %v", isFinished, false)
}
}
func TestGet(t *testing.T) {
loc := time.UTC // closure can be used if necessary
start := time.Date(2022, 1, 7, 1, 0, 0, 0, loc) // Jan 7th 2022
end := time.Date(2022, 1, 10, 1, 0, 0, 0, loc) // Jan 10th 2022
ts := NewTimeStepper(start, end, 0, 0, 1, 0, 0, 0)
// Without step operation.
expected := time.Date(2022, 1, 7, 1, 0, 0, 0, loc) // Jan 7th 2022
actual := ts.Get()
if expected != actual {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
// Perform our step operation and let's verify the value is correct.
ts.Next()
expected = time.Date(2022, 1, 8, 1, 0, 0, 0, loc) // Jan 8th 2022
actual = ts.Get()
if expected != actual {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
// Perform yet another step and verify value function.
ts.Next()
expected = time.Date(2022, 1, 9, 1, 0, 0, 0, loc) // Jan 9th 2022
actual = ts.Get()
if expected != actual {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
// Developers note: This is to verify the non-UTC timezone daylight saving issue is fixed.
func TestTimeStepperTorontoTimezoneBug(t *testing.T) {
loc, _ := time.LoadLocation("America/Toronto")
start := time.Date(2021, 7, 1, 1, 0, 0, 0, loc) // July 1st 2021
end := time.Date(2022, 1, 1, 1, 0, 0, 0, loc) // Jan 1th 2022
ts := NewTimeStepper(start, end, 0, 0, 0, 0, 5, 0)
var actual time.Time
running := true
for running {
// Get the value we are on in the timestepper.
actual = ts.Get()
// log.Println(actual) // For debugging purposes only.
// Run our timestepper to get our next value.
ts.Next()
running = ts.Done() == false
}
expected := time.Date(2022, 1, 1, 1, 0, 0, 0, loc) // Jan 1th 2022
if expected != actual {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestRangeFromTimeStepper(t *testing.T) {
loc := time.UTC // closure can be used if necessary
start := time.Date(2022, 1, 7, 1, 0, 0, 0, loc) // Jan 7th 2022
end := time.Date(2022, 1, 10, 1, 0, 0, 0, loc) // Jan 10th 2022
actual := RangeFromTimeStepper(start, end, 0, 0, 1, 0, 0, 0)
expected := []time.Time{
time.Date(2022, 1, 7, 1, 0, 0, 0, loc), // Jan 7th 2022
time.Date(2022, 1, 8, 1, 0, 0, 0, loc), // Jan 8th 2022
time.Date(2022, 1, 9, 1, 0, 0, 0, loc), // Jan 9th 2022
time.Date(2022, 1, 10, 1, 0, 0, 0, loc), // Jan 10th 2022
}
if reflect.DeepEqual(actual, expected) == false {
t.Errorf("Incorrect date ranges, got %s but was expecting %s", actual, expected)
}
}