-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmenter_test.go
109 lines (105 loc) · 2.44 KB
/
segmenter_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
package segmenter_test
import (
"testing"
"github.com/riandyrn/go-segmenter"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
testCases := []struct {
Name string
Config segmenter.Config[string]
ExpHasError bool
}{
{
Name: "Valid Config",
Config: segmenter.Config[string]{
Slice: []string{"1", "2", "3"},
SegmentLength: 5,
},
ExpHasError: false,
},
{
Name: "Empty Items",
Config: segmenter.Config[string]{
Slice: nil,
SegmentLength: 10,
},
ExpHasError: true,
},
{
Name: "No Segment Length",
Config: segmenter.Config[string]{
Slice: []string{"1", "2", "3"},
},
ExpHasError: true,
},
{
Name: "Negative Segment Length",
Config: segmenter.Config[string]{
Slice: []string{"1", "2", "3"},
SegmentLength: -5,
},
ExpHasError: true,
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
_, err := segmenter.New(testCase.Config)
require.Equal(t, testCase.ExpHasError, err != nil)
})
}
}
func TestNext(t *testing.T) {
testCases := []struct {
Name string
Slice []string
SegmentLength int
ExpResults [][]string
}{
{
Name: "Segment Length Evenly Divisible With List Length",
Slice: []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"},
SegmentLength: 5,
ExpResults: [][]string{
{"0", "1", "2", "3", "4"},
{"5", "6", "7", "8", "9"},
},
},
{
Name: "Segment Length Not Evenly Divisible With List Length",
Slice: []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"},
SegmentLength: 3,
ExpResults: [][]string{
{"0", "1", "2"},
{"3", "4", "5"},
{"6", "7", "8"},
{"9"},
},
},
{
Name: "Segment Length Larger Than List Length",
Slice: []string{"0", "1", "2", "3"},
SegmentLength: 5,
ExpResults: [][]string{
{"0", "1", "2", "3"},
},
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
// initialize segmenter
sgmntr, err := segmenter.New(segmenter.Config[string]{
Slice: testCase.Slice,
SegmentLength: testCase.SegmentLength,
})
require.NoError(t, err)
// collect all results
var results [][]string
for sgmntr.HasNext() {
results = append(results, sgmntr.Next())
}
// compare collected results
require.Equal(t, testCase.ExpResults, results)
})
}
}