diff options
| author | Sylvain Thénault <thenault@gmail.com> | 2013-11-05 07:10:46 +0100 |
|---|---|---|
| committer | Sylvain Thénault <thenault@gmail.com> | 2013-11-05 07:10:46 +0100 |
| commit | eb477e693f4e8eba11e7bf9180e4419ad2f9c5df (patch) | |
| tree | 6438cf73155559c07a6dc4fc4dfe8c258f46b4b0 | |
| parent | 539f8f38ca28f870986c4ce96fe02e0f27f23212 (diff) | |
| parent | b3e292683da4d30ec1cb77ae02ddb610df82aadd (diff) | |
| download | astroid-git-eb477e693f4e8eba11e7bf9180e4419ad2f9c5df.tar.gz | |
Merged in AndroWiiid/astroid/dev (pull request #12)
Manager
| -rw-r--r-- | test/unittest_manager.py | 127 | ||||
| -rw-r--r-- | test/unittest_raw_building.py | 43 |
2 files changed, 150 insertions, 20 deletions
diff --git a/test/unittest_manager.py b/test/unittest_manager.py index 5711a23e..9c372b5a 100644 --- a/test/unittest_manager.py +++ b/test/unittest_manager.py @@ -21,6 +21,7 @@ import sys from os.path import join, abspath, dirname from astroid.manager import AstroidManager, _silent_no_wrap from astroid.bases import BUILTINS +from astroid.exceptions import AstroidBuildingException DATA = join(dirname(abspath(__file__)), 'data') @@ -29,23 +30,56 @@ class AstroidManagerTC(TestCase): self.manager = AstroidManager() self.manager.astroid_cache.clear() - def test_ast_from_module(self): + def test_ast_from_file(self): + """check if the method return a good astroid object""" import unittest - astroid = self.manager.ast_from_module(unittest) - self.assertEqual(astroid.pure_python, True) - import time - astroid = self.manager.ast_from_module(time) - self.assertEqual(astroid.pure_python, False) + filepath = unittest.__file__ + astroid = self.manager.ast_from_file(filepath) + self.assertEqual(astroid.name, 'unittest') + self.assertIn('unittest', self.manager.astroid_cache) - def test_ast_from_class(self): - astroid = self.manager.ast_from_class(int) - self.assertEqual(astroid.name, 'int') - self.assertEqual(astroid.parent.frame().name, BUILTINS) + def test_ast_from_file_cache(self): + """check if the cache works""" + import unittest + filepath = unittest.__file__ + self.manager.ast_from_file(filepath) + astroid = self.manager.ast_from_file('unhandledName', 'unittest') + self.assertEqual(astroid.name, 'unittest') + self.assertIn('unittest', self.manager.astroid_cache) + + def test_ast_from_file_astro_builder(self): + """check if the source is at True, AstroidBuilder build a good astroid""" + import unittest + filepath = unittest.__file__ + astroid = self.manager.ast_from_file(filepath, None, True, True) + self.assertEqual(astroid.name, 'unittest') + self.assertIn('unittest', self.manager.astroid_cache) - astroid = self.manager.ast_from_class(object) - self.assertEqual(astroid.name, 'object') - self.assertEqual(astroid.parent.frame().name, BUILTINS) - self.assertIn('__setattr__', astroid) + def test_ast_from_file_name_astro_builder_exception(self): + """check if an exception is thrown if we give a wrong filepath""" + self.assertRaises(AstroidBuildingException, self.manager.ast_from_file, 'unhandledName') + + def test_do_not_expose_main(self): + obj = self.manager.ast_from_module_name('__main__') + self.assertEqual(obj.name, '__main__') + self.assertEqual(obj.items(), []) + + def test_ast_from_module_name(self): + """check if the ast_from_module_name method return a good astroid""" + astroid = self.manager.ast_from_module_name('unittest') + self.assertEqual(astroid.name, 'unittest') + self.assertIn('unittest', self.manager.astroid_cache) + + def test_ast_from_module_name_not_python_source(self): + """check if the ast_from_module_name method return a good astroid with a no python source module""" + astroid = self.manager.ast_from_module_name('time') + self.assertEqual(astroid.name, 'time') + self.assertIn('time', self.manager.astroid_cache) + self.assertEqual(astroid.pure_python, False) + + def test_ast_from_module_name_astro_builder_exception(self): + """check if the method raise an exception if we give a wrong module""" + self.assertRaises(AstroidBuildingException, self.manager.ast_from_module_name, 'unhandledModule') def _test_ast_from_zip(self, archive): origpath = sys.path[:] @@ -72,6 +106,65 @@ class AstroidManagerTC(TestCase): def test_ast_from_module_name_zip(self): self._test_ast_from_zip('MyPyPa-0.1.0-py2.5.zip') + def test_zip_import_data(self): + """check if zip_import_data works""" + filepath = self.datapath('MyPyPa-0.1.0-py2.5.zip/mypypa') + astroid = self.manager.zip_import_data(filepath) + self.assertEqual(astroid.name, 'mypypa') + + def test_zip_import_data_without_zipimport(self): + """check if zip_import_data return None without zipimport""" + self.assertEqual(self.manager.zip_import_data('path'), None) + + def test_file_from_module(self): + """check if the unittest filepath is equals to the result of the method""" + import unittest + self.assertEqual(unittest.__file__[:-1], self.manager.file_from_module_name('unittest', None)) + + 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) + + def test_ast_from_module(self): + import unittest + astroid = self.manager.ast_from_module(unittest) + self.assertEqual(astroid.pure_python, True) + import time + astroid = self.manager.ast_from_module(time) + self.assertEqual(astroid.pure_python, False) + + def test_ast_from_module_cache(self): + """check if the module is in the cache manager""" + import unittest + astroid = self.manager.ast_from_module(unittest) + self.assertEqual(astroid.name, 'unittest') + self.assertIn('unittest', self.manager.astroid_cache) + + def test_ast_from_class(self): + astroid = self.manager.ast_from_class(int) + self.assertEqual(astroid.name, 'int') + self.assertEqual(astroid.parent.frame().name, BUILTINS) + + astroid = self.manager.ast_from_class(object) + self.assertEqual(astroid.name, 'object') + self.assertEqual(astroid.parent.frame().name, BUILTINS) + self.assertIn('__setattr__', astroid) + + def test_ast_from_class_with_module(self): + """check if the method works with the module name""" + astroid = self.manager.ast_from_class(int, int.__module__) + self.assertEqual(astroid.name, 'int') + self.assertEqual(astroid.parent.frame().name, BUILTINS) + + astroid = self.manager.ast_from_class(object, object.__module__) + self.assertEqual(astroid.name, 'object') + self.assertEqual(astroid.parent.frame().name, BUILTINS) + self.assertIn('__setattr__', astroid) + + 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) + def test_from_directory(self): obj = self.manager.project_from_files([DATA], _silent_no_wrap, 'data') self.assertEqual(obj.name, 'data') @@ -88,12 +181,6 @@ class AstroidManagerTC(TestCase): 'data.nonregr', 'data.notall'] self.assertListEqual(sorted(k for k in obj.keys()), expected) - def test_do_not_expose_main(self): - obj = self.manager.ast_from_module_name('__main__') - self.assertEqual(obj.name, '__main__') - self.assertEqual(obj.items(), []) - - class BorgAstroidManagerTC(TestCase): def test_borg(self): diff --git a/test/unittest_raw_building.py b/test/unittest_raw_building.py new file mode 100644 index 00000000..414d8bed --- /dev/null +++ b/test/unittest_raw_building.py @@ -0,0 +1,43 @@ +from logilab.common.testlib import TestCase, unittest_main +from astroid.raw_building import (attach_dummy_node, build_module, build_class, build_function, build_from_import) + +class RawBuildingTC(TestCase): + + def test_attach_dummy_node(self): + node = build_module('MyModule') + dummy = attach_dummy_node(node, 'DummyNode') + self.assertEqual(1, len(list(node.get_children()))) + + def test_build_module(self): + node = build_module('MyModule') + self.assertEqual(node.name, 'MyModule') + self.assertEqual(node.pure_python, False) + self.assertEqual(node.package, False) + self.assertEqual(node.parent, None) + + def test_build_class(self): + node = build_class('MyClass') + self.assertEqual(node.name, 'MyClass') + self.assertEqual(node.doc, None) + + def test_build_function(self): + node = build_function('MyFunction') + self.assertEqual(node.name, 'MyFunction') + self.assertEqual(node.doc, None) + + def test_build_function_args(self): + args = ['myArgs1', 'myArgs2'] + node = build_function('MyFunction', args) + self.assertEqual('myArgs1', node.args.args[0].name) + self.assertEqual('myArgs2', node.args.args[1].name) + self.assertEqual(2, len(node.args.args)) + + def test_build_function_defaults(self): + defaults = [ 'defaults1', 'defaults2' ] + node = build_function('MyFunction', None, defaults) + self.assertEqual(2, len(node.args.defaults)) + + def test_build_from_import(self): + names = ['exceptions, inference, inspector'] + node = build_from_import('astroid', names) + self.assertEqual(len(names), len(node.names))
\ No newline at end of file |
