Commits

Yuya Nishihara  committed a4f706d

thgbackdoor: update test case to write readable multi-line code

  • Participants
  • Parent commits 8761a3f

Comments (0)

Files changed (1)

File tests/thgbackdoor_test.py

-import errno, os, shutil, socket, subprocess, tempfile, time
+import errno, os, shutil, socket, subprocess, sys, tempfile, time
 try:
     import cPickle as pickle
 except ImportError:
 class RemoteError(Exception):
     pass
 
+def _cleancode(code):
+    r"""Clean up indentation of multi-line code text
+
+    >>> _cleancode('')
+    ''
+    >>> _cleancode('''
+    ...            while True:
+    ...                do_something()
+    ...            ''')
+    '\nwhile True:\n    do_something()\n'
+    """
+    if not isinstance(code, basestring) or not code:
+        return code
+    # simplified version of inspect.cleandoc() which requires Python>=2.6
+    lines = code.splitlines()
+    margin = sys.maxint
+    for l in lines[1:]:
+        n = len(l.lstrip())
+        if n:
+            margin = min(margin, len(l) - n)
+    if margin == sys.maxint:
+        return code
+    return lines[0] + '\n' + '\n'.join(l[margin:] for l in lines[1:])
+
 class ThgClient(object):
     def __init__(self, cmdargs=None, timeout=None, connect=True):
         self._cmdargs = list(cmdargs or [])
         self._proc = None
 
     def eval(self, expression):
-        self._sendmsg(('eval', expression))
+        self._sendmsg(('eval', _cleancode(expression)))
         return self.wait()
 
     def exec_(self, code, delay=None):
-        self._sendmsg(('exec', code, delay))
+        self._sendmsg(('exec', _cleancode(code), delay))
         return self.wait()
 
     def _sendmsg(self, msg):
 def test_control_by_qtest():
     thg = ThgClient(timeout=5)
     try:
-        thg.exec_('from PyQt4.QtTest import QTest')
-        thg.exec_('QTest.qWaitForWindowShown(dlg)')
-        thg.exec_('QTest.keyClick(dlg, "N", Qt.ControlModifier)', delay=0)
+        thg.exec_('''
+        from PyQt4.QtTest import QTest
+        QTest.qWaitForWindowShown(dlg)
+        QTest.keyClick(dlg, "N", Qt.ControlModifier)
+        ''', delay=0)
         # introduce a helper function?
         for _i in xrange(1):
             time.sleep(0.1)