diff options
author | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-07-11 18:32:45 +0300 |
---|---|---|
committer | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-07-11 18:32:45 +0300 |
commit | d88a01ffbb2abefba90a9f26d0f3184ef672071c (patch) | |
tree | f0146ddd3ed2034f5e01d51eb986f7e6fc3062df | |
parent | 4cca0cded92e6d38f48ad10b1ff30d9d28b0c34d (diff) | |
download | astroid-d88a01ffbb2abefba90a9f26d0f3184ef672071c.tar.gz |
Small cleanup brought to the test files.
-rw-r--r-- | astroid/tests/unittest_lookup.py | 50 | ||||
-rw-r--r-- | astroid/tests/unittest_manager.py | 35 | ||||
-rw-r--r-- | astroid/tests/unittest_nodes.py | 37 |
3 files changed, 65 insertions, 57 deletions
diff --git a/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py index 42c50d6..491ae78 100644 --- a/astroid/tests/unittest_lookup.py +++ b/astroid/tests/unittest_lookup.py @@ -17,14 +17,15 @@ # with astroid. If not, see <http://www.gnu.org/licenses/>. """tests for the astroid variable lookup capabilities """ +import functools import sys -from functools import partial import unittest -from astroid import nodes, InferenceError, NotFoundError, UnresolvableName -from astroid.scoped_nodes import builtin_lookup -from astroid.bases import YES +from astroid import bases from astroid import builder +from astroid import exceptions +from astroid import nodes +from astroid import scoped_nodes from astroid import test_utils from astroid.tests import resources @@ -68,7 +69,7 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): b_value = next(b_infer) self.assertEqual(b_value.value, 1) # c - self.assertRaises(StopIteration, partial(next, b_infer)) + self.assertRaises(StopIteration, functools.partial(next, b_infer)) func = astroid.locals['func'][0] self.assertEqual(len(func.lookup('c')[1]), 1) @@ -80,7 +81,8 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): obj = next(astroid.ilookup('object')) self.assertIsInstance(obj, nodes.Class) self.assertEqual(obj.name, 'object') - self.assertRaises(InferenceError, partial(next, astroid.ilookup('YOAA'))) + self.assertRaises(exceptions.InferenceError, + functools.partial(next, astroid.ilookup('YOAA'))) # XXX self.assertEqual(len(list(self.nonregr.ilookup('enumerate'))), 2) @@ -106,8 +108,8 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): self.assertTrue(isinstance(my_dict, nodes.Dict), my_dict) none = next(method.ilookup('None')) self.assertIsNone(none.value) - self.assertRaises(InferenceError, partial(next, method.ilookup('YOAA'))) - + self.assertRaises(exceptions.InferenceError, + functools.partial(next, method.ilookup('YOAA'))) def test_function_argument_with_default(self): make_class = self.module2['make_class'] @@ -125,13 +127,13 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): obj = next(klass.ilookup('object')) self.assertIsInstance(obj, nodes.Class) self.assertEqual(obj.name, 'object') - self.assertRaises(InferenceError, partial(next, klass.ilookup('YOAA'))) + self.assertRaises(exceptions.InferenceError, + functools.partial(next, klass.ilookup('YOAA'))) def test_inner_classes(self): ddd = list(self.nonregr['Ccc'].ilookup('Ddd')) self.assertEqual(ddd[0].name, 'Ddd') - def test_loopvar_hiding(self): astroid = builder.parse(""" x = 10 @@ -170,9 +172,9 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): """) var = astroid.body[1].value if sys.version_info < (3, 0): - self.assertEqual(var.infered(), [YES]) + self.assertEqual(var.infered(), [bases.YES]) else: - self.assertRaises(UnresolvableName, var.infered) + self.assertRaises(exceptions.UnresolvableName, var.infered) def test_dict_comps(self): astroid = builder.parse(""" @@ -208,7 +210,7 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): var """) var = astroid.body[1].value - self.assertRaises(UnresolvableName, var.infered) + self.assertRaises(exceptions.UnresolvableName, var.infered) def test_generator_attributes(self): tree = builder.parse(""" @@ -219,8 +221,8 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): iterer = count() num = iterer.next() """) - next = tree.body[2].value.func # Getattr - gener = next.expr.infered()[0] # Generator + next_node = tree.body[2].value.func + gener = next_node.expr.infered()[0] if sys.version_info < (3, 0): self.assertIsInstance(gener.getattr('next')[0], nodes.Function) else: @@ -248,8 +250,7 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): self.assertTrue(p2.getattr('__name__')) self.assertTrue(astroid['NoName'].getattr('__name__')) p3 = next(astroid['p3'].infer()) - self.assertRaises(NotFoundError, p3.getattr, '__name__') - + self.assertRaises(exceptions.NotFoundError, p3.getattr, '__name__') def test_function_module_special(self): astroid = builder.parse(''' @@ -260,16 +261,14 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): path = [n for n in astroid.nodes_of_class(nodes.Name) if n.name == '__path__'][0] self.assertEqual(len(path.lookup('__path__')[1]), 1) - def test_builtin_lookup(self): - self.assertEqual(builtin_lookup('__dict__')[1], ()) - intstmts = builtin_lookup('int')[1] + self.assertEqual(scoped_nodes.builtin_lookup('__dict__')[1], ()) + intstmts = scoped_nodes.builtin_lookup('int')[1] self.assertEqual(len(intstmts), 1) self.assertIsInstance(intstmts[0], nodes.Class) self.assertEqual(intstmts[0].name, 'int') self.assertIs(intstmts[0], nodes.const_factory(1)._proxied) - def test_decorator_arguments_lookup(self): code = ''' def decorator(value): @@ -289,8 +288,7 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): obj = next(it) self.assertIsInstance(obj, nodes.Const) self.assertEqual(obj.value, 10) - self.assertRaises(StopIteration, partial(next, it)) - + self.assertRaises(StopIteration, functools.partial(next, it)) def test_inner_decorator_member_lookup(self): code = ''' @@ -306,8 +304,7 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): it = decname.infer() obj = next(it) self.assertIsInstance(obj, nodes.Function) - self.assertRaises(StopIteration, partial(next, it)) - + self.assertRaises(StopIteration, functools.partial(next, it)) def test_static_method_lookup(self): code = ''' @@ -327,8 +324,7 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): it = astroid['Test']['__init__'].ilookup('FileA') obj = next(it) self.assertIsInstance(obj, nodes.Class) - self.assertRaises(StopIteration, partial(next, it)) - + self.assertRaises(StopIteration, functools.partial(next, it)) def test_global_delete(self): code = ''' diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index fc11643..7e4eeae 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -20,12 +20,16 @@ import platform import sys import unittest -from astroid.manager import AstroidManager -from astroid.bases import BUILTINS -from astroid.exceptions import AstroidBuildingException +import six + +from astroid import exceptions +from astroid import manager from astroid.tests import resources +BUILTINS = six.moves.builtins.__name__ + + def _get_file_from_object(obj): if platform.python_implementation() == 'Jython': return obj.__file__.split("$py.class")[0] + ".py" @@ -41,7 +45,7 @@ class AstroidManagerTest(resources.SysPathSetup, def setUp(self): super(AstroidManagerTest, self).setUp() - self.manager = AstroidManager() + self.manager = manager.AstroidManager() self.manager.clear_cache(self._builtins) # take care of borg def test_ast_from_file(self): @@ -64,7 +68,8 @@ class AstroidManagerTest(resources.SysPathSetup, self.assertIn('unittest', self.manager.astroid_cache) def test_ast_from_file_name_astro_builder_exception(self): - self.assertRaises(AstroidBuildingException, self.manager.ast_from_file, 'unhandledName') + self.assertRaises(exceptions.AstroidBuildingException, + self.manager.ast_from_file, 'unhandledName') def test_do_not_expose_main(self): obj = self.manager.ast_from_module_name('__main__') @@ -83,7 +88,9 @@ class AstroidManagerTest(resources.SysPathSetup, self.assertEqual(astroid.pure_python, False) def test_ast_from_module_name_astro_builder_exception(self): - self.assertRaises(AstroidBuildingException, self.manager.ast_from_module_name, 'unhandledModule') + self.assertRaises(exceptions.AstroidBuildingException, + self.manager.ast_from_module_name, + 'unhandledModule') def _test_ast_from_zip(self, archive): origpath = sys.path[:] @@ -133,7 +140,8 @@ class AstroidManagerTest(resources.SysPathSetup, def test_file_from_module_name_astro_building_exception(self): """check if the method launch a exception with a wrong module name""" - self.assertRaises(AstroidBuildingException, self.manager.file_from_module_name, 'unhandledModule', None) + self.assertRaises(exceptions.AstroidBuildingException, + self.manager.file_from_module_name, 'unhandledModule', None) def test_ast_from_module(self): astroid = self.manager.ast_from_module(unittest) @@ -171,20 +179,21 @@ class AstroidManagerTest(resources.SysPathSetup, def test_ast_from_class_attr_error(self): """give a wrong class at the ast_from_class method""" - self.assertRaises(AstroidBuildingException, self.manager.ast_from_class, None) + self.assertRaises(exceptions.AstroidBuildingException, + self.manager.ast_from_class, None) def testFailedImportHooks(self): def hook(modname): if modname == 'foo.bar': return unittest else: - raise AstroidBuildingException() + raise exceptions.AstroidBuildingException() - with self.assertRaises(AstroidBuildingException): + with self.assertRaises(exceptions.AstroidBuildingException): self.manager.ast_from_module_name('foo.bar') self.manager.register_failed_import_hook(hook) self.assertEqual(unittest, self.manager.ast_from_module_name('foo.bar')) - with self.assertRaises(AstroidBuildingException): + with self.assertRaises(exceptions.AstroidBuildingException): self.manager.ast_from_module_name('foo.bar.baz') del self.manager._failed_import_hooks[0] @@ -194,10 +203,10 @@ class BorgAstroidManagerTC(unittest.TestCase): def test_borg(self): """test that the AstroidManager is really a borg, i.e. that two different instances has same cache""" - first_manager = AstroidManager() + first_manager = manager.AstroidManager() built = first_manager.ast_from_module_name(BUILTINS) - second_manager = AstroidManager() + second_manager = manager.AstroidManager() second_built = second_manager.ast_from_module_name(BUILTINS) self.assertIs(built, second_built) diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 86e42b8..b6f676d 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -19,20 +19,22 @@ """ import os import sys -import unittest import textwrap +import unittest + +import six -from astroid.node_classes import unpack_infer -from astroid.bases import BUILTINS, InferenceContext -from astroid.exceptions import AstroidBuildingException, NotFoundError from astroid import bases from astroid import builder +from astroid import exceptions +from astroid import node_classes from astroid import nodes from astroid import test_utils from astroid.tests import resources abuilder = builder.AstroidBuilder() +BUILTINS = six.moves.builtins.__name__ class AsStringTest(resources.SysPathSetup, unittest.TestCase): @@ -195,15 +197,15 @@ class IfNodeTest(_NodeTest): self.assertEqual(len(self.astroid.body), 4) for stmt in self.astroid.body: self.assertIsInstance(stmt, nodes.If) - self.assertFalse(self.astroid.body[0].orelse) # simple If - self.assertIsInstance(self.astroid.body[1].orelse[0], nodes.Pass) # If / else - self.assertIsInstance(self.astroid.body[2].orelse[0], nodes.If) # If / elif + self.assertFalse(self.astroid.body[0].orelse) # simple If + self.assertIsInstance(self.astroid.body[1].orelse[0], nodes.Pass) # If / else + self.assertIsInstance(self.astroid.body[2].orelse[0], nodes.If) # If / elif self.assertIsInstance(self.astroid.body[3].orelse[0].orelse[0], nodes.If) def test_block_range(self): # XXX ensure expected values self.assertEqual(self.astroid.block_range(1), (0, 22)) - self.assertEqual(self.astroid.block_range(10), (0, 22)) # XXX (10, 22) ? + self.assertEqual(self.astroid.block_range(10), (0, 22)) # XXX (10, 22) ? self.assertEqual(self.astroid.body[1].block_range(5), (5, 6)) self.assertEqual(self.astroid.body[1].block_range(6), (6, 6)) self.assertEqual(self.astroid.body[1].orelse[0].block_range(7), (7, 8)) @@ -270,6 +272,7 @@ class TryExceptFinallyNodeTest(_NodeTest): self.assertEqual(self.astroid.body[0].block_range(6), (6, 6)) +@unittest.skipIf(six.PY3, "Python 2 specific test.") class TryExcept2xNodeTest(_NodeTest): CODE = """ try: @@ -277,9 +280,9 @@ class TryExcept2xNodeTest(_NodeTest): except AttributeError, (retval, desc): pass """ + + def test_tuple_attribute(self): - if sys.version_info >= (3, 0): - self.skipTest('syntax removed from py3.x') handler = self.astroid.body[0].handlers[0] self.assertIsInstance(handler.name, nodes.Tuple) @@ -317,13 +320,13 @@ class ImportNodeTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(from_.real_name('pb'), 'ProgressBar') imp_ = self.module['os'] self.assertEqual(imp_.real_name('os'), 'os') - self.assertRaises(NotFoundError, imp_.real_name, 'os.path') + self.assertRaises(exceptions.NotFoundError, imp_.real_name, 'os.path') imp_ = self.module['pb'] self.assertEqual(imp_.real_name('pb'), 'ProgressBar') - self.assertRaises(NotFoundError, imp_.real_name, 'ProgressBar') + self.assertRaises(exceptions.NotFoundError, imp_.real_name, 'ProgressBar') imp_ = self.module2['YO'] self.assertEqual(imp_.real_name('YO'), 'YO') - self.assertRaises(NotFoundError, imp_.real_name, 'data') + self.assertRaises(exceptions.NotFoundError, imp_.real_name, 'data') def test_as_string(self): ast = self.module['modutils'] @@ -362,7 +365,7 @@ from ..cave import wine\n\n""" astroid = builder.parse(code) handler_type = astroid.body[1].handlers[0].type - excs = list(unpack_infer(handler_type)) + excs = list(node_classes.unpack_infer(handler_type)) # The number of returned object can differ on Python 2 # and Python 3. In one version, an additional item will # be returned, from the _pickle module, which is not @@ -373,7 +376,7 @@ from ..cave import wine\n\n""" def test_absolute_import(self): astroid = resources.build_file('data/absimport.py') - ctx = InferenceContext() + ctx = bases.InferenceContext() # will fail if absolute import failed ctx.lookupname = 'message' next(astroid['message'].infer(ctx)) @@ -434,7 +437,7 @@ class NameNodeTest(unittest.TestCase): del True """ if sys.version_info >= (3, 0): - with self.assertRaises(AstroidBuildingException): + with self.assertRaises(exceptions.AstroidBuildingException): builder.parse(code) else: ast = builder.parse(code) @@ -488,7 +491,7 @@ class UnboundMethodNodeTest(unittest.TestCase): meth = A.test ''') node = next(ast['meth'].infer()) - with self.assertRaises(NotFoundError): + with self.assertRaises(exceptions.NotFoundError): node.getattr('__missssing__') name = node.getattr('__name__')[0] self.assertIsInstance(name, nodes.Const) |