self.assertEqual(result, b"013456")
def test_break_outside_loop(self):
- try:
+ with self.assertRaises(ParseError):
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
- try:
+ with self.assertRaises(ParseError):
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')
two{{1/0}}
three
"""})
- try:
+ with self.assertRaises(ZeroDivisionError):
loader.load("test.html").generate()
- except ZeroDivisionError:
- exc_stack = traceback.format_exc()
+ exc_stack = traceback.format_exc()
self.assertTrue("# test.html:2" in exc_stack)
def test_error_line_number_directive(self):
two{%if 1/0%}
three{%end%}
"""})
- try:
+ with self.assertRaises(ZeroDivisionError):
loader.load("test.html").generate()
- except ZeroDivisionError:
- exc_stack = traceback.format_exc()
+ exc_stack = traceback.format_exc()
self.assertTrue("# test.html:2" in exc_stack)
def test_error_line_number_module(self):
"base.html": "{% module Template('sub.html') %}",
"sub.html": "{{1/0}}",
}, namespace={"_tt_modules": ObjectDict({"Template": lambda path, **kwargs: loader.load(path).generate(**kwargs)})})
- try:
+ with self.assertRaises(ZeroDivisionError):
loader.load("base.html").generate()
- except ZeroDivisionError:
- exc_stack = traceback.format_exc()
+ exc_stack = traceback.format_exc()
self.assertTrue('# base.html:1' in exc_stack)
self.assertTrue('# sub.html:1' in exc_stack)
"base.html": "{% include 'sub.html' %}",
"sub.html": "{{1/0}}",
})
- try:
+ with self.assertRaises(ZeroDivisionError):
loader.load("base.html").generate()
- except ZeroDivisionError:
- exc_stack = traceback.format_exc()
+ exc_stack = traceback.format_exc()
self.assertTrue("# sub.html:1 (via base.html:1)" in exc_stack)
def test_error_line_number_extends_base_error(self):
"base.html": "{{1/0}}",
"sub.html": "{% extends 'base.html' %}",
})
- try:
+ with self.assertRaises(ZeroDivisionError):
loader.load("sub.html").generate()
- except ZeroDivisionError:
- exc_stack = traceback.format_exc()
+ exc_stack = traceback.format_exc()
self.assertTrue("# base.html:1" in exc_stack)
def test_error_line_number_extends_sub_error(self):
{{1/0}}
{% end %}
"""})
- try:
+ with self.assertRaises(ZeroDivisionError):
loader.load("sub.html").generate()
- except ZeroDivisionError:
- exc_stack = traceback.format_exc()
+ exc_stack = traceback.format_exc()
self.assertTrue("# sub.html:4 (via base.html:1)" in exc_stack)
def test_multi_includes(self):
"b.html": "{% include 'c.html' %}",
"c.html": "{{1/0}}",
})
- try:
+ with self.assertRaises(ZeroDivisionError):
loader.load("a.html").generate()
- except ZeroDivisionError:
- exc_stack = traceback.format_exc()
+ exc_stack = traceback.format_exc()
self.assertTrue("# c.html:1 (via b.html:1, a.html:1)" in exc_stack)
class AsyncTestCaseTest(AsyncTestCase):
def test_exception_in_callback(self):
self.io_loop.add_callback(lambda: 1 / 0)
- try:
+ with self.assertRaises(ZeroDivisionError):
self.wait()
- self.fail("did not get expected exception")
- except ZeroDivisionError:
- pass
def test_wait_timeout(self):
time = self.io_loop.time
# This can't use assertRaises because we need to inspect the
# exc_info triple (and not just the exception object)
- try:
+ with self.assertRaises(ioloop.TimeoutError):
test(self)
- self.fail("did not get expected exception")
- except ioloop.TimeoutError:
- exc_stack = traceback.format_exc()
+ exc_stack = traceback.format_exc()
# The stack trace should blame the add_timeout line, not just
# unrelated IOLoop/testing internals.
self.assertIn(