-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanners_test.go
97 lines (92 loc) · 2.76 KB
/
scanners_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
package iohelper
import (
"io"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/jf-tech/go-corelib/strs"
)
func TestNewScannerByDelim(t *testing.T) {
for _, test := range []struct {
name string
input io.Reader
delim string
flags ScannerByDelimFlag
expectedTokens []string
}{
{
name: "multi-char delim | eof as delim | drop delim",
input: strings.NewReader("abc#123##efg####???##xyz##"),
delim: "##",
flags: ScannerByDelimFlagEofAsDelim | ScannerByDelimFlagDropDelimInReturn,
expectedTokens: []string{"abc#123", "efg", "", "???", "xyz"},
},
{
name: "CR LF delim | eof as delim | include delim",
input: strings.NewReader("\r\n\rabc\r"),
delim: "\r\n",
flags: ScannerByDelimFlagEofAsDelim | ScannerByDelimFlagIncludeDelimInReturn,
expectedTokens: []string{"\r\n", "\rabc\r"},
},
{
name: "empty reader",
input: strings.NewReader(""),
delim: "*",
flags: ScannerByDelimFlagDefault,
expectedTokens: []string{},
},
{
name: "empty token",
input: strings.NewReader("*"),
delim: "*",
flags: ScannerByDelimFlagEofNotAsDelim | ScannerByDelimFlagDropDelimInReturn,
expectedTokens: []string{""},
},
{
name: "trailing newlines",
input: strings.NewReader("*\n"),
delim: "*",
flags: ScannerByDelimFlagEofAsDelim | ScannerByDelimFlagIncludeDelimInReturn,
expectedTokens: []string{"*", "\n"},
},
} {
t.Run(test.name, func(t *testing.T) {
s := NewScannerByDelim(test.input, test.delim, test.flags)
tokens := []string{}
for s.Scan() {
tokens = append(tokens, s.Text())
}
assert.NoError(t, s.Err())
assert.Equal(t, test.expectedTokens, tokens)
})
}
}
func TestNewScannerByDelim2(t *testing.T) {
for _, test := range []struct {
name string
input io.Reader
delim string
esc rune
flags ScannerByDelimFlag
expectedTokens []string
}{
{
name: "multi-char delim | with delim esc | eof as delim | drop delim",
input: strings.NewReader("abc#123##efg####???##xyz##"),
delim: "##",
esc: rune('?'),
flags: ScannerByDelimFlagEofAsDelim | ScannerByDelimFlagDropDelimInReturn,
expectedTokens: []string{"abc#123", "efg", "", "???##xyz"},
},
} {
t.Run(test.name, func(t *testing.T) {
s := NewScannerByDelim2(test.input, test.delim, strs.RunePtr(test.esc), test.flags)
tokens := []string{}
for s.Scan() {
tokens = append(tokens, s.Text())
}
assert.NoError(t, s.Err())
assert.Equal(t, test.expectedTokens, tokens)
})
}
}