/
command_test.go
166 lines (149 loc) · 3.55 KB
/
command_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package fssh
import (
"errors"
"flag"
"fmt"
"io"
"reflect"
"testing"
)
type testCommand struct {
name string
description string
flagSet *flag.FlagSet
execFunc func(sh *Shell) error
ac AutoCompleterFunc
}
func (c *testCommand) Name() string {
return c.name
}
func (c *testCommand) Description() string {
return c.description
}
func (c *testCommand) FlagSet() *flag.FlagSet {
if c.flagSet == nil {
c.flagSet = flag.NewFlagSet(c.name, flag.ContinueOnError)
}
return c.flagSet
}
func (c *testCommand) Exec(sh *Shell) error {
if c.execFunc != nil {
return c.execFunc(sh)
}
fmt.Fprintf(sh.Stdout, "%s\n", c.name)
return nil
}
func (c *testCommand) Usage(w io.Writer) {
fmt.Fprintf(w, "Usage:\n %s\n", c.name)
}
func (c *testCommand) AutoCompleter() AutoCompleterFunc {
return c.ac
}
func (c *testCommand) Reset() {
}
func TestRegisterNewCommandFunc(t *testing.T) {
commandPoolsOrg := commandPools
commandNamesOrg := commandNames
commandNamesSortedOrg := commandNamesSorted
defer func() {
commandPools = commandPoolsOrg
commandNames = commandNamesOrg
commandNamesSorted = commandNamesSortedOrg
}()
testCmd1 := &testCommand{name: "test1"}
testCmd2 := &testCommand{name: "test2"}
testCmd3 := &testCommand{name: "test3"}
RegisterNewCommandFunc(func() Command { return testCmd1 })
RegisterNewCommandFunc(func() Command { return testCmd2 })
RegisterNewCommandFunc(func() Command { return testCmd3 })
DeregisterNewCommandFunc("test2")
wantCommandNames := []string{"test1", "test3"}
if !reflect.DeepEqual(commandNames, wantCommandNames) {
t.Errorf("got %v; want %v", commandNames, wantCommandNames)
}
}
func TestSortedCommandNames(t *testing.T) {
commandNamesOrg := commandNames
commandNamesSortedOrg := commandNamesSorted
defer func() {
commandNames = commandNamesOrg
commandNamesSorted = commandNamesSortedOrg
}()
commandNamesSorted = false
commandNames = []string{"c", "b", "a"}
want := []string{"a", "b", "c"}
got := SortedCommandNames()
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v; want %v", got, want)
}
}
func Test_autoComplete(t *testing.T) {
tests := []struct {
sh *Shell
cmd Command
line string
want []string
}{
{
sh: &Shell{
Stderr: io.Discard,
},
cmd: &testCommand{
name: "ls",
flagSet: &flag.FlagSet{},
ac: func(sh *Shell, arg string) ([]string, error) {
return []string{"a0", "a1", "a2"}, nil
},
},
line: "ls a",
want: []string{"a0", "a1", "a2"},
}, {
sh: &Shell{
Stderr: io.Discard,
},
cmd: &testCommand{
name: "ls",
flagSet: func() *flag.FlagSet {
flagSet := &flag.FlagSet{}
flagSet.Bool("l", false, "")
return flagSet
}(),
ac: func(sh *Shell, arg string) ([]string, error) {
return []string{"a0", "a1", "a2"}, nil
},
},
line: "ls -l a",
want: []string{"-l a0", "-l a1", "-l a2"},
}, {
sh: &Shell{
Stderr: io.Discard,
},
cmd: &testCommand{
name: "ls",
flagSet: &flag.FlagSet{},
ac: func(sh *Shell, arg string) ([]string, error) {
return []string{"a0", "a1", "a2"}, nil
},
},
line: "ls -l a",
}, {
sh: &Shell{
Stderr: io.Discard,
},
cmd: &testCommand{
name: "ls",
flagSet: &flag.FlagSet{},
ac: func(sh *Shell, arg string) ([]string, error) {
return nil, errors.New("test-error")
},
},
line: "ls a",
},
}
for i, test := range tests {
got := autoComplete(test.sh, test.cmd, test.line)
if !reflect.DeepEqual(got, test.want) {
t.Errorf("tests[%d]: got %v; want %v", i, got, test.want)
}
}
}