Anonymous avatar Anonymous committed 1e4bbe0

Merging changes from "clear" branch into default (manually because of a bug in
Mercurial itself - "path contains illegal component").

Comments (0)

Files changed (40)

lib3to2/tests/support.py

+"""Support code for test_*.py files"""
+import unittest
+from itertools import chain
+from lib2to3 import pygram
+from lib2to3 import refactor
+from textwrap import dedent
+
+
+def run_all_tests(test_mod=None, tests=None):
+    # From lib2to3.tests.support (copied without changes).
+    if tests is None:
+        tests = unittest.TestLoader().loadTestsFromModule(test_mod)
+    unittest.TextTestRunner(verbosity=2).run(tests)
+
+
+def reformat(string):
+    # From lib2to3.tests.support (copied without changes).
+    return dedent(string) + "\n\n"
+
+
+def get_refactorer(fixer_pkg="lib2to3", fixers=None, options=None):
+    # From lib2to3.tests.support (copied without changes).
+    """
+    A convenience function for creating a RefactoringTool for tests.
+
+    fixers is a list of fixers for the RefactoringTool to use. By default
+    "lib2to3.fixes.*" is used. options is an optional dictionary of options to
+    be passed to the RefactoringTool.
+    """
+    if fixers is not None:
+        fixers = [fixer_pkg + ".fixes.fix_" + fix for fix in fixers]
+    else:
+        fixers = refactor.get_fixers_from_package(fixer_pkg + ".fixes")
+    options = options or {}
+    return refactor.RefactoringTool(fixers, options, explicit=True)
+
+
+class FixerTestCase(unittest.TestCase):
+    # From lib2to3.tests.support (adapted with very minor changes).
+
+    # Other test cases can subclass this class and replace "fixer_pkg" with
+    # their own.
+    def setUp(self, fix_list=None, fixer_pkg="lib2to3", options=None):
+        if fix_list is None:
+            fix_list = [self.fixer]
+        self.refactor = get_refactorer(fixer_pkg, fix_list, options)
+        self.fixer_log = []
+        self.filename = "<string>"
+
+        for fixer in chain(self.refactor.pre_order,
+                           self.refactor.post_order):
+            fixer.log = self.fixer_log
+
+    def _check(self, before, after):
+        before = reformat(before)
+        after = reformat(after)
+        tree = self.refactor.refactor_string(before, self.filename)
+        self.assertEqual(after, str(tree))
+        return tree
+
+    def check(self, before, after, ignore_warnings=False):
+        tree = self._check(before, after)
+        self.assertTrue(tree.was_changed)
+        if not ignore_warnings:
+            self.assertEqual(self.fixer_log, [])
+
+    def warns(self, before, after, message, unchanged=False):
+        tree = self._check(before, after)
+        self.assertTrue(message in "".join(self.fixer_log))
+        if not unchanged:
+            self.assertTrue(tree.was_changed)
+
+    def warns_unchanged(self, before, message):
+        self.warns(before, before, message, unchanged=True)
+
+    def unchanged(self, before, ignore_warnings=False):
+        self._check(before, before)
+        if not ignore_warnings:
+            self.assertEqual(self.fixer_log, [])
+
+    def assert_runs_after(self, *names):
+        fixes = [self.fixer]
+        fixes.extend(names)
+        r = get_refactorer("lib2to3", fixes)
+        (pre, post) = r.get_fixers()
+        n = "fix_" + self.fixer
+        if post and post[-1].__class__.__module__.endswith(n):
+            # We're the last fixer to run
+            return
+        if pre and pre[-1].__class__.__module__.endswith(n) and not post:
+            # We're the last in pre and post is empty
+            return
+        self.fail("Fixer run order (%s) is incorrect; %s should be last."\
+               %(", ".join([x.__class__.__module__ for x in (pre+post)]), n))
+
+
+class lib3to2FixerTestCase(FixerTestCase):
+    # From lib3to2.tests.test_all_fixers (moved without changes).
+    def setUp(self, fix_list=None, fixer_pkg="lib3to2"):
+        super(lib3to2FixerTestCase, self).setUp(fixer_pkg=fixer_pkg)
+        self.refactor.driver.grammar = pygram.python_grammar_no_print_statement

