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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
#!/usr/bin/env python
from __future__ import with_statement
from string import Template
import re, fnmatch, os
VERSION = "0.7.0"
TEST_FUNC_REGEX = r"^(void\s+(test_%s__(\w+))\(\s*(void)?\s*\))\s*\{"
TEMPLATE_MAIN = Template(
r"""
/*
* Clay v${version}
*
* This is an autogenerated file. Do not modify.
* To add new unit tests or suites, regenerate the whole
* file with `./clay`
*/
#define clay_print(...) ${clay_print}
${clay_library}
${extern_declarations}
static const struct clay_func _all_callbacks[] = {
${test_callbacks}
};
static const struct clay_suite _all_suites[] = {
${test_suites}
};
static const char _suites_str[] = "${suites_str}";
int main(int argc, char *argv[])
{
return clay_test(
argc, argv, _suites_str,
_all_callbacks, ${cb_count},
_all_suites, ${suite_count}
);
}
""")
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('-o', '--output', dest='output')
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,
output_folder = options.output,
print_mode = options.print_mode)
builder.render()
class ClayTestBuilder:
def __init__(self, folder_name, output_folder = None, clay_path = None, print_mode = 'stdout'):
self.declarations = []
self.callbacks = []
self.suites = []
self.suite_list = []
self.clay_path = os.path.abspath(clay_path)
self.print_mode = print_mode
folder_name = os.path.abspath(folder_name)
if not output_folder:
output_folder = folder_name
self.output = os.path.join(output_folder, "clay_main.c")
self.output_header = os.path.join(output_folder, "clay.h")
self.modules = ["clay.c", "clay_sandbox.c"]
print("Loading test suites...")
for root, dirs, files in os.walk(folder_name):
module_root = root[len(folder_name):]
module_root = [c for c in module_root.split(os.sep) if c]
tests_in_module = fnmatch.filter(files, "*.c")
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):
template = TEMPLATE_MAIN.substitute(
version = VERSION,
clay_print = self._get_print_method(),
clay_library = self._get_library(),
extern_declarations = "\n".join(self.declarations),
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),
)
with open(self.output, "w") as out:
out.write(template)
with open(self.output_header, "w") as out:
out.write(self._load_file('clay.h'))
print ('Written test suite to "%s"' % self.output)
print ('Written header to "%s"' % self.output_header)
#####################################################
# 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_library(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"""eJy9WEtv2zgQPsu/gnWRWHIUN9mj3WRvPRW7wHYLFEgCg5bomFuZckUqTdr6v+8MX6Jebg+LPdkiZ4bfPDmc11xkRZ0z8pZKySq12N1OXvs1ydQ/+0NnTeUF3/TWeNldqrh4bK/tqdrhyuTNnFTsS80rlpNtWRFJRb4pn4GBzN+EQl7kG6loB1UtOByoBfnFaVbQl8VuOpnAwXWmCH6vWVWB9O+TKCuFhLUdrchcMalWk4gLRfDvWtT7DatWbSJZc8U6a1teMMtYcMGGGfWR6718xHW9kjOZVfygeClWk0nUxzcX7BkQHVcIniqeEUvTAU4zxZ/Y2uIf2LGgDUT9YU6QTt1S0cIvBSbIylqoEXBewsBeQYFZ/0fmp5LnJJ4XZQanZAWjoj4ksV6dJyu7395eH+hLUdIc2SHS1pt6S1RF94cSLexg+4U1E3RTMCA/kjU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