Skip to content

Commit

Permalink
Fix doc & step
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreDecan committed Mar 15, 2018
1 parent ee73d11 commit 590547b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
6 changes: 3 additions & 3 deletions docs/behavior.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Given/when I reproduce "{scenario}"
:emphasize-lines: 6


Given/when I repeat step "{step}" {repeat:d} times
Given/when I repeat "{step}" {repeat:d} times

This step repeats given step (including its step keyword) several times.

Expand All @@ -189,8 +189,8 @@ Given/when I send event {name} with {parameter}={value}
:emphasize-lines: 3-5


Given/when {seconds:g} seconds elapsed
Given/when {seconds:g} second elapsed
Given/when I wait {seconds:g} seconds
Given/when I wait {seconds:g} second

These steps increase the internal clock of the interpreter.

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/elevator/elevator.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Feature: Elevator
Scenario: Elevator reaches ground floor after 10 seconds
When I reproduce "Elevator can move to 7th floor"
Then variable current equals 7
When 10 seconds elapsed
When I wait 10 seconds
Then variable current equals 0
And expression current == 0 holds

Expand Down
5 changes: 3 additions & 2 deletions docs/examples/microwave/heating_human.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Feature: Cook 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
And I press increase timer button 5 times
And I press increase power button
When I press start button
Then heating turns on
Expand All @@ -29,4 +29,5 @@ Feature: Cook food

Scenario: Lamp is on while cooking
When I reproduce "Cook food"
Then lamp turns on
Then lamp turns on

3 changes: 2 additions & 1 deletion docs/examples/microwave/heating_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
action_alias('I open the door', 'I send event door_opened')
action_alias('I close the door', 'I send event door_closed')
action_alias('I place an item in the oven', 'I send event item_placed')
action_alias('I press increase timer button', 'I send event timer_inc')
action_alias('I press increase timer button {time} times', 'I repeat "given I send event timer_inc" {time} times')
action_alias('I press increase power button', 'I send event power_inc')
action_alias('I press start button', 'I send event cooking_start')
action_alias('{tick} seconds elapsed', 'I repeat "given I send event timer_tick" {tick} times')

assertion_alias('Heating turns on', 'Event heating_on is fired')
assertion_alias('Heating does not turn on', 'Event heating_on is not fired')
Expand Down
54 changes: 34 additions & 20 deletions sismic/bdd/steps.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
from behave import given, when, then # type: ignore
from typing import Union, List

from ..interpreter import Event

__all__ = ['action_alias', 'assertion_alias']


def action_alias(step, step_to_execute) -> None:
def action_alias(step: str, step_to_execute: Union[str, List]) -> None:
"""
Create an alias of a predefined "given"/"when" step.
Create an alias of a predefined "given"/"when" step. For example:
action_alias('I open door', 'I send event open_door')
Parameters are propagated to the original step as well. For example:
action_alias('Event {name} has to be sent', 'I send event {name}')
Example: action_alias('I open door', 'I send event open_door')
You can provide more than on "step_to_execute" if you want to alias several steps by a single one.
:param step: New step, without the "given" or "when" keyword.
:param step_to_execute: existing step, without the "given" or "when" keyword
:param step_to_execute: existing step, without the "given" or "when" keyword. Could be a list of steps.
"""
step_to_execute = step_to_execute if isinstance(step_to_execute, str) else '\nand '.join(step_to_execute)

@given(step)
def _(context):
context.execute_steps('Given ' + step_to_execute)
def _(context, **kwargs):
context.execute_steps('Given ' + step_to_execute.format(**kwargs))

@when(step)
def _(context):
context.execute_steps('When ' + step_to_execute)
def _(context, **kwargs):
context.execute_steps('When ' + step_to_execute.format(**kwargs))


def assertion_alias(step, step_to_execute) -> None:
def assertion_alias(step: str, step_to_execute: Union[str, List]) -> None:
"""
Create an alias of a predefined "then" step.
Create an alias of a predefined "then" step. For example:
assertion_alias('door is open', 'state door open is active')
Example: assertion_alias('door is open', 'state door open is active')
Parameters are propagated to the original step as well. For example:
assertion_alias('{x} seconds elapsed', 'I wait for {x} seconds')
You can provide more than on "step_to_execute" if you want to alias several steps by a single one.
:param step: New step, without the "then" keyword
:param step_to_execute: existing step, without "then" keyword
:param step_to_execute: existing step, without "then" keyword. Could be a list of steps.
"""
step_to_execute = step_to_execute if isinstance(step_to_execute, str) else '\nand '.join(step_to_execute)

@then(step)
def _(context):
context.execute_steps('Then ' + step_to_execute)
def _(context, **kwargs):
context.execute_steps('Then ' + step_to_execute.format(**kwargs))


@given('I do nothing')
Expand All @@ -59,8 +73,8 @@ def __reproduce_scenario(context, scenario):
return reproduce_scenario(context, scenario, keyword='When')


@given('I repeat step "{step}" {repeat:d} times')
@when('I repeat step "{step}" {repeat:d} times')
@given('I repeat "{step}" {repeat:d} times')
@when('I repeat "{step}" {repeat:d} times')
def repeat_step(context, step, repeat):
keyword = step.split(' ', 1)[0].lower()
assert keyword in ['given', 'when', 'and', 'but', 'then'], \
Expand All @@ -87,10 +101,10 @@ def send_event(context, name, parameter=None, value=None):
context.interpreter.queue(event)


@given('{seconds:g} seconds elapsed')
@given('{seconds:g} second elapsed')
@when('{seconds:g} seconds elapsed')
@when('{seconds:g} second elapsed')
@given('I wait {seconds:g} seconds')
@given('I wait {seconds:g} second')
@when('I wait {seconds:g} seconds')
@when('I wait {seconds:g} second')
def wait(context, seconds):
context.interpreter.time += seconds

Expand Down

0 comments on commit 590547b

Please sign in to comment.