mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-19 19:54:07 +00:00
validate fixtures: validate expected error count
This commit is contained in:
@@ -1,66 +0,0 @@
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Literal, TypeAlias
|
||||
|
||||
import ifcopenshell
|
||||
|
||||
|
||||
@dataclass
|
||||
class FailObj:
|
||||
expected_count: int = 1
|
||||
|
||||
|
||||
PassResult: TypeAlias = Literal["pass"]
|
||||
|
||||
Result = FailObj | PassResult
|
||||
|
||||
|
||||
def normalize_header(f: ifcopenshell.file) -> None:
|
||||
f.header.file_name.time_stamp = "1970-01-01T00:00:00"
|
||||
f.header.file_name.preprocessor_version = "IfcOpenShell"
|
||||
f.header.file_name.originating_system = "IfcOpenShell"
|
||||
|
||||
|
||||
def pass_if(condition: bool, *, errors_if_fail: int = 1) -> Result:
|
||||
return "pass" if condition else FailObj(expected_count=errors_if_fail)
|
||||
|
||||
|
||||
def fail_if(condition: bool, *, errors_if_fail: int = 1) -> Result:
|
||||
return pass_if(not condition, errors_if_fail=errors_if_fail)
|
||||
|
||||
|
||||
def parse_result(fixture_path: Path) -> Result:
|
||||
name = fixture_path.name
|
||||
if name.startswith("fail-"):
|
||||
expected_count = 1
|
||||
if match := re.search(r"^fail-expected-(\d+)", name):
|
||||
expected_count = int(match.group(1))
|
||||
return FailObj(expected_count=expected_count)
|
||||
if name.startswith("pass-"):
|
||||
return "pass"
|
||||
raise ValueError(f"Fixture filename must start with 'fail-' or 'pass-': {name}")
|
||||
|
||||
|
||||
def write_fixture(file: ifcopenshell.file, source: str, result: Result, stem: str) -> None:
|
||||
content = file.to_string()
|
||||
content += f"/* IFC fixture is generated by {Path(source).name} */\n"
|
||||
match result:
|
||||
case FailObj(expected_count=expected_count):
|
||||
prefix = "fail" if expected_count == 1 else f"fail-expected-{expected_count}"
|
||||
case _:
|
||||
prefix = result
|
||||
Path(f"{prefix}-{stem}.ifc").write_text(content)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
directory = Path(__file__).parent
|
||||
|
||||
for path in directory.glob("*.ifc"):
|
||||
path.unlink()
|
||||
|
||||
for path in sorted(directory.glob("generate_*.py")):
|
||||
print(f"=== {path.name} ===")
|
||||
subprocess.run([sys.executable, str(path)], cwd=directory, check=True)
|
||||
@@ -1,7 +1,7 @@
|
||||
import time
|
||||
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
for i in range(3):
|
||||
f = ifcopenshell.file(schema="IFC2X3")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
for i, box_alignment in enumerate(["top-left", "center", "invalid", "CENTER"]):
|
||||
f = ifcopenshell.file(schema="IFC2X3")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import time
|
||||
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
latitudes = [
|
||||
(False, (-361, 0, 0), 1),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
for depth in (-1.0, 0.0, 1.0):
|
||||
f = ifcopenshell.file(schema="IFC2X3")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import fail_if, normalize_header, write_fixture
|
||||
from ...fixture_generate import fail_if, normalize_header, write_fixture
|
||||
|
||||
for i, (r1, r2) in enumerate(
|
||||
[("SUPPLIER", None), ("SUPPLIER", "Valid"), ("USERDEFINED", "Valid"), ("USERDEFINED", None)]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
options = {
|
||||
"IfcPostalAddress": ({}, {"Country": "The Netherlands"}, {"Country": "The Netherlands", "Town": "Eindhoven"}),
|
||||
|
||||
@@ -2,7 +2,7 @@ import itertools
|
||||
import time
|
||||
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
for pty, ety in itertools.product(("GRILLE", "USERDEFINED"), (None, "Something")):
|
||||
f = ifcopenshell.file(schema="IFC2X3")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import fail_if, normalize_header, write_fixture
|
||||
from ...fixture_generate import fail_if, normalize_header, write_fixture
|
||||
|
||||
create_none = lambda f: None
|
||||
create_polyline = lambda f: f.createIfcPolyline(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import fail_if, normalize_header, write_fixture
|
||||
from ...fixture_generate import fail_if, normalize_header, write_fixture
|
||||
|
||||
create_plane = lambda f: f.createIfcPlane(f.createIfcAxis2Placement3D(f.createIfcCartesianPoint((0.0, 0.0, 0.0))))
|
||||
create_polyline = lambda f: f.createIfcPolyline(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
pts = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0)]
|
||||
dims = [(2, 3, 3), (3, 3, 2), (2, 2, 2), (3, 3, 3)]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import itertools
|
||||
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
defaults = {"Girth": 1.0, "WallThickness": 0.11}
|
||||
depths = [2.0, 3.0]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import ifcopenshell
|
||||
import ifcopenshell.util.schema
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
depth = 1.0
|
||||
for dir_x, dir_z in ((0.0, -1.0), (0.0, 0.0), (1.0, 0.0), (1.0, 0.001), (0.0, 1.0)):
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, write_fixture
|
||||
from ...fixture_generate import normalize_header, write_fixture
|
||||
|
||||
coords = [(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)]
|
||||
make_3d = lambda cs: [c + (0.0,) for c in cs]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, write_fixture
|
||||
from ...fixture_generate import normalize_header, write_fixture
|
||||
|
||||
coords = [(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)]
|
||||
make_3d = lambda cs: [c + (0.0,) for c in cs]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
dims = [
|
||||
(1, 0, 0, 0, 0, 0, 0),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
coords = [(0.0, 0.0), (10.0, 0.0)]
|
||||
coords_2 = [(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import time
|
||||
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
names = [("Same", "Same"), ("Different", "SomethingElse")]
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import time
|
||||
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
for i, nm in enumerate(("no-mls", "mls", "mlsu")):
|
||||
f = ifcopenshell.file(schema="IFC2X3")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
for i, type_decl in enumerate(("IfcLengthMeasure", "IfcPlaneAngleMeasure")):
|
||||
f = ifcopenshell.file(schema="IFC4X3")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
make_nd = lambda d: lambda *c: (c + (0.0,)) if d == 3 else c
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
task = lambda f: f.createIfcTask(ifcopenshell.guid.new(), None, "sleep", IsMilestone=True)
|
||||
wall = lambda f: f.createIfcWall(ifcopenshell.guid.new())
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import itertools
|
||||
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
|
||||
def EXISTS(v):
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
for cnt in range(0, 3):
|
||||
f = ifcopenshell.file(schema="IFC4")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
fns = [
|
||||
(False, lambda f: f.createIfcLengthMeasure(-1.0)),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import time
|
||||
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, pass_if, write_fixture
|
||||
from ...fixture_generate import normalize_header, pass_if, write_fixture
|
||||
|
||||
for ents in [
|
||||
("IfcWall",),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import fail_if, normalize_header, write_fixture
|
||||
from ...fixture_generate import fail_if, normalize_header, write_fixture
|
||||
|
||||
segs = [
|
||||
lambda _: None,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, write_fixture
|
||||
from ...fixture_generate import normalize_header, write_fixture
|
||||
|
||||
cases = (
|
||||
("pass", "label-values", ("test1", "test2")),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ifcopenshell
|
||||
from generate import normalize_header, write_fixture
|
||||
from ...fixture_generate import normalize_header, write_fixture
|
||||
|
||||
ifc_file = ifcopenshell.file(schema="IFC2X3")
|
||||
ifc_file.create_entity(
|
||||
|
||||
Reference in New Issue
Block a user