Commit f6069026 authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

tests: add testFixNestedExceptTrace

parent 44942257
from lib2to3.tests.test_fixers import FixerTestCase as lib2to3FixerTestCase
class FixerTestCase(lib2to3FixerTestCase):
# Subclass to replace "fixer_pkg"
def setUp(self, fix_list=None, fixer_pkg="my2to3", options=None):
super(FixerTestCase, self).setUp(fix_list, fixer_pkg, options)
if self.fixer.endswith('_trace'):
fix_name = 'fix_' + self.fixer
self.fixer_module = fixer_module = getattr(__import__('my2to3.fixes', fromlist=[fix_name]), fix_name)
self.traces = []
# Wrap fixer_module.trace, to populate self.traces
self.old_trace = fixer_module.trace
def decorate(func):
def call(*args):
self.traces.append(args)
return func(*args)
return call
fixer_module.trace = decorate(fixer_module.trace)
def tearDown(self, *args, **kw):
super(FixerTestCase, self).tearDown(*args, **kw)
if self.fixer.endswith('_trace'):
self.fixer_module.trace = self.old_trace
from lib2to3.tests.test_fixers import FixerTestCase as lib2to3FixerTestCase
import unittest
class FixerTestCase(lib2to3FixerTestCase):
# Subclass to replace "fixer_pkg"
def setUp(self, fix_list=None, fixer_pkg="my2to3", options=None):
super(FixerTestCase, self).setUp(fix_list, fixer_pkg, options)
from . import FixerTestCase
class testFixDivisionTrace(FixerTestCase):
......
import unittest
from . import FixerTestCase
class testFixNestedExceptTrace(FixerTestCase):
fixer = "nested_except_trace"
def test_try(self):
a = """
try:
try:
1
except Exception as e:
2
except Exception as e:
3
"""
self.assertEqual(self.traces, [])
self.unchanged(a)
self.assertEqual(self.traces, [])
def test_except(self):
a = """
try:
1
except Exception as e:
try:
2
except Exception as e:
3
"""
self.assertEqual(self.traces, [])
self.unchanged(a)
self.assertEqual(self.traces, [(u'<string>', 4, 7)])
def test_except_2(self):
a = """
try:
1
except Exception as e:
try:
2
except Exception as f:
3
"""
self.assertEqual(self.traces, [])
self.unchanged(a)
self.assertEqual(self.traces, [])
def test_multiple_except(self):
a = """
try:
1
except Exception as e:
try:
2
except Exception as e:
try:
3
except Exception as e:
4
"""
self.assertEqual(self.traces, [])
self.unchanged(a)
self.assertEqual(self.traces, [(u'<string>', 7, 10), (u'<string>', 4, 7), (u'<string>', 4, 10)])
def test_else(self):
a = """
try:
1
except Exception as e:
2
else:
try:
3
except Exception as e:
4
"""
self.assertEqual(self.traces, [])
self.unchanged(a)
self.assertEqual(self.traces, [])
def test_finally(self):
a = """
try:
1
except Exception as e:
2
finally:
try:
3
except Exception as e:
4
"""
self.assertEqual(self.traces, [])
self.unchanged(a)
self.assertEqual(self.traces, [])
if __name__ == '__main__':
unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment