Skip to content

Commit

Permalink
correct testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
Akihiro Yamazaki committed Mar 30, 2014
1 parent e914a51 commit 32c7a32
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 38 deletions.
8 changes: 5 additions & 3 deletions tornado/test/concurrent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,12 @@ def f(callback):
self.expected_frame = traceback.extract_tb(
sys.exc_info()[2], limit=1)[0]
raise
with self.assertRaises(ZeroDivisionError):
try:
yield f()
tb = traceback.extract_tb(sys.exc_info()[2])
self.assertIn(self.expected_frame, tb)
self.fail("didn't get expected exception")
except ZeroDivisionError:
tb = traceback.extract_tb(sys.exc_info()[2])
self.assertIn(self.expected_frame, tb)

# The following series of classes demonstrate and test various styles
# of use, with and without generators and futures.
Expand Down
7 changes: 5 additions & 2 deletions tornado/test/ioloop_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ def target():
thread = threading.Thread(target=target)
thread.start()
closing.wait()
with self.assertRaisesRegexp(RuntimeError, "\AIOLoop is closing\Z"):
for i in range(1000):
for i in range(1000):
try:
other_ioloop.add_callback(lambda: None)
except RuntimeError as e:
self.assertEqual("IOLoop is closing", str(e))
break

def test_handle_callback_exception(self):
# IOLoop.handle_callback_exception can be overridden to catch
Expand Down
64 changes: 41 additions & 23 deletions tornado/test/template_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,20 @@ def test_break_continue(self):
self.assertEqual(result, b"013456")

def test_break_outside_loop(self):
with self.assertRaises(ParseError):
try:
Template(utf8("{% break %}"))
raise Exception("Did not get expected exception")
except ParseError:
pass

def test_break_in_apply(self):
# This test verifies current behavior, although of course it would
# be nice if apply didn't cause seemingly unrelated breakage
with self.assertRaises(ParseError):
try:
Template(utf8("{% for i in [] %}{% apply foo %}{% break %}{% end %}{% end %}"))
raise Exception("Did not get expected exception")
except ParseError:
pass

@unittest.skipIf(sys.version_info >= division.getMandatoryRelease(),
'no testable future imports')
Expand All @@ -174,50 +180,58 @@ def test_error_line_number_expression(self):
two{{1/0}}
three
"""})
with self.assertRaises(ZeroDivisionError):
try:
loader.load("test.html").generate()
exc_stack = traceback.format_exc()
self.assertTrue("# test.html:2" in exc_stack)
self.fail("did not get expected exception")
except ZeroDivisionError:
self.assertTrue("# test.html:2" in traceback.format_exc())

def test_error_line_number_directive(self):
loader = DictLoader({"test.html": """one
two{%if 1/0%}
three{%end%}
"""})
with self.assertRaises(ZeroDivisionError):
try:
loader.load("test.html").generate()
exc_stack = traceback.format_exc()
self.assertTrue("# test.html:2" in exc_stack)
self.fail("did not get expected exception")
except ZeroDivisionError:
self.assertTrue("# test.html:2" in traceback.format_exc())

def test_error_line_number_module(self):
loader = DictLoader({
"base.html": "{% module Template('sub.html') %}",
"sub.html": "{{1/0}}",
}, namespace={"_tt_modules": ObjectDict({"Template": lambda path, **kwargs: loader.load(path).generate(**kwargs)})})
with self.assertRaises(ZeroDivisionError):
try:
loader.load("base.html").generate()
exc_stack = traceback.format_exc()
self.assertTrue('# base.html:1' in exc_stack)
self.assertTrue('# sub.html:1' in exc_stack)
self.fail("did not get expected exception")
except ZeroDivisionError:
exc_stack = traceback.format_exc()
self.assertTrue('# base.html:1' in exc_stack)
self.assertTrue('# sub.html:1' in exc_stack)

def test_error_line_number_include(self):
loader = DictLoader({
"base.html": "{% include 'sub.html' %}",
"sub.html": "{{1/0}}",
})
with self.assertRaises(ZeroDivisionError):
try:
loader.load("base.html").generate()
exc_stack = traceback.format_exc()
self.assertTrue("# sub.html:1 (via base.html:1)" in exc_stack)
self.fail("did not get expected exception")
except ZeroDivisionError:
self.assertTrue("# sub.html:1 (via base.html:1)" in
traceback.format_exc())

def test_error_line_number_extends_base_error(self):
loader = DictLoader({
"base.html": "{{1/0}}",
"sub.html": "{% extends 'base.html' %}",
})
with self.assertRaises(ZeroDivisionError):
try:
loader.load("sub.html").generate()
exc_stack = traceback.format_exc()
self.fail("did not get expected exception")
except ZeroDivisionError:
exc_stack = traceback.format_exc()
self.assertTrue("# base.html:1" in exc_stack)

def test_error_line_number_extends_sub_error(self):
Expand All @@ -229,21 +243,25 @@ def test_error_line_number_extends_sub_error(self):
{{1/0}}
{% end %}
"""})
with self.assertRaises(ZeroDivisionError):
try:
loader.load("sub.html").generate()
exc_stack = traceback.format_exc()
self.assertTrue("# sub.html:4 (via base.html:1)" in exc_stack)
self.fail("did not get expected exception")
except ZeroDivisionError:
self.assertTrue("# sub.html:4 (via base.html:1)" in
traceback.format_exc())

