|
17 | 17 | from prompt_toolkit.completion import DummyCompleter |
18 | 18 | from prompt_toolkit.input import DummyInput, create_pipe_input |
19 | 19 | from prompt_toolkit.output import DummyOutput |
20 | | -from prompt_toolkit.shortcuts import PromptSession |
| 20 | +from prompt_toolkit.shortcuts import CompleteStyle, PromptSession |
21 | 21 | from rich.style import Style |
22 | 22 | from rich.text import Text |
23 | 23 |
|
@@ -1544,6 +1544,13 @@ def test_help_verbose_with_fake_command(capsys) -> None: |
1544 | 1544 | assert cmds[1] not in out |
1545 | 1545 |
|
1546 | 1546 |
|
| 1547 | +def test_ipy_help(base_app: cmd2.Cmd) -> None: |
| 1548 | + """Verify that help for ipy builds its parser and displays correctly.""" |
| 1549 | + out, err = run_cmd(base_app, "help ipy") |
| 1550 | + assert "Run an interactive IPython shell." in out |
| 1551 | + assert not err |
| 1552 | + |
| 1553 | + |
1547 | 1554 | def test_render_columns_no_strs(help_app: HelpApp) -> None: |
1548 | 1555 | no_strs = [] |
1549 | 1556 | result = help_app.render_columns(no_strs) |
@@ -4297,6 +4304,20 @@ class SynonymApp(cmd2.cmd2.Cmd): |
4297 | 4304 | assert synonym_parser is help_parser |
4298 | 4305 |
|
4299 | 4306 |
|
| 4307 | +def test_command_parsers_contains() -> None: |
| 4308 | + class SampleApp(cmd2.Cmd): |
| 4309 | + def do_non_argparse(self, args: str) -> None: |
| 4310 | + """Plain command without an argparse decorator.""" |
| 4311 | + |
| 4312 | + app = SampleApp() |
| 4313 | + |
| 4314 | + # Argparse-based command returns True |
| 4315 | + assert app.do_help in app.command_parsers |
| 4316 | + |
| 4317 | + # Non-argparse command returns False |
| 4318 | + assert app.do_non_argparse not in app.command_parsers |
| 4319 | + |
| 4320 | + |
4300 | 4321 | def test_custom_completekey_ctrl_k(): |
4301 | 4322 | from prompt_toolkit.keys import Keys |
4302 | 4323 |
|
@@ -4378,6 +4399,13 @@ def test_path_complete_users_windows(monkeypatch, base_app): |
4378 | 4399 | assert expected in matches |
4379 | 4400 |
|
4380 | 4401 |
|
| 4402 | +def test_main_session_defaults(base_app: cmd2.Cmd) -> None: |
| 4403 | + """Verify default configuration of the main PromptSession.""" |
| 4404 | + assert base_app.main_session.complete_style == CompleteStyle.MULTI_COLUMN |
| 4405 | + assert base_app.main_session.complete_while_typing is False |
| 4406 | + assert base_app.main_session.enable_suspend is True |
| 4407 | + |
| 4408 | + |
4381 | 4409 | def test_refresh_interval() -> None: |
4382 | 4410 | # Test default value |
4383 | 4411 | default_app = cmd2.Cmd() |
@@ -4752,3 +4780,65 @@ def do_base(self, _: argparse.Namespace) -> None: |
4752 | 4780 | root_parser = cast(cmd2.Cmd2ArgumentParser, app.command_parsers.get(app.do_base)) |
4753 | 4781 | subparsers_action = root_parser.get_subparsers_action() |
4754 | 4782 | assert not subparsers_action._name_parser_map |
| 4783 | + |
| 4784 | + |
| 4785 | +@pytest.mark.skipif( |
| 4786 | + sys.platform.startswith("win"), |
| 4787 | + reason="Don't have a real Windows console with how we are currently running tests in GitHub Actions", |
| 4788 | +) |
| 4789 | +def test_bracketed_paste_single_line(base_app) -> None: |
| 4790 | + """Test pasting single line text without newlines.""" |
| 4791 | + with create_pipe_input() as pipe_input: |
| 4792 | + base_app.main_session = PromptSession( |
| 4793 | + input=pipe_input, |
| 4794 | + output=DummyOutput(), |
| 4795 | + key_bindings=base_app.main_session.key_bindings, |
| 4796 | + multiline=base_app.main_session.multiline, |
| 4797 | + ) |
| 4798 | + |
| 4799 | + pipe_input.send_text("\x1b[200~help\x1b[201~\n") |
| 4800 | + line = base_app._read_command_line("prompt> ") |
| 4801 | + assert line == "help" |
| 4802 | + |
| 4803 | + |
| 4804 | +@pytest.mark.skipif( |
| 4805 | + sys.platform.startswith("win"), |
| 4806 | + reason="Don't have a real Windows console with how we are currently running tests in GitHub Actions", |
| 4807 | +) |
| 4808 | +def test_bracketed_paste_multiple_commands(base_app) -> None: |
| 4809 | + """Test pasting multiple lines with newlines.""" |
| 4810 | + with create_pipe_input() as pipe_input: |
| 4811 | + base_app.main_session = PromptSession( |
| 4812 | + input=pipe_input, |
| 4813 | + output=DummyOutput(), |
| 4814 | + key_bindings=base_app.main_session.key_bindings, |
| 4815 | + multiline=base_app.main_session.multiline, |
| 4816 | + ) |
| 4817 | + |
| 4818 | + pipe_input.send_text("\x1b[200~help\nhistory\n\x1b[201~") |
| 4819 | + line1 = base_app._read_command_line("prompt> ") |
| 4820 | + assert line1 == "help" |
| 4821 | + line2 = base_app._read_command_line("prompt> ") |
| 4822 | + assert line2 == "history" |
| 4823 | + |
| 4824 | + |
| 4825 | +@pytest.mark.skipif( |
| 4826 | + sys.platform.startswith("win"), |
| 4827 | + reason="Don't have a real Windows console with how we are currently running tests in GitHub Actions", |
| 4828 | +) |
| 4829 | +def test_bracketed_paste_multiline_command(multiline_app) -> None: |
| 4830 | + """Test pasting multiline command awaiting terminator.""" |
| 4831 | + with create_pipe_input() as pipe_input: |
| 4832 | + multiline_app.main_session = PromptSession( |
| 4833 | + input=pipe_input, |
| 4834 | + output=DummyOutput(), |
| 4835 | + key_bindings=multiline_app.main_session.key_bindings, |
| 4836 | + multiline=multiline_app.main_session.multiline, |
| 4837 | + prompt_continuation=multiline_app.main_session.prompt_continuation, |
| 4838 | + ) |
| 4839 | + |
| 4840 | + pipe_input.send_text("\x1b[200~orate line 1\nline 2;\nhelp\n\x1b[201~") |
| 4841 | + line1 = multiline_app._read_command_line("prompt> ") |
| 4842 | + assert line1 == "orate line 1\nline 2;" |
| 4843 | + line2 = multiline_app._read_command_line("prompt> ") |
| 4844 | + assert line2 == "help" |
0 commit comments