Debian Codegraph @

tests/test_dynamic_complete_style.py

raw symbols SCIP
1 import pytest
2 from prompt_toolkit.shortcuts import CompleteStyle, PromptSession
3
4 import cmd2
5 from cmd2 import Completions
6
7
8 class AutoStyleApp(cmd2.Cmd):
9 def __init__(self):
10 super().__init__()
11
12 def do_foo(self, args):
13 pass
14
15 def complete_foo(self, text, line, begidx, endidx) -> Completions:
16 # Return 10 items
17 items = [f"item{i}" for i in range(10) if f"item{i}".startswith(text)]
18 return Completions.from_values(items)
19
20 def do_bar(self, args):
21 pass
22
23 def complete_bar(self, text, line, begidx, endidx) -> Completions:
24 # Return 5 items
25 items = [f"item{i}" for i in range(5) if f"item{i}".startswith(text)]
26 return Completions.from_values(items)
27
28
29 @pytest.fixture
30 def app():
31 return AutoStyleApp()
32
33
34 def test_dynamic_complete_style(app):
35 # Cmd.complete() interacts with app.active_session.
36 # Set it here since it's normally set when the prompt is created.
37 app.active_session: PromptSession[str] = PromptSession(
38 input=app.main_session.input,
39 output=app.main_session.output,
40 )
41
42 # Default max_column_completion_results is 7
43 assert app.max_column_completion_results == 7
44
45 # Complete 'foo' which has 10 items (> 7)
46 # text='item', state=0, line='foo item', begidx=4, endidx=8
47 app.complete("item", "foo item", 4, 8)
48 assert app.active_session.complete_style == CompleteStyle.MULTI_COLUMN
49
50 # Complete 'bar' which has 5 items (<= 7)
51 app.complete("item", "bar item", 4, 8)
52 assert app.active_session.complete_style == CompleteStyle.COLUMN
53
54
55 def test_dynamic_complete_style_custom_limit(app):
56 # Cmd.complete() interacts with app.active_session.
57 # Set it here since it's normally set when the prompt is created.
58 app.active_session: PromptSession[str] = PromptSession(
59 input=app.main_session.input,
60 output=app.main_session.output,
61 )
62
63 # Change limit to 3
64 app.max_column_completion_results = 3
65
66 # Complete 'bar' which has 5 items (> 3)
67 app.complete("item", "bar item", 4, 8)
68 assert app.active_session.complete_style == CompleteStyle.MULTI_COLUMN
69
70 # Change limit to 15
71 app.max_column_completion_results = 15
72
73 # Complete 'foo' which has 10 items (<= 15)
74 app.complete("item", "foo item", 4, 8)
75 assert app.active_session.complete_style == CompleteStyle.COLUMN