- Refactored `FalyxCompleter` to support longest common prefix (LCP) completions by default. - Added `_ensure_quote` helper to auto-quote completions containing spaces/tabs. - Integrated `_yield_lcp_completions` for consistent completion insertion logic. - Added `_suggest_paths()` helper to dynamically suggest filesystem paths for arguments of type `Path`. - Integrated path completion into `suggest_next()` for both positional and flagged arguments. - Updated `argument_examples.py` to include a `--path` argument (`Path | None`), demonstrating file path completion. - Enabled `CompleteStyle.COLUMN` for tab-completion menu formatting in interactive sessions. - Improved bottom bar docstring formatting with fenced code block examples. - Added safeguard to `word_validator` to reject `"N"` since it’s reserved for `yes_no_validator`. - Improved help panel rendering for commands (using `Padding` + `Panel`). - Added full test coverage for: - `FalyxCompleter` and LCP behavior (`tests/test_completer/`) - All validators (`tests/test_validators/`) - Bumped version to 0.1.80.
98 lines
3.3 KiB
Python
98 lines
3.3 KiB
Python
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
from prompt_toolkit.completion import Completion
|
|
from prompt_toolkit.document import Document
|
|
|
|
from falyx.completer import FalyxCompleter
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_falyx():
|
|
fake_arg_parser = SimpleNamespace(
|
|
suggest_next=lambda tokens, end: ["--tag", "--name", "value with space"]
|
|
)
|
|
fake_command = SimpleNamespace(key="R", aliases=["RUN"], arg_parser=fake_arg_parser)
|
|
return SimpleNamespace(
|
|
exit_command=SimpleNamespace(key="X", aliases=["EXIT"]),
|
|
help_command=SimpleNamespace(key="H", aliases=["HELP"]),
|
|
history_command=SimpleNamespace(key="Y", aliases=["HISTORY"]),
|
|
commands={"R": fake_command},
|
|
_name_map={"R": fake_command, "RUN": fake_command, "X": fake_command},
|
|
)
|
|
|
|
|
|
def test_suggest_commands(fake_falyx):
|
|
completer = FalyxCompleter(fake_falyx)
|
|
completions = list(completer._suggest_commands("R"))
|
|
assert any(c.text == "R" for c in completions)
|
|
assert any(c.text == "RUN" for c in completions)
|
|
|
|
|
|
def test_suggest_commands_empty(fake_falyx):
|
|
completer = FalyxCompleter(fake_falyx)
|
|
completions = list(completer._suggest_commands(""))
|
|
assert any(c.text == "X" for c in completions)
|
|
assert any(c.text == "H" for c in completions)
|
|
|
|
|
|
def test_suggest_commands_no_match(fake_falyx):
|
|
completer = FalyxCompleter(fake_falyx)
|
|
completions = list(completer._suggest_commands("Z"))
|
|
assert not completions
|
|
|
|
|
|
def test_get_completions_no_input(fake_falyx):
|
|
completer = FalyxCompleter(fake_falyx)
|
|
doc = Document("")
|
|
results = list(completer.get_completions(doc, None))
|
|
assert any(isinstance(c, Completion) for c in results)
|
|
assert any(c.text == "X" for c in results)
|
|
|
|
|
|
def test_get_completions_no_match(fake_falyx):
|
|
completer = FalyxCompleter(fake_falyx)
|
|
doc = Document("Z")
|
|
completions = list(completer.get_completions(doc, None))
|
|
assert not completions
|
|
doc = Document("Z Z")
|
|
completions = list(completer.get_completions(doc, None))
|
|
assert not completions
|
|
|
|
|
|
def test_get_completions_partial_command(fake_falyx):
|
|
completer = FalyxCompleter(fake_falyx)
|
|
doc = Document("R")
|
|
results = list(completer.get_completions(doc, None))
|
|
assert any(c.text in ("R", "RUN") for c in results)
|
|
|
|
|
|
def test_get_completions_with_flag(fake_falyx):
|
|
completer = FalyxCompleter(fake_falyx)
|
|
doc = Document("R ")
|
|
results = list(completer.get_completions(doc, None))
|
|
assert "--tag" in [c.text for c in results]
|
|
|
|
|
|
def test_get_completions_partial_flag(fake_falyx):
|
|
completer = FalyxCompleter(fake_falyx)
|
|
doc = Document("R --t")
|
|
results = list(completer.get_completions(doc, None))
|
|
assert all(c.start_position <= 0 for c in results)
|
|
assert any(c.text.startswith("--t") or c.display == "--tag" for c in results)
|
|
|
|
|
|
def test_get_completions_bad_input(fake_falyx):
|
|
completer = FalyxCompleter(fake_falyx)
|
|
doc = Document('R "unclosed quote')
|
|
results = list(completer.get_completions(doc, None))
|
|
assert results == []
|
|
|
|
|
|
def test_get_completions_exception_handling(fake_falyx):
|
|
completer = FalyxCompleter(fake_falyx)
|
|
fake_falyx.commands["R"].arg_parser.suggest_next = lambda *args: 1 / 0
|
|
doc = Document("R --tag")
|
|
results = list(completer.get_completions(doc, None))
|
|
assert results == []
|