-
Notifications
You must be signed in to change notification settings - Fork 257
/
parse_test.go
85 lines (80 loc) · 3.32 KB
/
parse_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
package main
import (
"testing"
)
type parseCmdTest struct {
input string
split []string
ok bool
}
var parseCmdTests = []parseCmdTest{
parseCmdTest{`w1 w2`, []string{`w1`, `w2`}, true},
parseCmdTest{`w1 w2 w3`, []string{`w1`, `w2`, `w3`}, true},
parseCmdTest{`w1 'w2 w3'`, []string{`w1`, `'w2 w3'`}, true},
parseCmdTest{`w1 "w2 w3"`, []string{`w1`, `"w2 w3"`}, true},
parseCmdTest{`w1 "w2 w3"`, []string{`w1`, `"w2 w3"`}, true},
parseCmdTest{`w1 'w2 w3'`, []string{`w1`, `'w2 w3'`}, true},
parseCmdTest{`w1"w2 w3"`, []string{`w1"w2 w3"`}, true},
parseCmdTest{`w1'w2 w3'`, []string{`w1'w2 w3'`}, true},
parseCmdTest{`w1"w2 'w3"`, []string{`w1"w2 'w3"`}, true},
parseCmdTest{`w1'w2 "w3'`, []string{`w1'w2 "w3'`}, true},
parseCmdTest{`"w1 w2" "w3"`, []string{`"w1 w2"`, `"w3"`}, true},
parseCmdTest{`'w1 w2' "w3"`, []string{`'w1 w2'`, `"w3"`}, true},
parseCmdTest{`'w1 \'w2' "w3"`, []string{`'w1 \'w2'`, `"w3"`}, true},
parseCmdTest{`'w1 \'w2 "w3"`, []string{}, false},
parseCmdTest{`w1 'w2 w3'"`, []string{}, false},
parseCmdTest{`w1 "w2 w3"'`, []string{}, false},
parseCmdTest{`w1 ' "w2 w3"`, []string{}, false},
parseCmdTest{`w1 " 'w2 w3'`, []string{}, false},
parseCmdTest{`w1"w2 w3""`, []string{}, false},
parseCmdTest{`w1'w2 w3''`, []string{}, false},
parseCmdTest{`w1"w2 'w3""`, []string{}, false},
parseCmdTest{`w1'w2 "w3''`, []string{}, false},
parseCmdTest{`"w1 w2" "w3"'`, []string{}, false},
parseCmdTest{`'w1 w2' "w3"'`, []string{}, false},
parseCmdTest{`w1,"w2 w3"`, []string{`w1`, `,`, `"w2 w3"`}, true},
parseCmdTest{`w1,'w2 w3'`, []string{`w1`, `,`, `'w2 w3'`}, true},
parseCmdTest{`w1 , "w2 w3"`, []string{`w1`, `,`, `"w2 w3"`}, true},
parseCmdTest{`w1 , 'w2 w3'`, []string{`w1`, `,`, `'w2 w3'`}, true},
parseCmdTest{`w1, "w2 w3"`, []string{`w1`, `,`, `"w2 w3"`}, true},
parseCmdTest{`w1, 'w2 w3'`, []string{`w1`, `,`, `'w2 w3'`}, true},
parseCmdTest{`w1 ,"w2 w3"`, []string{`w1`, `,`, `"w2 w3"`}, true},
parseCmdTest{`w1 ,'w2 w3'`, []string{`w1`, `,`, `'w2 w3'`}, true},
parseCmdTest{`w1"w2, w3"`, []string{`w1"w2, w3"`}, true},
parseCmdTest{`w1'w2, w3'`, []string{`w1'w2, w3'`}, true},
parseCmdTest{`w1"w2, 'w3"`, []string{`w1"w2, 'w3"`}, true},
parseCmdTest{`w1'w2, "w3'`, []string{`w1'w2, "w3'`}, true},
parseCmdTest{`"w1, w2" "w3"`, []string{`"w1, w2"`, `"w3"`}, true},
parseCmdTest{`'w1, w2' "w3"`, []string{`'w1, w2'`, `"w3"`}, true},
parseCmdTest{`'w1, \'w2' "w3"`, []string{`'w1, \'w2'`, `"w3"`}, true},
parseCmdTest{`h1, .article-teaser, .article-content`, []string{
`h1`, `,`, `.article-teaser`, `,`, `.article-content`,
}, true},
parseCmdTest{`h1 ,.article-teaser ,.article-content`, []string{
`h1`, `,`, `.article-teaser`, `,`, `.article-content`,
}, true},
parseCmdTest{`h1 , .article-teaser , .article-content`, []string{
`h1`, `,`, `.article-teaser`, `,`, `.article-content`,
}, true},
}
func sliceEq(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
for i := range s1 {
if s1[i] != s2[i] {
return false
}
}
return true
}
func TestParseCommands(t *testing.T) {
for _, test := range parseCmdTests {
parsed, err := ParseCommands(test.input)
if test.ok != (err == nil) {
t.Errorf("`%s`: should have cause error? %v", test.input, !test.ok)
} else if !sliceEq(test.split, parsed) {
t.Errorf("`%s`: `%s`: `%s`", test.input, test.split, parsed)
}
}
}