2026-08-03 17:50:50 +05:00
|
|
|
from pathlib import Path
|
2022-12-26 11:29:32 +01:00
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
import tabulate
|
|
|
|
|
|
|
|
|
|
import ifcopenshell.express.rule_executor
|
2025-12-19 18:53:04 +05:00
|
|
|
import ifcopenshell.validate
|
2026-08-03 17:50:50 +05:00
|
|
|
from .fixtures.rules.generate import FailObj, Result, parse_result
|
2022-12-26 11:29:32 +01:00
|
|
|
|
2026-07-26 18:03:09 +10:00
|
|
|
|
2026-08-03 17:50:50 +05:00
|
|
|
def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
|
|
|
|
|
if "filepath" not in metafunc.fixturenames:
|
2026-07-29 21:59:03 +03:00
|
|
|
return
|
|
|
|
|
rule = metafunc.config.getoption("--rule")
|
2026-08-03 17:50:50 +05:00
|
|
|
filepaths = [fp for fp in (Path(__file__).parent / "fixtures/rules").glob("*.ifc") if not rule or rule in fp.name]
|
|
|
|
|
metafunc.parametrize(
|
|
|
|
|
"filepath,expected_result",
|
|
|
|
|
[(fp, parse_result(fp)) for fp in filepaths],
|
|
|
|
|
ids=[fp.name for fp in filepaths],
|
|
|
|
|
)
|
2026-07-29 21:59:03 +03:00
|
|
|
|
|
|
|
|
|
2026-08-03 17:50:50 +05:00
|
|
|
def test_file(filepath: Path, expected_result: Result):
|
|
|
|
|
file = ifcopenshell.open(filepath)
|
2022-12-26 11:29:32 +01:00
|
|
|
logger = ifcopenshell.validate.json_logger()
|
|
|
|
|
ifcopenshell.express.rule_executor.run(file, logger)
|
|
|
|
|
results = logger.statements
|
|
|
|
|
|
|
|
|
|
print()
|
2026-08-03 17:50:50 +05:00
|
|
|
print(filepath.name)
|
2022-12-26 11:29:32 +01:00
|
|
|
print()
|
|
|
|
|
print(f"{len(results)} errors")
|
|
|
|
|
|
|
|
|
|
if results:
|
2024-07-26 12:00:28 +10:00
|
|
|
print(
|
|
|
|
|
tabulate.tabulate(
|
|
|
|
|
[[c or "" for c in r.values()] for r in results],
|
|
|
|
|
maxcolwidths=[20, 100, 20],
|
|
|
|
|
tablefmt="simple_grid",
|
|
|
|
|
headers=results[0].keys(),
|
|
|
|
|
)
|
|
|
|
|
)
|
2022-12-26 11:29:32 +01:00
|
|
|
|
2026-08-03 17:50:50 +05:00
|
|
|
match expected_result:
|
|
|
|
|
case FailObj(expected_count=expected_count):
|
|
|
|
|
assert len(results) == expected_count
|
|
|
|
|
case "pass":
|
|
|
|
|
assert len(results) == 0
|
2022-12-26 11:29:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2026-07-26 18:03:09 +10:00
|
|
|
pytest.main(["-sx", __file__, "--import-mode=importlib"])
|