Created
May 31, 2011 22:52
-
-
Save fperez/1001451 to your computer and use it in GitHub Desktop.
Convert nose parametric tests to normal ones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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