-
Notifications
You must be signed in to change notification settings - Fork 8
/
scanner_test.go
105 lines (100 loc) · 2.99 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package argv
import (
"math"
"testing"
)
var (
parseText = ` a aa a'aa' a"aa"a
a$PATH a"$PATH" a'$PATH'
a"$*" a"$0" a"$\"
a| a|a
a"\A" a"\a\b\f\n\r\t\v\\\$" \t a'\A' a'\t'` +
" a`ls /` `ls ~`"
)
func TestScanner(t *testing.T) {
gots, err := Scan(
parseText,
)
if err != nil {
t.Fatal(err)
}
expects := []Token{
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokSpace},
{Type: TokString, Value: []rune("aa")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokStringSingleQuote, Value: []rune("aa")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokStringDoubleQuote, Value: []rune("aa")},
{Type: TokString, Value: []rune("a")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a$PATH")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokStringDoubleQuote, Value: []rune("$PATH")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokStringSingleQuote, Value: []rune("$PATH")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokStringDoubleQuote, Value: []rune("$*")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokStringDoubleQuote, Value: []rune("$0")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokStringDoubleQuote, Value: []rune("$\\")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokPipe},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokPipe},
{Type: TokString, Value: []rune("a")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokStringDoubleQuote, Value: []rune("\\A")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokStringDoubleQuote, Value: []rune("\a\b\f\n\r\t\v\\$")},
{Type: TokSpace},
{Type: TokString, Value: []rune("t")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokStringSingleQuote, Value: []rune("\\A")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokStringSingleQuote, Value: []rune("\t")},
{Type: TokSpace},
{Type: TokString, Value: []rune("a")},
{Type: TokBackQuote, Value: []rune("ls /")},
{Type: TokSpace},
{Type: TokBackQuote, Value: []rune("ls ~")},
{Type: TokEOF},
}
if len(gots) != len(expects) {
t.Errorf("token count is not equal: expect %d, got %d", len(expects), len(gots))
}
l := int(math.Min(float64(len(gots)), float64(len(expects))))
for i := 0; i < l; i++ {
got := gots[i]
expect := expects[i]
if got.Type != expect.Type {
t.Errorf("token type is not equal: %d: expect %d, got %d", i, expect.Type, got.Type)
}
if expect.Type != TokSpace && string(got.Value) != string(expect.Value) {
t.Errorf("token value is not equal: %d: expect %s, got %s", i, string(expect.Value), string(got.Value))
}
}
for _, text := range []string{
`a"`, `a'`, `a"\`, "`ls ~", `a\`,
} {
_, err := Scan(text)
if err != ErrInvalidSyntax {
t.Errorf("expect unexpected eof error, but got: %v", err)
}
}
}