From 6e8548b44f4383b84f2fd7eadf7fe1a181e84244 2020-06-19 09:40:04 From: Corentin Cadiou Date: 2020-06-19 09:40:04 Subject: [PATCH] Add test for test_match_dict Also fix a mistake spotted using the tests. --- diff --git a/IPython/core/completer.py b/IPython/core/completer.py index 9a001e3..e442cb2 100644 --- a/IPython/core/completer.py +++ b/IPython/core/completer.py @@ -781,7 +781,7 @@ def match_dict_keys(keys: List[Union[str, bytes, Tuple[Union[str, bytes]]]], pre prefix_tuple = extra_prefix if extra_prefix else () Nprefix = len(prefix_tuple) def filter_by_prefix_tuple(key): - if len(key) < Nprefix: + if len(key) <= Nprefix: return False for k, pt in zip(key, prefix_tuple): if k != pt: diff --git a/IPython/core/tests/test_completer.py b/IPython/core/tests/test_completer.py index 80f479f..45e85d4 100644 --- a/IPython/core/tests/test_completer.py +++ b/IPython/core/tests/test_completer.py @@ -846,6 +846,35 @@ class TestCompleter(unittest.TestCase): match_dict_keys + def test_match_dict_keys_tuple(self): + """ + Test that match_dict_keys called with extra prefix works on a couple of use case, + does return what expected, and does not crash. + """ + delims = " \t\n`!@#$^&*()=+[{]}\\|;:'\",<>?" + + keys = [("foo", "bar"), ("foo", "oof"), ("foo", b"bar"), ('other', 'test')] + + # Completion on first key == "foo" + assert match_dict_keys(keys, "'", delims=delims, extra_prefix=("foo",)) == ("'", 1, ["bar", "oof"]) + assert match_dict_keys(keys, "\"", delims=delims, extra_prefix=("foo",)) == ("\"", 1, ["bar", "oof"]) + assert match_dict_keys(keys, "'o", delims=delims, extra_prefix=("foo",)) == ("'", 1, ["oof"]) + assert match_dict_keys(keys, "\"o", delims=delims, extra_prefix=("foo",)) == ("\"", 1, ["oof"]) + assert match_dict_keys(keys, "b'", delims=delims, extra_prefix=("foo",)) == ("'", 2, ["bar"]) + assert match_dict_keys(keys, "b\"", delims=delims, extra_prefix=("foo",)) == ("\"", 2, ["bar"]) + assert match_dict_keys(keys, "b'b", delims=delims, extra_prefix=("foo",)) == ("'", 2, ["bar"]) + assert match_dict_keys(keys, "b\"b", delims=delims, extra_prefix=("foo",)) == ("\"", 2, ["bar"]) + + # No Completion + assert match_dict_keys(keys, "'", delims=delims, extra_prefix=("no_foo",)) == ("'", 1, []) + assert match_dict_keys(keys, "'", delims=delims, extra_prefix=("fo",)) == ("'", 1, []) + + keys = [('foo1', 'foo2', 'foo3', 'foo4'), ('foo1', 'foo2', 'bar', 'foo4')] + assert match_dict_keys(keys, "'foo", delims=delims, extra_prefix=('foo1',)) == ("'", 1, ["foo2", "foo2"]) + assert match_dict_keys(keys, "'foo", delims=delims, extra_prefix=('foo1', 'foo2')) == ("'", 1, ["foo3"]) + assert match_dict_keys(keys, "'foo", delims=delims, extra_prefix=('foo1', 'foo2', 'foo3')) == ("'", 1, ["foo4"]) + assert match_dict_keys(keys, "'foo", delims=delims, extra_prefix=('foo1', 'foo2', 'foo3', 'foo4')) == ("'", 1, []) + def test_dict_key_completion_string(self): """Test dictionary key completion for string keys""" ip = get_ipython()