|
15 | 15 | SRC_DIR = os.path.dirname(os.path.realpath(__file__)) |
16 | 16 | sys.path.insert(0, os.path.join(SRC_DIR, "../lib/python")) |
17 | 17 |
|
18 | | -from kdoc.kdoc_parser import trim_private_members |
| 18 | +from kdoc.c_lex import CTokenizer |
19 | 19 | from unittest_helper import run_unittest |
20 | 20 |
|
| 21 | + |
21 | 22 | # |
22 | 23 | # List of tests. |
23 | 24 | # |
24 | 25 | # The code will dynamically generate one test for each key on this dictionary. |
25 | 26 | # |
26 | 27 |
|
| 28 | +def make_private_test(name, data): |
| 29 | + """ |
| 30 | + Create a test named ``name`` using parameters given by ``data`` dict. |
| 31 | + """ |
| 32 | + |
| 33 | + def test(self): |
| 34 | + """In-lined lambda-like function to run the test""" |
| 35 | + tokens = CTokenizer(data["source"]) |
| 36 | + result = str(tokens) |
| 37 | + |
| 38 | + # |
| 39 | + # Avoid whitespace false positives |
| 40 | + # |
| 41 | + result = re.sub(r"\s++", " ", result).strip() |
| 42 | + expected = re.sub(r"\s++", " ", data["trimmed"]).strip() |
| 43 | + |
| 44 | + msg = f"failed when parsing this source:\n{data['source']}" |
| 45 | + self.assertEqual(result, expected, msg=msg) |
| 46 | + |
| 47 | + return test |
| 48 | + |
27 | 49 | #: Tests to check if CTokenizer is handling properly public/private comments. |
28 | 50 | TESTS_PRIVATE = { |
29 | 51 | # |
30 | 52 | # Simplest case: no private. Ensure that trimming won't affect struct |
31 | 53 | # |
| 54 | + "__run__": make_private_test, |
32 | 55 | "no private": { |
33 | 56 | "source": """ |
34 | 57 | struct foo { |
|
288 | 311 | }, |
289 | 312 | } |
290 | 313 |
|
| 314 | +#: Dict containing all test groups fror CTokenizer |
| 315 | +TESTS = { |
| 316 | + "TestPublicPrivate": TESTS_PRIVATE, |
| 317 | +} |
291 | 318 |
|
292 | | -class TestPublicPrivate(unittest.TestCase): |
293 | | - """ |
294 | | - Main test class. Populated dynamically at runtime. |
295 | | - """ |
| 319 | +def setUp(self): |
| 320 | + self.maxDiff = None |
296 | 321 |
|
297 | | - def setUp(self): |
298 | | - self.maxDiff = None |
| 322 | +def build_test_class(group_name, table): |
| 323 | + """ |
| 324 | + Dynamically creates a class instance using type() as a generator |
| 325 | + for a new class derivated from unittest.TestCase. |
299 | 326 |
|
300 | | - def add_test(cls, name, source, trimmed): |
301 | | - """ |
302 | | - Dynamically add a test to the class |
303 | | - """ |
304 | | - def test(cls): |
305 | | - result = trim_private_members(source) |
| 327 | + We're opting to do it inside a function to avoid the risk of |
| 328 | + changing the globals() dictionary. |
| 329 | + """ |
306 | 330 |
|
307 | | - result = re.sub(r"\s++", " ", result).strip() |
308 | | - expected = re.sub(r"\s++", " ", trimmed).strip() |
| 331 | + class_dict = { |
| 332 | + "setUp": setUp |
| 333 | + } |
309 | 334 |
|
310 | | - msg = f"failed when parsing this source:\n" + source |
| 335 | + run = table["__run__"] |
311 | 336 |
|
312 | | - cls.assertEqual(result, expected, msg=msg) |
| 337 | + for test_name, data in table.items(): |
| 338 | + if test_name == "__run__": |
| 339 | + continue |
313 | 340 |
|
314 | | - test.__name__ = f'test {name}' |
| 341 | + class_dict[f"test_{test_name}"] = run(test_name, data) |
315 | 342 |
|
316 | | - setattr(TestPublicPrivate, test.__name__, test) |
| 343 | + cls = type(group_name, (unittest.TestCase,), class_dict) |
317 | 344 |
|
| 345 | + return cls.__name__, cls |
318 | 346 |
|
319 | 347 | # |
320 | | -# Populate TestPublicPrivate class |
| 348 | +# Create classes and add them to the global dictionary |
321 | 349 | # |
322 | | -test_class = TestPublicPrivate() |
323 | | -for name, test in TESTS_PRIVATE.items(): |
324 | | - test_class.add_test(name, test["source"], test["trimmed"]) |
325 | | - |
| 350 | +for group, table in TESTS.items(): |
| 351 | + t = build_test_class(group, table) |
| 352 | + globals()[t[0]] = t[1] |
326 | 353 |
|
327 | 354 | # |
328 | 355 | # main |
|
0 commit comments