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 227 228
#!/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 _CC 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) if clay_path else None
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", "clay_fixtures.c", "clay_fs.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"""eJy9GF1v2zbwWf4VrIPEUqK4SfdmLxmGbgWKFS3QpGiBJBBoiY61yqIrUk2yLf99d/wS9eXtYdiTrePd8b7veAd5mRZ1xsiPVAhWyfnmcnLgYILJ37e7DkxmRb7qwXLeBVV5ed+GbancIGTy8phU7FudVywja14RQctsxR+BgBy/9Jk8iZfyacdEhzeAhaRKWACvM7Ymyee37394NTkIHNZDXmb8QZM2UCNnAxAbVhR0l3fAGQiXanMEu4rebylJ+XbLShmC+jGZKrofXk0jECEACfKSkeT1a5IkacbSwoOiqOEOdI/hb0SS9neDt/0Kl5qDLc8YoDYgDy/dOCBJvI8Gg6YpE6LNqg/zJayyehfCjxLPfbQwynT3FEoek3XFtzGRPBH5H8DZHCVCHRqwxUquP356//rn6199M31OPvxGzl41g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