mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +01:00
heal test_ptk_multiline p1
2 failed 5 passed
This commit is contained in:
parent
9fbbe54ce2
commit
bcf32b7a23
1 changed files with 55 additions and 68 deletions
|
@ -1,113 +1,100 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""Tests sample inputs to PTK multiline and checks parser response"""
|
||||
import pytest
|
||||
import builtins
|
||||
from collections import namedtuple
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from prompt_toolkit.interface import CommandLineInterface
|
||||
from prompt_toolkit.document import Document
|
||||
from prompt_toolkit.buffer import Buffer, AcceptAction
|
||||
|
||||
from xonsh.environ import Env
|
||||
from xonsh.tools import ON_WINDOWS
|
||||
|
||||
def setup_module():
|
||||
global indent_
|
||||
global buffer
|
||||
global bufaccept
|
||||
global cli
|
||||
global carriage_return
|
||||
global builtins
|
||||
|
||||
import builtins
|
||||
Context = namedtuple('Context', ['indent', 'buffer', 'accept', 'cli', 'cr'])
|
||||
|
||||
builtins.__xonsh_env__ = Env()
|
||||
builtins.__xonsh_env__['INDENT'] = ' '
|
||||
|
||||
@pytest.fixture
|
||||
def ctx(xonsh_builtins):
|
||||
"""Context in which the ptk multiline functionality will be tested."""
|
||||
from xonsh.ptk.key_bindings import carriage_return
|
||||
|
||||
indent_ = ' '
|
||||
xonsh_builtins.__xonsh_env__ = Env()
|
||||
xonsh_builtins.__xonsh_env__['INDENT'] = ' '
|
||||
buffer = Buffer()
|
||||
bufaccept = MagicMock(name='accept', spec=AcceptAction)
|
||||
buffer.accept_action = MagicMock(name='accept', spec=AcceptAction)
|
||||
cli = MagicMock(name='cli', spec=CommandLineInterface)
|
||||
buffer.accept_action = bufaccept
|
||||
return Context(indent=' ',
|
||||
buffer=buffer,
|
||||
accept=buffer.accept_action,
|
||||
cli=cli,
|
||||
cr=carriage_return)
|
||||
|
||||
def teardown_module():
|
||||
global indent_
|
||||
global buffer
|
||||
global bufaccept
|
||||
global cli
|
||||
global carriage_return
|
||||
global builtins
|
||||
|
||||
del indent_
|
||||
del buffer
|
||||
del bufaccept
|
||||
del cli
|
||||
del carriage_return
|
||||
del builtins
|
||||
|
||||
def test_colon_indent():
|
||||
def test_colon_indent(ctx):
|
||||
document = Document('for i in range(5):')
|
||||
buffer.set_document(document)
|
||||
carriage_return(buffer, cli)
|
||||
assert buffer.document.current_line == indent_
|
||||
ctx.buffer.set_document(document)
|
||||
ctx.cr(ctx.buffer, ctx.cli)
|
||||
assert ctx.buffer.document.current_line == ctx.indent
|
||||
|
||||
def test_dedent():
|
||||
document = Document('\n'+indent_+'pass')
|
||||
buffer.set_document(document)
|
||||
carriage_return(buffer, cli)
|
||||
assert buffer.document.current_line == ''
|
||||
def test_dedent(ctx):
|
||||
document = Document('\n'+ctx.indent+'pass')
|
||||
ctx.buffer.set_document(document)
|
||||
ctx.cr(ctx.buffer, ctx.cli)
|
||||
assert ctx.buffer.document.current_line == ''
|
||||
|
||||
document = Document('\n'+2*indent_+'continue')
|
||||
buffer.set_document(document)
|
||||
carriage_return(buffer, cli)
|
||||
assert buffer.document.current_line == indent_
|
||||
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
|
||||
|
||||
def test_nodedent():
|
||||
'''don't dedent if first line of buffer'''
|
||||
def test_nodedent(ctx):
|
||||
'''don't dedent if first line of ctx.buffer'''
|
||||
mock = MagicMock(return_value = True)
|
||||
with patch('xonsh.ptk.key_bindings.can_compile', mock):
|
||||
document = Document('pass')
|
||||
buffer.set_document(document)
|
||||
carriage_return(buffer, cli)
|
||||
assert bufaccept.mock_calls is not None
|
||||
ctx.buffer.set_document(document)
|
||||
ctx.cr(ctx.buffer, ctx.cli)
|
||||
assert ctx.accept.mock_calls is not None
|
||||
|
||||
|
||||
mock = MagicMock(return_value = True)
|
||||
with patch('xonsh.ptk.key_bindings.can_compile', mock):
|
||||
document = Document(indent_+'pass')
|
||||
buffer.set_document(document)
|
||||
carriage_return(buffer, cli)
|
||||
assert bufaccept.mock_calls is not None
|
||||
document = Document(ctx.indent+'pass')
|
||||
ctx.buffer.set_document(document)
|
||||
ctx.cr(ctx.buffer, ctx.cli)
|
||||
assert ctx.accept.mock_calls is not None
|
||||
|
||||
def test_continuation_line():
|
||||
def test_continuation_line(ctx):
|
||||
document = Document('\nsecond line')
|
||||
buffer.set_document(document)
|
||||
carriage_return(buffer, cli)
|
||||
assert buffer.document.current_line == ''
|
||||
ctx.buffer.set_document(document)
|
||||
ctx.cr(ctx.buffer, ctx.cli)
|
||||
assert ctx.buffer.document.current_line == ''
|
||||
|
||||
def test_trailing_slash():
|
||||
def test_trailing_slash(ctx):
|
||||
mock = MagicMock(return_value = True)
|
||||
with patch('xonsh.ptk.key_bindings.can_compile', mock):
|
||||
document = Document('this line will \\')
|
||||
buffer.set_document(document)
|
||||
carriage_return(buffer, cli)
|
||||
ctx.buffer.set_document(document)
|
||||
ctx.cr(ctx.buffer, ctx.cli)
|
||||
if not ON_WINDOWS:
|
||||
assert buffer.document.current_line == ''
|
||||
assert ctx.buffer.document.current_line == ''
|
||||
else:
|
||||
assert bufaccept.mock_calls is not None
|
||||
assert ctx.accept.mock_calls is not None
|
||||
|
||||
def test_cant_compile_newline():
|
||||
def test_cant_compile_newline(ctx):
|
||||
mock = MagicMock(return_value = False)
|
||||
with patch('xonsh.ptk.key_bindings.can_compile', mock):
|
||||
document = Document('for i in (1, 2, ')
|
||||
buffer.set_document(document)
|
||||
carriage_return(buffer, cli)
|
||||
assert buffer.document.current_line == ''
|
||||
ctx.buffer.set_document(document)
|
||||
ctx.cr(ctx.buffer, ctx.cli)
|
||||
assert ctx.buffer.document.current_line == ''
|
||||
|
||||
def test_can_compile_and_executes():
|
||||
def test_can_compile_and_executes(ctx):
|
||||
mock = MagicMock(return_value = True)
|
||||
with patch('xonsh.ptk.key_bindings.can_compile', mock):
|
||||
document = Document('ls')
|
||||
buffer.set_document(document)
|
||||
carriage_return(buffer, cli)
|
||||
assert bufaccept.mock_calls is not None
|
||||
ctx.buffer.set_document(document)
|
||||
ctx.cr(ctx.buffer, ctx.cli)
|
||||
assert ctx.accept.mock_calls is not None
|
||||
|
|
Loading…
Add table
Reference in a new issue