lib3to2/tests/test_annotations.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_annotations(lib3to2FixerTestCase):
     fixer = "annotations"
         b = "def foo() -> 'bar': pass"
         a = "def foo(): pass"
         self.check(b, a, ignore_warnings=True)
-        
+
         b = """
         def foo() -> "bar":
             print "baz"
             print "what's next, again?"
         """
         self.check(b, a, ignore_warnings=True)
-        
+
     def test_single_param_annotations(self):
         b = "def foo(bar:'baz'): pass"
         a = "def foo(bar): pass"
         self.check(b, a, ignore_warnings=True)
-        
+
         b = """
         def foo(bar:"baz"="spam"):
             print "what's next, again?"
             print "whatever."
         """
         self.check(b, a, ignore_warnings=True)
-        
+
     def test_multiple_param_annotations(self):
         b = "def foo(bar:'spam'=False, baz:'eggs'=True, ham:False='spaghetti'): pass"
         a = "def foo(bar=False, baz=True, ham='spaghetti'): pass"
         self.check(b, a, ignore_warnings=True)
-        
+
         b = """
         def foo(bar:"spam"=False, baz:"eggs"=True, ham:False="spam"):
             print "this is filler, just doing a suite"
             print "suites require multiple lines."
         """
         self.check(b, a, ignore_warnings=True)
-        
+
     def test_mixed_annotations(self):
         b = "def foo(bar=False, baz:'eggs'=True, ham:False='spaghetti') -> 'zombies': pass"
         a = "def foo(bar=False, baz=True, ham='spaghetti'): pass"
         self.check(b, a, ignore_warnings=True)
-        
+
         b = """
         def foo(bar:"spam"=False, baz=True, ham:False="spam") -> 'air':
             print "this is filler, just doing a suite"
             print "suites require multiple lines."
         """
         self.check(b, a, ignore_warnings=True)
-        
+
         b = "def foo(bar) -> 'brains': pass"
         a = "def foo(bar): pass"
         self.check(b, a, ignore_warnings=True)
-        
+
     def test_unchanged(self):
         s = "def foo(): pass"
         self.unchanged(s)
-        
+
         s = """
         def foo():
             pass
             pass
         """
         self.unchanged(s)
-        
+
         s = """
         def foo(bar=baz):
             pass

lib3to2/tests/test_bitlength.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_bitlength(lib3to2FixerTestCase):
     fixer = 'bitlength'

lib3to2/tests/test_bool.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_bool(lib3to2FixerTestCase):
     fixer = "bool"

lib3to2/tests/test_bytes.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_bytes(lib3to2FixerTestCase):
     fixer = "bytes"

lib3to2/tests/test_classdecorator.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_classdecorator(lib3to2FixerTestCase):
     fixer = "classdecorator"

lib3to2/tests/test_collections.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_collections(lib3to2FixerTestCase):
     fixer = "collections"

lib3to2/tests/test_dctsetcomp.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_dctsetcomp(lib3to2FixerTestCase):
     fixer = "dctsetcomp"

lib3to2/tests/test_division.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 from itertools import count
 
 class Test_division(lib3to2FixerTestCase):

lib3to2/tests/test_except.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_except(lib3to2FixerTestCase):
     fixer = "except"

lib3to2/tests/test_fullargspec.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_fullargspec(lib3to2FixerTestCase):
 

lib3to2/tests/test_funcattrs.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_funcattrs(lib3to2FixerTestCase):
     fixer = "funcattrs"

lib3to2/tests/test_getcwd.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_getcwd(lib3to2FixerTestCase):
     fixer = "getcwd"

lib3to2/tests/test_imports.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_imports(lib3to2FixerTestCase):
     fixer = "imports"

lib3to2/tests/test_imports2.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_imports2(lib3to2FixerTestCase):
     fixer = "imports2"
         self.check(b, a)
 
     def test_name_scope_if(self):
-        
+
         b = """
         if thing:
             import http.server
         self.check(b, a)
 
     def test_name_scope_try_except(self):
-        
+
         b = """
         try:
             import http.server
         import math, string
         import urllib2, urllib
         import CGIHTTPServer, SimpleHTTPServer, BaseHTTPServer"""
-        
+
         self.check(b, a)
 
     def test_name_mutiple_imports_indented(self):
         b = "from tkinter.simpledialog import SimpleDialog"
         a = "from SimpleDialog import SimpleDialog"
         self.check(b, a)
