| 1 | # This program is free software; you can redistribute it and/or modify it under |
|---|
| 2 | # the terms of the GNU General Public License as published by the Free Software |
|---|
| 3 | # Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 4 | # version. |
|---|
| 5 | |
|---|
| 6 | # This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 7 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 8 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 9 | |
|---|
| 10 | # You should have received a copy of the GNU General Public License along with |
|---|
| 11 | # this program; if not, write to the Free Software Foundation, Inc., |
|---|
| 12 | # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 13 | """tests for specific behaviour of astng nodes |
|---|
| 14 | """ |
|---|
| 15 | |
|---|
| 16 | import unittest |
|---|
| 17 | |
|---|
| 18 | from logilab.astng import builder, nodes, NotFoundError |
|---|
| 19 | |
|---|
| 20 | from data import module as test_module |
|---|
| 21 | |
|---|
| 22 | abuilder = builder.ASTNGBuilder() |
|---|
| 23 | MODULE = abuilder.module_build(test_module) |
|---|
| 24 | MODULE2 = abuilder.file_build('data/module2.py', 'data.module2') |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | class ImportNodeTC(unittest.TestCase): |
|---|
| 28 | |
|---|
| 29 | def test_import_self_resolve(self): |
|---|
| 30 | myos = MODULE2.igetattr('myos').next() |
|---|
| 31 | self.failUnless(isinstance(myos, nodes.Module), myos) |
|---|
| 32 | self.failUnlessEqual(myos.name, 'os') |
|---|
| 33 | self.failUnlessEqual(myos.qname(), 'os') |
|---|
| 34 | self.failUnlessEqual(myos.pytype(), '__builtin__.module') |
|---|
| 35 | |
|---|
| 36 | def test_from_self_resolve(self): |
|---|
| 37 | spawn = MODULE.igetattr('spawn').next() |
|---|
| 38 | self.failUnless(isinstance(spawn, nodes.Class), spawn) |
|---|
| 39 | self.failUnlessEqual(spawn.root().name, 'logilab.common.shellutils') |
|---|
| 40 | self.failUnlessEqual(spawn.qname(), 'logilab.common.shellutils.Execute') |
|---|
| 41 | self.failUnlessEqual(spawn.pytype(), '__builtin__.classobj') |
|---|
| 42 | abspath = MODULE2.igetattr('abspath').next() |
|---|
| 43 | self.failUnless(isinstance(abspath, nodes.Function), abspath) |
|---|
| 44 | self.failUnlessEqual(abspath.root().name, 'os.path') |
|---|
| 45 | self.failUnlessEqual(abspath.qname(), 'os.path.abspath') |
|---|
| 46 | self.failUnlessEqual(abspath.pytype(), '__builtin__.function') |
|---|
| 47 | |
|---|
| 48 | def test_real_name(self): |
|---|
| 49 | from_ = MODULE['spawn'] |
|---|
| 50 | self.assertEquals(from_.real_name('spawn'), 'Execute') |
|---|
| 51 | imp_ = MODULE['os'] |
|---|
| 52 | self.assertEquals(imp_.real_name('os'), 'os') |
|---|
| 53 | self.assertRaises(NotFoundError, imp_.real_name, 'os.path') |
|---|
| 54 | imp_ = MODULE['spawn'] |
|---|
| 55 | self.assertEquals(imp_.real_name('spawn'), 'Execute') |
|---|
| 56 | self.assertRaises(NotFoundError, imp_.real_name, 'Execute') |
|---|
| 57 | imp_ = MODULE2['YO'] |
|---|
| 58 | self.assertEquals(imp_.real_name('YO'), 'YO') |
|---|
| 59 | self.assertRaises(NotFoundError, imp_.real_name, 'data') |
|---|
| 60 | |
|---|
| 61 | def test_as_string(self): |
|---|
| 62 | ast = MODULE['modutils'] |
|---|
| 63 | self.assertEquals(ast.as_string(), "from logilab.common import modutils") |
|---|
| 64 | ast = MODULE['spawn'] |
|---|
| 65 | self.assertEquals(ast.as_string(), "from logilab.common.shellutils import Execute as spawn") |
|---|
| 66 | ast = MODULE['os'] |
|---|
| 67 | self.assertEquals(ast.as_string(), "import os.path") |
|---|
| 68 | |
|---|
| 69 | class CmpNodeTC(unittest.TestCase): |
|---|
| 70 | def test_as_string(self): |
|---|
| 71 | ast = abuilder.string_build("a == 2") |
|---|
| 72 | self.assertEquals(ast.as_string(), "a == 2") |
|---|
| 73 | |
|---|
| 74 | __all__ = ('ImportNodeTC',) |
|---|
| 75 | |
|---|
| 76 | if __name__ == '__main__': |
|---|
| 77 | unittest.main() |
|---|