2016-01-11 17:50:37 -05:00
|
|
|
"""Tests sample inputs to PTK multiline and checks parser response"""
|
2024-01-30 12:23:50 +01:00
|
|
|
|
2016-10-16 03:03:23 +03:00
|
|
|
from collections import namedtuple
|
2016-01-11 17:09:20 -05:00
|
|
|
from unittest.mock import MagicMock, patch
|
2016-01-11 16:38:25 -05:00
|
|
|
|
2016-10-16 03:03:23 +03:00
|
|
|
import pytest
|
2018-07-14 19:00:22 -04:00
|
|
|
from prompt_toolkit.application import Application
|
|
|
|
from prompt_toolkit.buffer import Buffer
|
2022-01-31 21:26:34 +05:30
|
|
|
from prompt_toolkit.document import Document
|
2016-10-16 03:03:23 +03:00
|
|
|
|
2016-01-22 18:42:53 -05:00
|
|
|
from xonsh.tools import ON_WINDOWS
|
2020-08-26 10:10:59 -05:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
Context = namedtuple("Context", ["indent", "buffer", "accept", "cli", "cr"])
|
2016-01-12 09:46:50 -05:00
|
|
|
|
2016-01-11 16:38:25 -05:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
@pytest.fixture
|
|
|
|
def ctx(xession):
|
2016-10-16 03:03:23 +03:00
|
|
|
"""Context in which the ptk multiline functionality will be tested."""
|
2021-05-20 15:44:26 +05:30
|
|
|
xession.env["INDENT"] = " "
|
2020-04-18 10:44:27 -04:00
|
|
|
from xonsh.ptk_shell.key_bindings import carriage_return
|
2018-08-30 09:18:49 -05:00
|
|
|
|
2016-10-16 16:44:55 +03:00
|
|
|
ptk_buffer = Buffer()
|
2018-08-30 09:18:49 -05:00
|
|
|
ptk_buffer.accept_action = MagicMock(name="accept")
|
|
|
|
cli = MagicMock(name="cli", spec=Application)
|
|
|
|
yield Context(
|
|
|
|
indent=" ",
|
|
|
|
buffer=ptk_buffer,
|
|
|
|
accept=ptk_buffer.accept_action,
|
|
|
|
cli=cli,
|
|
|
|
cr=carriage_return,
|
|
|
|
)
|
2016-10-16 03:03:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_colon_indent(ctx):
|
2018-08-30 09:18:49 -05:00
|
|
|
document = Document("for i in range(5):")
|
2016-10-16 03:03:23 +03:00
|
|
|
ctx.buffer.set_document(document)
|
|
|
|
ctx.cr(ctx.buffer, ctx.cli)
|
2016-10-16 14:06:20 +03:00
|
|
|
assert ctx.buffer.document.current_line == ctx.indent
|
|
|
|
|
2016-10-16 03:03:23 +03:00
|
|
|
|
|
|
|
def test_dedent(ctx):
|
2018-08-30 09:18:49 -05:00
|
|
|
document = Document("\n" + ctx.indent + "pass")
|
2016-10-16 03:03:23 +03:00
|
|
|
ctx.buffer.set_document(document)
|
|
|
|
ctx.cr(ctx.buffer, ctx.cli)
|
2018-08-30 09:18:49 -05:00
|
|
|
assert ctx.buffer.document.current_line == ""
|
2016-10-16 03:03:23 +03:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
document = Document("\n" + 2 * ctx.indent + "continue")
|
2016-10-16 03:03:23 +03:00
|
|
|
ctx.buffer.set_document(document)
|
|
|
|
ctx.cr(ctx.buffer, ctx.cli)
|
|
|
|
assert ctx.buffer.document.current_line == ctx.indent
|
|
|
|
|
2016-10-16 14:06:20 +03:00
|
|
|
|
2016-10-16 03:03:23 +03:00
|
|
|
def test_nodedent(ctx):
|
2018-08-30 09:18:49 -05:00
|
|
|
"""don't dedent if first line of ctx.buffer"""
|
2016-10-16 14:06:20 +03:00
|
|
|
mock = MagicMock(return_value=True)
|
2020-04-18 10:44:27 -04:00
|
|
|
with patch("xonsh.ptk_shell.key_bindings.can_compile", mock):
|
2018-08-30 09:18:49 -05:00
|
|
|
document = Document("pass")
|
2016-10-16 03:03:23 +03:00
|
|
|
ctx.buffer.set_document(document)
|
|
|
|
ctx.cr(ctx.buffer, ctx.cli)
|
|
|
|
assert ctx.accept.mock_calls is not None
|
2016-03-06 12:16:24 -05:00
|
|
|
|
2016-10-16 14:06:20 +03:00
|
|
|
mock = MagicMock(return_value=True)
|
2020-04-18 10:44:27 -04:00
|
|
|
with patch("xonsh.ptk_shell.key_bindings.can_compile", mock):
|
2018-08-30 09:18:49 -05:00
|
|
|
document = Document(ctx.indent + "pass")
|
2016-10-16 03:03:23 +03:00
|
|
|
ctx.buffer.set_document(document)
|
|
|
|
ctx.cr(ctx.buffer, ctx.cli)
|
|
|
|
assert ctx.accept.mock_calls is not None
|
2016-03-06 12:16:24 -05:00
|
|
|
|
2016-10-16 14:06:20 +03:00
|
|
|
|
2016-10-16 03:03:23 +03:00
|
|
|
def test_continuation_line(ctx):
|
2018-08-30 09:18:49 -05:00
|
|
|
document = Document("\nsecond line")
|
2016-10-16 03:03:23 +03:00
|
|
|
ctx.buffer.set_document(document)
|
|
|
|
ctx.cr(ctx.buffer, ctx.cli)
|
2018-08-30 09:18:49 -05:00
|
|
|
assert ctx.buffer.document.current_line == ""
|
2016-10-16 14:06:20 +03:00
|
|
|
|
2016-01-11 17:09:20 -05:00
|
|
|
|
2016-10-16 03:03:23 +03:00
|
|
|
def test_trailing_slash(ctx):
|
2016-10-16 14:06:20 +03:00
|
|
|
mock = MagicMock(return_value=True)
|
2020-04-18 10:44:27 -04:00
|
|
|
with patch("xonsh.ptk_shell.key_bindings.can_compile", mock):
|
2018-08-30 09:18:49 -05:00
|
|
|
document = Document("this line will \\")
|
2016-10-16 03:03:23 +03:00
|
|
|
ctx.buffer.set_document(document)
|
|
|
|
ctx.cr(ctx.buffer, ctx.cli)
|
2016-01-22 18:42:53 -05:00
|
|
|
if not ON_WINDOWS:
|
2018-08-30 09:18:49 -05:00
|
|
|
assert ctx.buffer.document.current_line == ""
|
2016-01-22 18:42:53 -05:00
|
|
|
else:
|
2016-10-16 03:03:23 +03:00
|
|
|
assert ctx.accept.mock_calls is not None
|
2016-01-11 17:09:20 -05:00
|
|
|
|
2016-10-16 14:06:20 +03:00
|
|
|
|
2016-10-16 03:03:23 +03:00
|
|
|
def test_cant_compile_newline(ctx):
|
2016-10-16 14:06:20 +03:00
|
|
|
mock = MagicMock(return_value=False)
|
2020-04-18 10:44:27 -04:00
|
|
|
with patch("xonsh.ptk_shell.key_bindings.can_compile", mock):
|
2018-08-30 09:18:49 -05:00
|
|
|
document = Document("for i in (1, 2, ")
|
2016-10-16 03:03:23 +03:00
|
|
|
ctx.buffer.set_document(document)
|
|
|
|
ctx.cr(ctx.buffer, ctx.cli)
|
2018-08-30 09:18:49 -05:00
|
|
|
assert ctx.buffer.document.current_line == ""
|
2016-10-16 14:06:20 +03:00
|
|
|
|
2016-01-11 16:38:25 -05:00
|
|
|
|
2016-10-16 03:03:23 +03:00
|
|
|
def test_can_compile_and_executes(ctx):
|
2016-10-16 14:06:20 +03:00
|
|
|
mock = MagicMock(return_value=True)
|
2020-04-18 10:44:27 -04:00
|
|
|
with patch("xonsh.ptk_shell.key_bindings.can_compile", mock):
|
2018-08-30 09:18:49 -05:00
|
|
|
document = Document("ls")
|
2016-10-16 03:03:23 +03:00
|
|
|
ctx.buffer.set_document(document)
|
|
|
|
ctx.cr(ctx.buffer, ctx.cli)
|
|
|
|
assert ctx.accept.mock_calls is not None
|