]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Replace all assert statements in test code.
authorBen Darnell <ben@bendarnell.com>
Wed, 13 Jun 2012 17:42:14 +0000 (10:42 -0700)
committerBen Darnell <ben@bendarnell.com>
Wed, 13 Jun 2012 17:42:14 +0000 (10:42 -0700)
Use self.assertFoo where TestCase instances are available, otherwise
raise an exception manually.

tornado/test/auth_test.py
tornado/test/gen_test.py
tornado/test/httpserver_test.py
tornado/test/iostream_test.py
tornado/test/process_test.py
tornado/test/simple_httpclient_test.py
tornado/test/twisted_test.py
tornado/test/web_test.py

index 748c526f7857ac3cdb28a5d22cde7d8af49ed5ad..916194d29120f1e9286b12a569e0bd9e614cc9f7 100644 (file)
@@ -25,13 +25,15 @@ class OpenIdClientLoginHandler(RequestHandler, OpenIdMixin):
         self.authenticate_redirect()
 
     def on_user(self, user):
-        assert user is not None
+        if user is None:
+            raise Exception("user is None")
         self.finish(user)
 
 
 class OpenIdServerAuthenticateHandler(RequestHandler):
     def post(self):
-        assert self.get_argument('openid.mode') == 'check_authentication'
+        if self.get_argument('openid.mode') != 'check_authentication':
+            raise Exception("incorrect openid.mode %r")
         self.write('is_valid:true')
 
 
@@ -54,11 +56,13 @@ class OAuth1ClientLoginHandler(RequestHandler, OAuthMixin):
         self.authorize_redirect(http_client=self.settings['http_client'])
 
     def on_user(self, user):
-        assert user is not None
+        if user is None:
+            raise Exception("user is None")
         self.finish(user)
 
     def _oauth_get_user(self, access_token, callback):
-        assert access_token == dict(key=b('uiop'), secret=b('5678')), access_token
+        if access_token != dict(key=b('uiop'), secret=b('5678')):
+            raise Exception("incorrect access token %r" % access_token)
         callback(dict(email='foo@example.com'))
 
 
index d9ac9dabb3118ee0a58673727060385ef97b6d3c..4daad796c4c3c7dc446ddcd8b97afb60a4e2a450 100644 (file)
@@ -33,7 +33,7 @@ class GenTest(AsyncTestCase):
         def f():
             (yield gen.Callback("k1"))()
             res = yield gen.Wait("k1")
-            assert res is None
+            self.assertTrue(res is None)
             self.stop()
         self.run_gen(f)
 
index de09f234e6ff81e0f5ee79d4c53cb6ba67c7398a..6503965cb33e8d10f4bd8ca6235d7ee2feff6500 100644 (file)
@@ -38,7 +38,8 @@ class HelloWorldRequestHandler(RequestHandler):
         self.expected_protocol = protocol
 
     def get(self):
-        assert self.request.protocol == self.expected_protocol
+        if self.request.protocol != self.expected_protocol:
+            raise Exception("unexpected protocol")
         self.finish("Hello world")
 
     def post(self):
index 5aa1d9bf9900ca417c241fe6f7600f4ee66e2349..f62e4801c27b73d3ac040fa75f63ae1915d09220 100644 (file)
@@ -138,7 +138,7 @@ class TestIOStream(AsyncHTTPTestCase, LogTrapTestCase):
                 self.stop()
 
             def final_callback(data):
