Skip to content

Commit

Permalink
Merge branch 'tommens-pr'
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreDecan committed Aug 6, 2018
2 parents 3638ac7 + d6531e2 commit 9a06d6b
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 57 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ install:
script:
- coverage run --source sismic -m pytest
- cd docs && make doctest && cd ..
- python -m unittest docs/examples/microwave
after_success:
- coveralls
matrix:
Expand Down
14 changes: 7 additions & 7 deletions docs/behavior.rst
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ more intuitive to use when writing scenarios.

Consider the following scenarios expressed using a domain-specific language:

.. literalinclude:: examples/microwave/heating_human.feature
.. literalinclude:: examples/microwave/cooking_human.feature
:language: gherkin


Expand Down Expand Up @@ -324,23 +324,23 @@ predefined steps, Sismic provides two convenient helpers to map new steps to pre

Using these helpers, one can easily implement the domain-specific steps of our example:

.. literalinclude:: examples/microwave/heating_steps.py
.. literalinclude:: examples/microwave/steps.py
:language: python


Assuming that the features are defined in ``heating.feature``, these steps in ``steps.py``, and the microwave in
Assuming that the features are defined in ``cooking.feature``, these steps in ``steps.py``, and the microwave in
``microwave.yaml``, then ``sismic-bdd`` can be used as follows:

.. code-block:: none
$ sismic-bdd microwave.yaml --steps steps.py --features heating.feature
$ sismic-bdd microwave.yaml --steps steps.py --features cooking.feature
Feature: Cook food # heating.feature:1
Feature: Cooking # cooking.feature:1
[...]
1 feature passed, 0 failed, 0 skipped
5 scenarios passed, 0 failed, 0 skipped
21 steps passed, 0 failed, 0 skipped, 0 undefined
3 scenarios passed, 0 failed, 0 skipped
17 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.040s
26 changes: 26 additions & 0 deletions docs/examples/microwave/cooking_human.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Feature: Cooking

Scenario: Start cooking food
Given I open the door
And I place an item in the oven
And I close the door
And I press increase timer button 5 times
And I press increase power button
When I press start button
Then heating turns on

Scenario: Stop cooking food
Given I reproduce "Start cooking food"
When 2 seconds elapsed
Then variable timer equals 3
When I press stop button
Then variable timer equals 0
And heating turns off

Scenario: Cooking stops after preset time
Given I reproduce "Start cooking food"
When 5 seconds elapsed
Then variable timer equals 0
And heating turns off


41 changes: 0 additions & 41 deletions docs/examples/microwave/heating_human.feature

This file was deleted.

24 changes: 24 additions & 0 deletions docs/examples/microwave/lighting_human.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Feature: Lighting

Scenario: Lamp is on when door is open
When I open the door
Then lamp turns on

Scenario: Lamp is off when door is closed
Given I reproduce "Lamp is on when door is open"
When I close the door
Then lamp turns off

Scenario: Lamp is on while cooking
Given I open the door
And I place an item in the oven
And I close the door
And I press increase timer button 5 times
When I press start button
Then lamp turns on

Scenario: Lamp turns off after cooking
Given I reproduce "Lamp is on while cooking"
When I press stop button
Then lamp turns off

18 changes: 18 additions & 0 deletions docs/examples/microwave/safety_human.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Feature: Safety criterion

Background: Start cooking food
Given I open the door
And I place an item in the oven
And I close the door
And I press increase timer button 5 times

Scenario: NO cooking when door is not closed
Given I open the door
When I press start button
Then heating does not turn on

Scenario: Opening door interrupts cooking
Given I press start button
And 3 seconds elapsed
When I open the door
Then heating turns off
File renamed without changes.
33 changes: 33 additions & 0 deletions docs/examples/microwave/test_microwave.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
from sismic.io import import_from_yaml
from sismic.interpreter import Interpreter


class MicrowaveTests(unittest.TestCase):
def setUp(self):
with open('microwave.yaml') as f:
sc = import_from_yaml(f)

self.oven = Interpreter(sc)
self.oven.execute_once()

def test_no_heating_when_door_is_not_closed(self):
self.oven.queue('door_opened', 'item_placed', 'timer_inc')
self.oven.execute()

self.oven.queue('cooking_start')

for step in iter(self.oven.execute_once, None):
for event in step.sent_events:
self.assertNotEqual(event.name, 'heating_on')

self.assertNotIn('cooking_mode', self.oven.configuration)

def test_increase_timer(self):
self.oven.queue('door_opened', 'item_placed', 'door_closed')

events = 10 * ['timer_inc']
self.oven.queue(*events)
self.oven.execute()

self.assertEqual(self.oven.context['timer'], 10)
17 changes: 10 additions & 7 deletions tests/test_bdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,30 @@ def test_microwave_with_properties(self, microwave, property_statecharts):
)

def test_microwave_with_steps(self, microwave):
features = ['cooking_human', 'lighting_human', 'safety_human']

assert 0 == execute_bdd(
microwave.statechart,
[os.path.join('docs', 'examples', 'microwave', 'heating_human.feature')],
step_filepaths=[os.path.join('docs', 'examples', 'microwave', 'heating_steps.py')],
[os.path.join('docs', 'examples', 'microwave', f+'.feature') for f in features],
step_filepaths=[os.path.join('docs', 'examples', 'microwave', 'steps.py')],
)

def test_microwave_with_steps_and_properties(self, microwave, property_statecharts):
features = ['cooking_human', 'lighting_human', 'safety_human']

assert 0 == execute_bdd(
microwave.statechart,
[os.path.join('docs', 'examples', 'microwave', 'heating_human.feature')],
step_filepaths=[os.path.join('docs', 'examples', 'microwave', 'heating_steps.py')],
[os.path.join('docs', 'examples', 'microwave', f+'.feature') for f in features],
step_filepaths=[os.path.join('docs', 'examples', 'microwave', 'steps.py')],
property_statecharts=property_statecharts
)


def test_cli():
assert 0 == cli([
'docs/examples/microwave/microwave.yaml',
'--features', 'docs/examples/microwave/heating.feature', 'docs/examples/microwave/heating_human.feature',
'--steps', 'docs/examples/microwave/heating_steps.py',
'--features', 'docs/examples/microwave/heating.feature', 'docs/examples/microwave/cooking_human.feature', 'docs/examples/microwave/lighting_human.feature', 'docs/examples/microwave/safety_human.feature',
'--steps', 'docs/examples/microwave/steps.py',
'--properties', 'docs/examples/microwave/heating_on_property.yaml', 'docs/examples/microwave/heating_off_property.yaml'
])

Expand Down Expand Up @@ -274,4 +277,4 @@ def test_not_final_configuration(self, context):

context.interpreter.final = True
with pytest.raises(AssertionError):
steps.not_final_configuration(context)
steps.not_final_configuration(context)
8 changes: 6 additions & 2 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import pytest
import os

from subprocess import check_call

from sismic.interpreter import Event
from sismic import testing

Expand Down Expand Up @@ -90,7 +95,6 @@ def test_heating_on(self, microwave):

microwave.queue(Event('cooking_start'))
steps = microwave.execute()

assert testing.event_is_fired(steps, 'heating_on')
assert testing.event_is_fired(steps, 'lamp_switch_on')
assert testing.event_is_fired(steps, 'turntable_start')
assert testing.event_is_fired(steps, 'turntable_start')

0 comments on commit 9a06d6b

Please sign in to comment.