diff options
author | Alex Beregszaszi <alex@rtfs.hu> | 2017-07-11 05:52:47 +0800 |
---|---|---|
committer | Alex Beregszaszi <alex@rtfs.hu> | 2017-07-13 23:10:37 +0800 |
commit | 32a76f15e058ff02b63e742e7801ede937ed31a2 (patch) | |
tree | 40d2a62ac0425d0b8f04190ccaa213ca475d980e /scripts | |
parent | b5da5f6e42c9d60206d5045ace471c7b5839ed30 (diff) | |
download | dexon-solidity-32a76f15e058ff02b63e742e7801ede937ed31a2.tar.gz dexon-solidity-32a76f15e058ff02b63e742e7801ede937ed31a2.tar.zst dexon-solidity-32a76f15e058ff02b63e742e7801ede937ed31a2.zip |
Upgrade isolate_tests.py to support extracting code from docs
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/isolate_tests.py | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/scripts/isolate_tests.py b/scripts/isolate_tests.py index a1d1c75c..d075b07f 100755 --- a/scripts/isolate_tests.py +++ b/scripts/isolate_tests.py @@ -1,6 +1,6 @@ #!/usr/bin/python # -# This script reads C++ source files and writes all +# 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 @@ -12,7 +12,7 @@ import os import hashlib from os.path import join -def extract_cases(path): +def extract_test_cases(path): lines = open(path, 'rb').read().splitlines() inside = False @@ -34,6 +34,44 @@ def extract_cases(path): return tests +# Contract sources are indented by 4 spaces. +# Look for `pragma solidity` and abort a line not indented properly. +# If the comment `// This will not compile` is above the pragma, +# the code is skipped. +def extract_docs_cases(path): + # Note: this code works, because splitlines() removes empty new lines + # and thus even if the empty new lines are missing indentation + lines = open(path, 'rb').read().splitlines() + + ignore = False + inside = False + tests = [] + + for l in lines: + if inside: + # Abort if indentation is missing + m = re.search(r'^[^ ]+', l) + if m: + inside = False + else: + tests[-1] += l + '\n' + else: + m = re.search(r'^ // This will not compile', l) + if m: + ignore = True + + if ignore: + # Abort if indentation is missing + m = re.search(r'^[^ ]+', l) + if m: + ignore = False + else: + m = re.search(r'^ pragma solidity .*[0-9]+\.[0-9]+\.[0-9]+;$', l) + if m: + inside = True + tests += [l] + + return tests def write_cases(tests): for test in tests: @@ -41,8 +79,15 @@ def write_cases(tests): if __name__ == '__main__': path = sys.argv[1] + docs = False + if len(sys.argv) > 2 and sys.argv[2] == 'docs': + docs = True for root, dir, files in os.walk(path): for f in files: - cases = extract_cases(join(root, f)) + path = join(root, f) + if docs: + cases = extract_docs_cases(path) + else: + cases = extract_test_cases(path) write_cases(cases) |