-        
+
     def test_from_star(self):
-        
+
         b = """
         def try_import(package):
             try:
                 print('try again!')
         """
         self.check(b, a, ignore_warnings=True)
-        
+
         b = """
         def testing_http_server():
             from http.server import *

lib3to2/tests/test_input.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_input(lib3to2FixerTestCase):
     fixer = "input"

lib3to2/tests/test_int.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_int(lib3to2FixerTestCase):
     fixer = "int"

lib3to2/tests/test_intern.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_intern(lib3to2FixerTestCase):
     fixer = "intern"

lib3to2/tests/test_itertools.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_itertoools(lib3to2FixerTestCase):
     fixer = "itertools"

lib3to2/tests/test_kwargs.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_kwargs(lib3to2FixerTestCase):
     fixer = 'kwargs'
             dinosaurs = stuff['dinosaurs']; del stuff['dinosaurs']
             funky()"""
         self.check(b, a)
-        
+
     def test_bare_star_named_complicated_defaults(self):
         b = """
         def spam(ham, *, dinosaurs, eggs=call_fn(lambda a: b), monkeys=[i.split() for i in something(args)]):
             dinosaurs = stuff['dinosaurs']; del stuff['dinosaurs']
             funky()"""
         self.check(b, a)
-        
+

lib3to2/tests/test_memoryview.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_memoryview(lib3to2FixerTestCase):
     fixer = "memoryview"

lib3to2/tests/test_metaclass.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_metaclass(lib3to2FixerTestCase):
 

lib3to2/tests/test_methodattrs.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_methodattrs(lib3to2FixerTestCase):
     fixer = "methodattrs"

lib3to2/tests/test_newstyle.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_newstyle(lib3to2FixerTestCase):
     fixer = "newstyle"

lib3to2/tests/test_next.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_next(lib3to2FixerTestCase):
     fixer = "next"

lib3to2/tests/test_numliterals.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_numliterals(lib3to2FixerTestCase):
     fixer = "numliterals"

lib3to2/tests/test_open.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_open(lib3to2FixerTestCase):
     fixer = "open"

lib3to2/tests/test_print.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_print(lib3to2FixerTestCase):
     fixer = "print"

lib3to2/tests/test_printfunction.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_printfunction(lib3to2FixerTestCase):
     fixer = "printfunction"

lib3to2/tests/test_raise.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_raise(lib3to2FixerTestCase):
 

lib3to2/tests/test_range.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_range(lib3to2FixerTestCase):
     fixer = "range"

lib3to2/tests/test_reduce.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_reduce(lib3to2FixerTestCase):
     fixer = "reduce"

lib3to2/tests/test_setliteral.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_setliteral(lib3to2FixerTestCase):
     fixer = "setliteral"

lib3to2/tests/test_str.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_str(lib3to2FixerTestCase):
     fixer = "str"

lib3to2/tests/test_super.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_super(lib3to2FixerTestCase):
     fixer = "super"

lib3to2/tests/test_throw.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_throw(lib3to2FixerTestCase):
 

lib3to2/tests/test_unittest.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_unittest(lib3to2FixerTestCase):
     fixer = 'unittest'
     def test_not_base(self):
         s = "not_unittest.unittest.stuff()"
         self.unchanged(s)
-        
+

lib3to2/tests/test_unpacking.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_unpacking(lib3to2FixerTestCase):
 

lib3to2/tests/test_with.py

-from test_all_fixers import lib3to2FixerTestCase
+from lib3to2.tests.support import lib3to2FixerTestCase
 
 class Test_with(lib3to2FixerTestCase):
     fixer = "with"

test_all_fixers.py

+#!/usr/bin/env python3.1
+"""
+Runs all test cases in tests directory named test_*.py
+"""
+
+import os.path
+import os
+import importlib
+import lib3to2.tests.support as support
+
+tests_directory = 'lib3to2/tests'  # Relative path to test directory.
+
+if __name__ == "__main__":
+    for module in os.listdir(tests_directory):
+        if module.endswith('.py') and module.startswith('test_'):
+            module = os.path.join(tests_directory, module[:-3])
+            module = module.replace(os.sep, '.')
+            _module = importlib.import_module(module)
+            support.run_all_tests(_module)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.