-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: expect_override decorator to mark model methods safe for ove…
…rriding
- Loading branch information
Showing
5 changed files
with
60 additions
and
4 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,46 @@ | ||
from unittest import TestCase | ||
from unittest.mock import MagicMock | ||
|
||
from transitions import Machine | ||
from transitions.experimental.decoration import expect_override | ||
|
||
|
||
class TestExperimental(TestCase): | ||
|
||
def setUp(self) -> None: | ||
self.machine_cls = Machine | ||
return super().setUp() | ||
|
||
def test_override_decorator(self): | ||
b_mock = MagicMock() | ||
c_mock = MagicMock() | ||
|
||
class Model: | ||
|
||
@expect_override | ||
def is_A(self) -> bool: | ||
raise RuntimeError("Should be overridden") | ||
|
||
def is_B(self) -> bool: | ||
b_mock() | ||
return False | ||
|
||
@expect_override | ||
def is_C(self) -> bool: | ||
c_mock() | ||
return False | ||
|
||
model = Model() | ||
machine = self.machine_cls(model, states=["A", "B"], initial="A") | ||
self.assertTrue(model.is_A()) | ||
self.assertTrue(model.to_B()) | ||
self.assertFalse(model.is_B()) # not overridden with convenience function | ||
self.assertTrue(b_mock.called) | ||
self.assertFalse(model.is_C()) # not overridden yet | ||
self.assertTrue(c_mock.called) | ||
machine.add_state("C") | ||
self.assertFalse(model.is_C()) # now it is! | ||
self.assertEqual(1, c_mock.call_count) # call_count is not increased | ||
self.assertTrue(model.to_C()) | ||
self.assertTrue(model.is_C()) | ||
self.assertEqual(1, c_mock.call_count) |
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
Empty file.
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,8 @@ | ||
from typing import Callable, ParamSpec | ||
|
||
P = ParamSpec("P") | ||
|
||
|
||
def expect_override(func: Callable[P, bool | None]) -> Callable[P, bool | None]: | ||
setattr(func, "expect_override", True) | ||
return func |