summaryrefslogtreecommitdiff
path: root/vendor/github.com/smartystreets/assertions/time.go
blob: 7e05026143f13b4e083d5c8dd39f23e02fb96362 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
package assertions

import (
	"fmt"
	"time"
)

// ShouldHappenBefore receives exactly 2 time.Time arguments and asserts that the first happens before the second.
func ShouldHappenBefore(actual interface{}, expected ...interface{}) string {
	if fail := need(1, expected); fail != success {
		return fail
	}
	actualTime, firstOk := actual.(time.Time)
	expectedTime, secondOk := expected[0].(time.Time)

	if !firstOk || !secondOk {
		return shouldUseTimes
	}

	if !actualTime.Before(expectedTime) {
		return fmt.Sprintf(shouldHaveHappenedBefore, actualTime, expectedTime, actualTime.Sub(expectedTime))
	}

	return success
}

// ShouldHappenOnOrBefore receives exactly 2 time.Time arguments and asserts that the first happens on or before the second.
func ShouldHappenOnOrBefore(actual interface{}, expected ...interface{}) string {
	if fail := need(1, expected); fail != success {
		return fail
	}
	actualTime, firstOk := actual.(time.Time)
	expectedTime, secondOk := expected[0].(time.Time)

	if !firstOk || !secondOk {
		return shouldUseTimes
	}

	if actualTime.Equal(expectedTime) {
		return success
	}
	return ShouldHappenBefore(actualTime, expectedTime)
}

// ShouldHappenAfter receives exactly 2 time.Time arguments and asserts that the first happens after the second.
func ShouldHappenAfter(actual interface{}, expected ...interface{}) string {
	if fail := need(1, expected); fail != success {
		return fail
	}
	actualTime, firstOk := actual.(time.Time)
	expectedTime, secondOk := expected[0].(time.Time)

	if !firstOk || !secondOk {
		return shouldUseTimes
	}
	if !actualTime.After(expectedTime) {
		return fmt.Sprintf(shouldHaveHappenedAfter, actualTime, expectedTime, expectedTime.Sub(actualTime))
	}
	return success
}

// ShouldHappenOnOrAfter receives exactly 2 time.Time arguments and asserts that the first happens on or after the second.
func ShouldHappenOnOrAfter(actual interface{}, expected ...interface{}) string {
	if fail := need(1, expected); fail != success {
		return fail
	}
	actualTime, firstOk := actual.(time.Time)
	expectedTime, secondOk := expected[0].(time.Time)

	if !firstOk || !secondOk {
		return shouldUseTimes
	}
	if actualTime.Equal(expectedTime) {
		return success
	}
	return ShouldHappenAfter(actualTime, expectedTime)
}

// ShouldHappenBetween receives exactly 3 time.Time arguments and asserts that the first happens between (not on) the second and third.
func ShouldHappenBetween(actual interface{}, expected ...interface{}) string {
	if fail := need(2, expected); fail != success {
		return fail
	}
	actualTime, firstOk := actual.(time.Time)
	min, secondOk := expected[0].(time.Time)
	max, thirdOk := expected[1].(time.Time)

	if !firstOk || !secondOk || !thirdOk {
		return shouldUseTimes
	}

	if !actualTime.After(min) {
		return fmt.Sprintf(shouldHaveHappenedBetween, actualTime, min, max, min.Sub(actualTime))
	}
	if !actualTime.Before(max) {
		return fmt.Sprintf(shouldHaveHappenedBetween, actualTime, min, max, actualTime.Sub(max))
	}
	return success
}

// ShouldHappenOnOrBetween receives exactly 3 time.Time arguments and asserts that the first happens between or on the second and third.
func ShouldHappenOnOrBetween(actual interface{}, expected ...interface{}) string {
	if fail := need(2, expected); fail != success {
		return fail
	}
	actualTime, firstOk := actual.(time.Time)
	min, secondOk := expected[0].(time.Time)
	max, thirdOk := expected[1].(time.Time)

	if !firstOk || !secondOk || !thirdOk {
		return shouldUseTimes
	}
	if actualTime.Equal(min) || actualTime.Equal(max) {
		return success
	}
	return ShouldHappenBetween(actualTime, min, max)
}

// ShouldNotHappenOnOrBetween receives exactly 3 time.Time arguments and asserts that the first
// does NOT happen between or on the second or third.
func ShouldNotHappenOnOrBetween(actual interface{}, expected ...interface{}) string {
	if fail := need(2, expected); fail != success {
		return fail
	}
	actualTime, firstOk := actual.(time.Time)
	min, secondOk := expected[0].(time.Time)
	max, thirdOk := expected[1].(time.Time)

	if !firstOk || !secondOk || !thirdOk {
		return shouldUseTimes
	}
	if actualTime.Equal(min) || actualTime.Equal(max) {
		return fmt.Sprintf(shouldNotHaveHappenedOnOrBetween, actualTime, min, max)
	}
	if actualTime.After(min) && actualTime.Before(max) {
		return fmt.Sprintf(shouldNotHaveHappenedOnOrBetween, actualTime, min, max)
	}
	return success
}

// ShouldHappenWithin receives a time.Time, a time.Duration, and a time.Time (3 arguments)
// and asserts that the first time.Time happens within or on the duration specified relative to
// the other time.Time.
func ShouldHappenWithin(actual interface{}, expected ...interface{}) string {
	if fail := need(2, expected); fail != success {
		return fail
	}
	actualTime, firstOk := actual.(time.Time)
	tolerance, secondOk := expected[0].(time.Duration)
	threshold, thirdOk := expected[1].(time.Time)

	if !firstOk || !secondOk || !thirdOk {
		return shouldUseDurationAndTime
	}

	min := threshold.Add(-tolerance)
	max := threshold.Add(tolerance)
	return ShouldHappenOnOrBetween(actualTime, min, max)
}

// ShouldNotHappenWithin receives a time.Time, a time.Duration, and a time.Time (3 arguments)
// and asserts that the first time.Time does NOT happen within or on the duration specified relative to
// the other time.Time.
func ShouldNotHappenWithin(actual interface{}, expected ...interface{}) string {
	if fail := need(2, expected); fail != success {
		return fail
	}
	actualTime, firstOk := actual.(time.Time)
	tolerance, secondOk := expected[0].(time.Duration)
	threshold, thirdOk := expected[1].(time.Time)

	if !firstOk || !secondOk || !thirdOk {
		return shouldUseDurationAndTime
	}

	min := threshold.Add(-tolerance)
	max := threshold.Add(tolerance)
	return ShouldNotHappenOnOrBetween(actualTime, min, max)
}

// ShouldBeChronological receives a []time.Time slice and asserts that the are
// in chronological order starting with the first time.Time as the earliest.
func ShouldBeChronological(actual interface{}, expected ...interface{}) string {
	if fail := need(0, expected); fail != success {
		return fail
	}

	times, ok := actual.([]time.Time)
	if !ok {
		return shouldUseTimeSlice
	}

	var previous time.Time
	for i, current := range times {
		if i > 0 && current.Before(previous) {
			return fmt.Sprintf(shouldHaveBeenChronological,
				i, i-1, previous.String(), i, current.String())
		}
		previous = current
	}
	return ""
}