Skip to content

Commit

Permalink
BDD steps that involve a state raises a StatechartError if state …
Browse files Browse the repository at this point in the history
…does not exist.
  • Loading branch information
AlexandreDecan committed Apr 18, 2018
1 parent e69575e commit 64f893d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

1.0.1 (2018-04-18)
------------------

- (Fixed) BDD steps that involve a state raises a ``StatechartError`` if state does not exist.
This prevents *state X is active* (and its variants) to fail, e.g., because *X* is misspelled.


1.0.0 (2018-04-11)
------------------

Expand Down
2 changes: 1 addition & 1 deletion sismic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__package__ = 'sismic'
__version__ = '1.0.0.post1'
__version__ = '1.0.1'
__licence__ = 'LGPL3'
__author__ = 'Alexandre Decan'
__url__ = 'https://github.com/AlexandreDecan/sismic/'
Expand Down
18 changes: 18 additions & 0 deletions sismic/bdd/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def wait(context, seconds):

@then('state {name} is entered')
def state_is_entered(context, name):
# Check that state exists
context.interpreter.statechart.state_for(name)

for macrostep in context.monitored_trace:
if name in macrostep.entered_states:
return
Expand All @@ -71,13 +74,19 @@ def state_is_entered(context, name):

@then('state {name} is not entered')
def state_is_not_entered(context, name):
# Check that state exists
context.interpreter.statechart.state_for(name)

for macrostep in context.monitored_trace:
if name in macrostep.entered_states:
assert False, 'State {} is entered'.format(name)


@then('state {name} is exited')
def state_is_exited(context, name):
# Check that state exists
context.interpreter.statechart.state_for(name)

for macrostep in context.monitored_trace:
if name in macrostep.exited_states:
return
Expand All @@ -86,18 +95,27 @@ def state_is_exited(context, name):

@then('state {name} is not exited')
def state_is_not_exited(context, name):
# Check that state exists
context.interpreter.statechart.state_for(name)

for macrostep in context.monitored_trace:
if name in macrostep.exited_states:
assert False, 'State {} is exited'.format(name)


@then('state {name} is active')
def state_is_active(context, name):
# Check that state exists
context.interpreter.statechart.state_for(name)

assert name in context.interpreter.configuration, 'State {} is not active'.format(name)


@then('state {name} is not active')
def state_is_not_active(context, name):
# Check that state exists
context.interpreter.statechart.state_for(name)

assert name not in context.interpreter.configuration, 'State {} is active'.format(name)


Expand Down
21 changes: 21 additions & 0 deletions tests/test_bdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os

from sismic.exceptions import StatechartError
from sismic.interpreter import Event
from sismic.bdd import steps, execute_bdd
from sismic.bdd.__main__ import cli
Expand Down Expand Up @@ -71,6 +72,14 @@ def context(self, mocker):
context.interpreter = mocker.MagicMock(name='interpreter')
context.interpreter.queue = mocker.MagicMock(name='queue')
context.interpreter.context = {'x': 1}

def state_for(name):
if name == 'unknown state':
raise StatechartError()
else:
return mocker.DEFAULT

context.interpreter.statechart.state_for = mocker.MagicMock(side_effect=state_for)
context.execute_steps = mocker.MagicMock(name='execute_steps')
context.monitored_trace = []

Expand Down Expand Up @@ -121,6 +130,8 @@ def test_state_is_entered(self, context, trace):
steps.state_is_entered(context, 'c')
with pytest.raises(AssertionError):
steps.state_is_entered(context, 'state')
with pytest.raises(StatechartError):
steps.state_is_entered(context, 'unknown state')

def test_state_is_not_entered(self, context, trace):
context.monitored_trace = []
Expand All @@ -130,6 +141,8 @@ def test_state_is_not_entered(self, context, trace):
with pytest.raises(AssertionError):
steps.state_is_not_entered(context, 'a')
steps.state_is_not_entered(context, 'state')
with pytest.raises(StatechartError):
steps.state_is_entered(context, 'unknown state')

def test_state_is_exited(self, context, trace):
context.monitored_trace = []
Expand All @@ -142,6 +155,8 @@ def test_state_is_exited(self, context, trace):
steps.state_is_exited(context, 'z')
with pytest.raises(AssertionError):
steps.state_is_entered(context, 'state')
with pytest.raises(StatechartError):
steps.state_is_entered(context, 'unknown state')

def test_state_is_not_exited(self, context, trace):
context.monitored_trace = []
Expand All @@ -151,6 +166,8 @@ def test_state_is_not_exited(self, context, trace):
with pytest.raises(AssertionError):
steps.state_is_not_exited(context, 'x')
steps.state_is_not_exited(context, 'state')
with pytest.raises(StatechartError):
steps.state_is_entered(context, 'unknown state')

def test_state_is_active(self, context):
context.interpreter.configuration = []
Expand All @@ -163,6 +180,8 @@ def test_state_is_active(self, context):
steps.state_is_active(context, 'c')
with pytest.raises(AssertionError):
steps.state_is_active(context, 'state')
with pytest.raises(StatechartError):
steps.state_is_entered(context, 'unknown state')

def test_state_is_not_active(self, context):
context.interpreter.configuration = []
Expand All @@ -174,6 +193,8 @@ def test_state_is_not_active(self, context):
steps.state_is_not_active(context, 'a')

steps.state_is_not_active(context, 'state')
with pytest.raises(StatechartError):
steps.state_is_entered(context, 'unknown state')

def test_event_is_fired(self, context, trace):
context.monitored_trace = []
Expand Down

0 comments on commit 64f893d

Please sign in to comment.