-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner_test.go
83 lines (73 loc) · 1.69 KB
/
scanner_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
package argh
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func BenchmarkScannerPOSIXyScannerScan(b *testing.B) {
for i := 0; i < b.N; i++ {
scanner := NewScanner(strings.NewReader(strings.Join([]string{
"walrus",
"-what",
"--ball=awesome",
"--elapsed",
"carrot cake",
}, string(nul))), nil)
for {
tok, _, _ := scanner.Scan()
if tok == EOL {
break
}
}
}
}
func TestScannerPOSIXyScanner(t *testing.T) {
for _, tc := range []struct {
name string
argv []string
expectedTokens []Token
expectedLiterals []string
expectedPositions []Pos
}{
{
name: "simple",
argv: []string{"walrus", "-cake", "--corn-dog", "awkward"},
expectedTokens: []Token{
IDENT,
ARG_DELIMITER,
COMPOUND_SHORT_FLAG,
ARG_DELIMITER,
LONG_FLAG,
ARG_DELIMITER,
IDENT,
EOL,
},
expectedLiterals: []string{
"walrus", string(nul), "-cake", string(nul), "--corn-dog", string(nul), "awkward", "",
},
expectedPositions: []Pos{
6, 7, 12, 13, 23, 24, 31, 32,
},
},
} {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
scanner := NewScanner(strings.NewReader(strings.Join(tc.argv, string(nul))), nil)
actualTokens := []Token{}
actualLiterals := []string{}
actualPositions := []Pos{}
for {
tok, lit, pos := scanner.Scan()
actualTokens = append(actualTokens, tok)
actualLiterals = append(actualLiterals, lit)
actualPositions = append(actualPositions, pos)
if tok == EOL {
break
}
}
r.Equal(tc.expectedTokens, actualTokens)
r.Equal(tc.expectedLiterals, actualLiterals)
r.Equal(tc.expectedPositions, actualPositions)
})
}
}