Skip to content

Commit 504ddd3

Browse files
Test for more general simple expression finding
ast autocompletion will work so well after this
1 parent fbf72d8 commit 504ddd3

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

bpython/line.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,15 @@ def current_indexed_member_access_member(cursor_offset, line):
263263
for m in matches:
264264
if m.start(3) <= cursor_offset and m.end(3) >= cursor_offset:
265265
return LinePart(m.start(3), m.end(3), m.group(3))
266+
267+
def current_simple_expression(cursor_offset, line):
268+
"""The expression attribute lookup being performed on
269+
270+
e.g. <foo[0][1].bar>.ba|z
271+
A "simple expression" contains only . lookup and [] indexing."""
272+
273+
def current_simple_expression_attribute(cursor_offset, line):
274+
"""The attribute being looked up on a simple expression
275+
276+
e.g. foo[0][1].bar.<ba|z>
277+
A "simple expression" contains only . lookup and [] indexing."""

bpython/test/test_line_properties.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
current_method_definition_name, current_single_word, \
88
current_string_literal_attr, current_indexed_member_access_identifier, \
99
current_indexed_member_access_identifier_with_index, \
10-
current_indexed_member_access_member
10+
current_indexed_member_access_member, \
11+
current_simple_expression, current_simple_expression_attribute
1112

1213

1314
def cursor(s):
@@ -342,5 +343,42 @@ def test_simple(self):
342343
self.assertAccess('abc[def].gh |i')
343344
self.assertAccess('abc[def]|')
344345

346+
@unittest.skip("TODO")
347+
class TestCurrentSimpleExpression(LineTestCase):
348+
def setUp(self):
349+
self.func = current_simple_expression
350+
351+
def test_only_dots(self):
352+
self.assertAccess('<Object>.attr1|')
353+
self.assertAccess('<Object>.|')
354+
self.assertAccess('Object|')
355+
self.assertAccess('Object|.')
356+
self.assertAccess('<Object>.|')
357+
self.assertAccess('<Object.attr1>.attr2|')
358+
self.assertAccess('<Object>.att|r1.attr2')
359+
self.assertAccess('stuff[stuff] + {123: 456} + <Object.attr1>.attr2|')
360+
self.assertAccess('stuff[asd|fg]')
361+
self.assertAccess('stuff[asdf[asd|fg]')
362+
363+
def test_with_brackets(self):
364+
self.assertAccess('<foo[a]>.ba|r')
365+
self.assertAccess('<foo[a]>.ba|r baz[qux]xyzzy')
366+
self.assertAccess('foo[<bar[baz]>.qux|].xyzzy')
367+
self.assertAccess('<foo[bar[baz].qux]>.xyzzy|')
368+
self.assertAccess('foo[bar[baz].qux].xyzzy, <a>.b|')
369+
self.assertAccess('foo[bar[<baz>.|')
370+
self.assertAccess('foo[bar[<baz>.|] + 1].qux')
371+
372+
def test_cases_disallowed_by_simple_eval(self):
373+
# These are allowed for now, but could be changed.
374+
# for example, function calls are not allowed in simple expressions but
375+
# seem like they'd be a pain to weed out so we catch them in the next step."""
376+
self.assertAccess('foo().bar|')
377+
self.assertAccess('foo[bar(a, b)].baz|')
378+
self.assertAccess('foo(a, b).bar|')
379+
self.assertAccess('<(1 + 1)>.bar|')
380+
self.assertAccess('<(1 + 1 - foo.bar()[1])>.baz|')
381+
382+
345383
if __name__ == '__main__':
346384
unittest.main()

0 commit comments

Comments
 (0)