Fix CommandParser and don't use a generator

This commit is contained in:
Florian Bruhin 2017-07-02 17:18:58 +02:00
parent a8c7e8ba05
commit 978013e750
3 changed files with 17 additions and 15 deletions

View File

@ -118,7 +118,7 @@ class CommandParser:
new_cmd += ' ' new_cmd += ' '
return new_cmd return new_cmd
def parse_all(self, text, aliases=True, *args, **kwargs): def _parse_all_gen(self, text, aliases=True, *args, **kwargs):
"""Split a command on ;; and parse all parts. """Split a command on ;; and parse all parts.
If the first command in the commandline is a non-split one, it only If the first command in the commandline is a non-split one, it only
@ -153,6 +153,10 @@ class CommandParser:
for sub in sub_texts: for sub in sub_texts:
yield self.parse(sub, *args, **kwargs) yield self.parse(sub, *args, **kwargs)
def parse_all(self, *args, **kwargs):
"""Wrapper over parse_all."""
return list(self._parse_all_gen(*args, **kwargs))
def parse(self, text, *, fallback=False, keep=False): def parse(self, text, *, fallback=False, keep=False):
"""Split the commandline text into command and arguments. """Split the commandline text into command and arguments.

View File

@ -747,7 +747,7 @@ class Command(BaseType):
from qutebrowser.commands import runners, cmdexc from qutebrowser.commands import runners, cmdexc
parser = runners.CommandParser() parser = runners.CommandParser()
try: try:
list(parser.parse_all(value)) parser.parse_all(value)
except cmdexc.Error as e: except cmdexc.Error as e:
raise configexc.ValidationError(value, str(e)) raise configexc.ValidationError(value, str(e))
finally: finally:

View File

@ -24,9 +24,7 @@ import pytest
from qutebrowser.commands import runners, cmdexc from qutebrowser.commands import runners, cmdexc
class TestCommandRunner: class TestCommandParser:
"""Tests for CommandRunner."""
def test_parse_all(self, cmdline_test): def test_parse_all(self, cmdline_test):
"""Test parsing of commands. """Test parsing of commands.
@ -36,22 +34,22 @@ class TestCommandRunner:
Args: Args:
cmdline_test: A pytest fixture which provides testcases. cmdline_test: A pytest fixture which provides testcases.
""" """
cr = runners.CommandRunner(0) parser = runners.CommandParser()
if cmdline_test.valid: if cmdline_test.valid:
list(cr.parse_all(cmdline_test.cmd, aliases=False)) parser.parse_all(cmdline_test.cmd, aliases=False)
else: else:
with pytest.raises(cmdexc.NoSuchCommandError): with pytest.raises(cmdexc.NoSuchCommandError):
list(cr.parse_all(cmdline_test.cmd, aliases=False)) parser.parse_all(cmdline_test.cmd, aliases=False)
def test_parse_all_with_alias(self, cmdline_test, config_stub): def test_parse_all_with_alias(self, cmdline_test, config_stub):
config_stub.data = {'aliases': {'alias_name': cmdline_test.cmd}} config_stub.data = {'aliases': {'alias_name': cmdline_test.cmd}}
cr = runners.CommandRunner(0) parser = runners.CommandParser()
if cmdline_test.valid: if cmdline_test.valid:
assert len(list(cr.parse_all("alias_name"))) > 0 assert len(parser.parse_all("alias_name")) > 0
else: else:
with pytest.raises(cmdexc.NoSuchCommandError): with pytest.raises(cmdexc.NoSuchCommandError):
list(cr.parse_all("alias_name")) parser.parse_all("alias_name")
@pytest.mark.parametrize('command', ['', ' ']) @pytest.mark.parametrize('command', ['', ' '])
def test_parse_empty_with_alias(self, command): def test_parse_empty_with_alias(self, command):
@ -60,15 +58,15 @@ class TestCommandRunner:
See https://github.com/qutebrowser/qutebrowser/issues/1690 See https://github.com/qutebrowser/qutebrowser/issues/1690
and https://github.com/qutebrowser/qutebrowser/issues/1773 and https://github.com/qutebrowser/qutebrowser/issues/1773
""" """
cr = runners.CommandRunner(0) parser = runners.CommandParser()
with pytest.raises(cmdexc.NoSuchCommandError): with pytest.raises(cmdexc.NoSuchCommandError):
list(cr.parse_all(command)) parser.parse_all(command)
def test_partial_parsing(self): def test_partial_parsing(self):
"""Test partial parsing with a runner where it's enabled. """Test partial parsing with a runner where it's enabled.
The same with it being disabled is tested by test_parse_all. The same with it being disabled is tested by test_parse_all.
""" """
cr = runners.CommandRunner(0, partial_match=True) parser = runners.CommandParser(partial_match=True)
result = cr.parse('message-i') result = parser.parse('message-i')
assert result.cmd.name == 'message-info' assert result.cmd.name == 'message-info'