-
Notifications
You must be signed in to change notification settings - Fork 5
/
goweek_test.go
162 lines (140 loc) · 5.57 KB
/
goweek_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 goweek
import (
"reflect"
"testing"
"time"
)
var expectedDays = []time.Time{
time.Date(2015, 11, 9, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 10, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 11, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 12, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 13, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 14, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 15, 0, 0, 0, 0, time.UTC),
}
var expectedDaysForNextWeek = []time.Time{
time.Date(2015, 11, 16, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 17, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 18, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 19, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 20, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 21, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 22, 0, 0, 0, 0, time.UTC),
}
// assuming that in case of 2015 year last week of 2015 in first week of 2016
var expectedDaysForNextWeekWithYearSwitch = []time.Time{
time.Date(2016, 1, 4, 0, 0, 0, 0, time.UTC),
time.Date(2016, 1, 5, 0, 0, 0, 0, time.UTC),
time.Date(2016, 1, 6, 0, 0, 0, 0, time.UTC),
time.Date(2016, 1, 7, 0, 0, 0, 0, time.UTC),
time.Date(2016, 1, 8, 0, 0, 0, 0, time.UTC),
time.Date(2016, 1, 9, 0, 0, 0, 0, time.UTC),
time.Date(2016, 1, 10, 0, 0, 0, 0, time.UTC),
}
var expectedDaysForPreviousWeek = []time.Time{
time.Date(2015, 11, 2, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 3, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 4, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 5, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 6, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 7, 0, 0, 0, 0, time.UTC),
time.Date(2015, 11, 8, 0, 0, 0, 0, time.UTC),
}
var expectedDaysForPreviousWeekWithYearSwitch = []time.Time{
time.Date(2014, 12, 29, 0, 0, 0, 0, time.UTC),
time.Date(2014, 12, 30, 0, 0, 0, 0, time.UTC),
time.Date(2014, 12, 31, 0, 0, 0, 0, time.UTC),
time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2015, 1, 2, 0, 0, 0, 0, time.UTC),
time.Date(2015, 1, 3, 0, 0, 0, 0, time.UTC),
time.Date(2015, 1, 4, 0, 0, 0, 0, time.UTC),
}
var expectedDaysForPreviousWeekWithYearSwitch2017 = []time.Time{
time.Date(2016, 12, 26, 0, 0, 0, 0, time.UTC),
time.Date(2016, 12, 27, 0, 0, 0, 0, time.UTC),
time.Date(2016, 12, 28, 0, 0, 0, 0, time.UTC),
time.Date(2016, 12, 29, 0, 0, 0, 0, time.UTC),
time.Date(2016, 12, 30, 0, 0, 0, 0, time.UTC),
time.Date(2016, 12, 31, 0, 0, 0, 0, time.UTC),
time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC),
}
func Test_NormalUsage(t *testing.T) {
var week, _ = NewWeek(2015, 46)
if len(week.Days) != 7 {
t.Errorf("Unexpected number of Week.Days, \n expected %v, \n given %v", 7, len(week.Days))
}
if week.Year != 2015 {
t.Errorf("Unexpected Week.Year, \n expected %v, \n given %v", 2015, week.Year)
}
if week.Number != 46 {
t.Errorf("Unexpected Week.Number, \n expected %v, \n given %v", 46, week.Number)
}
if !reflect.DeepEqual(expectedDays, week.Days) {
t.Errorf("Unexpected Week.Days, \n expected %v, \n given %v", expectedDays, week.Days)
}
}
func Test_ErrorThrowing(t *testing.T) {
var _, errorA = NewWeek()
if errorA.Error() != "NewWeek(): too few arguments, specify year and number of week" {
t.Error("Error expected when passing too few arguments (no args given)")
}
var _, errorB = NewWeek(2015)
if errorB.Error() != "NewWeek(): too few arguments, specify year and number of week" {
t.Error("Error expected when passing too few arguments (only year given)")
}
var _, errorC = NewWeek(2015, 54)
if errorC.Error() != "NewWeek(): number of week can't be less than 1 or greater than 53" {
t.Error("Error expected when passing incorrect week number")
}
var _, errorD = NewWeek(-1, 53)
if errorD.Error() != "NewWeek(): year can't be less than zero" {
t.Error("Error expected when passing incorrect year number")
}
}
func Test_NextWeek(t *testing.T) {
var week, _ = NewWeek(2015, 46)
var nextWeek, _ = week.Next()
if !reflect.DeepEqual(expectedDaysForNextWeek, nextWeek.Days) {
t.Errorf("Unexpected Week.Next(), \n expected %v, \n given %v", expectedDaysForNextWeek, nextWeek.Days)
}
var weekA, _ = NewWeek(2015, 53)
var nextWeekA, errA = weekA.Next()
if !reflect.DeepEqual(expectedDaysForNextWeekWithYearSwitch, nextWeekA.Days) {
t.Errorf("Unexpected Week.Next() with year switch, \n expected %v, \n given %v", expectedDaysForNextWeekWithYearSwitch, nextWeekA.Days)
}
if errA != nil {
t.Error(errA.Error())
}
}
func Test_PreviousWeek(t *testing.T) {
var week, _ = NewWeek(2015, 46)
var previousWeek, _ = week.Previous()
if !reflect.DeepEqual(expectedDaysForPreviousWeek, previousWeek.Days) {
t.Errorf("Unexpected Week.Previous(), \n expected %v, \n given %v", expectedDaysForPreviousWeek, previousWeek.Days)
}
var weekA, _ = NewWeek(2015, 1)
var previousWeekA, errA = weekA.Previous()
if !reflect.DeepEqual(expectedDaysForPreviousWeekWithYearSwitch, previousWeekA.Days) {
t.Errorf("Unexpected Week.Previous() with year switch, \n expected %v, \n given %v", expectedDaysForPreviousWeekWithYearSwitch, previousWeekA.Days)
}
if errA != nil {
t.Error(errA.Error())
}
var weekB, _ = NewWeek(2017, 1)
var previousWeekB, errB = weekB.Previous()
if !reflect.DeepEqual(expectedDaysForPreviousWeekWithYearSwitch2017, previousWeekB.Days) {
t.Errorf("Unexpected Week.Previous() with year switch, \n expected %v, \n given %v", expectedDaysForPreviousWeekWithYearSwitch2017, previousWeekB.Days)
}
if errB != nil {
t.Error(errB.Error())
}
}
func Test_ISOWeek_Compatibility(t *testing.T) {
var week, _ = NewWeek(2016, 1)
var w, y = week.Days[0].ISOWeek()
var weekDay = week.Days[0].Weekday()
if w != 2016 && y != 1 && weekDay != 3 {
t.Error("Broken ISO8601 compatibility")
}
}