Hash :
681008c7
Author :
Date :
2011-10-07T22:31:08
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
#!/usr/bin/env python
from __future__ import with_statement
from string import Template
import re, fnmatch, os
VERSION = "0.8.0"
TEST_FUNC_REGEX = r"^(void\s+(test_%s__(\w+))\(\s*(void)?\s*\))\s*\{"
CLAY_HEADER = """
/*
* Clay v0.7.0
*
* This is an autogenerated file. Do not modify.
* To add new unit tests or suites, regenerate the whole
* file with `./clay`
*/
"""
TEMPLATE_SUITE = Template(
r"""
{
"${clean_name}",
${initialize},
${cleanup},
${cb_ptr}, ${cb_count}
}
""")
def main():
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-c', '--clay-path', dest='clay_path')
parser.add_option('-v', '--report-to', dest='print_mode', default='stdout')
options, args = parser.parse_args()
for folder in args:
builder = ClayTestBuilder(folder,
clay_path = options.clay_path,
print_mode = options.print_mode)
builder.render()
class ClayTestBuilder:
def __init__(self, path, clay_path = None, print_mode = 'stdout'):
self.declarations = []
self.callbacks = []
self.suites = []
self.suite_list = []
self.clay_path = os.path.abspath(clay_path) if clay_path else None
self.print_mode = print_mode
self.path = os.path.abspath(path)
self.modules = ["clay_sandbox.c", "clay_fixtures.c", "clay_fs.c"]
print("Loading test suites...")
for root, dirs, files in os.walk(self.path):
module_root = root[len(self.path):]
module_root = [c for c in module_root.split(os.sep) if c]
tests_in_module = fnmatch.filter(files, "*.c")
tests_in_module.sort()
for test_file in tests_in_module:
full_path = os.path.join(root, test_file)
test_name = "_".join(module_root + [test_file[:-2]])
with open(full_path) as f:
self._process_test_file(test_name, f.read())
if not self.suites:
raise RuntimeError(
'No tests found under "%s"' % folder_name)
def render(self):
main_file = os.path.join(self.path, 'clay_main.c')
with open(main_file, "w") as out:
template = Template(self._load_file('clay.c'))
output = template.substitute(
clay_print = self._get_print_method(),
clay_modules = self._get_modules(),
suites_str = ", ".join(self.suite_list),
test_callbacks = ",\n\t".join(self.callbacks),
cb_count = len(self.callbacks),
test_suites = ",\n\t".join(self.suites),
suite_count = len(self.suites),
)
out.write(output)
header_file = os.path.join(self.path, 'clay.h')
with open(header_file, "w") as out:
template = Template(self._load_file('clay.h'))
output = template.substitute(
extern_declarations = "\n".join(self.declarations),
)
out.write(output)
print ('Written Clay suite to "%s"' % self.path)
#####################################################
# Internal methods
#####################################################
def _get_print_method(self):
return {
'stdout' : 'printf(__VA_ARGS__)',
'stderr' : 'fprintf(stderr, __VA_ARGS__)',
'silent' : ''
}[self.print_mode]
def _load_file(self, filename):
if self.clay_path:
filename = os.path.join(self.clay_path, filename)
with open(filename) as cfile:
return cfile.read()
else:
import zlib, base64, sys
content = CLAY_FILES[filename]
if sys.version_info >= (3, 0):
content = bytearray(content, 'utf_8')
content = base64.b64decode(content)
content = zlib.decompress(content)
return str(content)
else:
content = base64.b64decode(content)
return zlib.decompress(content)
def _get_modules(self):
return "\n".join(self._load_file(f) for f in self.modules)
def _parse_comment(self, comment):
comment = comment[2:-2]
comment = comment.splitlines()
comment = [line.strip() for line in comment]
comment = "\n".join(comment)
return comment
def _process_test_file(self, test_name, contents):
regex_string = TEST_FUNC_REGEX % test_name
regex = re.compile(regex_string, re.MULTILINE)
callbacks = []
initialize = cleanup = "{NULL, NULL, 0}"
for (declaration, symbol, short_name, _) in regex.findall(contents):
self.declarations.append("extern %s;" % declaration)
func_ptr = '{"%s", &%s, %d}' % (
short_name, symbol, len(self.suites)
)
if short_name == 'initialize':
initialize = func_ptr
elif short_name == 'cleanup':
cleanup = func_ptr
else:
callbacks.append(func_ptr)
if not callbacks:
return
clean_name = test_name.replace("_", "::")
suite = TEMPLATE_SUITE.substitute(
clean_name = clean_name,
initialize = initialize,
cleanup = cleanup,
cb_ptr = "&_all_callbacks[%d]" % len(self.callbacks),
cb_count = len(callbacks)
).strip()
self.callbacks += callbacks
self.suites.append(suite)
self.suite_list.append(clean_name)
print(" %s (%d tests)" % (clean_name, len(callbacks)))
CLAY_FILES = {
"clay.c" : r"""eJy9GWtT20jys/wrZk3AEggHyDd74SqVu1xRl2WrAqlsFaFUsjTGc9HDqxkFONb//brnpdHLux+uji+gnu6efndPc8CKJKtTSn6OOaeVmG+uJgcWxqn4d77twESasVUPxsouqGLFYxuWx2KDkMnbY1LR32tW0ZSsy4rwuEhX5TMQkOO3LpMX/la8bCnv8AYwF7EUdnKQ0jUrKEmy+CXawqXCn8/nAXnz2kB2gMbWgEiir9c37y4mB55l9sSKtHxSNzRQrU4D4BuaZfGWdcAp6JBoQTwtSfTL++ub6MMHEkVJSpPMOUKp/S2YIYQ/AxK1vxu8/Dsw1gd5mVJAbUAOXrKxQBI5Hw1GnCSU8zarPsyVsErrrQ+/pHj2A5Vg60LaMPrl+uafX99dRBEAvW0VP+YxSco8p2B8CI6QTKW53l1MkbPDuki2L74oQ7Kuyjwkoow4+w+IpI8iLg812GBFd5+/3Hx4f/cPl9nX6Nd/kbMLB3IbXd/+/fqz/xwQ338mRyQCyEeABOSnS3LmEuffBc23kTFBRgtp4B4QSGiRsvXEwyhE3UHQOhHKceT27v1ddLecHNCM01awQIA+xQzjgkCs459blvoXgQzvBq8uGKSOCqlO8PSu7NwoxWpyYorBPt9MJxPEYwn5UTLILR5VuZ+UBRcQKnFFjiNe1lVCg2UXLynBMwOYIXGBKYUwXdpL3KPJmj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