-
Notifications
You must be signed in to change notification settings - Fork 11
/
iter_test.go
111 lines (96 loc) · 1.99 KB
/
iter_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
package route
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/suite"
)
type IterSuite struct {
suite.Suite
}
func TestIterSuite(t *testing.T) {
suite.Run(t, new(IterSuite))
}
func (s *IterSuite) TestEmptyOperationsSucceed() {
var values []string
var seps []byte
i := newIter(values, seps)
_, _, ok := i.next()
s.Equal(false, ok)
_, _, ok = i.next()
s.Equal(false, ok)
}
func (s *IterSuite) TestUnwind() {
tc := []charTc{
{
name: "Simple iteration",
input: []string{"hello"},
sep: []byte{pathSep},
},
{
name: "Combined iteration",
input: []string{"hello", "world", "ha"},
sep: []byte{pathSep, domainSep, domainSep},
},
}
for _, test := range tc {
i := newIter(test.input, test.sep)
var out []byte
for {
ch, _, ok := i.next()
if !ok {
break
}
out = append(out, ch)
}
s.Equal(test.String(), string(out), "%v", test.name)
}
}
func (s *IterSuite) TestRecoverPosition() {
i := newIter([]string{"hi", "world"}, []byte{pathSep, domainSep})
i.next()
i.next()
p := i.position()
i.next()
i.setPosition(p)
ch, sep, ok := i.next()
s.True(ok)
s.Equal(byte('w'), ch)
s.Equal(byte(domainSep), sep)
}
func (s *IterSuite) TestPushBack() {
i := newIter([]string{"hi", "world"}, []byte{pathSep, domainSep})
i.pushBack()
i.pushBack()
ch, sep, ok := i.next()
s.True(ok)
s.Equal(byte('h'), ch)
s.Equal(byte(pathSep), sep)
}
func (s *IterSuite) TestPushBackBoundary() {
i := newIter([]string{"hi", "world"}, []byte{pathSep, domainSep})
i.next()
i.next()
i.next()
i.pushBack()
i.pushBack()
ch, sep, ok := i.next()
s.True(ok)
s.Equal("i", fmt.Sprintf("%c", ch))
s.Equal(fmt.Sprintf("%c", pathSep), fmt.Sprintf("%c", sep))
}
func (s *IterSuite) TestString() {
i := newIter([]string{"hi"}, []byte{pathSep})
i.next()
s.Equal("<1:hi>", i.String())
i.next()
s.Equal("<end>", i.String())
}
type charTc struct {
name string
input []string
sep []byte
}
func (c *charTc) String() string {
return strings.Join(c.input, "")
}