-
Notifications
You must be signed in to change notification settings - Fork 43
/
arguments_test.go
112 lines (109 loc) · 2.31 KB
/
arguments_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
package cmds
import (
"bytes"
"io"
"testing"
)
func TestArguments(t *testing.T) {
var testCases = []struct {
input string
arguments []string
}{
{
input: "",
arguments: []string{},
},
{
input: "\n",
arguments: []string{""},
},
{
input: "\r\n",
arguments: []string{""},
},
{
input: "\r",
arguments: []string{"\r"},
},
{
input: "one",
arguments: []string{"one"},
},
{
input: "one\n",
arguments: []string{"one"},
},
{
input: "one\r\n",
arguments: []string{"one"},
},
{
input: "one\r",
arguments: []string{"one\r"},
},
{
input: "one\n\ntwo",
arguments: []string{"one", "", "two"},
},
{
input: "first\nsecond\nthird",
arguments: []string{"first", "second", "third"},
},
{
input: "first\r\nsecond\nthird",
arguments: []string{"first", "second", "third"},
},
{
input: "first\nsecond\nthird\n",
arguments: []string{"first", "second", "third"},
},
{
input: "first\r\nsecond\r\nthird\r\n",
arguments: []string{"first", "second", "third"},
},
{
input: "first\nsecond\nthird\n\n",
arguments: []string{"first", "second", "third", ""},
},
{
input: "\nfirst\nsecond\nthird\n",
arguments: []string{"", "first", "second", "third"},
},
}
for i, tc := range testCases {
for cut := 0; cut <= len(tc.arguments); cut++ {
args := newArguments(io.NopCloser(bytes.NewBufferString(tc.input)))
for j, arg := range tc.arguments[:cut] {
if !args.Scan() {
t.Errorf("in test case %d, missing argument %d", i, j)
continue
}
got := args.Argument()
if got != arg {
t.Errorf("in test case %d, expected argument %d to be %s, got %s", i, j, arg, got)
}
if args.Err() != nil {
t.Error(args.Err())
}
}
args = newArguments(args)
// Tests stopping in the middle.
for j, arg := range tc.arguments[cut:] {
if !args.Scan() {
t.Errorf("in test case %d, missing argument %d", i, j+cut)
continue
}
got := args.Argument()
if got != arg {
t.Errorf("in test case %d, expected argument %d to be %s, got %s", i, j+cut, arg, got)
}
if args.Err() != nil {
t.Error(args.Err())
}
}
if args.Scan() {
t.Errorf("in test case %d, got too many arguments", i)
}
}
}
}