| 178 | |
| 179 | |
| 180 | class TestResult: |
| 181 | name: str = "" |
| 182 | time: float = float('inf') |
| 183 | test_msg: str = "" |
| 184 | extra_info: str = "" |
| 185 | |
| 186 | # there should be only one result be True. |
| 187 | __unique_state: Result = None |
| 188 | |
| 189 | def __init__(self, **kwargs) -> None: |
| 190 | # set all attr from metaclass |
| 191 | for result_name, result_cls in MetaResult.cls_map().items(): |
| 192 | setattr(self, result_name, result_cls.default) |
| 193 | |
| 194 | # overwrite attr from kwargs |
| 195 | for name, value in kwargs.items(): |
| 196 | # check attr name |
| 197 | if not (hasattr(self, name) or name in MetaResult.cls_map()): |
| 198 | raise KeyError(f'`{name}` is not a valid result type.') |
| 199 | |
| 200 | setattr(self, name, value) |
| 201 | |
| 202 | if name in MetaResult.cls_map() and value: |
| 203 | if self.__unique_state is not None: |
| 204 | logger.warning('Only one result state should be True.') |
| 205 | |
| 206 | self.__unique_state = MetaResult.get(name) |
| 207 | |
| 208 | if self.__unique_state is None: |
| 209 | logger.warning('Default result will be set to FAILED!') |
| 210 | setattr(self, RFailed.name, True) |
| 211 | self.__unique_state = RFailed |
| 212 | |
| 213 | @property |
| 214 | def state(self) -> Result: |
| 215 | return self.__unique_state |
| 216 | |
| 217 | def __str__(self) -> str: |
| 218 | return f'{self.name}, running time: {self.time:.3f}s' |
| 219 | |
| 220 | |
| 221 | class DocTester: |