Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
10d5f4d9
Commit
10d5f4d9
authored
Oct 09, 2014
by
R David Murray
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#0712: 2to3 has a new "asserts" fixer that replaces deprecated names of unittest methods.
Patch by Ezio Melotti, docs by Berker Peksag.
parent
16d1af49
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
120 additions
and
0 deletions
+120
-0
Doc/library/2to3.rst
Doc/library/2to3.rst
+33
-0
Lib/lib2to3/fixes/fix_asserts.py
Lib/lib2to3/fixes/fix_asserts.py
+34
-0
Lib/lib2to3/tests/test_fixers.py
Lib/lib2to3/tests/test_fixers.py
+50
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/2to3.rst
View file @
10d5f4d9
...
@@ -142,6 +142,39 @@ and off individually. They are described here in more detail.
...
@@ -142,6 +142,39 @@ and off individually. They are described here in more detail.
Removes usage of :func:`apply`. For example ``apply(function, *args,
Removes usage of :func:`apply`. For example ``apply(function, *args,
**kwargs)`` is converted to ``function(*args, **kwargs)``.
**kwargs)`` is converted to ``function(*args, **kwargs)``.
.. 2to3fixer:: asserts
Replaces deprecated :mod:`unittest` method names with the correct ones.
================================ ==========================================
From To
================================ ==========================================
``failUnlessEqual(a, b)`` :meth:`assertEqual(a, b)
<unittest.TestCase.assertEqual>`
``assertEquals(a, b)`` :meth:`assertEqual(a, b)
<unittest.TestCase.assertEqual>`
``failIfEqual(a, b)`` :meth:`assertNotEqual(a, b)
<unittest.TestCase.assertNotEqual>`
``assertNotEquals(a, b)`` :meth:`assertNotEqual(a, b)
<unittest.TestCase.assertNotEqual>`
``failUnless(a)`` :meth:`assertTrue(a)
<unittest.TestCase.assertTrue>`
``assert_(a)`` :meth:`assertTrue(a)
<unittest.TestCase.assertTrue>`
``failIf(a)`` :meth:`assertFalse(a)
<unittest.TestCase.assertFalse>`
``failUnlessRaises(exc, cal)`` :meth:`assertRaises(exc, cal)
<unittest.TestCase.assertRaises>`
``failUnlessAlmostEqual(a, b)`` :meth:`assertAlmostEqual(a, b)
<unittest.TestCase.assertAlmostEqual>`
``assertAlmostEquals(a, b)`` :meth:`assertAlmostEqual(a, b)
<unittest.TestCase.assertAlmostEqual>`
``failIfAlmostEqual(a, b)`` :meth:`assertNotAlmostEqual(a, b)
<unittest.TestCase.assertNotAlmostEqual>`
``assertNotAlmostEquals(a, b)`` :meth:`assertNotAlmostEqual(a, b)
<unittest.TestCase.assertNotAlmostEqual>`
================================ ==========================================
.. 2to3fixer:: basestring
.. 2to3fixer:: basestring
Converts :class:`basestring` to :class:`str`.
Converts :class:`basestring` to :class:`str`.
...
...
Lib/lib2to3/fixes/fix_asserts.py
0 → 100644
View file @
10d5f4d9
"""Fixer that replaces deprecated unittest method names."""
# Author: Ezio Melotti
from
..fixer_base
import
BaseFix
from
..fixer_util
import
Name
NAMES
=
dict
(
assert_
=
"assertTrue"
,
assertEquals
=
"assertEqual"
,
assertNotEquals
=
"assertNotEqual"
,
assertAlmostEquals
=
"assertAlmostEqual"
,
assertNotAlmostEquals
=
"assertNotAlmostEqual"
,
assertRegexpMatches
=
"assertRegex"
,
assertRaisesRegexp
=
"assertRaisesRegex"
,
failUnlessEqual
=
"assertEqual"
,
failIfEqual
=
"assertNotEqual"
,
failUnlessAlmostEqual
=
"assertAlmostEqual"
,
failIfAlmostEqual
=
"assertNotAlmostEqual"
,
failUnless
=
"assertTrue"
,
failUnlessRaises
=
"assertRaises"
,
failIf
=
"assertFalse"
,
)
class
FixAsserts
(
BaseFix
):
PATTERN
=
"""
power< any+ trailer< '.' meth=(%s)> any* >
"""
%
'|'
.
join
(
map
(
repr
,
NAMES
))
def
transform
(
self
,
node
,
results
):
name
=
results
[
"meth"
][
0
]
name
.
replace
(
Name
(
NAMES
[
str
(
name
)],
prefix
=
name
.
prefix
))
Lib/lib2to3/tests/test_fixers.py
View file @
10d5f4d9
...
@@ -4576,3 +4576,53 @@ class Test_exitfunc(FixerTestCase):
...
@@ -4576,3 +4576,53 @@ class Test_exitfunc(FixerTestCase):
def
test_unchanged
(
self
):
def
test_unchanged
(
self
):
s
=
"""f(sys.exitfunc)"""
s
=
"""f(sys.exitfunc)"""
self
.
unchanged
(
s
)
self
.
unchanged
(
s
)
class
Test_asserts
(
FixerTestCase
):
fixer
=
"asserts"
def
test_deprecated_names
(
self
):
tests
=
[
(
'self.assert_(True)'
,
'self.assertTrue(True)'
),
(
'self.assertEquals(2, 2)'
,
'self.assertEqual(2, 2)'
),
(
'self.assertNotEquals(2, 3)'
,
'self.assertNotEqual(2, 3)'
),
(
'self.assertAlmostEquals(2, 3)'
,
'self.assertAlmostEqual(2, 3)'
),
(
'self.assertNotAlmostEquals(2, 8)'
,
'self.assertNotAlmostEqual(2, 8)'
),
(
'self.failUnlessEqual(2, 2)'
,
'self.assertEqual(2, 2)'
),
(
'self.failIfEqual(2, 3)'
,
'self.assertNotEqual(2, 3)'
),
(
'self.failUnlessAlmostEqual(2, 3)'
,
'self.assertAlmostEqual(2, 3)'
),
(
'self.failIfAlmostEqual(2, 8)'
,
'self.assertNotAlmostEqual(2, 8)'
),
(
'self.failUnless(True)'
,
'self.assertTrue(True)'
),
(
'self.failUnlessRaises(foo)'
,
'self.assertRaises(foo)'
),
(
'self.failIf(False)'
,
'self.assertFalse(False)'
),
]
for
b
,
a
in
tests
:
self
.
check
(
b
,
a
)
def
test_variants
(
self
):
b
=
'eq = self.assertEquals'
a
=
'eq = self.assertEqual'
self
.
check
(
b
,
a
)
b
=
'self.assertEquals(2, 3, msg="fail")'
a
=
'self.assertEqual(2, 3, msg="fail")'
self
.
check
(
b
,
a
)
b
=
'self.assertEquals(2, 3, msg="fail") # foo'
a
=
'self.assertEqual(2, 3, msg="fail") # foo'
self
.
check
(
b
,
a
)
b
=
'self.assertEquals (2, 3)'
a
=
'self.assertEqual (2, 3)'
self
.
check
(
b
,
a
)
b
=
' self.assertEquals (2, 3)'
a
=
' self.assertEqual (2, 3)'
self
.
check
(
b
,
a
)
b
=
'with self.failUnlessRaises(Explosion): explode()'
a
=
'with self.assertRaises(Explosion): explode()'
self
.
check
(
b
,
a
)
b
=
'with self.failUnlessRaises(Explosion) as cm: explode()'
a
=
'with self.assertRaises(Explosion) as cm: explode()'
self
.
check
(
b
,
a
)
def
test_unchanged
(
self
):
self
.
unchanged
(
'self.assertEqualsOnSaturday'
)
self
.
unchanged
(
'self.assertEqualsOnSaturday(3, 5)'
)
Misc/NEWS
View file @
10d5f4d9
...
@@ -222,6 +222,9 @@ Extension Modules
...
@@ -222,6 +222,9 @@ Extension Modules
Tools/Demos
Tools/Demos
-----------
-----------
- Issue #10712: 2to3 has a new "
asserts
" fixer that replaces deprecated names
of unittest methods (e.g. failUnlessEqual -> assertEqual).
- Issue #22221: 2to3 and the findnocoding.py script now ignore the source
- Issue #22221: 2to3 and the findnocoding.py script now ignore the source
encoding declaration on the second line if the first line contains anything
encoding declaration on the second line if the first line contains anything
except a comment.
except a comment.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment