solidity/scripts/extract_test_cases.py

44 lines
1.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2018-03-15 23:46:57 +00:00
#
# This script reads C++ or RST source files and writes all
# multi-line strings into individual files.
# This can be used to extract the Solidity test cases
# into files for e.g. fuzz testing as
# scripts/isolate_tests.py test/libsolidity/*
import sys
import re
2020-01-15 13:21:33 +00:00
def extract_test_cases(_path):
2020-01-23 14:06:47 +00:00
lines = open(_path, mode='rb', encoding='utf8').read().splitlines()
2018-03-15 23:46:57 +00:00
inside = False
delimiter = ''
test = ''
ctr = 1
test_name = ''
for l in lines:
2020-01-15 13:21:33 +00:00
if inside:
if l.strip().endswith(')' + delimiter + '";'):
2020-01-23 14:06:47 +00:00
open('%03d_%s.sol' % (ctr, test_name), mode='wb', encoding='utf8').write(test)
2020-01-15 13:21:33 +00:00
ctr += 1
inside = False
test = ''
else:
l = re.sub('^\t\t', '', l)
l = l.replace('\t', ' ')
test += l + '\n'
2018-03-15 23:46:57 +00:00
else:
2020-01-15 13:21:33 +00:00
m = re.search(r'BOOST_AUTO_TEST_CASE\(([^(]*)\)', l.strip())
if m:
test_name = m.group(1)
m = re.search(r'R"([^(]*)\($', l.strip())
if m:
inside = True
delimiter = m.group(1)
2018-03-15 23:46:57 +00:00
if __name__ == '__main__':
2020-01-15 13:21:33 +00:00
extract_test_cases(sys.argv[1])