This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
shlex_test.go
101 lines (89 loc) · 2.81 KB
/
shlex_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
/*
Copyright 2012 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package shlex
import (
"strings"
"testing"
)
var (
// one two "three four" "five \"six\"" seven#eight # nine # ten
// eleven 'twelve\'
testString = "one two \"three four\" \"five \\\"six\\\"\" seven#eight # nine # ten\n eleven 'twelve\\' thirteen=13 fourteen/14"
)
func TestClassifier(t *testing.T) {
classifier := newDefaultClassifier()
tests := map[rune]runeTokenClass{
' ': spaceRuneClass,
'"': escapingQuoteRuneClass,
'\'': nonEscapingQuoteRuneClass,
'#': commentRuneClass}
for runeChar, want := range tests {
got := classifier.ClassifyRune(runeChar)
if got != want {
t.Errorf("ClassifyRune(%v) -> %v. Want: %v", runeChar, got, want)
}
}
}
func TestTokenizer(t *testing.T) {
testInput := strings.NewReader(testString)
expectedTokens := []*Token{
&Token{WordToken, "one"},
&Token{WordToken, "two"},
&Token{WordToken, "three four"},
&Token{WordToken, "five \"six\""},
&Token{WordToken, "seven#eight"},
&Token{CommentToken, " nine # ten"},
&Token{WordToken, "eleven"},
&Token{WordToken, "twelve\\"},
&Token{WordToken, "thirteen=13"},
&Token{WordToken, "fourteen/14"}}
tokenizer := NewTokenizer(testInput)
for i, want := range expectedTokens {
got, err := tokenizer.Next()
if err != nil {
t.Error(err)
}
if !got.Equal(want) {
t.Errorf("Tokenizer.Next()[%v] of %q -> %v. Want: %v", i, testString, got, want)
}
}
}
func TestLexer(t *testing.T) {
testInput := strings.NewReader(testString)
expectedStrings := []string{"one", "two", "three four", "five \"six\"", "seven#eight", "eleven", "twelve\\", "thirteen=13", "fourteen/14"}
lexer := NewLexer(testInput)
for i, want := range expectedStrings {
got, err := lexer.Next()
if err != nil {
t.Error(err)
}
if got != want {
t.Errorf("Lexer.Next()[%v] of %q -> %v. Want: %v", i, testString, got, want)
}
}
}
func TestSplit(t *testing.T) {
want := []string{"one", "two", "three four", "five \"six\"", "seven#eight", "eleven", "twelve\\", "thirteen=13", "fourteen/14"}
got, err := Split(testString)
if err != nil {
t.Error(err)
}
if len(want) != len(got) {
t.Errorf("Split(%q) -> %v. Want: %v", testString, got, want)
}
for i := range got {
if got[i] != want[i] {
t.Errorf("Split(%q)[%v] -> %v. Want: %v", testString, i, got[i], want[i])
}
}
}