| 1 | |
|---|
| 2 | from logilab.common.testlib import unittest_main, TestCase |
|---|
| 3 | |
|---|
| 4 | from logilab.astng import ResolveError, MANAGER as m, Instance, YES, InferenceError |
|---|
| 5 | from logilab.astng.builder import ASTNGBuilder, build_module |
|---|
| 6 | |
|---|
| 7 | import sys |
|---|
| 8 | from os.path import abspath |
|---|
| 9 | sys.path.insert(1, abspath('regrtest_data')) |
|---|
| 10 | |
|---|
| 11 | class NonRegressionTC(TestCase): |
|---|
| 12 | |
|---|
| 13 | ## def test_resolve1(self): |
|---|
| 14 | ## mod = m.astng_from_module_name('data.nonregr') |
|---|
| 15 | ## cls = mod['OptionParser'] |
|---|
| 16 | ## self.assertRaises(ResolveError, cls.resolve_dotted, cls.basenames[0]) |
|---|
| 17 | ## #self.assert_(cls is not cls.resolve_dotted(cls.basenames[0])) |
|---|
| 18 | |
|---|
| 19 | def test_module_path(self): |
|---|
| 20 | mod = m.astng_from_module_name('import_package_subpackage_module') |
|---|
| 21 | package = mod.igetattr('package').next() |
|---|
| 22 | self.failUnlessEqual(package.name, 'package') |
|---|
| 23 | subpackage = package.igetattr('subpackage').next() |
|---|
| 24 | self.failUnlessEqual(subpackage.name, 'package.subpackage') |
|---|
| 25 | module = subpackage.igetattr('module').next() |
|---|
| 26 | self.failUnlessEqual(module.name, 'package.subpackage.module') |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | def test_living_property(self): |
|---|
| 30 | builder = ASTNGBuilder() |
|---|
| 31 | builder._done = {} |
|---|
| 32 | builder._module = sys.modules[__name__] |
|---|
| 33 | builder.object_build(build_module('module_name', ''), Whatever) |
|---|
| 34 | |
|---|
| 35 | def test_new_style_class_detection(self): |
|---|
| 36 | try: |
|---|
| 37 | import pygtk |
|---|
| 38 | except ImportError: |
|---|
| 39 | self.skip('test skipped: pygtk is not available') |
|---|
| 40 | else: |
|---|
| 41 | builder = ASTNGBuilder() |
|---|
| 42 | data = """ |
|---|
| 43 | import pygtk |
|---|
| 44 | pygtk.require("2.6") |
|---|
| 45 | import gobject |
|---|
| 46 | |
|---|
| 47 | class A(gobject.GObject): |
|---|
| 48 | def __init__(self, val): |
|---|
| 49 | gobject.GObject.__init__(self) |
|---|
| 50 | self._val = val |
|---|
| 51 | |
|---|
| 52 | def _get_val(self): |
|---|
| 53 | print "get" |
|---|
| 54 | return self._val |
|---|
| 55 | |
|---|
| 56 | def _set_val(self, val): |
|---|
| 57 | print "set" |
|---|
| 58 | self._val = val |
|---|
| 59 | |
|---|
| 60 | val = property(_get_val, _set_val) |
|---|
| 61 | |
|---|
| 62 | if __name__ == "__main__": |
|---|
| 63 | print gobject.GObject.__bases__ |
|---|
| 64 | a = A(7) |
|---|
| 65 | print a.val |
|---|
| 66 | a.val = 6 |
|---|
| 67 | print a.val |
|---|
| 68 | """ |
|---|
| 69 | astng = builder.string_build(data, __name__, __file__) |
|---|
| 70 | a = astng['A'] |
|---|
| 71 | self.assert_(a.newstyle) |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | def test_pylint_config_attr(self): |
|---|
| 75 | try: |
|---|
| 76 | from pylint import lint |
|---|
| 77 | except ImportError: |
|---|
| 78 | self.skip('pylint not available') |
|---|
| 79 | mod = m.astng_from_module_name('pylint.lint') |
|---|
| 80 | pylinter = mod['PyLinter'] |
|---|
| 81 | self.assertEquals([c.name for c in pylinter.ancestors()], |
|---|
| 82 | ['OptionsManagerMixIn', 'object', 'MessagesHandlerMixIn', |
|---|
| 83 | 'ReportsHandlerMixIn', 'BaseRawChecker', 'BaseChecker', |
|---|
| 84 | 'OptionsProviderMixIn', 'ASTWalker']) |
|---|
| 85 | |
|---|
| 86 | self.assert_(list(Instance(pylinter).getattr('config'))) |
|---|
| 87 | infered = list(Instance(pylinter).igetattr('config')) |
|---|
| 88 | self.assertEquals(len(infered), 2) |
|---|
| 89 | infered = [c for c in infered if not c is YES] |
|---|
| 90 | self.assertEquals(len(infered), 1) |
|---|
| 91 | self.assertEquals(infered[0].root().name, 'optparse') |
|---|
| 92 | self.assertEquals(infered[0].name, 'Values') |
|---|
| 93 | |
|---|
| 94 | def test_numpy_crash(self): |
|---|
| 95 | try: |
|---|
| 96 | import numpy |
|---|
| 97 | except ImportError: |
|---|
| 98 | self.skip('test skipped: numpy is not available') |
|---|
| 99 | else: |
|---|
| 100 | builder = ASTNGBuilder() |
|---|
| 101 | data = """ |
|---|
| 102 | from numpy import multiply |
|---|
| 103 | |
|---|
| 104 | multiply(1, 2, 3) |
|---|
| 105 | """ |
|---|
| 106 | astng = builder.string_build(data, __name__, __file__) |
|---|
| 107 | callfunc = astng.node.nodes[1].expr |
|---|
| 108 | # well, InferenceError instead of a crash is better |
|---|
| 109 | self.assertRaises(InferenceError, list, callfunc.infer()) |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | class Whatever(object): |
|---|
| 113 | a = property(lambda x: x, lambda x: x) |
|---|
| 114 | |
|---|
| 115 | if __name__ == '__main__': |
|---|
| 116 | unittest_main() |
|---|