Skip to content

Commit

Permalink
Add tests about nonlocal as it bit me recently
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainDe committed Jun 19, 2015
1 parent 99810e2 commit bb850c2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
4 changes: 3 additions & 1 deletion didyoumean/didyoumean_internal_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
class GetObjectInFrameTests(unittest2.TestCase):
""" Class for tests related to frame/backtrace/etc inspection.
Tested functions are : get_objects_in_frame."""
Tested functions are : get_objects_in_frame.
No tests about 'nonlocal' is written because it is only supported
from Python 3."""

def name_corresponds_to(self, name, expected):
""" Helper functions to test get_objects_in_frame.
Expand Down
1 change: 1 addition & 0 deletions didyoumean/didyoumean_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
MUST_BE_CALLED_WITH_INST_RE = r"^unbound method (\w+)\(\) must be called " \
r"with (\w+) instance as first argument \(got (\w+) instance instead\)$"
OBJECT_HAS_NO_FUNC_RE = r"^(?:object of type )?'(\w+)' has no (\w+)(?:\(\))?$"
NO_BINDING_NONLOCAL_RE = r"^no binding for nonlocal '(\w+)' found$"


def match(pattern, string):
Expand Down
4 changes: 4 additions & 0 deletions didyoumean/didyoumean_re_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ def test_object_has_no(self):
for name, msg in msgs.items():
self.re_matches(msg, re.OBJECT_HAS_NO_FUNC_RE, ('generator', name))

def test_nobinding_nonlocal(self):
""" Test NO_BINDING_NONLOCAL_RE ."""
msg = "no binding for nonlocal 'foo' found"
self.re_matches(msg, re.NO_BINDING_NONLOCAL_RE, ('foo', ))

if __name__ == '__main__':
print(sys.version_info)
Expand Down
36 changes: 31 additions & 5 deletions didyoumean/didyoumean_sugg_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def get_exception(code):
UNKNOWN_ATTRIBUTEERROR = (AttributeError, None)
# SyntaxError for SyntaxErrorTests
INVALIDSYNTAX = (SyntaxError, re.INVALID_SYNTAX_RE)
NOBINDING = (SyntaxError, re.NO_BINDING_NONLOCAL_RE)
OUTSIDEFUNC = (SyntaxError, re.OUTSIDE_FUNCTION_RE)
MISSINGPARENT = (SyntaxError, re.MISSING_PARENT_RE)
INVALIDCOMP = (SyntaxError, re.INVALID_COMP_RE)
Expand Down Expand Up @@ -272,6 +273,12 @@ def test_global(self):
self.throws(typo, NAMEERROR, "'" + sugg + "' (global)")
self.runs(sugg)

def test_name(self):
"""Should be '__name__'."""
typo, sugg = '__name_', '__name__'
self.throws(typo, NAMEERROR, "'" + sugg + "' (global)")
self.runs(sugg)

def test_import(self):
"""Should be math."""
code = 'import math\n{0}'
Expand Down Expand Up @@ -445,11 +452,6 @@ def test_cls(self):
'FoobarClass().nameerror_cls()', NAMEERROR,
["'FoobarClass.this_is_cls_mthd'", "'cls.this_is_cls_mthd'"])

def test_main(self):
"""Should be '__main__'."""
# NICE_TO_HAVE
self.throws('__main_', NAMEERROR)

def test_complex_numbers(self):
""" Should be 1j ."""
code = 'assert {0} ** 2 == -1'
Expand Down Expand Up @@ -1156,6 +1158,30 @@ def test_unpack2(self):
self.runs(code, up_to_version(version))
self.throws(code, INVALIDSYNTAX, [], from_version(version))

def test_nonlocal(self):
""" nonlocal keyword is added in Python 3."""
# NICE_TO_HAVE
version = (3, 0)
code = 'def func():\n\tfoo = 1\n\tdef nested():\n\t\tnonlocal foo'
self.runs(code, from_version(version))
self.throws(code, INVALIDSYNTAX, [], up_to_version(version))

def test_nonlocal2(self):
""" nonlocal must be used only when binding exists."""
# NICE_TO_HAVE
version = (3, 0)
code = 'def func():\n\tdef nested():\n\t\tnonlocal foo'
self.throws(code, NOBINDING, [], from_version(version))
self.throws(code, INVALIDSYNTAX, [], up_to_version(version))

def test_nonlocal3(self):
""" nonlocal must be used only when binding to non-global exists."""
# NICE_TO_HAVE
version = (3, 0)
code = 'foo = 1\ndef func():\n\tdef nested():\n\t\tnonlocal foo'
self.throws(code, NOBINDING, [], from_version(version))
self.throws(code, INVALIDSYNTAX, [], up_to_version(version))


class MemoryErrorTests(GetSuggestionsTests):
"""Class for tests related to MemoryError."""
Expand Down

0 comments on commit bb850c2

Please sign in to comment.