def test_runCmd_noCmd(self): """ if there's no such command, tell the person so """ s = ShellProtocol() s.showPrompt = lambda: None called = [] s.sendLine = called.append r = s.runCmd(*['foo', 'bar']) self.assertEqual(r, False) self.assertEqual(len(called), 1) self.assertTrue('foo' in called[0])
def test_runCmd_error(self): """ If there's any other kind of error """ s = ShellProtocol() s.showPrompt = lambda: None called = [] s.sendLine = called.append def fake(arg1): raise Exception('foo') s.cmd_foo = fake r = s.runCmd(*['foo', 'arg1']) self.assertEqual(r, False) self.assertEqual(len(called), 1)
def test_runCmd_badArgs(self): """ If the user supplies bad arguments """ s = ShellProtocol() s.showPrompt = lambda: None called = [] s.sendLine = called.append def fake(arg1, arg2): pass s.cmd_foo = fake r = s.runCmd(*['foo', 'arg1']) self.assertEqual(r, False) self.assertEqual(len(called), 1)
def test_runCmd(self): """ runCmd should find the command from arg[0] and run it with args[1:]* """ s = ShellProtocol() s.showPrompt = lambda: None called = [] def fake(*args): called.append(args) return 'hey' s.cmd_foo = fake r = s.runCmd(*['foo', 'arg1', 'arg2', 'arg3']) self.assertEqual(r, 'hey') self.assertEqual(len(called), 1) self.assertEqual(called[0], ('arg1', 'arg2', 'arg3'))
def test_lineReceived(self): """ lineReceived should parse the args and call runCmd """ s = ShellProtocol() called = [] def parseCmd(s): called.append(('parse', s)) return ['parsed', 'arg'] def runCmd(cmd, *args): called.append(('run', cmd, args)) s.parseCmd = parseCmd s.runCmd = runCmd s.lineReceived('how are you') self.assertEqual(len(called), 2) self.assertEqual(called[0], ('parse', 'how are you')) self.assertEqual(called[1], ('run', 'parsed', ('arg',)))