-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a mock State class that can be instantiated and queried while …
…testing (#2101) * Adding a testing State class that can be instantiated and queried during tests I hoped it would not have any impact ont the real code but it has. Any discussion is welcome before merge. resolves #2098 * tricky to get gui :-) * consistency * linter * test => mock more tests * following Long comments * Fab's right Co-authored-by: Fabien Lelaquais <[email protected]> --------- Co-authored-by: Fred Lefévère-Laoide <[email protected]> Co-authored-by: Fabien Lelaquais <[email protected]>
- Loading branch information
1 parent
5e58fd1
commit 691af71
Showing
6 changed files
with
293 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright 2021-2024 Avaiga Private Limited | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright 2021-2024 Avaiga Private Limited | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations under the License. | ||
import typing as t | ||
|
||
from .. import Gui, State | ||
from ..utils import _MapDict | ||
|
||
|
||
class MockState(State): | ||
"""A Mock implementation for `State`. | ||
TODO | ||
example of use: | ||
```py | ||
def test_callback(): | ||
ms = MockState(Gui(""), a = 1) | ||
on_action(ms) # function to test | ||
assert ms.a == 2 | ||
``` | ||
""" | ||
|
||
__VARS = "vars" | ||
|
||
def __init__(self, gui: Gui, **kwargs) -> None: | ||
super().__setattr__(MockState.__VARS, {k: _MapDict(v) if isinstance(v, dict) else v for k, v in kwargs.items()}) | ||
self._gui = gui | ||
super().__init__() | ||
|
||
def get_gui(self) -> Gui: | ||
return self._gui | ||
|
||
def __getattribute__(self, name: str) -> t.Any: | ||
if attr := t.cast(dict, super().__getattribute__(MockState.__VARS)).get(name): | ||
return attr | ||
try: | ||
return super().__getattribute__(name) | ||
except Exception: | ||
return None | ||
|
||
def __setattr__(self, name: str, value: t.Any) -> None: | ||
t.cast(dict, super().__getattribute__(MockState.__VARS))[name] = ( | ||
_MapDict(value) if isinstance(value, dict) else value | ||
) | ||
|
||
def __getitem__(self, key: str): | ||
return self | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
return True | ||
|
||
def broadcast(self, name: str, value: t.Any): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.