Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Mauro Carvalho Chehab and committed by
Jonathan Corbet
fe79f85f cd77a9aa

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