diff wibble/tests.py @ 8:685479d1f0a7

Tests available for both py2 and py3
author Ben Croston <ben@croston.org>
date Sun, 04 Sep 2011 23:37:41 +0100
parents 3a6f3193cc7d
children 23743b0f67ab
line wrap: on
line diff
--- a/wibble/tests.py	Thu Sep 01 19:19:03 2011 +0100
+++ b/wibble/tests.py	Sun Sep 04 23:37:41 2011 +0100
@@ -8,19 +8,19 @@
 
 ##### server vvv #####
 class api(object):
-  def mymethod(self):
-      return 'wibbler woz ere'
+    def mymethod(self):
+        return 'wibbler woz ere'
 
-  def echo(self, mystring):
-      return 'ECHO: ' + mystring
+    def echo(self, mystring):
+        return 'ECHO: ' + mystring
 
-  def raiseexception(self):
-      dividebyzeroerror = 1/0
+    def raiseexception(self):
+        dividebyzeroerror = 1/0
 
 def myauth(username, password, useragent=None):
-  return username == 'testuser' and \
-         hashlib.md5('s3cr3t').hexdigest() == password and \
-         useragent == 'wibble_unittest'
+    return username == 'testuser' and \
+           hashlib.md5('s3cr3t').hexdigest() == password and \
+           useragent == 'wibble_unittest'
 
 def make_server():
     from server import JsonRpcApp
@@ -38,7 +38,7 @@
         self.client = ServerProxy('http://localhost:1337/',
                                   username='testuser',
                                   password='s3cr3t',
-                                  user_agent='InternerExploiter')
+                                  user_agent='InternetExploiter')
         with self.assertRaises(UnauthorisedException):
             self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
 
@@ -67,25 +67,25 @@
 
 class EchoTest(WibbleTests):
     def runTest(self):
-        POUND = '\xc2\xa3'
+        if platform.python_version().startswith('3'):
+            POUND = '\u00A3'
+        else:
+            POUND = unicode('\u00A3')
         self.assertEqual(self.client.echo(POUND), 'ECHO: ' + POUND)
         self.assertEqual(self.client.echo('hello mum!'), 'ECHO: hello mum!')
 ##### client ^^^ #####
 
 finished = False
 def suite():
-    if platform.python_version().startswith('3'):
-        # no tests for python 3 because server not ported yet
-        return unittest.TestSuite()
-
-    # create server
-    def test_wrapper():
-        server = make_server()
-        while not finished:
-            server.handle_request()
-    thread = Thread(target=test_wrapper)
-    thread.start()
-    time.sleep(0.1) # wait for server thread to start
+    if platform.python_version().startswith('2'):
+        # create server
+        def test_wrapper():
+            server = make_server()
+            while not finished:
+                server.handle_request()
+        thread = Thread(target=test_wrapper)
+        thread.start()
+        time.sleep(0.1) # wait for server thread to start
 
     # tests are as client
     suite = unittest.TestSuite()
@@ -97,6 +97,15 @@
     return suite
 
 if __name__ == '__main__':
+    import sys
+    if platform.python_version().startswith('2 ') and 'serve' in sys.argv:
+        print 'Listening on port 1337 (Ctrl-C qo quit)...'
+        server = make_server()
+        try:
+            server.serve_forever()
+        except KeyboardInterrupt:
+            sys.exit()
+
     unittest.TextTestRunner(verbosity=2).run(suite())
     finished = True