import pytest from core.sheerka.services.SheerkaFunctionsParametersHistory import SheerkaFunctionsParametersHistory from prompt_toolkit.completion import CompleteEvent from prompt_toolkit.document import Document from repl.SheerkaPromptCompleter import SheerkaPromptCompleter, FuncFound from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka class TestSheerkaPromptCompleter(TestUsingMemoryBasedSheerka): def test_i_can_complete_with_builtins(self): sheerka = self.get_sheerka() completions = SheerkaPromptCompleter(sheerka).get_completions(Document("get"), CompleteEvent()) as_dict = {c.display_text: c for c in completions} assert "get_partition" in as_dict assert as_dict["get_partition"].text == "get_partition(" assert as_dict["get_partition"].display_text == "get_partition" assert as_dict["get_partition"].display_meta_text == "builtin" completions = SheerkaPromptCompleter(sheerka).get_completions(Document("cache"), CompleteEvent()) as_dict = {c.display_text: c for c in completions} assert "caches_names" in as_dict assert as_dict["caches_names"].text == "caches_names()" assert as_dict["caches_names"].display_text == "caches_names" assert as_dict["caches_names"].display_meta_text == "builtin" def test_i_can_complete_with_commands(self): sheerka = self.get_sheerka() completions = SheerkaPromptCompleter(sheerka).get_completions(Document("q"), CompleteEvent()) as_dict = {c.display_text: c for c in completions} assert "quit" in as_dict assert as_dict["quit"].text == "quit" assert as_dict["quit"].display_text == "quit" assert as_dict["quit"].display_meta_text == "command" def test_i_can_complete_with_pipeable(self): sheerka = self.get_sheerka() document = Document("| ") completions = SheerkaPromptCompleter(sheerka).get_completions(document, CompleteEvent()) as_dict = {c.display_text: c for c in completions} assert "first" in as_dict assert as_dict["first"].text == "first()" assert as_dict["first"].display_text == "first" assert as_dict["first"].display_meta_text == "builtin" assert "filter" in as_dict assert as_dict["filter"].text == "filter(" assert as_dict["filter"].display_text == "filter" assert as_dict["filter"].display_meta_text == "builtin" def test_i_can_complete_with_pipeable_when_starting_to_write(self): sheerka = self.get_sheerka() document = Document("| f") completions = SheerkaPromptCompleter(sheerka).get_completions(document, CompleteEvent()) as_dict = {c.display_text: c for c in completions} assert "first" in as_dict assert as_dict["first"].text == "first()" assert as_dict["first"].display_text == "first" assert as_dict["first"].display_meta_text == "builtin" assert "filter" in as_dict assert as_dict["filter"].text == "filter(" assert as_dict["filter"].display_text == "filter" assert as_dict["filter"].display_meta_text == "builtin" @pytest.mark.parametrize("text, expected", [ ("func(", ["10", "20", "30"]), ("func(1", ["10"]), ("func( 1", ["10"]), ("func( 10, ", ["'hello'"]), ("func( 10, v", []), ("func( 10, 'hel", ["'hello'"]), ('func( 10, "hel', []), ("func('hell,,', func2(2,4), 'w", ["'world'"]), ]) def test_i_can_complete_function_parameters(self, text, expected): sheerka = self.get_sheerka() context = self.get_context(sheerka) params_history_service = sheerka.services[SheerkaFunctionsParametersHistory.NAME] params_history_service.record_function_parameter(context, "func", 0, "10") params_history_service.record_function_parameter(context, "func", 0, "20") params_history_service.record_function_parameter(context, "func", 0, "30") params_history_service.record_function_parameter(context, "func", 1, "'hello'") params_history_service.record_function_parameter(context, "func", 2, "'world'") document = Document(text) completions = SheerkaPromptCompleter(sheerka).get_completions(document, CompleteEvent()) as_list = [c.display_text for c in completions] assert as_list == expected @pytest.mark.parametrize("text, pos, expected", [ ("", 0, False), ("foo", 3, False), ("|", 1, True), ("xxx | foo", 9, True), ("xxx | foo", 5, True), ("xxx | foo", 4, False), ]) def test_after_pipe(self, text, pos, expected): assert SheerkaPromptCompleter.after_pipe(text, pos) == expected @pytest.mark.parametrize("text, pos, expected", [ ("", 0, ""), ("foo", 3, "foo"), ("foo ", 4, "foo "), ("foo", 2, "fo"), ("foo bar", 7, "bar"), ("foo bar", 4, "foo "), ]) def test_last_word(self, text, pos, expected): assert SheerkaPromptCompleter.last_word(text, pos) == expected @pytest.mark.parametrize("text, pos, expected", [ ("", 0, None), ("foo", 3, None), ("foo(", 4, FuncFound("foo", 0, 3)), ("foo(a, ", 7, FuncFound("foo", 0, 3)), ("foo( a , ", 9, FuncFound("foo", 0, 3)), ("foo(bar)", 8, None), ("foo(bar)", 7, FuncFound("foo", 0, 3)), ("foo()", 5, None), ("foo()", 4, FuncFound("foo", 0, 3)), ("xxx foo(", 8, FuncFound("foo", 4, 7)), ("foo (", 5, FuncFound("foo", 0, 4)), ("foo (", 6, FuncFound("foo", 0, 5)), ]) def test_inside_function(self, text, pos, expected): assert SheerkaPromptCompleter.inside_function(text, pos) == expected @pytest.mark.parametrize("text, expected_param_number, expected_comma_index", [ ("", 0, -1), ("foo", 0, -1), ("foo, ", 1, 3), ("foo, ", 1, 3), ("foo, 'he,llo', ", 2, 13), ("foo, (he,llo), ", 2, 13), ("foo, (he,llo ", 1, 3), ("foo, 'he,llo ", 1, 3), ]) def test_get_param_number(self, text, expected_param_number, expected_comma_index): assert SheerkaPromptCompleter.get_param_number(text) == (expected_param_number, expected_comma_index) # def test_jedi_infer(self): # sheerka = self.get_sheerka() # # document = Document("get_partition(") # SheerkaPromptCompleter(sheerka).test_jedi(document) # pass # # def test_parso_parser(self): # import parso # module = parso.parse("get_results() | filter('id==4', param2) | format_d()") # pass