From 431d564b950e85b4e8f258d13fc23257df3a5e6b 2014-03-10 00:59:45 From: Joel Nothman Date: 2014-03-10 00:59:45 Subject: [PATCH] Additional test coverage for dict key completion --- diff --git a/IPython/core/tests/test_completer.py b/IPython/core/tests/test_completer.py index 975f749..75dc9d1 100644 --- a/IPython/core/tests/test_completer.py +++ b/IPython/core/tests/test_completer.py @@ -534,7 +534,7 @@ def test_dict_key_completion_bytes(): @dec.onlyif(sys.version_info[0] < 3, 'This test only applies in Py<3') -def test_dict_key_completion_unicode(): +def test_dict_key_completion_unicode_py2(): """Test handling of unicode in dict key completion""" ip = get_ipython() complete = ip.Completer.complete @@ -558,6 +558,31 @@ def test_dict_key_completion_unicode(): nt.assert_in("abc']", matches) nt.assert_in("a\\u05d0']", matches) + # query using escape + _, matches = complete(line_buffer="d[u'a\\u05d0") + nt.assert_in("u05d0']", matches) # tokenized after \\ + + # query using character + _, matches = complete(line_buffer=unicode_type("d[u'a\xd7\x90", 'utf8')) + nt.assert_in("']", matches) + + +@dec.onlyif(sys.version_info[0] >= 3, 'This test only applies in Py>=3') +def test_dict_key_completion_unicode_py3(): + """Test handling of unicode in dict key completion""" + ip = get_ipython() + complete = ip.Completer.complete + + ip.user_ns['d'] = {unicode_type(b'a\xd7\x90', 'utf8'): None} + + # query using escape + _, matches = complete(line_buffer="d['a\\u05d0") + nt.assert_in("u05d0']", matches) # tokenized after \\ + + # query using character + _, matches = complete(line_buffer=unicode_type(b"d['a\xd7\x90", 'utf8')) + nt.assert_in(unicode_type(b"a\xd7\x90']", 'utf8'), matches) + def test_dict_like_key_completion(): """Test dict key completion applies where __getitem__ and keys exist""" @@ -596,3 +621,22 @@ def test_dataframe_key_completion(): _, matches = complete(line_buffer="d['") nt.assert_in("hello']", matches) nt.assert_in("world']", matches) + + +def test_dict_key_completion_invalids(): + """Smoke test cases dict key completion can't handle""" + ip = get_ipython() + complete = ip.Completer.complete + + ip.user_ns['no_getitem'] = None + ip.user_ns['no_keys'] = [] + ip.user_ns['cant_call_keys'] = dict + ip.user_ns['empty'] = {} + ip.user_ns['d'] = {'abc': 5} + + _, matches = complete(line_buffer="no_getitem['") + _, matches = complete(line_buffer="no_keys['") + _, matches = complete(line_buffer="cant_call_keys['") + _, matches = complete(line_buffer="empty['") + _, matches = complete(line_buffer="name_error['") + _, matches = complete(line_buffer="d['\\") # incomplete escape