-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparser_test.go
126 lines (110 loc) · 3.88 KB
/
parser_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package parseargs_test
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/txgruppi/parseargs-go"
)
func TestParser(t *testing.T) {
Convey("valid", t, func() {
Convey("should understand simple words", func() {
input := `my string of arguments`
expected := []string{`my`, `string`, `of`, `arguments`}
actual, err := parseargs.Parse(input)
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
Convey("should understand single quote", func() {
input := `my 'string of arguments'`
expected := []string{`my`, `string of arguments`}
actual, err := parseargs.Parse(input)
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
Convey("should understand double quote", func() {
input := `"my string" of arguments`
expected := []string{`my string`, `of`, `arguments`}
actual, err := parseargs.Parse(input)
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
Convey("should understand escaped single quote", func() {
input := `my 'string \'of\'' arguments`
expected := []string{`my`, `string 'of'`, `arguments`}
actual, err := parseargs.Parse(input)
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
Convey("should understand escaped double quote", func() {
input := `my "string \"of\"" arguments`
expected := []string{`my`, `string "of"`, `arguments`}
actual, err := parseargs.Parse(input)
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
Convey("should understand double quotes inside single quotted string", func() {
input := `my 'string "of" arguments'`
expected := []string{`my`, `string "of" arguments`}
actual, err := parseargs.Parse(input)
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
Convey("should understand single quotes inside double quotted string", func() {
input := `my "string 'of' arguments"`
expected := []string{`my`, `string 'of' arguments`}
actual, err := parseargs.Parse(input)
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
Convey("should ignore consecutive spaces", func() {
input := `my string of arguments`
expected := []string{`my`, `string`, `of`, `arguments`}
actual, err := parseargs.Parse(input)
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
Convey("should accept tabs, newlines and cartridge returns as spaces", func() {
input := "my\tstring\nof\rarguments"
expected := []string{`my`, `string`, `of`, `arguments`}
actual, err := parseargs.Parse(input)
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
Convey("should read a one char word at the end of the input", func() {
input := `my string of arguments 0`
expected := []string{`my`, `string`, `of`, `arguments`, `0`}
actual, err := parseargs.Parse(input)
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
})
Convey("invalid", t, func() {
Convey("should not allow for a quotted string to start right after a word", func() {
input := `my"string" of arguments`
expected := `invalid argument(s)`
_, err := parseargs.Parse(input)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, expected)
})
Convey("should detect unexpected EOF", func() {
input := `my "string of arguments`
expected := `unexpected end of input`
_, err := parseargs.Parse(input)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, expected)
})
Convey("should detect wrongly escaped quotes", func() {
input := `my \\"string\\" of arguments`
expected := `invalid argument(s)`
_, err := parseargs.Parse(input)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, expected)
})
Convey("should not allow escaped spaces", func() {
input := `my\ string of arguments`
expected := `invalid syntax`
_, err := parseargs.Parse(input)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, expected)
})
})
}