forked from GuilhermeCaruso/kair
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharpdate_test.go
191 lines (167 loc) · 5.92 KB
/
sharpdate_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package sharpdate
import (
"testing"
"time"
)
func TestDateTimeAddDays(t *testing.T) {
expected := "31-12-2018 23:55:59"
actual := NewDateTime(2018, 12, 28, 23, 55, 59).AddDays(3).ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeAddMonths(t *testing.T) {
expected := "01-05-2018 23:55:59"
actual := NewDateTime(2018, 2, 1, 23, 55, 59).AddMonths(3).ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeAddYears(t *testing.T) {
expected := "01-05-2018 23:55:59"
actual := NewDateTime(2015, 5, 1, 23, 55, 59).AddYears(3).ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeAdd(t *testing.T) {
expected := "03-05-2018 23:55:59"
actual := NewDateTime(2015, 4, 1, 23, 55, 59).Add(3, 1, 2).ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeSubtract(t *testing.T) {
expected := "01-04-2015 22:50:50"
actual := NewDateTime(2018, 5, 3, 22, 50, 50).Subtract(3, 1, 2).ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeAddTime(t *testing.T) {
expected := "03-05-2018 23:55:59"
actual := NewDateTime(2018, 5, 3, 22, 50, 50).AddTime(1, 5, 9).ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeSubtractTime(t *testing.T) {
expected := "03-05-2018 22:50:50"
actual := NewDateTime(2018, 5, 3, 23, 55, 59).SubtractTime(1, 5, 9).ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeAddMinutes(t *testing.T) {
expected := "03-05-2018 23:55:59"
actual := NewDateTime(2018, 5, 3, 23, 45, 59).AddMinutes(10).ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeAddHours(t *testing.T) {
expected := "03-05-2018 23:55:59"
actual := NewDateTime(2018, 5, 3, 22, 55, 59).AddHours(1).ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeAddSeconds(t *testing.T) {
expected := "03-05-2018 23:55:59"
actual := NewDateTime(2018, 5, 3, 23, 55, 49).AddSeconds(10).ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeAddMilliseconds(t *testing.T) {
expected := "03-05-2018 23:55:59.010"
actual := NewDateTime(2018, 5, 3, 23, 55, 59, 0).AddMilliseconds(10).ToString("dd-MM-yyyy HH:mm:ss.f")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeAddMicroseconds(t *testing.T) {
expected := "03-05-2018 23:55:59.000011"
actual := NewDateTime(2018, 5, 3, 23, 55, 59, 0).AddMicroseconds(11).ToString("dd-MM-yyyy HH:mm:ss.ff")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestNewDateTimeToString(t *testing.T) {
expected := "31-12-2018 23:55:59"
actual := NewDateTime(2018, 12, 31, 23, 55, 59).ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestNewDateTimeNoTimeToString(t *testing.T) {
expected := "31-12-2018"
actual := NewDateTime(2018, 12, 31).ToString("dd-MM-yyyy")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestNewDateTimeShortTimeToString(t *testing.T) {
expected := "31-12-2018 23"
actual := NewDateTime(2018, 12, 31, 23).ToString("dd-MM-yyyy HH")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestNewDateTimeShortTimeMinutesToString(t *testing.T) {
expected := "31-12-2018 23:55"
actual := NewDateTime(2018, 12, 31, 23, 55).ToString("dd-MM-yyyy HH:mm")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeStructToString(t *testing.T) {
expected := "31-12-2018 23:55:59"
actual := DateTime{Year: 2018, Month: 12, Day: 31, Hour: 23, Min: 55, Sec: 59}.ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeNewToString(t *testing.T) {
expected := "31-12-2018 23:55:59"
actual := DateTime{}.New(2018, 12, 31, 23, 55, 59).ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeNowToString(t *testing.T) {
expected := time.Now().Format("02-01-2006 15:04:05")
actual := DateTime{}.Now().ToString("dd-MM-yyyy HH:mm:ss")
if expected != actual {
t.Errorf("\nExpected '%s' but got '%s' instead.", expected, actual)
}
}
func TestDateTimeUnix(t *testing.T) {
expected := time.Now().Unix()
actual := DateTime{}.Now().Time.Unix()
if expected != actual {
t.Errorf("\nExpected '%v' but got '%v' instead.", expected, actual)
}
}
func TestDateTimeYearMonth(t *testing.T) {
expected := "12-2018"
actual := DateTime{Year: 2018, Month: 12}.ToString("MM-yyyy")
if expected != actual {
t.Errorf("\nExpected '%v' but got '%v' instead.", expected, actual)
}
}
func TestDateTimeYear(t *testing.T) {
expected := "2018"
actual := DateTime{Year: 2018}.ToString("yyyy")
if expected != actual {
t.Errorf("\nExpected '%v' but got '%v' instead.", expected, actual)
}
}
func TestNewDateTimeInvalidArguments(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("\nExpected panic but got no panic instead!")
}
}()
NewDateTime("2018").ToString("yyyy")
}