X Tutup
Skip to content

Commit eea8b79

Browse files
committed
add tests for cli, refactor cw
1 parent 2d61b19 commit eea8b79

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

bpython/cli.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ def clear_wrapped_lines(self):
426426
self.scr.clrtoeol()
427427

428428
def complete(self, tab=False):
429+
"""Get Autcomplete list and window."""
429430
if self.paste_mode and self.list_win_visible:
430431
self.scr.touchwin()
431432

@@ -483,24 +484,23 @@ def cw(self):
483484
"""Return the current word, i.e. the (incomplete) word directly to the
484485
left of the cursor"""
485486

486-
if self.cpos:
487-
# I don't know if autocomplete should be disabled if the cursor
488-
# isn't at the end of the line, but that's what this does for now.
489-
return
487+
# I don't know if autocomplete should be disabled if the cursor
488+
# isn't at the end of the line, but that's what this does for now.
489+
if self.cpos: return
490490

491+
# look from right to left for a bad method character
491492
l = len(self.s)
493+
is_method_char = lambda c: c.isalnum() or c in ('.', '_')
492494

493-
if (not self.s or
494-
(not self.s[l - 1].isalnum() and
495-
self.s[l - 1] not in ('.', '_'))):
495+
if not self.s or not is_method_char(self.s[l-1]):
496496
return
497497

498-
i = 1
499-
while i < l + 1:
500-
if not self.s[-i].isalnum() and self.s[-i] not in ('.', '_'):
498+
for i in range(1, l+1):
499+
if not is_method_char(self.s[-i]):
500+
i -= 1
501501
break
502-
i += 1
503-
return self.s[-i + 1:]
502+
503+
return self.s[-i:]
504504

505505
def delete(self):
506506
"""Process a del"""

bpython/test/test_repl.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import os
22
import unittest
3+
import sys
34
from itertools import islice
4-
5-
from bpython import config, repl
5+
from mock import Mock
6+
from bpython import config, repl, cli
67

78

89
class TestHistory(unittest.TestCase):
@@ -130,6 +131,13 @@ def test_update(self):
130131
self.assertNotEqual(list(slice), self.matches)
131132
self.assertEqual(list(newslice), newmatches)
132133

134+
class FakeHistory(repl.History):
135+
136+
def __init__(self):
137+
pass
138+
139+
def reset(self):
140+
pass
133141

134142
class FakeRepl(repl.Repl):
135143
def __init__(self, conf={}):
@@ -150,6 +158,11 @@ def current_line(self):
150158
def cw(self):
151159
return self.current_word
152160

161+
class FakeCliRepl(cli.CLIRepl, FakeRepl):
162+
def __init__(self):
163+
self.s = ''
164+
self.cpos = 0
165+
self.rl_history = FakeHistory()
153166

154167
class TestArgspec(unittest.TestCase):
155168
def setUp(self):
@@ -229,8 +242,55 @@ def test_alternate_complete(self):
229242
self.assertEqual(self.repl.completer.matches,
230243
['UnboundLocalError(', '__doc__'])
231244

245+
class TestCliRepl(unittest.TestCase):
246+
247+
def setUp(self):
248+
self.repl = FakeCliRepl()
249+
250+
def test_atbol(self):
251+
self.assertTrue(self.repl.atbol())
252+
self.repl.s = "\t\t"
253+
self.assertTrue(self.repl.atbol())
254+
self.repl.s = "\t\tnot an empty line"
255+
self.assertFalse(self.repl.atbol())
256+
257+
def test_addstr(self):
258+
self.repl.complete = Mock(True)
259+
260+
self.repl.s = "foo"
261+
self.repl.addstr("bar")
262+
self.assertEqual(self.repl.s, "foobar")
263+
264+
self.repl.cpos = 3
265+
self.repl.addstr('buzz')
266+
self.assertEqual(self.repl.s, "foobuzzbar")
267+
268+
def test_cw(self):
269+
270+
self.repl.cpos = 2
271+
self.assertEqual(self.repl.cw(), None)
272+
self.repl.cpos = 0
273+
274+
self.repl.s = ''
275+
self.assertEqual(self.repl.cw(), None)
276+
277+
self.repl.s = "this.is.a.test\t"
278+
self.assertEqual(self.repl.cw(), None)
279+
280+
s = "this.is.a.test"
281+
self.repl.s = s
282+
self.assertEqual(self.repl.cw(), s)
283+
284+
s = "\t\tthis.is.a.test"
285+
self.repl.s = s
286+
self.assertEqual(self.repl.cw(), s.lstrip())
232287

288+
self.repl.s = "this.is.\ta.test"
289+
self.assertEqual(self.repl.cw(), 'a.test')
233290

291+
def test_tab(self):
292+
pass
293+
# self.repl.tab()
234294

235295
if __name__ == '__main__':
236296
unittest.main()

0 commit comments

Comments
 (0)
X Tutup