test_check_structures.py
1 """Verify check structures.""" 2 3 from collections.abc import Collection 4 from typing import TypeVar 5 6 from pytest import mark 7 8 from proselint.registry.checks import BATCH_COUNT, types 9 10 from .common import extract_checks, get_module_names, verify_module 11 12 T = TypeVar("T") 13 14 15 def eq_unordered(a: Collection[T], b: Collection[T]) -> bool: 16 """Check whether a and b are equal, irrespective of order.""" 17 return all(x in b for x in a) and all(x in a for x in b) 18 19 20 @mark.parametrize("module_name", get_module_names()) 21 def test_module_register(module_name: str) -> None: 22 """Verify that a check module has a register, and registers its checks.""" 23 module = verify_module(module_name) 24 assert "__register__" in dir(module), f"{module_name} has no register." 25 assert eq_unordered(module.__register__, extract_checks(module)), ( # pyright: ignore[reportAny] 26 f"{module_name} does not register all viable checks." 27 ) 28 29 30 @mark.parametrize("module_name", get_module_names()) 31 def test_check_paths(module_name: str) -> None: 32 """Verify that check paths match the module they reside in.""" 33 checks = extract_checks(verify_module(module_name)) 34 for check in checks: 35 assert module_name in check.path, ( 36 f"{check.path} does not contain {module_name}." 37 ) 38 assert check.matches_partial(module_name), ( 39 f"{check.path} fails matches_partial against {module_name}." 40 ) 41 42 43 @mark.parametrize("module_name", get_module_names()) 44 def test_check_batching(module_name: str) -> None: 45 """Verify that checks adhere to the global batch count.""" 46 checks = extract_checks(verify_module(module_name)) 47 for check in checks: 48 if isinstance( 49 check.check_type, 50 (types.Existence, types.PreferredForms, types.PreferredFormsSimple), 51 ): 52 items = check.check_type.items 53 elif isinstance(check.check_type, types.Consistency): 54 items = check.check_type.term_pairs 55 else: 56 continue 57 assert len(items) <= int(BATCH_COUNT * 1.05), ( 58 f"{check.path} has too many items ({len(items)} vs. {BATCH_COUNT})." 59 )