From 4c2306093c545cf3b534485206113d4b7626a21d Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Wed, 13 Jun 2012 10:42:14 -0700 Subject: [PATCH] Replace all assert statements in test code. Use self.assertFoo where TestCase instances are available, otherwise raise an exception manually. --- tornado/test/auth_test.py | 12 +++++--- tornado/test/gen_test.py | 2 +- tornado/test/httpserver_test.py | 3 +- tornado/test/iostream_test.py | 2 +- tornado/test/process_test.py | 2 +- tornado/test/simple_httpclient_test.py | 6 ++-- tornado/test/twisted_test.py | 3 +- tornado/test/web_test.py | 38 +++++++++++++++++--------- 8 files changed, 44 insertions(+), 24 deletions(-) diff --git a/tornado/test/auth_test.py b/tornado/test/auth_test.py index 748c526f..916194d2 100644 --- a/tornado/test/auth_test.py +++ b/tornado/test/auth_test.py @@ -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')) diff --git a/tornado/test/gen_test.py b/tornado/test/gen_test.py index d9ac9dab..4daad796 100644 --- a/tornado/test/gen_test.py +++ b/tornado/test/gen_test.py @@ -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) diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index de09f234..6503965c 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -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): diff --git a/tornado/test/iostream_test.py b/tornado/test/iostream_test.py index 5aa1d9bf..f62e4801 100644 --- a/tornado/test/iostream_test.py +++ b/tornado/test/iostream_test.py @@ -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, diff --git a/tornado/test/process_test.py b/tornado/test/process_test.py index 9f7ecf2c..65e9796f 100644 --- a/tornado/test/process_test.py +++ b/tornado/test/process_test.py @@ -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 diff --git a/tornado/test/simple_httpclient_test.py b/tornado/test/simple_httpclient_test.py index db7562f5..380463e3 100644 --- a/tornado/test/simple_httpclient_test.py +++ b/tornado/test/simple_httpclient_test.py @@ -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") diff --git a/tornado/test/twisted_test.py b/tornado/test/twisted_test.py index 3fe7ee97..7862c87f 100644 --- a/tornado/test/twisted_test.py +++ b/tornado/test/twisted_test.py @@ -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 diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index 23d8d07a..37a098b3 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -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