Skip to content
Browse files

command: make testable and flesh out command parsing tests

  • Loading branch information...
1 parent 8673dc2 commit 96b1223123c22d4d4ae9f2660151802d4dc8218a Marc Sibson committed Feb 6, 2011
Showing with 115 additions and 24 deletions.
  1. +84 −0 tests/test_command.py
  2. +31 −24 vncdotool/command.py
View
84 tests/test_command.py
@@ -0,0 +1,84 @@
+
+from vncdotool import command
+import mock
+
+@mock.isolate(command.build_command_list)
+class TestBuildCommandList(object):
+ def setUp(self):
+ self.factory = mock.Mock()
+ self.client = command.VNCDoToolClient
+ self.deferred = self.factory.deferred
+
+ def assertCalled(self, fn, *args):
+ self.deferred.addCallback.assert_called_once_with(fn, *args)
+
+ def call_build_commands_list(self, commands):
+ command.build_command_list(self.factory, commands.split())
+ def test_alphanum_key(self):
+ self.call_build_commands_list('key a')
+ self.assertCalled(self.client.keyPress, 'a')
+
+ def test_control_key(self):
+ self.call_build_commands_list('key ctrl-c')
+ self.assertCalled(self.client.keyPress, 'ctrl-c')
+
+ def test_key_missing(self):
+ pass
+
+ def test_move(self):
+ self.call_build_commands_list('move 100 200')
+ self.assertCalled(self.client.mouseMove, 100, 200)
+
+ def test_move_missing(self):
+ pass
+
+ def test_click(self):
+ self.call_build_commands_list('click 1')
+ self.assertCalled(self.client.mousePress, 1)
+
+ def test_click_missing(self):
+ pass
+
+ def test_type(self):
+ self.call_build_commands_list('type foobar')
+ call = self.factory.deferred.addCallback
+ for key in 'foobar':
+ call.assert_calls_exist_with(self.client.keyPress, key)
+
+ def test_type_missing(self):
+ pass
+
+ def test_capture(self):
+ self.call_build_commands_list('capture foo.png')
+ self.assertCalled(self.client.captureScreen, 'foo.png')
+
+ def test_capture_not_png(self):
+ pass
+
+ def test_capture_missing(self):
+ pass
+
+ def test_expect(self):
+ self.call_build_commands_list('expect foo.png 10')
+ self.assertCalled(self.client.expectScreen, 'foo.png', 10)
+
+ def test_expect_not_png(self):
+ pass
+
+ def test_expect_missing(self):
+ pass
+
+ def test_chain_key_commands(self):
+ self.call_build_commands_list('type foobar key enter')
+ call = self.factory.deferred.addCallback
+ for key in 'foobar':
+ call.assert_calls_exist_with(self.client.keyPress, key)
+ call.assert_calls_exist_with(self.client.keyPress, 'enter')
+
+ def test_chain_type_expect(self):
+ self.call_build_commands_list('type username expect password.png 0')
+ call = self.factory.deferred.addCallback
+ for key in 'username':
+ call.assert_calls_exist_with(self.client.keyPress, key)
+
+ call.assert_calls_exist_with(self.client.expectScreen, 'password.png', 0)
View
55 vncdotool/command.py
@@ -53,6 +53,35 @@ def format_help(self, **kwargs):
return result
+def build_command_list(factory, args):
+ f = factory
+ client = VNCDoToolClient
+
+ while args:
+ cmd = args.pop(0)
+ if cmd == 'key':
+ key = args.pop(0)
+ f.deferred.addCallback(client.keyPress, key)
+ elif cmd == 'move':
+ x, y = int(args.pop(0)), int(args.pop(0))
+ f.deferred.addCallback(client.mouseMove, x, y)
+ elif cmd == 'click':
+ button = int(args.pop(0))
+ f.deferred.addCallback(client.mousePress, button)
+ elif cmd == 'type':
+ for key in args.pop(0):
+ f.deferred.addCallback(client.keyPress, key)
+ elif cmd == 'capture':
+ filename = args.pop(0)
+ f.deferred.addCallback(client.captureScreen, filename)
+ elif cmd == 'expect':
+ filename = args.pop(0)
+ rms = int(args.pop(0))
+ f.deferred.addCallback(client.expectScreen, filename, rms)
+ else:
+ print 'unknown cmd "%s"' % cmd
+
+
def main():
usage = '%prog [options] CMD CMDARGS'
description='Command line interaction with a VNC server'
@@ -86,30 +115,8 @@ def main():
if opts.verbose:
f.deferred.addCallbacks(log_connected)
- while args:
- cmd = args.pop(0)
- if cmd == 'key':
- key = args.pop(0)
- f.deferred.addCallback(VNCDoToolClient.keyPress, key)
- elif cmd == 'move':
- x, y = int(args.pop(0)), int(args.pop(0))
- f.deferred.addCallback(VNCDoToolClient.mouseMove, x, y)
- elif cmd == 'click':
- button = int(args.pop(0))
- f.deferred.addCallback(VNCDoToolClient.mousePress, button)
- elif cmd == 'type':
- for key in args.pop(0):
- f.deferred.addCallback(VNCDoToolClient.keyPress, key)
- elif cmd == 'capture':
- filename = args.pop(0)
- f.deferred.addCallback(VNCDoToolClient.captureScreen, filename)
- elif cmd == 'expect':
- filename = args.pop(0)
- rms = int(args.pop(0))
- f.deferred.addCallback(VNCDoToolClient.expectScreen, filename, rms)
- else:
- print 'unknown cmd "%s"' % cmd
-
+ build_command_list(f, args)
+
f.deferred.addCallback(stop)
f.deferred.addErrback(error)

0 comments on commit 96b1223

Please sign in to comment.
Something went wrong with that request. Please try again.