summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/tests/test_refactor.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/lib2to3/tests/test_refactor.py')
-rw-r--r--Lib/lib2to3/tests/test_refactor.py59
1 files changed, 24 insertions, 35 deletions
diff --git a/Lib/lib2to3/tests/test_refactor.py b/Lib/lib2to3/tests/test_refactor.py
index 9e3b8fb..c737aa5 100644
--- a/Lib/lib2to3/tests/test_refactor.py
+++ b/Lib/lib2to3/tests/test_refactor.py
@@ -2,18 +2,24 @@
Unit tests for refactor.py.
"""
+from __future__ import with_statement
+
import sys
import os
import codecs
-import io
+import operator
import re
+import StringIO
import tempfile
import shutil
import unittest
+import warnings
from lib2to3 import refactor, pygram, fixer_base
from lib2to3.pgen2 import token
+from . import support
+
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
FIXER_DIR = os.path.join(TEST_DATA_DIR, "fixers")
@@ -127,7 +133,7 @@ from __future__ import print_function"""
self.assertEqual(top_fixes, [with_head, no_head])
name_fixes = d.pop(token.NAME)
self.assertEqual(name_fixes, [simple, no_head])
- for fixes in d.values():
+ for fixes in d.itervalues():
self.assertEqual(fixes, [no_head])
def test_fixer_loading(self):
@@ -167,7 +173,7 @@ from __future__ import print_function"""
results = []
rt = MyRT(_DEFAULT_FIXERS)
save = sys.stdin
- sys.stdin = io.StringIO("def parrot(): pass\n\n")
+ sys.stdin = StringIO.StringIO("def parrot(): pass\n\n")
try:
rt.refactor_stdin()
finally:
@@ -180,42 +186,32 @@ from __future__ import print_function"""
def check_file_refactoring(self, test_file, fixers=_2TO3_FIXERS,
options=None, mock_log_debug=None,
actually_write=True):
- test_file = self.init_test_file(test_file)
- old_contents = self.read_file(test_file)
+ tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor")
+ self.addCleanup(shutil.rmtree, tmpdir)
+ # make a copy of the tested file that we can write to
+ shutil.copy(test_file, tmpdir)
+ test_file = os.path.join(tmpdir, os.path.basename(test_file))
+ os.chmod(test_file, 0o644)
+
+ def read_file():
+ with open(test_file, "rb") as fp:
+ return fp.read()
+
+ old_contents = read_file()
rt = self.rt(fixers=fixers, options=options)
if mock_log_debug:
rt.log_debug = mock_log_debug
rt.refactor_file(test_file)
- self.assertEqual(old_contents, self.read_file(test_file))
+ self.assertEqual(old_contents, read_file())
if not actually_write:
return
rt.refactor_file(test_file, True)
- new_contents = self.read_file(test_file)
+ new_contents = read_file()
self.assertNotEqual(old_contents, new_contents)
return new_contents
- def init_test_file(self, test_file):
- tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor")
- self.addCleanup(shutil.rmtree, tmpdir)
- shutil.copy(test_file, tmpdir)
- test_file = os.path.join(tmpdir, os.path.basename(test_file))
- os.chmod(test_file, 0o644)
- return test_file
-
- def read_file(self, test_file):
- with open(test_file, "rb") as fp:
- return fp.read()
-
- def refactor_file(self, test_file, fixers=_2TO3_FIXERS):
- test_file = self.init_test_file(test_file)
- old_contents = self.read_file(test_file)
- rt = self.rt(fixers=fixers)
- rt.refactor_file(test_file, True)
- new_contents = self.read_file(test_file)
- return old_contents, new_contents
-
def test_refactor_file(self):
test_file = os.path.join(FIXER_DIR, "parrot_example.py")
self.check_file_refactoring(test_file, _DEFAULT_FIXERS)
@@ -235,7 +231,7 @@ from __future__ import print_function"""
re.escape(os.sep + os.path.basename(test_file))
for message in debug_messages:
if "Not writing changes" in message:
- self.assertRegex(message, message_regex)
+ self.assertRegexpMatches(message, message_regex)
break
else:
self.fail("%r not matched in %r" % (message_regex, debug_messages))
@@ -295,13 +291,6 @@ from __future__ import print_function"""
finally:
os.linesep = old_sep
- def test_crlf_unchanged(self):
- fn = os.path.join(TEST_DATA_DIR, "crlf.py")
- old, new = self.refactor_file(fn)
- self.assertIn(b"\r\n", old)
- self.assertIn(b"\r\n", new)
- self.assertNotIn(b"\r\r\n", new)
-
def test_refactor_docstring(self):
rt = self.rt()
mples. .. method:: aifc.getframerate() Return the sampling rate (number of audio frames per second). .. method:: aifc.getnframes() Return the number of audio frames in the file. .. method:: aifc.getcomptype() Return a bytes array of length 4 describing the type of compression used in the audio file. For AIFF files, the returned value is ``b'NONE'``. .. method:: aifc.getcompname() Return a bytes array convertible to a human-readable description of the type of compression used in the audio file. For AIFF files, the returned value is ``b'not compressed'``. .. method:: aifc.getparams() Return a tuple consisting of all of the above values in the above order. .. method:: aifc.getmarkers() Return a list of markers in the audio file. A marker consists of a tuple of three elements. The first is the mark ID (an integer), the second is the mark position in frames from the beginning of the data (an integer), the third is the name of the mark (a string). .. method:: aifc.getmark(id) Return the tuple as described in :meth:`getmarkers` for the mark with the given *id*. .. method:: aifc.readframes(nframes) Read and return the next *nframes* frames from the audio file. The returned data is a string containing for each frame the uncompressed samples of all channels. .. method:: aifc.rewind() Rewind the read pointer. The next :meth:`readframes` will start from the beginning. .. method:: aifc.setpos(pos) Seek to the specified frame number. .. method:: aifc.tell() Return the current frame number. .. method:: aifc.close() Close the AIFF file. After calling this method, the object can no longer be used. Objects returned by :func:`.open` when a file is opened for writing have all the above methods, except for :meth:`readframes` and :meth:`setpos`. In addition the following methods exist. The :meth:`get\*` methods can only be called after the corresponding :meth:`set\*` methods have been called. Before the first :meth:`writeframes` or :meth:`writeframesraw`, all parameters except for the number of frames must be filled in. .. method:: aifc.aiff() Create an AIFF file. The default is that an AIFF-C file is created, unless the name of the file ends in ``'.aiff'`` in which case the default is an AIFF file. .. method:: aifc.aifc() Create an AIFF-C file. The default is that an AIFF-C file is created, unless the name of the file ends in ``'.aiff'`` in which case the default is an AIFF file. .. method:: aifc.setnchannels(nchannels) Specify the number of channels in the audio file. .. method:: aifc.setsampwidth(width) Specify the size in bytes of audio samples. .. method:: aifc.setframerate(rate) Specify the sampling frequency in frames per second. .. method:: aifc.setnframes(nframes) Specify the number of frames that are to be written to the audio file. If this parameter is not set, or not set correctly, the file needs to support seeking. .. method:: aifc.setcomptype(type, name) .. index:: single: u-LAW single: A-LAW single: G.722 Specify the compression type. If not specified, the audio data will not be compressed. In AIFF files, compression is not possible. The name parameter should be a human-readable description of the compression type as a bytes array, the type parameter should be a bytes array of length 4. Currently the following compression types are supported: ``b'NONE'``, ``b'ULAW'``, ``b'ALAW'``, ``b'G722'``. .. method:: aifc.setparams(nchannels, sampwidth, framerate, comptype, compname) Set all the above parameters at once. The argument is a tuple consisting of the various parameters. This means that it is possible to use the result of a :meth:`getparams` call as argument to :meth:`setparams`. .. method:: aifc.setmark(id, pos, name) Add a mark with the given id (larger than 0), and the given name at the given position. This method can be called at any time before :meth:`close`. .. method:: aifc.tell() Return the current write position in the output file. Useful in combination with :meth:`setmark`. .. method:: aifc.writeframes(data) Write data to the output file. This method can only be called after the audio file parameters have been set. .. method:: aifc.writeframesraw(data) Like :meth:`writeframes`, except that the header of the audio file is not updated. .. method:: aifc.close() Close the AIFF file. The header of the file is updated to reflect the actual size of the audio data. After calling this method, the object can no longer be used.