Created
August 30, 2020 21:44
-
-
Save jbosboom/1d4541f5bdb7631f2e1bc5b0765b99d7 to your computer and use it in GitHub Desktop.
Convert gtest macros to doctest macros
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 python2 | |
import sys, os, re | |
conversions = [ | |
('<gtest/gtest.h>', '<doctest.h>'), | |
(r'TEST\(([a-zA-Z0-9]+), ([a-zA-Z0-9]+)\)', r'TEST_CASE("\1_\2")') | |
] | |
severity_map = {'ASSERT': 'REQUIRE', 'EXPECT': 'CHECK'} | |
predicate_map = {x: x for x in ('EQ', 'NE', 'GT', 'LT', 'GE', 'LE')} | |
predicate_map.update({'TRUE': 'UNARY', 'FALSE': 'UNARY_FALSE'}) | |
for sf, st in severity_map.iteritems(): | |
for pf, pt in predicate_map.iteritems(): | |
conversions.append((r'{}_{}\('.format(sf, pf), r'{}_{}('.format(st, pt))) | |
any_severity = '(?:{})'.format('|'.join(severity_map.itervalues())) | |
conversions.append(('('+any_severity+r'_.*\))\s*<<\s*(.*$)', r'\1; //OLDTEST: \2')) | |
for f, t in conversions: | |
print '{} -> {}'.format(f, t) | |
def convert(line): | |
for pattern, replacement in conversions: | |
line = re.sub(pattern, replacement, line) | |
return line | |
src_str, dest_str = sys.argv[1], sys.argv[2] | |
if os.path.isdir(src_str): | |
sources = [] | |
for root, _, files in os.walk(src_str): | |
for f in files: | |
if f[f.rindex('.')+1:] in ('cpp', 'hpp'): | |
sources.append(os.path.join(root, f)) | |
elif os.path.isfile(src_str): | |
sources = [src_str] | |
else: | |
print 'bad:', src_str | |
sys.exit(1) | |
if len(sources) > 1: | |
if not os.path.isdir(dest_str): | |
print 'bad:', dest_str | |
sys.exit(1) | |
destination = dest_str | |
else: | |
if not os.path.isfile(dest_str) and os.path.exists(dest_str): | |
print 'bad:', dest_str | |
sys.exit(1) | |
destination = dest_str | |
for source in sources: | |
dest = os.path.join(destination, os.path.relpath(source, src_str)) if os.path.isdir(destination) else destination | |
with open(source, 'r') as s, open(dest, 'w') as d: | |
for line in s: | |
d.write(convert(line)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment