Skip to content

Commit

Permalink
Merge d22284f into 4b015a3
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst committed May 21, 2019
2 parents 4b015a3 + d22284f commit c08abeb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void beforeScenario(Scenario scenario) {
List<Description> steps = new ArrayList<>(
testState.currentScenario.getChildren());
steps.removeAll(examples);
testState.stepDescriptions = getAllDescendants(steps).iterator();
testState.loadStepDescriptions(steps);
testState.moveToNextStep();
processBeforeScenario();
}
Expand All @@ -162,15 +162,6 @@ private List<Description> filterExamples(List<Description> children) {
return Collections.emptyList();
}

private Collection<Description> getAllDescendants(List<Description> steps) {
List<Description> descendants = new ArrayList<>();
for (Description child : steps) {
descendants.add(child);
descendants.addAll(getAllDescendants(child.getChildren()));
}
return descendants;
}

@Override
public void afterScenario() {
TestState testState = this.testState.get();
Expand Down Expand Up @@ -246,7 +237,7 @@ public void example(Map<String, String> tableRow, int exampleIndex) {
processAfterScenario();
}
testState.moveToNextExample();
testState.stepDescriptions = getAllDescendants(testState.currentExample.getChildren()).iterator();
testState.loadStepDescriptions(testState.currentExample.getChildren());
testState.moveToNextStep();
processBeforeScenario();
}
Expand All @@ -262,6 +253,10 @@ public void beforeStep(String title) {
}
if (testState.currentStepStatus == StepStatus.STARTED) {
testState.parentSteps.push(testState.currentStep);
// Composite Lifecycle Before/After story steps
if (testState.stepDescriptions == null) {
testState.loadStepDescriptions(testState.currentStep.getChildren());
}
testState.moveToNextStep();
}
notifier.fireTestStarted(testState.currentStep);
Expand Down Expand Up @@ -301,8 +296,9 @@ private void prepareNextStep() {
if (testState.currentStep.isTest()) {
testCounter.incrementAndGet();
}
// Lifecycle After story steps
if (testState.currentStep == testState.currentScenario) {
// Lifecycle Before/After story steps
if (testState.currentStep == testState.currentScenario || !testState.parentSteps.isEmpty()
&& testState.parentSteps.peekLast() == testState.currentScenario) {
testState.moveToNextScenario();
return;
}
Expand Down Expand Up @@ -394,6 +390,7 @@ private class TestState {
private void moveToNextScenario() {
currentScenario = getNextOrNull(scenarioDescriptions);
currentStep = currentScenario;
stepDescriptions = null;
}

private void moveToNextExample() {
Expand All @@ -408,9 +405,22 @@ private boolean isGivenStoryRunning() {
return givenStoryLevel != 0;
}

private void loadStepDescriptions(List<Description> steps) {
stepDescriptions = getAllDescendants(steps).iterator();
}

private <T> T getNextOrNull(Iterator<T> iterator) {
return iterator.hasNext() ? iterator.next() : null;
}

private Collection<Description> getAllDescendants(List<Description> steps) {
List<Description> descendants = new ArrayList<>();
for (Description child : steps) {
descendants.add(child);
descendants.addAll(getAllDescendants(child.getChildren()));
}
return descendants;
}
}

private enum StepStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,56 @@ public void shouldNotifyAboutLifecycleStorySteps() {
verifyStoryFinished();
}

@Test
public void shouldNotifyAboutCompositeLifecycleStorySteps() {
storyDescription = storyDescription.childlessCopy();

rootDescription = rootDescription.childlessCopy();
rootDescription.addChild(storyDescription);

addTestToStory("@BeforeStory");
Description beforeStoryStep = addTestToStory("before story step");
Description composedBefore = Description.createTestDescription(this.getClass(), "composedBefore");
beforeStoryStep.addChild(composedBefore);
storyDescription.addChild(scenarioDescription);
Description scenarioStep = addTestToScenario("scenario step");
Description afterStoryStep = addTestToStory("after story step");
Description composedAfter = Description.createTestDescription(this.getClass(), "composedAfter");
Description composedComposedAfter = Description.createTestDescription(this.getClass(), "composedComposedAfter");
composedAfter.addChild(composedComposedAfter);
afterStoryStep.addChild(composedAfter);
addTestToStory("@AfterStory");

reporter = new JUnitScenarioReporter(notifier, 5, rootDescription, keywords);

reportBeforeStory(story, false);
verifyStoryStarted();

reporter.beforeStep(beforeStoryStep.getDisplayName());
reportSuccessfulStep(composedBefore);
reporter.successful(beforeStoryStep.getDisplayName());
verifyStepSuccess(beforeStoryStep);

reportBeforeScenario(NAME_SCENARIO);
verifyScenarioStarted();

reportSuccessfulStep(scenarioStep);

reportScenarioFinish(reporter);
verifyScenarioFinished();

reporter.beforeStep(afterStoryStep.getDisplayName());
reporter.beforeStep(composedAfter.getDisplayName());
reportSuccessfulStep(composedComposedAfter);
reporter.successful(composedAfter.getDisplayName());
verifyStepSuccess(composedAfter);
reporter.successful(afterStoryStep.getDisplayName());
verifyStepSuccess(afterStoryStep);

reportStoryFinish(reporter);
verifyStoryFinished();
}

@Test
public void shouldNotNotifySubStepWithoutBeforeAction() {
scenarioDescription.addChild(Description.TEST_MECHANISM);
Expand Down Expand Up @@ -486,6 +536,12 @@ private void reportScenarioFinish(JUnitScenarioReporter reporter) {
Mockito.<Result> any());
}

private void reportSuccessfulStep(Description step) {
reporter.beforeStep(step.getDisplayName());
reporter.successful(step.getDisplayName());
verifyStepSuccess(step);
}

private void reportStepSuccess(JUnitScenarioReporter reporter) {
reporter.beforeStep("child");
reporter.successful("child");
Expand Down

0 comments on commit c08abeb

Please sign in to comment.