Skip to content

Commit

Permalink
Add unbind() to interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreDecan committed Aug 17, 2018
1 parent 0b6de26 commit 5fe80e5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
Unreleased
----------

- (Added) Some documentation about running multiple statecharts.
- (Added) An ``unbind`` method for an ``Interpreter``.
- (Fixed) Hook-errors reported by ``sismic-bdd`` CLI are a little bit more verbose (`#81 <https://github.com/AlexandreDecan/sismic/issues/81>`__).


Expand Down
4 changes: 4 additions & 0 deletions docs/concurrent.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ is sent both to ``interpreter_1`` and ``interpreter_2``.
Events for interpreter_2: Event('test')
Events for interpreter_3: InternalEvent('test')

.. note::

Bound interpreters or callables can be unbound using the :py:meth:`~sismic.interpreter.Interpreter.unbind` method.


Example of communicating statecharts
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
11 changes: 11 additions & 0 deletions sismic/interpreter/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ def bind(self, interpreter_or_callable: Union['Interpreter', Callable[[Event], A
else:
self._bound.append(interpreter_or_callable)

def unbind(self, interpreter_or_callable: Union['Interpreter', Callable[[Event], Any]]) -> None:
"""
Unbind a previously bound interpreter or callable.
:param interpreter_or_callable: interpreter or callable to unbind
"""
if isinstance(interpreter_or_callable, Interpreter):
self._bound.remove(interpreter_or_callable.queue)
else:
self._bound.remove(interpreter_or_callable)

def bind_property_statechart(self, statechart_or_interpreter: Union[Statechart, 'Interpreter']) -> None:
"""
Bind a property statechart to the current interpreter.
Expand Down
15 changes: 15 additions & 0 deletions tests/test_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,21 @@ def test_bind_callable(self, interpreter):
assert i2._select_event(consume=False) == Event('test')
assert not isinstance(i2._select_event(consume=False), InternalEvent)

def test_unbind(self, interpreter):
i1, i2 = interpreter

i1.bind(i2)
i1.unbind(i2)
assert i2.queue not in i1._bound

i1.bind(i2.queue)
i1.unbind(i2)
assert i2.queue not in i1._bound

i1.bind(i2.queue)
i1.unbind(i2.queue)
assert i2.queue not in i1._bound

def test_metaevent(self, interpreter):
i1, i2 = interpreter

Expand Down

0 comments on commit 5fe80e5

Please sign in to comment.