Skip to content

Commit fe79f85

Browse files
mchehabJonathan Corbet
authored andcommitted
unittests: test_private: modify it to use CTokenizer directly
Change the logic to use the tokenizer directly. This allows adding more unit tests to check the validty of the tokenizer itself. Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org> Message-ID: <2672257233ff73a9464c09b50924be51e25d4f59.1773074166.git.mchehab+huawei@kernel.org> Signed-off-by: Jonathan Corbet <corbet@lwn.net> Message-ID: <66e6320a4d5ad9730c1c0ceea79b5021e90c66c6.1773770483.git.mchehab+huawei@kernel.org>
1 parent cd77a9a commit fe79f85

1 file changed

Lines changed: 51 additions & 24 deletions

File tree

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,43 @@
1515
SRC_DIR = os.path.dirname(os.path.realpath(__file__))
1616
sys.path.insert(0, os.path.join(SRC_DIR, "../lib/python"))
1717

18-
from kdoc.kdoc_parser import trim_private_members
18+
from kdoc.c_lex import CTokenizer
1919
from unittest_helper import run_unittest
2020

21+
2122
#
2223
# List of tests.
2324
#
2425
# The code will dynamically generate one test for each key on this dictionary.
2526
#
2627

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+
2749
#: Tests to check if CTokenizer is handling properly public/private comments.
2850
TESTS_PRIVATE = {
2951
#
3052
# Simplest case: no private. Ensure that trimming won't affect struct
3153
#
54+
"__run__": make_private_test,
3255
"no private": {
3356
"source": """
3457
struct foo {
@@ -288,41 +311,45 @@
288311
},
289312
}
290313

314+
#: Dict containing all test groups fror CTokenizer
315+
TESTS = {
316+
"TestPublicPrivate": TESTS_PRIVATE,
317+
}
291318

292-
class TestPublicPrivate(unittest.TestCase):
293-
"""
294-
Main test class. Populated dynamically at runtime.
295-
"""
319+
def setUp(self):
320+
self.maxDiff = None
296321

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.
299326
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+
"""
306330

307-
result = re.sub(r"\s++", " ", result).strip()
308-
expected = re.sub(r"\s++", " ", trimmed).strip()
331+
class_dict = {
332+
"setUp": setUp
333+
}
309334

310-
msg = f"failed when parsing this source:\n" + source
335+
run = table["__run__"]
311336

312-
cls.assertEqual(result, expected, msg=msg)
337+
for test_name, data in table.items():
338+
if test_name == "__run__":
339+
continue
313340

314-
test.__name__ = f'test {name}'
341+
class_dict[f"test_{test_name}"] = run(test_name, data)
315342

316-
setattr(TestPublicPrivate, test.__name__, test)
343+
cls = type(group_name, (unittest.TestCase,), class_dict)
317344

345+
return cls.__name__, cls
318346

319347
#
320-
# Populate TestPublicPrivate class
348+
# Create classes and add them to the global dictionary
321349
#
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]
326353

327354
#
328355
# main

0 commit comments

Comments
 (0)