def test_multi_includes(self):
loader = DictLoader({
"a.html": "{% include 'b.html' %}",
"b.html": "{% include 'c.html' %}",
"c.html": "{{1/0}}",
})
with self.assertRaises(ZeroDivisionError):
try:
loader.load("a.html").generate()
exc_stack = traceback.format_exc()
self.assertTrue("# c.html:1 (via b.html:1, a.html:1)" in exc_stack)
self.fail("did not get expected exception")
except ZeroDivisionError:
self.assertTrue("# c.html:1 (via b.html:1, a.html:1)" in
traceback.format_exc())


class AutoEscapeTest(unittest.TestCase):
Expand Down
20 changes: 12 additions & 8 deletions tornado/test/testing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ def set_environ(name, value):
class AsyncTestCaseTest(AsyncTestCase):
def test_exception_in_callback(self):
self.io_loop.add_callback(lambda: 1 / 0)
with self.assertRaises(ZeroDivisionError):
try:
self.wait()
self.fail("did not get expected exception")
except ZeroDivisionError:
pass

def test_wait_timeout(self):
time = self.io_loop.time
Expand Down Expand Up @@ -148,14 +151,15 @@ def test(self):

# This can't use assertRaises because we need to inspect the
# exc_info triple (and not just the exception object)
with self.assertRaises(ioloop.TimeoutError):
try:
test(self)
exc_stack = traceback.format_exc()
# The stack trace should blame the add_timeout line, not just
# unrelated IOLoop/testing internals.
self.assertIn(
"gen.Task(self.io_loop.add_timeout, self.io_loop.time() + 1)",
exc_stack)
self.fail("did not get expected exception")
except ioloop.TimeoutError:
# The stack trace should blame the add_timeout line, not just
# unrelated IOLoop/testing internals.
self.assertIn(
"gen.Task(self.io_loop.add_timeout, self.io_loop.time() + 1)",
traceback.format_exc())

self.finished = True

Expand Down
6 changes: 4 additions & 2 deletions tornado/test/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ def __init__(self, a, b):
raise TwoArgException(1, 2)
except TwoArgException:
exc_info = sys.exc_info()
with self.assertRaises(TwoArgException) as context:
try:
raise_exc_info(exc_info)
self.assertIs(context.exception, exc_info[1])
self.fail("didn't get expected exception")
except TwoArgException as e:
self.assertIs(e, exc_info[1])


class TestConfigurable(Configurable):
Expand Down

0 comments on commit 32c7a32

Please sign in to comment.