-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathcommands_test.go
228 lines (180 loc) · 4.74 KB
/
commands_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package gore
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAction_Type(t *testing.T) {
var stdout, stderr strings.Builder
s, err := NewSession(&stdout, &stderr)
t.Cleanup(func() { s.Clear() })
require.NoError(t, err)
codes := []string{
`:type "hello"`,
":type 128",
":type 3.14",
"func f() []int { return nil }",
":t f",
":t f()",
":i fmt encoding/json",
":t fmt.Sprint",
":t fmt.Println",
":t json.NewEncoder",
":t x",
":t fmt",
}
for _, code := range codes {
_ = s.Eval(code)
}
assert.Regexp(t, `string
int
float64
func\(\) \[\]int
\[\]int
func\(a \.\.\.(?:interface\{\}|any)\) string
func\(a \.\.\.(?:interface\{\}|any)\) \(n int, err error\)
func\(w io\.Writer\) \*encoding/json\.Encoder
`, stdout.String())
assert.Equal(t, `type: cannot get type: x
type: cannot get type: fmt
`, stderr.String())
}
func TestAction_Doc(t *testing.T) {
var stdout, stderr strings.Builder
s, err := NewSession(&stdout, &stderr)
t.Cleanup(func() { s.Clear() })
require.NoError(t, err)
err = s.Eval(":import encoding/json")
require.NoError(t, err)
err = s.Eval(":i fmt")
require.NoError(t, err)
test := func() {
err = s.Eval(":doc fmt")
require.NoError(t, err)
err = s.Eval(":doc fmt.Print")
require.NoError(t, err)
err = s.Eval(":d json.NewEncoder(nil).Encode")
require.NoError(t, err)
}
test()
// test :doc works after some code
err = s.Eval("a := 1")
require.NoError(t, err)
err = s.Eval("fmt.Print()")
require.NoError(t, err)
test()
assert.Contains(t, stdout.String(), "package fmt")
assert.Contains(t, stdout.String(), "func Printf")
assert.Equal(t, "", stderr.String())
}
func TestAction_Import(t *testing.T) {
var stdout, stderr strings.Builder
s, err := NewSession(&stdout, &stderr)
t.Cleanup(func() { s.Clear() })
require.NoError(t, err)
err = s.Eval(":import encoding/json fmt")
require.NoError(t, err)
err = s.Eval("fmt.Print")
require.NoError(t, err)
err = s.Eval("json.Encoder{}")
require.NoError(t, err)
assert.Contains(t, stdout.String(), "(func(...interface {}) (int, error))")
assert.Contains(t, stdout.String(), "json.Encoder")
assert.Equal(t, "", stderr.String())
err = s.Eval(":import invalid")
require.Error(t, err)
err = s.Eval("fmt.Sprint")
require.NoError(t, err)
assert.Equal(t, "import: could not import \"invalid\"\n", stderr.String())
}
func TestAction_Clear(t *testing.T) {
var stdout, stderr strings.Builder
s, err := NewSession(&stdout, &stderr)
t.Cleanup(func() { s.Clear() })
require.NoError(t, err)
codes := []string{
`x := 10`,
`x`,
`:clear`,
`x := "foo"`,
`x`,
`:clear`,
`x`,
}
for _, code := range codes {
_ = s.Eval(code)
}
assert.Equal(t, `10
10
"foo"
"foo"
`, stdout.String())
assert.Equal(t, "undefined: x\n", stderr.String())
}
func TestAction_Help(t *testing.T) {
var stdout, stderr strings.Builder
s, err := NewSession(&stdout, &stderr)
t.Cleanup(func() { s.Clear() })
require.NoError(t, err)
err = s.Eval(": : : help ")
require.NoError(t, err)
assert.Contains(t, stdout.String(), ":import <package>")
assert.Contains(t, stdout.String(), ":write [<file>]")
assert.Contains(t, stdout.String(), "show this help")
assert.Contains(t, stdout.String(), "quit the session")
assert.Equal(t, "", stderr.String())
err = s.Eval(":h")
require.NoError(t, err)
}
func TestAction_Quit(t *testing.T) {
var stdout, stderr strings.Builder
s, err := NewSession(&stdout, &stderr)
t.Cleanup(func() { s.Clear() })
require.NoError(t, err)
err = s.Eval(" :\t: quit")
require.Equal(t, ErrQuit, err)
assert.Equal(t, "", stdout.String())
assert.Equal(t, "", stderr.String())
err = s.Eval(":q")
require.Equal(t, ErrQuit, err)
}
func TestAction_CommandNotFound(t *testing.T) {
var stdout, stderr strings.Builder
s, err := NewSession(&stdout, &stderr)
t.Cleanup(func() { s.Clear() })
require.NoError(t, err)
err = s.Eval(":::")
require.NoError(t, err)
err = s.Eval(":foo")
require.Error(t, err)
err = s.Eval(":ii")
require.Error(t, err)
err = s.Eval(":docc")
require.Error(t, err)
err = s.Eval(":help]")
require.Error(t, err)
assert.Equal(t, "", stdout.String())
assert.Equal(t, `command not found: foo
command not found: ii
command not found: docc
command not found: help]
`, stderr.String())
}
func TestAction_ArgumentRequired(t *testing.T) {
var stdout, stderr strings.Builder
s, err := NewSession(&stdout, &stderr)
t.Cleanup(func() { s.Clear() })
require.NoError(t, err)
err = s.Eval(":import")
require.Error(t, err)
err = s.Eval(":type")
require.Error(t, err)
err = s.Eval(":doc")
require.Error(t, err)
assert.Equal(t, "", stdout.String())
assert.Equal(t, `import: argument is required
type: argument is required
doc: argument is required
`, stderr.String())
}