Skip to content

Commit 394951a

Browse files
committed
tests: allow for simple data-driven tests
Right now, we're not able to use data-driven tests at all. E.g. given a set of tests which we'd like to repeat with different test data, one has to hand-write any outer loop that iterates over the test data and then call each of the test functions. Next to being bothersome, this also has the downside that error reporting is quite lacking, as one never knows which test data actually produced failures. So let's implement the ability to re-run complete test modules with changing test data. To retain backwards compatibility and enable trivial addition of new runs with changed test data, we simply extend clar's generate.py. Instead of scanning for a single `_initialize` function, each test module may now implement multiple `_initialize_foo` functions. The generated test harness will then run all test functions for each of the provided initializer functions, making it possible to provide different test data inside of each of the initializer functions. Example: ``` void test_git_example__initialize_with_nonbare_repo(void) { g_repo = cl_git_sandbox_init("testrepo"); } void test_git_example__initialize_with_bare_repo(void) { g_repo = cl_git_sandbox_init("testrepo.git"); } void test_git_example__cleanup(void) { cl_git_sandbox_cleanup(); } void test_git_example__test1(void) { test1(); } void test_git_example__test2(void) { test2(); } ``` Executing this test module will cause the following output: ``` $ ./libgit2_clar -sgit::example git::example (with nonbare repo).. git::example (with bare repo).. ```
1 parent e50d138 commit 394951a

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

tests/generate.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class DeclarationTemplate(Template):
2424
def render(self):
2525
out = "\n".join("extern %s;" % cb['declaration'] for cb in self.module.callbacks) + "\n"
2626

27-
if self.module.initialize:
28-
out += "extern %s;\n" % self.module.initialize['declaration']
27+
for initializer in self.module.initializers:
28+
out += "extern %s;\n" % initializer['declaration']
2929

3030
if self.module.cleanup:
3131
out += "extern %s;\n" % self.module.cleanup['declaration']
@@ -41,22 +41,37 @@ def render(self):
4141

4242
class InfoTemplate(Template):
4343
def render(self):
44-
return Template(
44+
templates = []
45+
46+
initializers = self.module.initializers
47+
if len(initializers) == 0:
48+
initializers = [ None ]
49+
50+
for initializer in initializers:
51+
name = self.module.clean_name()
52+
if initializer and initializer['short_name'].startswith('initialize_'):
53+
variant = initializer['short_name'][len('initialize_'):]
54+
name += " (%s)" % variant.replace('_', ' ')
55+
56+
template = Template(
4557
r"""
4658
{
4759
"${clean_name}",
4860
${initialize},
4961
${cleanup},
5062
${cb_ptr}, ${cb_count}, ${enabled}
5163
}"""
52-
).substitute(
53-
clean_name = self.module.clean_name(),
54-
initialize = self._render_callback(self.module.initialize),
55-
cleanup = self._render_callback(self.module.cleanup),
56-
cb_ptr = "_clar_cb_%s" % self.module.name,
57-
cb_count = len(self.module.callbacks),
58-
enabled = int(self.module.enabled)
59-
)
64+
).substitute(
65+
clean_name = name,
66+
initialize = self._render_callback(initializer),
67+
cleanup = self._render_callback(self.module.cleanup),
68+
cb_ptr = "_clar_cb_%s" % self.module.name,
69+
cb_count = len(self.module.callbacks),
70+
enabled = int(self.module.enabled)
71+
)
72+
templates.append(template)
73+
74+
return ','.join(templates)
6075

6176
def __init__(self, name):
6277
self.name = name
@@ -86,7 +101,7 @@ def parse(self, contents):
86101
regex = re.compile(TEST_FUNC_REGEX % self.name, re.MULTILINE)
87102

88103
self.callbacks = []
89-
self.initialize = None
104+
self.initializers = []
90105
self.cleanup = None
91106

92107
for (declaration, symbol, short_name) in regex.findall(contents):
@@ -96,8 +111,8 @@ def parse(self, contents):
96111
"symbol" : symbol
97112
}
98113

99-
if short_name == 'initialize':
100-
self.initialize = data
114+
if short_name.startswith('initialize'):
115+
self.initializers.append(data)
101116
elif short_name == 'cleanup':
102117
self.cleanup = data
103118
else:

0 commit comments

Comments
 (0)