-
Notifications
You must be signed in to change notification settings - Fork 1
/
range_test.go
76 lines (62 loc) · 1.46 KB
/
range_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
package enu_test
import (
"math"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/yuemori/enu"
)
type Month time.Month
func (m Month) Next(step int) enu.RangeValuer[time.Month, int] {
val := int(m) + step
if val > 12 {
val -= 12
}
return Month(val)
}
func (m Month) Value() time.Month {
return time.Month(m)
}
func (m Month) Compare(other time.Month) int {
if int(m) < int(other) {
return -1
}
if int(m) > int(other) {
return 1
}
return 0
}
func TestRange(t *testing.T) {
t.Parallel()
is := assert.New(t)
r1 := enu.FromRange[time.Month, int](Month(time.January), Month(time.July), 1).ToSlice()
is.Equal([]time.Month{time.January, time.February, time.March, time.April, time.May, time.June, time.July}, r1)
r2 := enu.FromRange[time.Month, int](Month(time.January), Month(math.MaxInt), 1).Take(13).ToSlice()
is.Equal([]time.Month{
time.January,
time.February,
time.March,
time.April,
time.May,
time.June,
time.July,
time.August,
time.September,
time.October,
time.November,
time.December,
time.January,
}, r2)
}
func TestRangeFind(t *testing.T) {
t.Parallel()
is := assert.New(t)
months := enu.FromRange[time.Month, int](Month(time.January), Month(time.December), 1)
r1, ok := months.Find(func(m time.Month, _ int) bool {
return m == time.October
})
is.Equal(true, ok)
is.Equal(time.October, r1)
r2 := months.Take(3).ToSlice()
is.Equal([]time.Month{time.January, time.February, time.March}, r2)
}