diff options
-rw-r--r-- | as_string.py | 8 | ||||
-rw-r--r-- | test/data/module2.py | 127 | ||||
-rw-r--r-- | test/unittest_nodes.py | 7 |
3 files changed, 79 insertions, 63 deletions
diff --git a/as_string.py b/as_string.py index 3dd17dd..4259274 100644 --- a/as_string.py +++ b/as_string.py @@ -113,12 +113,12 @@ class AsStringVisitor(ASTVisitor): def visit_callfunc(self, node): """return an astng.CallFunc node as string""" expr_str = node.func.accept(self) - args = ', '.join([arg.accept(self) for arg in node.args]) + args = [arg.accept(self) for arg in node.args] if node.starargs: - args += ', *%s' % node.starargs.accept(self) + args.append( '*' + node.starargs.accept(self)) if node.kwargs: - args += ', **%s' % node.kwargs.accept(self) - return '%s(%s)' % (expr_str, args) + args.append( '**' + node.kwargs.accept(self)) + return '%s(%s)' % (expr_str, ', '.join(args)) def visit_class(self, node): """return an astng.Class node as string""" diff --git a/test/data/module2.py b/test/data/module2.py index 5da2502..1ff08db 100644 --- a/test/data/module2.py +++ b/test/data/module2.py @@ -1,111 +1,124 @@ -# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# copyright 2003-2010 Sylvain Thenault, all rights reserved. -# contact mailto:thenault@gmail.com -# -# This file is part of logilab-astng. -# -# logilab-astng is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# logilab-astng is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with logilab-astng. If not, see <http://www.gnu.org/licenses/>. from __future__ import generators - from data.module import YO, YOUPI import data -class Specialization(YOUPI, YO): pass -class Metaclass(type): pass +class Specialization(YOUPI, YO): + pass -class Interface: pass -class MyIFace(Interface): pass -class AnotherIFace(Interface): pass +class Metaclass(type): + pass -class MyException(Exception): pass -class MyError(MyException): pass -class AbstractClass(object): +class Interface: + pass + + + +class MyIFace(Interface): + pass + + + +class AnotherIFace(Interface): + pass + + + +class MyException(Exception): + pass + + + +class MyError(MyException): + pass + + + +class AbstractClass(object): + def to_override(self, whatever): raise NotImplementedError() - + def return_something(self, param): if param: return 'toto' return - + + + class Concrete0: __implements__ = MyIFace + + + class Concrete1: - __implements__ = MyIFace, AnotherIFace + __implements__ = (MyIFace, AnotherIFace) + + + class Concrete2: - __implements__ = (MyIFace, - AnotherIFace) -class Concrete23(Concrete1): pass + __implements__ = (MyIFace, AnotherIFace) -del YO.member + +class Concrete23(Concrete1): + pass + +del YO.member del YO -[SYN1, SYN2] = Concrete0, Concrete1 +[SYN1, SYN2] = (Concrete0, Concrete1) assert `1` -b = 1 | 2 & 3 ^ 8 -bb = 1 | two | 6 -ccc = one & two & three -dddd = x ^ o ^ r +b = (1) | (((2) & (3)) ^ (8)) +bb = ((1) | (two)) | (6) +ccc = ((one) & (two)) & (three) +dddd = ((x) ^ (o)) ^ (r) exec 'c = 3' exec 'c = 3' in {}, {} def raise_string(a=2, *args, **kwargs): raise Exception, 'yo' yield 'coucou' - -a = b + 2 -c = b * 2 -c = b / 2 -c = b // 2 -c = b - 2 -c = b % 2 -c = b ** 2 -c = b << 2 -c = b >> 2 +a = (b) + (2) +c = (b) * (2) +c = (b) / (2) +c = (b) // (2) +c = (b) - (2) +c = (b) % (2) +c = (b) ** (2) +c = (b) << (2) +c = (b) >> (2) c = ~b - c = not b - d = [c] e = d[:] e = d[a:b:c] - raise_string(*args, **kwargs) - print >> stream, 'bonjour' print >> stream, 'salut', - def make_class(any, base=data.module.YO, *args, **kwargs): """check base is correctly resolved to Concrete0""" + + class Aaaa(base): """dynamic class""" + + return Aaaa - from os.path import abspath - import os as myos class A: pass + + class A(A): pass + + diff --git a/test/unittest_nodes.py b/test/unittest_nodes.py index 7a6ad6c..d4ae958 100644 --- a/test/unittest_nodes.py +++ b/test/unittest_nodes.py @@ -223,14 +223,17 @@ class ImportNodeTC(testlib.TestCase): self.assertEqual(as_string(ast), "from logilab.common.shellutils import Execute as spawn") ast = MODULE['os'] self.assertEqual(as_string(ast), "import os.path") + ast = abuilder.string_build( 'raise_string(*args, **kwargs)').body[0] + self.assertEqual(as_string(ast), 'raise_string(*args, **kwargs)') def test_module_as_string(self): """check as_string on a whole module prepared to be returned identically """ data = open(join(DATA, 'module.py')).read() self.assertMultiLineEqual(as_string(MODULE), data) - self.assert_(as_string(MODULE2)) - + data = open(join(DATA, 'module2.py')).read() + self.assertMultiLineEqual(as_string(MODULE2), data) + class CmpNodeTC(testlib.TestCase): def test_as_string(self): |