Files
IfcOpenShell/src/bonsai/test/core/bootstrap.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

307 lines
7.0 KiB
Python
Raw Normal View History

2024-08-13 15:47:27 +10:00
# Bonsai - OpenBIM Blender Add-on
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
#
2024-08-13 15:47:27 +10:00
# This file is part of Bonsai.
#
2024-08-13 15:47:27 +10:00
# Bonsai is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
2024-08-13 15:47:27 +10:00
# Bonsai is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2024-08-13 15:47:27 +10:00
# along with Bonsai. If not, see <http://www.gnu.org/licenses/>.
import sys
import json
import pytest
2024-08-14 15:41:52 +05:00
import bonsai.core.tool
2024-08-02 16:15:47 +05:00
from typing import Any, Optional
from typing_extensions import Self
@pytest.fixture
def ifc():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Ifc)
yield prophet
prophet.verify()
@pytest.fixture
def blender():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Blender)
yield prophet
prophet.verify()
2021-11-04 18:02:50 +11:00
@pytest.fixture
def brick():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Brick)
2021-11-04 18:02:50 +11:00
yield prophet
prophet.verify()
@pytest.fixture
def aggregate():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Aggregate)
yield prophet
prophet.verify()
@pytest.fixture
def collector():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Collector)
yield prophet
prophet.verify()
@pytest.fixture
def context():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Context)
yield prophet
prophet.verify()
@pytest.fixture
def debug():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Debug)
yield prophet
prophet.verify()
@pytest.fixture
def demo():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Demo)
yield prophet
prophet.verify()
@pytest.fixture
def document():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Document)
yield prophet
prophet.verify()
@pytest.fixture
def drawing():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Drawing)
yield prophet
prophet.verify()
@pytest.fixture
def geometry():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Geometry)
yield prophet
prophet.verify()
@pytest.fixture
def georeference():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Georeference)
yield prophet
prophet.verify()
@pytest.fixture
def library():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Library)
yield prophet
prophet.verify()
@pytest.fixture
def material():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Material)
yield prophet
prophet.verify()
@pytest.fixture
def misc():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Misc)
yield prophet
prophet.verify()
@pytest.fixture
def nest():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Nest)
yield prophet
prophet.verify()
@pytest.fixture
def owner():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Owner)
yield prophet
prophet.verify()
@pytest.fixture
def patch():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Patch)
yield prophet
prophet.verify()
@pytest.fixture
def project():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Project)
yield prophet
prophet.verify()
@pytest.fixture
def pset():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Pset)
yield prophet
prophet.verify()
@pytest.fixture
def qto():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Qto)
yield prophet
prophet.verify()
@pytest.fixture
def root():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Root)
yield prophet
prophet.verify()
@pytest.fixture
def selector():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Selector)
yield prophet
prophet.verify()
2022-08-30 11:08:26 +10:00
@pytest.fixture
def sequence():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Sequence)
2022-08-30 11:08:26 +10:00
yield prophet
prophet.verify()
@pytest.fixture
def spatial():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Spatial)
yield prophet
prophet.verify()
@pytest.fixture
def style():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Style)
yield prophet
prophet.verify()
2021-10-04 13:14:38 +11:00
@pytest.fixture
def surveyor():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Surveyor)
2021-10-04 13:14:38 +11:00
yield prophet
prophet.verify()
@pytest.fixture
def system():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.System)
yield prophet
prophet.verify()
@pytest.fixture
def type():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Type)
yield prophet
prophet.verify()
@pytest.fixture
def unit():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Unit)
yield prophet
prophet.verify()
@pytest.fixture
def voider():
2024-08-14 15:41:52 +05:00
prophet = Prophecy(bonsai.core.tool.Voider)
yield prophet
prophet.verify()
class Prophecy:
def __init__(self, cls):
self.subject = cls
2024-04-25 11:24:42 +05:00
self.predictions: list[dict] = []
self.calls: list[dict] = []
self.return_values: dict[str, Any] = {}
self.should_call: Optional[dict] = None
2024-04-25 11:24:42 +05:00
def __getattr__(self, attr: str):
if not hasattr(self.subject, attr):
raise AttributeError(f"Prophecy {self.subject} has no attribute {attr}")
def decorate(*args, **kwargs):
call = {"name": attr, "args": args, "kwargs": kwargs}
# Ensure that signature is valid
getattr(self.subject, attr)(*args, **kwargs)
2025-02-04 13:36:28 +11:00
key = json.dumps(call, sort_keys=True)
self.calls.append(call)
if key in self.return_values:
return self.return_values[key]
return self
return decorate
def should_be_called(self, number=None):
self.should_call = self.calls.pop()
self.predictions.append({"type": "SHOULD_BE_CALLED", "number": number, "call": self.should_call})
return self
2024-04-25 11:24:42 +05:00
def will_return(self, value: Any) -> Self:
key = json.dumps(self.should_call, sort_keys=True)
self.return_values[key] = value
return self
2024-04-25 11:24:42 +05:00
def verify(self) -> None:
predicted_calls = []
for prediction in self.predictions:
predicted_calls.append(prediction["call"])
if prediction["type"] == "SHOULD_BE_CALLED":
self.verify_should_be_called(prediction)
for call in self.calls:
if call not in predicted_calls:
raise Exception(f"Unpredicted call: {call}")
2024-04-25 11:24:42 +05:00
def verify_should_be_called(self, prediction: dict) -> None:
if prediction["number"]:
count = self.calls.count(prediction["call"])
if count != prediction["number"]:
raise Exception(f"Called {count}: {prediction}")
else:
if prediction["call"] not in self.calls:
error_msg = f"{self.subject} was not called with {prediction['call']['name']}:\n - {prediction}"
# Print all unprocessed calls if pytest was started in verbose mode.
if "-v" in sys.argv or "-vv" in sys.argv:
if not self.calls:
error_msg += "\nNo unprocessed calls."
else:
error_msg += "\nUnprocessed calls:"
for call in self.calls:
error_msg += f"\n - {call}"
raise Exception(error_msg)