| 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 |
|
| 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 |
|
| 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 |
|
| 36 |
|
| 37 |
app.active_session: PromptSession[str] = PromptSession( |
| 38 |
input=app.main_session.input, |
| 39 |
output=app.main_session.output, |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
assert app.max_column_completion_results == 7 |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
app.complete("item", "foo item", 4, 8) |
| 48 |
assert app.active_session.complete_style == CompleteStyle.MULTI_COLUMN |
| 49 |
|
| 50 |
|
| 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 |
|
| 57 |
|
| 58 |
app.active_session: PromptSession[str] = PromptSession( |
| 59 |
input=app.main_session.input, |
| 60 |
output=app.main_session.output, |
| 61 |
) |
| 62 |
|
| 63 |
|
| 64 |
app.max_column_completion_results = 3 |
| 65 |
|
| 66 |
|
| 67 |
app.complete("item", "bar item", 4, 8) |
| 68 |
assert app.active_session.complete_style == CompleteStyle.MULTI_COLUMN |
| 69 |
|
| 70 |
|
| 71 |
app.max_column_completion_results = 15 |
| 72 |
|
| 73 |
|
| 74 |
app.complete("item", "foo item", 4, 8) |
| 75 |
assert app.active_session.complete_style == CompleteStyle.COLUMN |