Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert nose parametric tests to normal ones
#!/usr/bin/env python
"""Simple script to remove nose-style parametric tests and turn them into
normal ones.
Usage:
param2normal.py file1.py file2.py ...
By default it overwrites the files in-place, you can change that below in the
__main__ section.
"""
#-----------------------------------------------------------------------------
# imports
#-----------------------------------------------------------------------------
import re
import sys
#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
def p2n(line):
if line.rstrip().endswith('\\'):
print >> sys.stderr, "WARNING:, continuation at line\n", line
simple = re.match(r"(\s*)yield\s*([\w\.]*)\s*$", line)
if simple:
return '%s%s()\n' % simple.groups()
fullform = re.match(r"(\s*)yield\s*([\w\.]*),\s*(.*)$", line)
if fullform:
return '%s%s(%s)\n' % fullform.groups()
callform = re.match(r"(\s*)yield\s*([\w\.]*)\s*(\(.*)$", line)
if callform:
return '%s%s%s\n' % callform.groups()
else:
return line
#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------
import nose.tools as nt
def test():
tests = [("yield x", "x()"),
(" yield x", " x()"),
("yield x, y, z", "x(y, z)"),
(" yield x, y, z", " x(y, z)"),
("yield x, y, z, a, b, c", "x(y, z, a, b, c)"),
("yield npt.assert_array_almost_equal, l, unos",
"npt.assert_array_almost_equal(l, unos)"),
("yield npt.assert_array_almost_equal",
"npt.assert_array_almost_equal()"),
("yield f(x)", "f(x)"),
(" yield f(x)", " f(x)"),
]
for ori, new in tests:
nt.assert_equal(p2n(ori), new+'\n')
#-----------------------------------------------------------------------------
# Main script
#-----------------------------------------------------------------------------
if __name__ == '__main__':
# Default behavior is to overwrite files in-place, if you disable this
# things will go to stdout instead.
inplace = True
#inplace = False
import fileinput
for line in fileinput.input(inplace=inplace):
print p2n(line),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.