Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Parameterized Tests in Swift
import sys
import re
import glob
import os
files = []
for arg in sys.argv[1:]:
if os.path.isdir(arg):
arg += "/*.swift"
for gfile in glob.glob(arg):
if gfile[-6:] == '.swift':
files.append(gfile)
for file in files:
print(file)
dividing_line = '// DO NOT EDIT BELOW THIS LINE\n'
generated = '\n' + dividing_line + '\n'
with open(file) as f:
lines = f.readlines()
className, tests = '', []
for line in lines:
search = re.search(r'^\s*class\s+(\w+)', line)
if search:
className = search.group(1)
generated += 'extension %s {\n' % className
search = re.search(r'^\s*//>\s*(\w*)(.*)', line)
if search:
tests.append([search.group(1), search.group(2)])
search = re.search(r'^\s*func (test\w+)', line)
if search:
functionName = search.group(1)
for i, test in enumerate(tests):
if not test[0]:
test[0] = i
generated += ' func %s%s() {\n' % (functionName, test[0])
generated += ' %s%s\n' % (functionName, test[1].strip())
generated += ' }\n\n'
className, tests = '', []
generated += '}\n'
try:
lines = lines[0:lines.index(dividing_line) - 1]
except:
pass
lines.append(generated)
with open(file, "w") as f:
f.write(''.join(lines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment