Test that str/repr of user-facing objects never raise#974
Conversation
There was a problem hiding this comment.
Pull request overview
Adds an integration regression test intended to catch crashes in user-facing __str__/__repr__ implementations (per #152) by iterating through objects from several parsed-problem fixtures and calling str()/repr() on each.
Changes:
- Add
_user_facing_objects(problem)generator to enumerate problem/collections/members/data inputs and selected cell modifiers. - Add parametrized integration test to assert
str()/repr()calls do not raise across multiple fixtures.
| def _user_facing_objects(problem): | ||
| """Yield a problem's user-facing objects: the problem, every collection | ||
| and its members, the data-block inputs, and the cell-block modifiers.""" | ||
| yield problem | ||
| for collection in ( | ||
| problem.cells, | ||
| problem.surfaces, | ||
| problem.materials, | ||
| problem.universes, | ||
| problem.transforms, | ||
| ): | ||
| yield collection | ||
| yield from collection | ||
| # data-block inputs, e.g. ``Volume``, ``Mode`` (see the original report) | ||
| yield from problem.data_inputs | ||
| # cell-block modifier objects | ||
| for cell in problem.cells: | ||
| yield cell.importance | ||
| yield cell.fill | ||
|
|
There was a problem hiding this comment.
Added back then as well: the helper now yields input_file, message, title, and the problem level mode (skipping None for problems without a message). Also just merged develop in and added a type hint to the helper in d5dcd3d.
Add a regression test exercising `str()` and `repr()` across the problem, its top-level objects (input file, message, title, mode), every object collection and its members, the data-block inputs (e.g. `Volume`), and the cell-block modifiers, over four fixtures covering both the cell-block and data-block forms. Guards against `__repr__` accessing an attribute that is not populated in every context (e.g. a `Volume` parsed in the data block). The test only asserts the result is a string, since an empty string is a valid representation. Fixes idaholab#152
bb0c327 to
b2ee4ee
Compare
|
@yrasool Please make sure to use the full PR template. |
HI Michael I appreciate the feedback and I do apologize for not using the format. I did edit my description again according to format. |
Description
#152 asked for unit tests so that broken custom
__str__/__repr__methods get caught before users hit them. The original report was aVolume.__repr__crash because_volumeis not set when aVolumeis parsed in the data block.This adds one regression test that calls
str()andrepr()on every user-facing object of a parsed problem and asserts neither raises. That covers the problem itself, its top-level objects (input file, message, title, mode), each object collection and its members, the data-block inputs, and the cell-block modifiers. It runs over four fixtures so both the cell-block and data-block forms are exercised.Fixes #152
General Checklist
blackversion 25 or 26.LLM Disclosure
Are you?
Were any large language models (LLM or "AI") used in to generate any of this code?
Documentation Checklist
(Documentation checklist N/A: test-only change, no new public classes or methods.)
Additional Notes for Reviewers
Ensure that:
📚 Documentation preview 📚: https://montepy--974.org.readthedocs.build/en/974/