-                assert not data
+                self.assertFalse(data)
                 final_called.append(True)
                 self.stop()
             server.read_bytes(6, callback=final_callback,
index 9f7ecf2cf3adda7ad87b40d3d048b53307c80015..65e9796f6805528d674d151e165a974ee4b2ee5d 100644 (file)
@@ -60,7 +60,7 @@ class ProcessTest(LogTrapTestCase):
         signal.alarm(5)  # master process
         try:
             id = fork_processes(3, max_restarts=3)
-            assert id is not None
+            self.assertTrue(id is not None)
             signal.alarm(5)  # child processes
         except SystemExit, e:
             # if we exit cleanly from fork_processes, all the child processes
index db7562f5f1b1267b4ec8dafa39a3411d21df430d..380463e35273061f8e931cba638f1fe33acfceda 100644 (file)
@@ -74,14 +74,16 @@ class NoContentHandler(RequestHandler):
 
 class SeeOther303PostHandler(RequestHandler):
     def post(self):
-        assert self.request.body == b("blah")
+        if self.request.body != b("blah"):
+            raise Exception("unexpected body %r" % self.request.body)
         self.set_header("Location", "/303_get")
         self.set_status(303)
 
 
 class SeeOther303GetHandler(RequestHandler):
     def get(self):
-        assert not self.request.body
+        if self.request.body:
+            raise Exception("unexpected body %r" % self.request.body)
         self.write("ok")
 
 
index 3fe7ee97d5f2fca0e78abb36b2d0eb238135c0e9..7862c87fff9b188519ade0fceaad55e71dbf89e5 100644 (file)
@@ -57,7 +57,8 @@ def save_signal_handlers():
     saved = {}
     for sig in [signal.SIGINT, signal.SIGTERM, signal.SIGCHLD]:
         saved[sig] = signal.getsignal(sig)
-    assert "twisted" not in repr(saved), repr(saved)
+    if "twisted" in repr(saved):
+        raise Exception("twisted signal handlers already installed")
     return saved
 
 
index 23d8d07a0b00205f36d2ddba2a79495bf6f7b4f2..37a098b366fd4a137ccdca60e158e23909811d5d 100644 (file)
@@ -51,7 +51,7 @@ class SecureCookieTest(LogTrapTestCase):
         handler.set_secure_cookie('foo', binascii.a2b_hex(b('d76df8e7aefc')))
         cookie = handler._cookies['foo']
         match = re.match(b(r'12345678\|([0-9]+)\|([0-9a-f]+)'), cookie)
-        assert match
+        self.assertTrue(match)
         timestamp = match.group(1)
         sig = match.group(2)
         self.assertEqual(
@@ -68,7 +68,7 @@ class SecureCookieTest(LogTrapTestCase):
         # tamper with the cookie
         handler._cookies['foo'] = utf8('1234|5678%s|%s' % (timestamp, sig))
         # it gets rejected
-        assert handler.get_secure_cookie('foo') is None
+        self.assertTrue(handler.get_secure_cookie('foo') is None)
 
     def test_arbitrary_bytes(self):
         # Secure cookies accept arbitrary data (which is base64 encoded).
@@ -250,13 +250,19 @@ class EchoHandler(RequestHandler):
         # In httpserver.py (i.e. self.request.arguments), they're left
         # as bytes.  Keys are always native strings.
         for key in self.request.arguments:
-            assert type(key) == str, repr(key)
+            if type(key) != str:
+                raise Exception("incorrect type for key: %r" % type(key))
             for value in self.request.arguments[key]:
-                assert type(value) == bytes_type, repr(value)
+                if type(value) != bytes_type:
+                    raise Exception("incorrect type for value: %r" %
+                                    type(value))
             for value in self.get_arguments(key):
-                assert type(value) == unicode, repr(value)
+                if type(value) != unicode:
+                    raise Exception("incorrect type for value: %r" %
+                                    type(value))
         for arg in path_args:
-            assert type(arg) == unicode, repr(arg)
+            if type(arg) != unicode:
+                raise Exception("incorrect type for path arg: %r" % type(arg))
         self.write(dict(path=self.request.path,
                         path_args=path_args,
                         args=recursive_unicode(self.request.arguments)))
@@ -313,7 +319,9 @@ class TypeCheckHandler(RequestHandler):
 
         # Secure cookies return bytes because they can contain arbitrary
         # data, but regular cookies are native strings.
-        assert self.cookies.keys() == ['asdf']
+        if self.cookies.keys() != ['asdf']:
+            raise Exception("unexpected values for cookie keys: %r" %
+                            self.cookies.keys())
         self.check_type('get_secure_cookie', self.get_secure_cookie('asdf'), bytes_type)
         self.check_type('get_cookie', self.get_cookie('asdf'), str)
 
@@ -343,7 +351,8 @@ class TypeCheckHandler(RequestHandler):
 
 class DecodeArgHandler(RequestHandler):
     def decode_argument(self, value, name=None):
-        assert type(value) == bytes_type, repr(value)
+        if type(value) != bytes_type:
+            raise Exception("unexpected type for value: %r" % type(value))
         # use self.request.arguments directly to avoid recursion
         if 'encoding' in self.request.arguments:
             return value.decode(to_unicode(self.request.arguments['encoding'][0]))
@@ -432,8 +441,10 @@ class HeaderInjectionHandler(RequestHandler):
             self.set_header("X-Foo", "foo\r\nX-Bar: baz")
             raise Exception("Didn't get expected exception")
         except ValueError, e:
-            assert "Unsafe header value" in str(e)
-            self.finish(b("ok"))
+            if "Unsafe header value" in str(e):
+                self.finish(b("ok"))
+            else:
+                raise
 
 
 class WebTest(AsyncHTTPTestCase, LogTrapTestCase):
@@ -703,10 +714,10 @@ class StaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
 
     def test_static_files(self):
         response = self.fetch('/robots.txt')
-        assert b("Disallow: /") in response.body
+        self.assertTrue(b("Disallow: /") in response.body)
 
         response = self.fetch('/static/robots.txt')
-        assert b("Disallow: /") in response.body
+        self.assertTrue(b("Disallow: /") in response.body)
 
     def test_static_url(self):
         response = self.fetch("/static_url/robots.txt")
@@ -740,7 +751,8 @@ class CustomStaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
         class MyStaticFileHandler(StaticFileHandler):
             def get(self, path):
                 path = self.parse_url_path(path)
-                assert path == "foo.txt"
+                if path != "foo.txt":
+                    raise Exception("unexpected path: %r" % path)
                 self.write("bar")
 
             @classmethod