Skip to content

Instantly share code, notes, and snippets.

@honzakral
Created June 21, 2009 19:33
Show Gist options
  • Save honzakral/133620 to your computer and use it in GitHub Desktop.
Save honzakral/133620 to your computer and use it in GitHub Desktop.
'''
VERY HACKY, DO NOT USE OR DO NOT COMPLAIN.
It was written to deal with forms.fields tests and those tests only.
'''
f = open('forms.py', 'r')
out = open('out.py', 'w')
res_words = ['def', 'from', 'try', 'import']
exc = open('exc.py', 'w')
case_started = False
case_no = 0
commands = 0
def print_command(command, result):
global commands
global case_started
global case_no
command = '\n'.join(command)
if not result:
if '=' in command:
if not case_started:
out.write('\n def test_converted_%s(self):\n' % (case_no))
case_started = True
case_no += 1
out.write(' ' + command + '\n')
else:
for w in res_words:
if command.startswith(w):
if not case_started:
out.write('\n def test_converted_%s(self):\n' % (case_no))
case_started = True
case_no += 1
out.write(' ' + command + '\n')
break
else:
case_started = False
out.write(' self.assertEqual(None, %s)\n' % (command))
elif result[0].startswith('Traceback'):
err, message = result[-1].split(': ')
out.write(' self.assertRaisesWithMessage(%s, %r, %s)\n' % (err, message, command))
case_started = False
else:
out.write(' self.assertEqual(%s, %s)\n' % ('\n'.join(result), command))
case_started = False
commands += 1
def parse_command(lines, start, stop):
i = start
while i < stop and not lines[i].startswith('>>> '):
if lines[i].startswith('#'):
out.write(lines[i])
i += 1
command = []
if i < stop:
command.append(lines[i][4:].rstrip())
i += 1
while i < stop and lines[i].startswith('...'):
command.append(' ' + lines[i][4:].rstrip())
i += 1
return command, i
def parse_result(lines, start, stop):
i = start
result = []
while i < stop:
line = lines[i].strip()
if not line or line.startswith('>>> ') or line.startswith('"""'):
break
result.append(lines[i].strip())
i += 1
return result, i
def parse_statement(lines, start, stop):
command, next = parse_command(lines, start, stop)
result, next = parse_result(lines, next, stop)
print_command(command, result)
return next
lines = f.readlines()
stop = len(lines)
next = 0
while next < stop:
next = parse_statement(lines, next, stop)
print commands
f.close()
out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment