Skip to content

Instantly share code, notes, and snippets.

@katzenbaer
Forked from elliotchance/generate_tests.py
Last active September 8, 2017 06:48
Show Gist options
  • Save katzenbaer/0c36f4ee0fa72e649e5c15dc06cb1882 to your computer and use it in GitHub Desktop.
Save katzenbaer/0c36f4ee0fa72e649e5c15dc06cb1882 to your computer and use it in GitHub Desktop.
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*// params: \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