PageRenderTime 59ms CodeModel.GetById 3ms app.highlight 49ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/lib2to3/tests/test_util.py

http://unladen-swallow.googlecode.com/
Python | 559 lines | 504 code | 41 blank | 14 comment | 22 complexity | 34b1ab2a57dada2c15c0b151b66ad03f MD5 | raw file
  1#!/usr/bin/env python2.5
  2""" Test suite for the code in fixes.util """
  3# Author: Collin Winter
  4
  5# Testing imports
  6from . import support
  7
  8# Python imports
  9import os.path
 10
 11# Local imports
 12from .. import pytree
 13from .. import fixer_util
 14from ..fixer_util import Attr, Name
 15
 16
 17def parse(code, strip_levels=0):
 18    # The topmost node is file_input, which we don't care about.
 19    # The next-topmost node is a *_stmt node, which we also don't care about
 20    tree = support.parse_string(code)
 21    for i in range(strip_levels):
 22        tree = tree.children[0]
 23    tree.parent = None
 24    return tree
 25
 26class MacroTestCase(support.TestCase):
 27    def assertStr(self, node, string):
 28        if isinstance(node, (tuple, list)):
 29            node = pytree.Node(fixer_util.syms.simple_stmt, node)
 30        self.assertEqual(str(node), string)
 31
 32
 33class Test_is_tuple(support.TestCase):
 34    def is_tuple(self, string):
 35        return fixer_util.is_tuple(parse(string, strip_levels=2))
 36
 37    def test_valid(self):
 38        self.failUnless(self.is_tuple("(a, b)"))
 39        self.failUnless(self.is_tuple("(a, (b, c))"))
 40        self.failUnless(self.is_tuple("((a, (b, c)),)"))
 41        self.failUnless(self.is_tuple("(a,)"))
 42        self.failUnless(self.is_tuple("()"))
 43
 44    def test_invalid(self):
 45        self.failIf(self.is_tuple("(a)"))
 46        self.failIf(self.is_tuple("('foo') % (b, c)"))
 47
 48
 49class Test_is_list(support.TestCase):
 50    def is_list(self, string):
 51        return fixer_util.is_list(parse(string, strip_levels=2))
 52
 53    def test_valid(self):
 54        self.failUnless(self.is_list("[]"))
 55        self.failUnless(self.is_list("[a]"))
 56        self.failUnless(self.is_list("[a, b]"))
 57        self.failUnless(self.is_list("[a, [b, c]]"))
 58        self.failUnless(self.is_list("[[a, [b, c]],]"))
 59
 60    def test_invalid(self):
 61        self.failIf(self.is_list("[]+[]"))
 62
 63
 64class Test_Attr(MacroTestCase):
 65    def test(self):
 66        call = parse("foo()", strip_levels=2)
 67
 68        self.assertStr(Attr(Name("a"), Name("b")), "a.b")
 69        self.assertStr(Attr(call, Name("b")), "foo().b")
 70
 71    def test_returns(self):
 72        attr = Attr(Name("a"), Name("b"))
 73        self.assertEqual(type(attr), list)
 74
 75
 76class Test_Name(MacroTestCase):
 77    def test(self):
 78        self.assertStr(Name("a"), "a")
 79        self.assertStr(Name("foo.foo().bar"), "foo.foo().bar")
 80        self.assertStr(Name("a", prefix="b"), "ba")
 81
 82
 83class Test_does_tree_import(support.TestCase):
 84    def _find_bind_rec(self, name, node):
 85        # Search a tree for a binding -- used to find the starting
 86        # point for these tests.
 87        c = fixer_util.find_binding(name, node)
 88        if c: return c
 89        for child in node.children:
 90            c = self._find_bind_rec(name, child)
 91            if c: return c
 92
 93    def does_tree_import(self, package, name, string):
 94        node = parse(string)
 95        # Find the binding of start -- that's what we'll go from
 96        node = self._find_bind_rec('start', node)
 97        return fixer_util.does_tree_import(package, name, node)
 98
 99    def try_with(self, string):
100        failing_tests = (("a", "a", "from a import b"),
101                         ("a.d", "a", "from a.d import b"),
102                         ("d.a", "a", "from d.a import b"),
103                         (None, "a", "import b"),
104                         (None, "a", "import b, c, d"))
105        for package, name, import_ in failing_tests:
106            n = self.does_tree_import(package, name, import_ + "\n" + string)
107            self.failIf(n)
108            n = self.does_tree_import(package, name, string + "\n" + import_)
109            self.failIf(n)
110
111        passing_tests = (("a", "a", "from a import a"),
112                         ("x", "a", "from x import a"),
113                         ("x", "a", "from x import b, c, a, d"),
114                         ("x.b", "a", "from x.b import a"),
115                         ("x.b", "a", "from x.b import b, c, a, d"),
116                         (None, "a", "import a"),
117                         (None, "a", "import b, c, a, d"))
118        for package, name, import_ in passing_tests:
119            n = self.does_tree_import(package, name, import_ + "\n" + string)
120            self.failUnless(n)
121            n = self.does_tree_import(package, name, string + "\n" + import_)
122            self.failUnless(n)
123
124    def test_in_function(self):
125        self.try_with("def foo():\n\tbar.baz()\n\tstart=3")
126
127class Test_find_binding(support.TestCase):
128    def find_binding(self, name, string, package=None):
129        return fixer_util.find_binding(name, parse(string), package)
130
131    def test_simple_assignment(self):
132        self.failUnless(self.find_binding("a", "a = b"))
133        self.failUnless(self.find_binding("a", "a = [b, c, d]"))
134        self.failUnless(self.find_binding("a", "a = foo()"))
135        self.failUnless(self.find_binding("a", "a = foo().foo.foo[6][foo]"))
136        self.failIf(self.find_binding("a", "foo = a"))
137        self.failIf(self.find_binding("a", "foo = (a, b, c)"))
138
139    def test_tuple_assignment(self):
140        self.failUnless(self.find_binding("a", "(a,) = b"))
141        self.failUnless(self.find_binding("a", "(a, b, c) = [b, c, d]"))
142        self.failUnless(self.find_binding("a", "(c, (d, a), b) = foo()"))
143        self.failUnless(self.find_binding("a", "(a, b) = foo().foo[6][foo]"))
144        self.failIf(self.find_binding("a", "(foo, b) = (b, a)"))
145        self.failIf(self.find_binding("a", "(foo, (b, c)) = (a, b, c)"))
146
147    def test_list_assignment(self):
148        self.failUnless(self.find_binding("a", "[a] = b"))
149        self.failUnless(self.find_binding("a", "[a, b, c] = [b, c, d]"))
150        self.failUnless(self.find_binding("a", "[c, [d, a], b] = foo()"))
151        self.failUnless(self.find_binding("a", "[a, b] = foo().foo[a][foo]"))
152        self.failIf(self.find_binding("a", "[foo, b] = (b, a)"))
153        self.failIf(self.find_binding("a", "[foo, [b, c]] = (a, b, c)"))
154
155    def test_invalid_assignments(self):
156        self.failIf(self.find_binding("a", "foo.a = 5"))
157        self.failIf(self.find_binding("a", "foo[a] = 5"))
158        self.failIf(self.find_binding("a", "foo(a) = 5"))
159        self.failIf(self.find_binding("a", "foo(a, b) = 5"))
160
161    def test_simple_import(self):
162        self.failUnless(self.find_binding("a", "import a"))
163        self.failUnless(self.find_binding("a", "import b, c, a, d"))
164        self.failIf(self.find_binding("a", "import b"))
165        self.failIf(self.find_binding("a", "import b, c, d"))
166
167    def test_from_import(self):
168        self.failUnless(self.find_binding("a", "from x import a"))
169        self.failUnless(self.find_binding("a", "from a import a"))
170        self.failUnless(self.find_binding("a", "from x import b, c, a, d"))
171        self.failUnless(self.find_binding("a", "from x.b import a"))
172        self.failUnless(self.find_binding("a", "from x.b import b, c, a, d"))
173        self.failIf(self.find_binding("a", "from a import b"))
174        self.failIf(self.find_binding("a", "from a.d import b"))
175        self.failIf(self.find_binding("a", "from d.a import b"))
176
177    def test_import_as(self):
178        self.failUnless(self.find_binding("a", "import b as a"))
179        self.failUnless(self.find_binding("a", "import b as a, c, a as f, d"))
180        self.failIf(self.find_binding("a", "import a as f"))
181        self.failIf(self.find_binding("a", "import b, c as f, d as e"))
182
183    def test_from_import_as(self):
184        self.failUnless(self.find_binding("a", "from x import b as a"))
185        self.failUnless(self.find_binding("a", "from x import g as a, d as b"))
186        self.failUnless(self.find_binding("a", "from x.b import t as a"))
187        self.failUnless(self.find_binding("a", "from x.b import g as a, d"))
188        self.failIf(self.find_binding("a", "from a import b as t"))
189        self.failIf(self.find_binding("a", "from a.d import b as t"))
190        self.failIf(self.find_binding("a", "from d.a import b as t"))
191
192    def test_simple_import_with_package(self):
193        self.failUnless(self.find_binding("b", "import b"))
194        self.failUnless(self.find_binding("b", "import b, c, d"))
195        self.failIf(self.find_binding("b", "import b", "b"))
196        self.failIf(self.find_binding("b", "import b, c, d", "c"))
197
198    def test_from_import_with_package(self):
199        self.failUnless(self.find_binding("a", "from x import a", "x"))
200        self.failUnless(self.find_binding("a", "from a import a", "a"))
201        self.failUnless(self.find_binding("a", "from x import *", "x"))
202        self.failUnless(self.find_binding("a", "from x import b, c, a, d", "x"))
203        self.failUnless(self.find_binding("a", "from x.b import a", "x.b"))
204        self.failUnless(self.find_binding("a", "from x.b import *", "x.b"))
205        self.failUnless(self.find_binding("a", "from x.b import b, c, a, d", "x.b"))
206        self.failIf(self.find_binding("a", "from a import b", "a"))
207        self.failIf(self.find_binding("a", "from a.d import b", "a.d"))
208        self.failIf(self.find_binding("a", "from d.a import b", "a.d"))
209        self.failIf(self.find_binding("a", "from x.y import *", "a.b"))
210
211    def test_import_as_with_package(self):
212        self.failIf(self.find_binding("a", "import b.c as a", "b.c"))
213        self.failIf(self.find_binding("a", "import a as f", "f"))
214        self.failIf(self.find_binding("a", "import a as f", "a"))
215
216    def test_from_import_as_with_package(self):
217        # Because it would take a lot of special-case code in the fixers
218        # to deal with from foo import bar as baz, we'll simply always
219        # fail if there is an "from ... import ... as ..."
220        self.failIf(self.find_binding("a", "from x import b as a", "x"))
221        self.failIf(self.find_binding("a", "from x import g as a, d as b", "x"))
222        self.failIf(self.find_binding("a", "from x.b import t as a", "x.b"))
223        self.failIf(self.find_binding("a", "from x.b import g as a, d", "x.b"))
224        self.failIf(self.find_binding("a", "from a import b as t", "a"))
225        self.failIf(self.find_binding("a", "from a import b as t", "b"))
226        self.failIf(self.find_binding("a", "from a import b as t", "t"))
227
228    def test_function_def(self):
229        self.failUnless(self.find_binding("a", "def a(): pass"))
230        self.failUnless(self.find_binding("a", "def a(b, c, d): pass"))
231        self.failUnless(self.find_binding("a", "def a(): b = 7"))
232        self.failIf(self.find_binding("a", "def d(b, (c, a), e): pass"))
233        self.failIf(self.find_binding("a", "def d(a=7): pass"))
234        self.failIf(self.find_binding("a", "def d(a): pass"))
235        self.failIf(self.find_binding("a", "def d(): a = 7"))
236
237        s = """
238            def d():
239                def a():
240                    pass"""
241        self.failIf(self.find_binding("a", s))
242
243    def test_class_def(self):
244        self.failUnless(self.find_binding("a", "class a: pass"))
245        self.failUnless(self.find_binding("a", "class a(): pass"))
246        self.failUnless(self.find_binding("a", "class a(b): pass"))
247        self.failUnless(self.find_binding("a", "class a(b, c=8): pass"))
248        self.failIf(self.find_binding("a", "class d: pass"))
249        self.failIf(self.find_binding("a", "class d(a): pass"))
250        self.failIf(self.find_binding("a", "class d(b, a=7): pass"))
251        self.failIf(self.find_binding("a", "class d(b, *a): pass"))
252        self.failIf(self.find_binding("a", "class d(b, **a): pass"))
253        self.failIf(self.find_binding("a", "class d: a = 7"))
254
255        s = """
256            class d():
257                class a():
258                    pass"""
259        self.failIf(self.find_binding("a", s))
260
261    def test_for(self):
262        self.failUnless(self.find_binding("a", "for a in r: pass"))
263        self.failUnless(self.find_binding("a", "for a, b in r: pass"))
264        self.failUnless(self.find_binding("a", "for (a, b) in r: pass"))
265        self.failUnless(self.find_binding("a", "for c, (a,) in r: pass"))
266        self.failUnless(self.find_binding("a", "for c, (a, b) in r: pass"))
267        self.failUnless(self.find_binding("a", "for c in r: a = c"))
268        self.failIf(self.find_binding("a", "for c in a: pass"))
269
270    def test_for_nested(self):
271        s = """
272            for b in r:
273                for a in b:
274                    pass"""
275        self.failUnless(self.find_binding("a", s))
276
277        s = """
278            for b in r:
279                for a, c in b:
280                    pass"""
281        self.failUnless(self.find_binding("a", s))
282
283        s = """
284            for b in r:
285                for (a, c) in b:
286                    pass"""
287        self.failUnless(self.find_binding("a", s))
288
289        s = """
290            for b in r:
291                for (a,) in b:
292                    pass"""
293        self.failUnless(self.find_binding("a", s))
294
295        s = """
296            for b in r:
297                for c, (a, d) in b:
298                    pass"""
299        self.failUnless(self.find_binding("a", s))
300
301        s = """
302            for b in r:
303                for c in b:
304                    a = 7"""
305        self.failUnless(self.find_binding("a", s))
306
307        s = """
308            for b in r:
309                for c in b:
310                    d = a"""
311        self.failIf(self.find_binding("a", s))
312
313        s = """
314            for b in r:
315                for c in a:
316                    d = 7"""
317        self.failIf(self.find_binding("a", s))
318
319    def test_if(self):
320        self.failUnless(self.find_binding("a", "if b in r: a = c"))
321        self.failIf(self.find_binding("a", "if a in r: d = e"))
322
323    def test_if_nested(self):
324        s = """
325            if b in r:
326                if c in d:
327                    a = c"""
328        self.failUnless(self.find_binding("a", s))
329
330        s = """
331            if b in r:
332                if c in d:
333                    c = a"""
334        self.failIf(self.find_binding("a", s))
335
336    def test_while(self):
337        self.failUnless(self.find_binding("a", "while b in r: a = c"))
338        self.failIf(self.find_binding("a", "while a in r: d = e"))
339
340    def test_while_nested(self):
341        s = """
342            while b in r:
343                while c in d:
344                    a = c"""
345        self.failUnless(self.find_binding("a", s))
346
347        s = """
348            while b in r:
349                while c in d:
350                    c = a"""
351        self.failIf(self.find_binding("a", s))
352
353    def test_try_except(self):
354        s = """
355            try:
356                a = 6
357            except:
358                b = 8"""
359        self.failUnless(self.find_binding("a", s))
360
361        s = """
362            try:
363                b = 8
364            except:
365                a = 6"""
366        self.failUnless(self.find_binding("a", s))
367
368        s = """
369            try:
370                b = 8
371            except KeyError:
372                pass
373            except:
374                a = 6"""
375        self.failUnless(self.find_binding("a", s))
376
377        s = """
378            try:
379                b = 8
380            except:
381                b = 6"""
382        self.failIf(self.find_binding("a", s))
383
384    def test_try_except_nested(self):
385        s = """
386            try:
387                try:
388                    a = 6
389                except:
390                    pass
391            except:
392                b = 8"""
393        self.failUnless(self.find_binding("a", s))
394
395        s = """
396            try:
397                b = 8
398            except:
399                try:
400                    a = 6
401                except:
402                    pass"""
403        self.failUnless(self.find_binding("a", s))
404
405        s = """
406            try:
407                b = 8
408            except:
409                try:
410                    pass
411                except:
412                    a = 6"""
413        self.failUnless(self.find_binding("a", s))
414
415        s = """
416            try:
417                try:
418                    b = 8
419                except KeyError:
420                    pass
421                except:
422                    a = 6
423            except:
424                pass"""
425        self.failUnless(self.find_binding("a", s))
426
427        s = """
428            try:
429                pass
430            except:
431                try:
432                    b = 8
433                except KeyError:
434                    pass
435                except:
436                    a = 6"""
437        self.failUnless(self.find_binding("a", s))
438
439        s = """
440            try:
441                b = 8
442            except:
443                b = 6"""
444        self.failIf(self.find_binding("a", s))
445
446        s = """
447            try:
448                try:
449                    b = 8
450                except:
451                    c = d
452            except:
453                try:
454                    b = 6
455                except:
456                    t = 8
457                except:
458                    o = y"""
459        self.failIf(self.find_binding("a", s))
460
461    def test_try_except_finally(self):
462        s = """
463            try:
464                c = 6
465            except:
466                b = 8
467            finally:
468                a = 9"""
469        self.failUnless(self.find_binding("a", s))
470
471        s = """
472            try:
473                b = 8
474            finally:
475                a = 6"""
476        self.failUnless(self.find_binding("a", s))
477
478        s = """
479            try:
480                b = 8
481            finally:
482                b = 6"""
483        self.failIf(self.find_binding("a", s))
484
485        s = """
486            try:
487                b = 8
488            except:
489                b = 9
490            finally:
491                b = 6"""
492        self.failIf(self.find_binding("a", s))
493
494    def test_try_except_finally_nested(self):
495        s = """
496            try:
497                c = 6
498            except:
499                b = 8
500            finally:
501                try:
502                    a = 9
503                except:
504                    b = 9
505                finally:
506                    c = 9"""
507        self.failUnless(self.find_binding("a", s))
508
509        s = """
510            try:
511                b = 8
512            finally:
513                try:
514                    pass
515                finally:
516                    a = 6"""
517        self.failUnless(self.find_binding("a", s))
518
519        s = """
520            try:
521                b = 8
522            finally:
523                try:
524                    b = 6
525                finally:
526                    b = 7"""
527        self.failIf(self.find_binding("a", s))
528
529class Test_touch_import(support.TestCase):
530
531    def test_after_docstring(self):
532        node = parse('"""foo"""\nbar()')
533        fixer_util.touch_import(None, "foo", node)
534        self.assertEqual(str(node), '"""foo"""\nimport foo\nbar()\n\n')
535
536    def test_after_imports(self):
537        node = parse('"""foo"""\nimport bar\nbar()')
538        fixer_util.touch_import(None, "foo", node)
539        self.assertEqual(str(node), '"""foo"""\nimport bar\nimport foo\nbar()\n\n')
540
541    def test_beginning(self):
542        node = parse('bar()')
543        fixer_util.touch_import(None, "foo", node)
544        self.assertEqual(str(node), 'import foo\nbar()\n\n')
545
546    def test_from_import(self):
547        node = parse('bar()')
548        fixer_util.touch_import("cgi", "escape", node)
549        self.assertEqual(str(node), 'from cgi import escape\nbar()\n\n')
550
551    def test_name_import(self):
552        node = parse('bar()')
553        fixer_util.touch_import(None, "cgi", node)
554        self.assertEqual(str(node), 'import cgi\nbar()\n\n')
555
556
557if __name__ == "__main__":
558    import __main__
559    support.run_all_tests(__main__)