[Pkg-mpd-commits] [python-mpd] 24/91: Twisted client test coverage 100%
Simon McVittie
smcv at debian.org
Sat Feb 24 14:55:29 UTC 2018
This is an automated email from the git hooks/post-receive script.
smcv pushed a commit to branch debian/master
in repository python-mpd.
commit be5ce6258aa7cb7b01c25500e3a43f7e62bc3e68
Author: Robert Niederreiter <office at squarewave.at>
Date: Mon Sep 12 13:24:06 2016 +0200
Twisted client test coverage 100%
---
mpd/tests.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
mpd/twisted.py | 11 +++++---
2 files changed, 83 insertions(+), 11 deletions(-)
diff --git a/mpd/tests.py b/mpd/tests.py
index b1da61e..39e4f3b 100755
--- a/mpd/tests.py
+++ b/mpd/tests.py
@@ -732,14 +732,6 @@ class TestMPDProtocol(unittest.TestCase):
self.protocol.lineReceived(b'OK')
def test_command_list_failure(self):
- """
- OK MPD 0.18.0
- command_list_ok_begin
- load "Foo"
- play
- command_list_end
- ACK [50 at 0] {load} No such playlist
- """
self.init_protocol(default_idle=False)
def load_command_error(result):
@@ -800,6 +792,65 @@ class TestMPDProtocol(unittest.TestCase):
self.protocol.lineReceived(b'OK')
self.assertEqual([b'idle\n'], self.protocol.transport.written)
+ def test_command_list_failure_when_default_idle(self):
+ self.init_protocol()
+ self.protocol.lineReceived(b'OK MPD 0.18.0')
+
+ def load_command_error(result):
+ self.assertIsInstance(result, Failure)
+ self.assertEqual(
+ result.getErrorMessage(),
+ '[50 at 0] {load} No such playlist'
+ )
+
+ def command_list_general_error(result):
+ self.assertIsInstance(result, Failure)
+ self.assertEqual(
+ result.getErrorMessage(),
+ 'An earlier command failed.'
+ )
+
+ self.protocol.command_list_ok_begin()
+ self.protocol.load('Foo').addErrback(load_command_error)
+ self.protocol.play().addErrback(command_list_general_error)
+ self.protocol.command_list_end().addErrback(load_command_error)
+ self.assertEqual(
+ [
+ b'idle\n',
+ b'noidle\n',
+ b'command_list_ok_begin\n',
+ b'load "Foo"\n',
+ b'play\n',
+ b'command_list_end\n',
+ ],
+ self.protocol.transport.written
+ )
+ self.protocol.transport.clear()
+ self.protocol.lineReceived(b'OK')
+ self.protocol.lineReceived(b'ACK [50 at 0] {load} No such playlist')
+ self.assertEqual([b'idle\n'], self.protocol.transport.written)
+
+ def test_command_list_item_is_generator(self):
+ self.init_protocol(default_idle=False)
+
+ def success(result):
+ self.assertEqual(result, [[
+ u"Weezer - Say It Ain't So.mp3",
+ u'Dire Straits - Walk of Life.mp3',
+ u'01 - Love Delicatessen.mp3',
+ u"Guns N' Roses - Paradise City.mp3"
+ ]])
+
+ self.protocol.command_list_ok_begin()
+ self.protocol.listplaylist('Foo')
+ self.protocol.command_list_end().addCallback(success)
+ self.protocol.lineReceived(b'file: Weezer - Say It Ain\'t So.mp3')
+ self.protocol.lineReceived(b'file: Dire Straits - Walk of Life.mp3')
+ self.protocol.lineReceived(b'file: 01 - Love Delicatessen.mp3')
+ self.protocol.lineReceived(b'file: Guns N\' Roses - Paradise City.mp3')
+ self.protocol.lineReceived(b'list_OK')
+ self.protocol.lineReceived(b'OK')
+
def test_already_in_command_list(self):
self.init_protocol(default_idle=False)
self.protocol.command_list_ok_begin()
@@ -815,6 +866,22 @@ class TestMPDProtocol(unittest.TestCase):
lambda: self.protocol.command_list_end()
)
+ def test_invalid_command_in_command_list(self):
+ self.init_protocol(default_idle=False)
+ self.protocol.command_list_ok_begin()
+ self.assertRaises(
+ mpd.CommandListError,
+ lambda: self.protocol.kill()
+ )
+
+ def test_close(self):
+ self.init_protocol(default_idle=False)
+
+ def success(result):
+ self.assertEqual(result, None)
+
+ self.protocol.close().addCallback(success)
+
if __name__ == '__main__':
unittest.main()
diff --git a/mpd/twisted.py b/mpd/twisted.py
index 3793d81..4b65c6f 100644
--- a/mpd/twisted.py
+++ b/mpd/twisted.py
@@ -44,6 +44,7 @@ def _create_command(wrapper, name, callback):
def mpd_command(self, *args):
def bound_callback(lines):
return callback(self, lines)
+ bound_callback.callback = callback
return wrapper(self, name, args, bound_callback)
return mpd_command
@@ -99,7 +100,6 @@ class MPDProtocol(basic.LineReceiver, MPDClientBase):
del self._command_list_results[0]
else:
state_list.pop(0).errback(CommandError(error))
- # XXX: reset received lines here?
self._continue_idle()
elif line == SUCCESS or (command_list and line == NEXT):
state_list.pop(0).callback(self._rcvd_lines[:])
@@ -110,9 +110,14 @@ class MPDProtocol(basic.LineReceiver, MPDClientBase):
else:
self._rcvd_lines.append(line)
+ def _lookup_callback(self, parser):
+ if hasattr(parser, 'callback'):
+ return parser.callback
+ return parser
+
def _execute(self, command, args, parser):
# close or kill command in command list not allowed
- if self._command_list and not callable(parser):
+ if self._command_list and self._lookup_callback(parser) is self.NOOP:
msg = '{} not allowed in command list'.format(command)
raise CommandListError(msg)
# default state idle and currently in idle state, trigger noidle
@@ -126,7 +131,7 @@ class MPDProtocol(basic.LineReceiver, MPDClientBase):
state = self._state[-1] if self._command_list else self._state
state.append(deferred)
# NOOP is for close and kill commands
- if parser is not self.NOOP:
+ if self._lookup_callback(parser) is not self.NOOP:
# attach command related result parser
deferred.addCallback(parser)
# command list, attach handler for collecting command list results
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mpd/python-mpd.git
More information about the Pkg-mpd-commits
mailing list