-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice_test.go
94 lines (78 loc) · 1.69 KB
/
slice_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
package iter
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestSlice(t *testing.T) {
t.Run("iterate till end", func(t *testing.T) {
data := []string{"i", "want", "pizza"}
iter := Slice(data)
for i := 0; ; i++ {
str, cont := iter.Next()
if !cont {
require.Equal(t, 3, i)
break
}
require.Less(t, i, len(data))
require.Equal(t, data[i], str)
}
})
t.Run("with break", func(t *testing.T) {
data := []string{"i", "want", "pizza"}
iter := Slice(data)
for i := 0; ; i++ {
_, cont := iter.Next()
if !cont {
require.Equal(t, 1, i)
break
}
iter.Break()
}
})
}
func TestPairedSlice(t *testing.T) {
t.Run("iterate till end", func(t *testing.T) {
data := []string{"i", "want", "pizza", "pepperoni"}
want := [][]string{
{"i", "want"}, {"pizza", "pepperoni"},
}
iter := PairedSlice(data)
for i := 0; ; i++ {
pair, cont := iter.Next()
if !cont {
require.Equal(t, 2, i)
break
}
require.Less(t, i, len(want))
require.Equal(t, want[i], pair)
}
})
t.Run("iterate by even amount of elements", func(t *testing.T) {
data := []string{"i", "want", "pizza", "pepperoni", "special"}
want := [][]string{
{"i", "want"}, {"pizza", "pepperoni"},
}
iter := PairedSlice(data)
for i := 0; ; i++ {
pair, cont := iter.Next()
if !cont {
require.Equal(t, 2, i)
break
}
require.Less(t, i, len(want))
require.Equal(t, want[i], pair)
}
})
t.Run("with break", func(t *testing.T) {
data := []string{"i", "want", "pizza", "pepperoni"}
iter := PairedSlice(data)
for i := 0; ; i++ {
_, cont := iter.Next()
if !cont {
require.Equal(t, 1, i)
break
}
iter.Break()
}
})
}