Skip to content

Commit

Permalink
Reset generator hook after each "after" call
Browse files Browse the repository at this point in the history
  • Loading branch information
timofurrer committed Oct 26, 2020
1 parent 5295238 commit 156399a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/radish/hookregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ def __call__(self, *args, **kwargs):
except StopIteration:
pass # raised when the generator is exhausted

# reset the generator for the next "before" call.
# NOTE(TF): this introduces the thread-unsafety,
# which is fine for the moment, I guess ...
# A better implementation might be to use
# a factory approach which would create
# unique hook instances for each occurance.
self.generator = None

def __name__(self):
return self.func.name

Expand Down
37 changes: 37 additions & 0 deletions tests/integration/features/Generator-Hooks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,43 @@ Feature: Support Generator Hooks
from radish import when, each_scenario, after
@each_scenario(order=50)
def for_each_scenario(scenario):
scenario.context.number = 1
yield
scenario.context.number += 1
@after.each_scenario(order=100)
def assert_number(scenario):
assert scenario.context.number == 2, (
"number should have been 2 but was {}".format(
scenario.context.number
)
)
@when("there is a Step")
def some_step(step):
pass
"""
When the "generator-hooks.feature" is run
Then the exit code should be 0

Scenario: A simple Generator Hook used in multiple Scenarios
Given the Feature File "generator-hooks.feature"
"""
Feature: Some Feature
Scenario: Some Scenario
When there is a Step
Scenario: Some other Scenario
When there is a Step
"""
And the base dir module "steps.py"
"""
from radish import when, each_scenario, after
@each_scenario(order=50)
def for_each_scenario(scenario):
scenario.context.number = 1
Expand Down
57 changes: 57 additions & 0 deletions tests/unit/test_hookregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,63 @@ def func(x, y, foo=None):
assert second_foo == 5


def test_generatorhookimpls_can_be_wrapped_in_a_hookimpl():
# given
def func(x, y, foo=None):
x += 1
y += 1
foo += 1
yield x, y, foo
x += 1
y += 1
foo += 1
yield x, y, foo

gen_hook = GeneratorHookImpl(func)
hook = HookImpl("what", "when", gen_hook, [], 1)

# when
first_x, first_y, first_foo = hook(1, 2, foo=3)
second_x, second_y, second_foo = hook(1, 2, foo=3)

# then
assert first_x == 2
assert first_y == 3
assert first_foo == 4
assert second_x == 3
assert second_y == 4
assert second_foo == 5


def test_generatorhookimpls_can_be_wrapped_in_multiple_hookimpl():
# given
def func(x, y, foo=None):
x += 1
y += 1
foo += 1
yield x, y, foo
x += 1
y += 1
foo += 1
yield x, y, foo

gen_hook = GeneratorHookImpl(func)
hook_before = HookImpl("what", "when", gen_hook, [], 1)
hook_after = HookImpl("what", "when", gen_hook, [], 1)

# when
first_x, first_y, first_foo = hook_before(1, 2, foo=3)
second_x, second_y, second_foo = hook_after(1, 2, foo=3)

# then
assert first_x == 2
assert first_y == 3
assert first_foo == 4
assert second_x == 3
assert second_y == 4
assert second_foo == 5


def test_hookimpls_can_be_sorted_by_the_order():
"""The ``HookImpl``s can be sorted by it's order"""
# given
Expand Down

0 comments on commit 156399a

Please sign in to comment.