Commit f943e9e0 authored by Dylan Trotter's avatar Dylan Trotter Committed by Dylan Trotter

Fix broken unit tests

In bc04caf4 I moved a bunch of tests into imputil_test.py but I failed to
add a __main__ check so the tests were never run. This change remedies
that and fixes the tests that were broken the whole time.
parent eb5931d1
...@@ -187,12 +187,23 @@ _REDUNDANT_FUTURE_FEATURES = ('generators', 'with_statement', 'nested_scopes') ...@@ -187,12 +187,23 @@ _REDUNDANT_FUTURE_FEATURES = ('generators', 'with_statement', 'nested_scopes')
class FutureFeatures(object): class FutureFeatures(object):
"""Spec for future feature flags imported by a module."""
def __init__(self): def __init__(self, absolute_import=False, division=False,
self.absolute_import = False print_function=False, unicode_literals=False):
self.division = False self.absolute_import = absolute_import
self.print_function = False self.division = division
self.unicode_literals = False self.print_function = print_function
self.unicode_literals = unicode_literals
def __repr__(self):
return '<FutureFeatures {!r}>'.format(self.__dict__)
def __eq__(self, other):
return isinstance(other, FutureFeatures) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not self.__eq__(other)
def _make_future_features(node): def _make_future_features(node):
......
...@@ -211,8 +211,15 @@ class ImportVisitorTest(unittest.TestCase): ...@@ -211,8 +211,15 @@ class ImportVisitorTest(unittest.TestCase):
self.assertRaises(util.ImportError, self._visit_import, 'from foo import *') self.assertRaises(util.ImportError, self._visit_import, 'from foo import *')
def testImportFromFuture(self): def testImportFromFuture(self):
result = self._visit_import('from __future__ import print_function') mod = pythonparser.parse('from __future__ import print_function')
self.assertEqual([], result) visitor = imputil.ImportVisitor(MockPath(), mod.body[0])
visitor.visit(mod)
self.assertEqual([], visitor.imports)
def testImportLateFuture(self):
mod = pythonparser.parse('import os\nfrom __future__ import print_function')
visitor = imputil.ImportVisitor(MockPath())
self.assertRaises(util.LateFutureError, visitor.visit, mod)
def testImportFromNative(self): def testImportFromNative(self):
imp = imputil.Import('fmt', is_native=True) imp = imputil.Import('fmt', is_native=True)
...@@ -250,14 +257,12 @@ class ImportVisitorTest(unittest.TestCase): ...@@ -250,14 +257,12 @@ class ImportVisitorTest(unittest.TestCase):
class MakeFutureFeaturesTest(unittest.TestCase): class MakeFutureFeaturesTest(unittest.TestCase):
def testImportFromFuture(self): def testImportFromFuture(self):
print_function_features = imputil.FutureFeatures()
print_function_features.print_function = True
testcases = [ testcases = [
('from __future__ import print_function', ('from __future__ import print_function',
print_function_features), imputil.FutureFeatures(print_function=True)),
('from __future__ import generators', imputil.FutureFeatures()), ('from __future__ import generators', imputil.FutureFeatures()),
('from __future__ import generators, print_function', ('from __future__ import generators, print_function',
print_function_features), imputil.FutureFeatures(print_function=True)),
] ]
for tc in testcases: for tc in testcases:
...@@ -271,8 +276,6 @@ class MakeFutureFeaturesTest(unittest.TestCase): ...@@ -271,8 +276,6 @@ class MakeFutureFeaturesTest(unittest.TestCase):
testcases = [ testcases = [
# NOTE: move this group to testImportFromFuture as they are implemented # NOTE: move this group to testImportFromFuture as they are implemented
# by grumpy # by grumpy
('from __future__ import absolute_import',
r'future feature \w+ not yet implemented'),
('from __future__ import division', ('from __future__ import division',
r'future feature \w+ not yet implemented'), r'future feature \w+ not yet implemented'),
('from __future__ import unicode_literals', ('from __future__ import unicode_literals',
...@@ -293,44 +296,35 @@ class MakeFutureFeaturesTest(unittest.TestCase): ...@@ -293,44 +296,35 @@ class MakeFutureFeaturesTest(unittest.TestCase):
class ParseFutureFeaturesTest(unittest.TestCase): class ParseFutureFeaturesTest(unittest.TestCase):
def testFutureFeatures(self): def testFutureFeatures(self):
print_function_features = {'print_function': True}
absolute_import_features = {'absolute_import': True}
all_features = {'print_function': True, 'absolute_import': True}
testcases = [ testcases = [
('from __future__ import print_function', ('from __future__ import print_function',
print_function_features), imputil.FutureFeatures(print_function=True)),
("""\ ("""\
"module docstring" "module docstring"
from __future__ import print_function from __future__ import print_function
""", print_function_features), """, imputil.FutureFeatures(print_function=True)),
("""\ ("""\
"module docstring" "module docstring"
from __future__ import print_function, with_statement from __future__ import print_function, with_statement
from __future__ import nested_scopes from __future__ import nested_scopes
""", print_function_features), """, imputil.FutureFeatures(print_function=True)),
('from __future__ import absolute_import', ('from __future__ import absolute_import',
absolute_import_features), imputil.FutureFeatures(absolute_import=True)),
('from __future__ import absolute_import, print_function', ('from __future__ import absolute_import, print_function',
all_features), imputil.FutureFeatures(absolute_import=True, print_function=True)),
('foo = 123\nfrom __future__ import print_function',
imputil.FutureFeatures()),
('import os\nfrom __future__ import print_function',
imputil.FutureFeatures()),
] ]
for tc in testcases: for tc in testcases:
source, want = tc source, want = tc
mod = pythonparser.parse(textwrap.dedent(source)) mod = pythonparser.parse(textwrap.dedent(source))
_, got = imputil.parse_future_features(mod) _, got = imputil.parse_future_features(mod)
self.assertEqual(want, got.__dict__) self.assertEqual(want, got)
def testFutureAfterAssignRaises(self):
source = 'foo = 123\nfrom __future__ import print_function'
mod = pythonparser.parse(source)
self.assertRaises(util.LateFutureError, imputil.parse_future_features, mod)
def testFutureAfterImportRaises(self):
source = 'import os\nfrom __future__ import print_function'
mod = pythonparser.parse(source)
self.assertRaises(util.LateFutureError, imputil.parse_future_features, mod)
def testUnimplementedFutureRaises(self): def testUnimplementedFutureRaises(self):
mod = pythonparser.parse('from __future__ import division') mod = pythonparser.parse('from __future__ import division')
...@@ -343,3 +337,7 @@ class ParseFutureFeaturesTest(unittest.TestCase): ...@@ -343,3 +337,7 @@ class ParseFutureFeaturesTest(unittest.TestCase):
self.assertRaisesRegexp( self.assertRaisesRegexp(
util.ParseError, 'future feature foo is not defined', util.ParseError, 'future feature foo is not defined',
imputil.parse_future_features, mod) imputil.parse_future_features, mod)
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