xonsh/tests/test_ptk_multiline.py

113 lines
3.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2016-01-11 17:50:37 -05:00
"""Tests sample inputs to PTK multiline and checks parser response"""
import builtins
from collections import namedtuple
from unittest.mock import MagicMock, patch
import pytest
2018-07-14 19:00:22 -04:00
from prompt_toolkit.application import Application
from prompt_toolkit.document import Document
2018-07-14 19:00:22 -04:00
from prompt_toolkit.buffer import Buffer
from xonsh.tools import ON_WINDOWS
2018-09-13 16:49:09 -04:00
from xonsh.built_ins import XonshSession
from tools import DummyEnv
2020-08-26 10:10:59 -05:00
2018-08-30 09:18:49 -05:00
Context = namedtuple("Context", ["indent", "buffer", "accept", "cli", "cr"])
2018-11-19 20:22:18 -05:00
@pytest.fixture(scope="module")
2016-10-16 14:06:20 +03:00
def ctx():
"""Context in which the ptk multiline functionality will be tested."""
2018-09-13 16:49:09 -04:00
builtins.__xonsh__ = XonshSession()
2018-09-13 14:03:35 -04:00
builtins.__xonsh__.env = DummyEnv()
builtins.__xonsh__.env["INDENT"] = " "
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,
)
2018-09-13 14:03:35 -04:00
del builtins.__xonsh__.env
2018-09-13 16:49:09 -04:00
del builtins.__xonsh__
def test_colon_indent(ctx):
2018-08-30 09:18:49 -05:00
document = Document("for i in range(5):")
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
def test_dedent(ctx):
2018-08-30 09:18:49 -05:00
document = Document("\n" + ctx.indent + "pass")
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 == ""
2018-08-30 09:18:49 -05:00
document = Document("\n" + 2 * ctx.indent + "continue")
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
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)
with patch("xonsh.ptk_shell.key_bindings.can_compile", mock):
2018-08-30 09:18:49 -05:00
document = Document("pass")
ctx.buffer.set_document(document)
ctx.cr(ctx.buffer, ctx.cli)
assert ctx.accept.mock_calls is not None
2016-10-16 14:06:20 +03:00
mock = MagicMock(return_value=True)
with patch("xonsh.ptk_shell.key_bindings.can_compile", mock):
2018-08-30 09:18:49 -05:00
document = Document(ctx.indent + "pass")
ctx.buffer.set_document(document)
ctx.cr(ctx.buffer, ctx.cli)
assert ctx.accept.mock_calls is not None
2016-10-16 14:06:20 +03:00
def test_continuation_line(ctx):
2018-08-30 09:18:49 -05:00
document = Document("\nsecond line")
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
def test_trailing_slash(ctx):
2016-10-16 14:06:20 +03:00
mock = MagicMock(return_value=True)
with patch("xonsh.ptk_shell.key_bindings.can_compile", mock):
2018-08-30 09:18:49 -05:00
document = Document("this line will \\")
ctx.buffer.set_document(document)
ctx.cr(ctx.buffer, ctx.cli)
if not ON_WINDOWS:
2018-08-30 09:18:49 -05:00
assert ctx.buffer.document.current_line == ""
else:
assert ctx.accept.mock_calls is not None
2016-10-16 14:06:20 +03:00
def test_cant_compile_newline(ctx):
2016-10-16 14:06:20 +03:00
mock = MagicMock(return_value=False)
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, ")
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
def test_can_compile_and_executes(ctx):
2016-10-16 14:06:20 +03:00
mock = MagicMock(return_value=True)
with patch("xonsh.ptk_shell.key_bindings.can_compile", mock):
2018-08-30 09:18:49 -05:00
document = Document("ls")
ctx.buffer.set_document(document)
ctx.cr(ctx.buffer, ctx.cli)
assert ctx.accept.mock_calls is not None