Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test.py Source File

test.py

00001 #!/usr/bin/env python
00002 
00003 import re
00004 import sys
00005 import subprocess
00006 import os
00007 import yaml
00008 
00009 def generate(test):
00010     with open('replacements.yml') as file:
00011         replacements = yaml.load(file)
00012 
00013     lines = []
00014     for line in re.split('(?<=[;{}])\n', test.read()):
00015         for pattern, replacement in replacements:
00016             line = re.sub(pattern, replacement, line, 0, re.DOTALL | re.MULTILINE)
00017 
00018         match = re.match('(?: *\n)*( *)(.*)=>(.*);', line, re.DOTALL | re.MULTILINE)
00019         if match:
00020             tab, test, expect = match.groups()
00021             lines.append(tab+'res = {test};'.format(test=test.strip()))
00022             lines.append(tab+'TEST_ASSERT_EQUAL({expect}, res);'.format(
00023                     name=re.match('\w*', test.strip()).group(),
00024                     expect=expect.strip()))
00025         else:
00026             lines.append(line)
00027 
00028     lines = lines[:-1]
00029 
00030     with open('template_subunit.fmt') as file:
00031         template = file.read()
00032 
00033     with open('main.cpp', 'a') as file:
00034         file.write(template.format(
00035                 test=('\n'.join(
00036                     4*' '+line.replace('\n', '\n'+4*' ')
00037                     for line in lines))))
00038 
00039 def main(test=None):
00040     if test and not test.startswith('-'):
00041         with open(test) as file:
00042             generate(file)
00043     else:
00044         generate(sys.stdin)
00045 
00046 
00047 if __name__ == "__main__":
00048     main(*sys.argv[1:])