/
completion_test.py
executable file
·278 lines (202 loc) · 6.58 KB
/
completion_test.py
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
# Copyright 2016 Andy Chu. 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
"""
completion_test.py: Tests for completion.py
"""
import unittest
from core import alloc
from core import cmd_exec_test
from core import completion # module under test
from core import legacy
from core import state
from core import test_lib
from core import word_eval
from core import ui
from core.id_kind import Id
from osh import ast_ as ast
from osh import parse_lib
assign_op_e = ast.assign_op_e
A1 = completion.WordsAction(['foo.py', 'foo', 'bar.py'])
C1 = completion.ChainedCompleter([A1])
status_lines = [ui.TestStatusLine()] * 10 # A bunch of dummies
mem = state.Mem('', [], {}, None)
exec_opts = state.ExecOpts(mem)
STATUS = completion.StatusOutput(status_lines, exec_opts)
V1 = completion.WordsAction(['$var1', '$var2', '$another_var'])
EMPTY = completion.WordsAction(['grep', 'sed', 'test'])
FIRST = completion.WordsAction(['grep', 'sed', 'test'])
class CompletionTest(unittest.TestCase):
def testLookup(self):
c = completion.CompletionLookup()
c.RegisterName('grep', C1)
print(c.GetCompleterForName('grep'))
print(c.GetCompleterForName('/usr/bin/grep'))
c.RegisterGlob('*.py', C1)
print(c.GetCompleterForName('/usr/bin/foo.py'))
print(c.GetCompleterForName('foo.rb'))
def testWordsAction(self):
print(list(A1.Matches(['f'], 0, 'f')))
def testExternalCommandAction(self):
mem = state.Mem('dummy', [], {}, None)
a = completion.ExternalCommandAction(mem)
print(list(a.Matches([], 0, 'f')))
def testShellFuncExecution(self):
ex = cmd_exec_test.InitExecutor()
func_node = ast.FuncDef()
c1 = ast.CompoundWord()
t1 = ast.token(Id.Lit_Chars, 'f1')
c1.parts.append(ast.LiteralPart(t1))
c2 = ast.CompoundWord()
t2 = ast.token(Id.Lit_Chars, 'f2')
c2.parts.append(ast.LiteralPart(t2))
a = ast.ArrayLiteralPart()
a.words = [c1, c2]
w = ast.CompoundWord()
w.parts.append(a)
# Set global COMPREPLY=(f1 f2)
pair = ast.assign_pair(ast.LhsName('COMPREPLY'), assign_op_e.Equal, w)
pair.spids.append(0) # dummy
pairs = [pair]
body_node = ast.Assignment(Id.Assign_None, [], pairs)
func_node.name = 'myfunc'
func_node.body = body_node
a = completion.ShellFuncAction(ex, func_node)
matches = list(a.Matches([], 0, 'f'))
self.assertEqual(['f1 ', 'f2 '], matches)
def testChainedCompleter(self):
print(list(C1.Matches(['f'], 0, 'f')))
p = completion.GlobPredicate('*.py')
c2 = completion.ChainedCompleter([A1], predicate=p)
print(list(c2.Matches(['f'], 0, 'f')))
def testRootCompleter(self):
comp_lookup = completion.CompletionLookup()
comp_lookup.RegisterName('grep', C1)
comp_lookup.RegisterEmpty(EMPTY)
comp_lookup.RegisterFirst(FIRST)
ev = _MakeTestEvaluator()
pool = alloc.Pool()
var_comp = V1
r = completion.RootCompleter(pool, ev, comp_lookup, var_comp)
m = list(r.Matches('grep f', STATUS))
self.assertEqual(['foo.py ', 'foo '], m)
m = list(r.Matches('grep g', STATUS))
self.assertEqual([], m)
m = list(r.Matches('ls $v', STATUS))
self.assertEqual(['$var1 ', '$var2 '], m)
m = list(r.Matches('g', STATUS))
self.assertEqual(['grep '], m)
# Empty completer
m = list(r.Matches('', STATUS))
self.assertEqual(['grep ', 'sed ', 'test '], m)
# Test compound commands. These PARSE
m = list(r.Matches('echo hi || grep f', STATUS))
m = list(r.Matches('echo hi; grep f', STATUS))
# Brace -- does NOT parse
m = list(r.Matches('{ echo hi; grep f', STATUS))
# TODO: Test if/for/while/case/etc.
m = list(r.Matches('var=$v', STATUS))
m = list(r.Matches('local var=$v', STATUS))
def _MakeTestEvaluator():
mem = state.Mem('', [], {}, None)
exec_opts = state.ExecOpts(mem)
splitter = legacy.SplitContext(mem)
ev = word_eval.CompletionWordEvaluator(mem, exec_opts, splitter)
return ev
def _TestGetCompletionType(buf):
ev = _MakeTestEvaluator()
arena = test_lib.MakeArena('<completion_test.py>')
w_parser, c_parser = parse_lib.MakeParserForCompletion(buf, arena)
print('---', buf)
return completion._GetCompletionType(w_parser, c_parser, ev, STATUS)
f = _TestGetCompletionType
class PartialParseTest(unittest.TestCase):
def testEmpty(self):
print(f(''))
print(f(' '))
def testCommands(self):
# External
print(f('ls'))
print(f('ls '))
# Redirect
print(f('cat <'))
print(f('cat <input'))
# Builtin
print(f('time'))
print(f('time '))
print(f('time echo'))
# Pipeline
print(f('ls |'))
print(f('ls | wc -l'))
# AndOr
print(f('ls && '))
print(f('ls && echo'))
# List
print(f('echo a;'))
print(f('echo a; echo'))
# BraceGroup
print(f('{ echo hi;'))
print(f('{ echo hi; echo')) # second word
print(f('{ echo hi; echo bye;')) # new command
# Subshell
print(f('( echo hi'))
print(f('( echo hi; echo'))
# FunctionDef
print(f('f() {'))
print(f('f() { echo'))
print(f('f() { echo hi;'))
print(f('if'))
print(f('if '))
print(f('if test '))
print(f('while'))
print(f('while '))
print(f('while test '))
print(f('case $foo ')) # in
print(f('case $foo in a)'))
print(f('case $foo in a) echo'))
def testVarSub(self):
# TODO: Mem needs variable "f"
# BracedVarSub
print(f('echo $'))
print(f('echo $f'))
# Double Quoted BracedVarSub
print(f('echo "$'))
print(f('echo "$f'))
# Braced var sub
#print(f('echo ${')) # TODO: FIx bug
#print(f('echo ${f'))
# Quoted Braced var sub
#print(f('echo "${'))
#print(f('echo "${f'))
# Single quoted var sub should give nothing
print(f("echo '${"))
print(f("echo '${f"))
# Array index
#print(f('echo ${a['))
#print(f('echo ${a[k'))
# Var sub in command sub
print(f('echo $(ls $'))
print(f('echo $(ls $f'))
# Var sub in var sub (bash doesn't do this)
#print(f('echo ${a:-$'))
#print(f('echo ${a:-$f'))
def testCommandSub(self):
# CommandSubPart
print(f('echo $('))
print(f('echo $(ls '))
#print(f('echo `'))
#print(f('echo `ls '))
# Command sub in var sub
#print(f('echo ${a:-$('))
#print(f('echo ${a:-$(l'))
def testArithSub(self):
pass
# BONUS points: ArithSubPart
#print(f('echo $((a'))
#print(f('echo $(($a'))
if __name__ == '__main__':
unittest.main()