Skip to content

Commit

Permalink
Correctly handle nested steps in parametrized scenarios.
Browse files Browse the repository at this point in the history
Fixes #248
  • Loading branch information
Jan Schäfer committed Nov 1, 2016
1 parent 0fd9f7e commit b1a23a8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ In order to fix issue #239, a backwards incompatible change had to be done:
## Bug Fixes

* TestNG: executing test methods in parallel is now possible. [#239](https://github.com/TNG/JGiven/issues/239)
* Correctly handle nested steps in parametrized scenarios. [#248](https://github.com/TNG/JGiven/issues/248)

# v0.12.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,24 +241,32 @@ private boolean isStructuralIdentical( ScenarioModel scenarioModel ) {
return true;
}

private boolean stepsAreDifferent( ScenarioCaseModel firstCase, ScenarioCaseModel caseModel ) {
if( firstCase.getSteps().size() != caseModel.getSteps().size() ) {
boolean stepsAreDifferent( ScenarioCaseModel firstCase, ScenarioCaseModel secondCase ) {
return stepsAreDifferent(firstCase.getSteps(), secondCase.getSteps());
}

boolean stepsAreDifferent(List<StepModel> firstSteps, List<StepModel> secondSteps) {
if( firstSteps.size() != secondSteps.size() ) {
return true;
}

for( int iStep = 0; iStep < firstCase.getSteps().size(); iStep++ ) {
StepModel firstStep = firstCase.getStep( iStep );
StepModel stepModel = caseModel.getStep( iStep );
for(int iStep = 0; iStep < firstSteps.size(); iStep++ ) {
StepModel firstStep = firstSteps.get( iStep );
StepModel secondStep = secondSteps.get( iStep );

if( firstStep.getWords().size() != secondStep.getWords().size() ) {
return true;
}

if( firstStep.getWords().size() != stepModel.getWords().size() ) {
if( attachmentsAreStructurallyDifferent( firstStep.getAttachments(), secondStep.getAttachments() ) ) {
return true;
}

if( attachmentsAreStructurallyDifferent( firstStep.getAttachments(), stepModel.getAttachments() ) ) {
if( wordsAreDifferent( firstStep, secondStep ) ) {
return true;
}

if( wordsAreDifferent( firstStep, stepModel ) ) {
if (stepsAreDifferent(firstStep.getNestedSteps(), secondStep.getNestedSteps())) {
return true;
}
}
Expand Down Expand Up @@ -380,7 +388,7 @@ List<List<Word>> getDifferentArguments( List<List<Word>> argumentWords ) {
return result;
}

private List<List<Word>> collectArguments( ScenarioModel scenarioModel ) {
List<List<Word>> collectArguments( ScenarioModel scenarioModel ) {
List<List<Word>> argumentWords = Lists.newArrayList();

for( ScenarioCaseModel scenarioCaseModel : scenarioModel.getScenarioCases() ) {
Expand All @@ -402,13 +410,19 @@ private boolean argumentCountDiffer( List<List<Word>> argumentWords ) {

private List<Word> findArgumentWords( ScenarioCaseModel scenarioCaseModel ) {
List<Word> arguments = Lists.newArrayList();
for( StepModel step : scenarioCaseModel.getSteps() ) {
List<StepModel> steps = scenarioCaseModel.getSteps();
findArgumentWords(steps, arguments);
return arguments;
}

private void findArgumentWords(List<StepModel> steps, List<Word> arguments) {
for( StepModel step : steps) {
for( Word word : step.getWords() ) {
if( word.isArg() ) {
arguments.add( word );
}
}
findArgumentWords(step.getNestedSteps(), arguments);
}
return arguments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import com.tngtech.jgiven.report.analysis.CaseArgumentAnalyser.JoinedArgs;
import com.tngtech.jgiven.report.model.AttachmentModel;
import com.tngtech.jgiven.report.model.DataTable;
import com.tngtech.jgiven.report.model.ScenarioCaseModel;
import com.tngtech.jgiven.report.model.ScenarioModel;
import com.tngtech.jgiven.report.model.StepModel;
import com.tngtech.jgiven.report.model.Word;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -110,4 +113,35 @@ public void equal_data_tables_are_found() {
assertThat( analyser.getDifferentArguments( cases ).get( 0 ) ).isEmpty();

}

@Test
public void nested_steps_are_considered_in_structure_analysis() {
ScenarioCaseModel case0 = caseWithNestedStep("foo");
ScenarioCaseModel case1 = caseWithNestedStep("bar");
assertThat(analyser.stepsAreDifferent(case0, case1)).isTrue();
}

@Test
public void arguments_of_nested_steps_are_collected() {
Word word = Word.argWord("testName", "testValue", "testValue");
ScenarioCaseModel case0 = caseWithNestedStep(word);
ScenarioModel model = new ScenarioModel();
model.addCase(case0);
assertThat(analyser.collectArguments(model).get(0)).contains(word);

}

private ScenarioCaseModel caseWithNestedStep(String nestedStepName) {
return caseWithNestedStep(new Word(nestedStepName));
}

private ScenarioCaseModel caseWithNestedStep(Word word) {
ScenarioCaseModel case0 = new ScenarioCaseModel();
StepModel step0 = new StepModel();
StepModel nestedStep0 = new StepModel();
nestedStep0.addWords(word);
step0.addNestedStep(nestedStep0);
case0.addStep(step0);
return case0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ public void arguments_with_the_same_name_but_different_values_are_handled_correc
}
}

@Test
@DataProvider( {"1", "2"})
public void differences_in_nested_steps_should_be_detected(Integer methodParameter) throws Throwable {
given().a_nested_step(methodParameter + 1);
getScenario().finished();

ScenarioModel scenarioModel = getScenario().getModel().getLastScenarioModel();
if( scenarioModel.getScenarioCases().size() == 2 ) {
CaseArgumentAnalyser analyser = new CaseArgumentAnalyser();
analyser.analyze( scenarioModel );
ScenarioCaseModel case0 = scenarioModel.getCase( 0 );
assertParameter( case0, 0, "nestedStepArg" );

Word word = case0.getStep( 0 ).getNestedSteps().get(0).getWords().get( 1 );
assertThat( word.getArgumentInfo().getParameterName() ).isEqualTo( "someIntValue" );
}

}

@Test
@DataProvider( { "1", "2" } )
public void table_parameters_work_with_primitive_arrays( Integer arg ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import com.google.common.collect.Lists;
import com.tngtech.jgiven.Stage;
import com.tngtech.jgiven.annotation.Format;
import com.tngtech.jgiven.annotation.Hidden;
import com.tngtech.jgiven.annotation.IsTag;
import com.tngtech.jgiven.annotation.NestedSteps;
import com.tngtech.jgiven.annotation.ProvidedScenarioState;
import com.tngtech.jgiven.annotation.Quoted;
import com.tngtech.jgiven.annotation.Table;
Expand Down Expand Up @@ -38,6 +40,11 @@ public void some_boolean_value( boolean someBooleanValue ) {

}

@NestedSteps
public void a_nested_step(Integer nestedStepArg) {
some_integer_value(nestedStepArg+1);
}

@IsTag
@Retention( RetentionPolicy.RUNTIME )
@interface StepMethodTag {}
Expand Down

0 comments on commit b1a23a8

Please sign in to comment.