Skip to content

Commit

Permalink
Refactored playstatus command.
Browse files Browse the repository at this point in the history
1. It no longer prints directly -- it returns a string, and the main
block takes care of printing.
2. This also makes it easily testable, so I added a unit test.
  • Loading branch information
mackstann committed Feb 20, 2011
1 parent 155c563 commit 6122c60
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
27 changes: 15 additions & 12 deletions mpris-remote
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -330,10 +330,10 @@ class MPRISRemote(object):
@explain_numargs(0) @explain_numargs(0)
def playstatus(self): def playstatus(self):
status = self.player.GetStatus() status = self.player.GetStatus()
print "playing: %s" % playstatus_from_int(status[0]) return ("playing: %s\n" % playstatus_from_int(status[0])
print "random/shuffle: %s" % ("true" if status[1] else "false") + "random/shuffle: %s\n" % ("true" if status[1] else "false")
print "repeat track: %s" % ("true" if status[2] else "false") + "repeat track: %s\n" % ("true" if status[2] else "false")
print "repeat list: %s" % ("true" if status[3] else "false") + "repeat list: %s\n" % ("true" if status[3] else "false"))


@explain_numargs(0, 1) @explain_numargs(0, 1)
@explain_argtype(0, is_track_num_or_star, optional=True) @explain_argtype(0, is_track_num_or_star, optional=True)
Expand Down Expand Up @@ -560,16 +560,19 @@ if __name__ == '__main__':
encoding = sys.stdout.encoding or locale.getpreferredencoding() or 'ascii' encoding = sys.stdout.encoding or locale.getpreferredencoding() or 'ascii'


if len(sys.argv) == 1: if len(sys.argv) == 1:
sys.stdout.write(remote.verbose_status().encode(encoding, 'replace')) method_name = 'verbose_status'
args = []
else: else:
method_name = sys.argv[1] method_name = sys.argv[1]
args = sys.argv[2:] args = sys.argv[2:]


try: try:
getattr(remote, method_name)(*args) ret = getattr(remote, method_name)(*args)
except BadUserInput, e: if ret:
print >>sys.stderr, e sys.stdout.write(ret.encode(encoding, 'replace'))
raise SystemExit(1) except BadUserInput, e:
except KeyboardInterrupt: print >>sys.stderr, e
raise SystemExit(2) raise SystemExit(1)
except KeyboardInterrupt:
raise SystemExit(2)


10 changes: 10 additions & 0 deletions tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ def test_print_verbose_status_typical(self):
r.find_player('foo') r.find_player('foo')
self.assertEquals(expected_output, r.verbose_status()) self.assertEquals(expected_output, r.verbose_status())


def test_playstatus(self):
dbus.mock_method('/Player', 'GetStatus', lambda: [0, 1, 0, 1])
expected_output = ("playing: playing\n"
+ "random/shuffle: true\n"
+ "repeat track: false\n"
+ "repeat list: true\n")
r = mprisremote.MPRISRemote()
r.find_player('foo')
self.assertEquals(expected_output, r.playstatus())

def test_find_player_success(self): def test_find_player_success(self):
r = mprisremote.MPRISRemote() r = mprisremote.MPRISRemote()
r.find_player('foo') r.find_player('foo')
Expand Down

0 comments on commit 6122c60

Please sign in to comment.