Created
October 6, 2016 21:46
-
-
Save elliotchance/dd4e19a9acdf5304a617b9258f5ddc2e to your computer and use it in GitHub Desktop.
Parameterized tests in Swift
This file contains hidden or 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
| 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*//>(.*)', line) | |
| if search: | |
| tests.append(search.group(1)) | |
| search = re.search(r'^\s*func (test\w+)', line) | |
| if search: | |
| functionName = search.group(1) | |
| for i, test in enumerate(tests): | |
| generated += ' func %s%d() {\n' % (functionName, i) | |
| generated += ' %s%s\n' % (functionName, test.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