Skip to content

Commit

Permalink
Fixed the issue that different formatted parameters having the same v…
Browse files Browse the repository at this point in the history
…alue in all cases were collapsed to a single parameter (fixes #104)
  • Loading branch information
Jan Schäfer committed Aug 26, 2015
1 parent 1b7c85f commit 33eb875
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 21 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# v0.8.1

* nothing yet
## Fixed Issues

* Fixed an issue where different formatted parameters having the same value in all cases were collapsed to a single parameter [#104](https://github.com/TNG/JGiven/issues/104)

# v0.8.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public void analyze( ScenarioModel scenarioModel ) {
}

} catch( IndexOutOfBoundsException e ) {
log.info( "Scenario model " + scenarioModel.getClassName() + "." + scenarioModel.getTestMethodName() + " has no homogene cases."
log.info( "Scenario model " + scenarioModel.getClassName() + "." + scenarioModel.getTestMethodName()
+ " has no homogene cases."
+ " Cannot analyse argument cases" );
scenarioModel.setCasesAsTable( false );
}
Expand Down Expand Up @@ -89,11 +90,9 @@ private boolean wordsAreEqual( List<Word> firstWords, List<Word> words ) {
}

private static final class CaseArguments {
final ScenarioCaseModel caseModel;
final List<ArgumentHolder> arguments;

private CaseArguments( ScenarioCaseModel model, List<ArgumentHolder> arguments ) {
this.caseModel = model;
private CaseArguments( List<ArgumentHolder> arguments ) {
this.arguments = arguments;
}

Expand Down Expand Up @@ -149,13 +148,6 @@ private void reduceMatrix( ScenarioModel scenarioModel, List<CaseArguments> argu
replacement.match = match;
replacement.replacementName = match.parameter;

if( usedParameters.containsKey( match.parameter ) ) {
ParameterReplacement usedReplacement = usedParameters.get( match.parameter );
if( match.formattedValueMatches && !usedReplacement.match.formattedValueMatches ) {
usedReplacement.updateToStepParameterName( usedNames );
}
}

usedNames.add( replacement.replacementName );
usedParameters.put( match.parameter, replacement );

Expand Down Expand Up @@ -192,8 +184,6 @@ private Collection<ParameterMatch> getPossibleParameterNames( List<CaseArguments
for( String key : Lists.newArrayList( result.keySet() ) ) {
if( !map.containsKey( key ) ) {
result.remove( key );
} else {
result.get( key ).formattedValueMatches &= map.get( key ).formattedValueMatches;
}
}
}
Expand Down Expand Up @@ -230,7 +220,6 @@ private boolean allArgumentsAreEqual( List<Word> arguments ) {
static class ParameterMatch {
String parameter;
int index;
boolean formattedValueMatches;
}

static class ArgumentHolder {
Expand Down Expand Up @@ -258,13 +247,13 @@ public CollectPhase( ScenarioModel model ) {
public void visit( ScenarioCaseModel scenarioCase ) {
currentCase = scenarioCase;
argumentsOfCurrentCase = Lists.newArrayList();
argumentMatrix.add( new CaseArguments( currentCase, argumentsOfCurrentCase ) );
argumentMatrix.add( new CaseArguments( argumentsOfCurrentCase ) );
allWordsOfCurrentCase = Lists.newArrayList();
allWords.add( allWordsOfCurrentCase );
}

@Override
public void visit( StepModel stepModel) {
public void visit( StepModel stepModel ) {
for( Word word : stepModel.words ) {
if( word.isArg() ) {
ArgumentHolder holder = new ArgumentHolder();
Expand All @@ -285,8 +274,9 @@ private Set<ParameterMatch> getMatchingParameters( Word word ) {
ParameterMatch match = new ParameterMatch();
match.index = i;
match.parameter = scenarioModel.getExplicitParameters().get( i );
match.formattedValueMatches = Objects.equal( word.getFormattedValue(), argumentValue );
matchingParameters.add( match );
if( Objects.equal( word.getFormattedValue(), argumentValue ) ) {
matchingParameters.add( match );
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.tngtech.jgiven.examples.parameters;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.jgiven.annotation.Format;
import com.tngtech.jgiven.format.BooleanFormatter;
import com.tngtech.jgiven.junit.SimpleScenarioTest;

@RunWith( DataProviderRunner.class )
public class ParameterFormattingTest extends SimpleScenarioTest<ParameterFormattingTest.TestSteps> {

@Test
@DataProvider( {
"true, true",
"false, false"
} )
public void parameters_can_be_formatted( boolean onOff, boolean isOrIsNot ) {

given().a_machine_that_is( onOff );
then().the_power_light_$_on( isOrIsNot );

}

public static class TestSteps {

public void a_machine_that_is( @Format( value = BooleanFormatter.class, args = { "on", "off" } ) boolean onOff ) {}

public void the_power_light_$_on( @Format( value = BooleanFormatter.class, args = { "is", "is not" } ) boolean isOrIsNot ) {}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import java.util.List;

import com.tngtech.jgiven.Stage;
import com.tngtech.jgiven.annotation.*;
import com.tngtech.jgiven.annotation.AfterStage;
import com.tngtech.jgiven.annotation.ExtendedDescription;
import com.tngtech.jgiven.annotation.ProvidedScenarioState;
import com.tngtech.jgiven.annotation.Quoted;
import com.tngtech.jgiven.annotation.Table;
import com.tngtech.jgiven.attachment.Attachment;
import com.tngtech.jgiven.attachment.MediaType;
import com.tngtech.jgiven.report.analysis.CaseArgumentAnalyser;
Expand All @@ -20,6 +24,8 @@ public class GivenReportModel<SELF extends GivenReportModel<?>> extends Stage<SE
private Tag latestTag;
private Word latestWord;

private Word lastArgWord;

@ExtendedDescription( "A report model where the analysers have not been executed on" )
public SELF an_unanalyzed_report_model_with_one_scenario() {
analyze = false;
Expand Down Expand Up @@ -185,10 +191,16 @@ private ScenarioCaseModel getCase( int ncase ) {

public SELF case_$_has_a_when_step_$_with_argument_$_and_argument_name_$( int ncase, @Quoted String name, @Quoted String arg,
@Quoted String argName ) {
lastArgWord = Word.argWord( argName, arg, arg );
getCase( ncase )
.addStep(
new StepModel( name,
Arrays.asList( Word.introWord( "when" ), new Word( name ), Word.argWord( argName, arg, (String) null ) ) ) );
Arrays.asList( Word.introWord( "when" ), new Word( name ), lastArgWord ) ) );
return self();
}

public SELF formatted_value( @Quoted String formattedValue ) {
lastArgWord.getArgumentInfo().setFormattedValue( formattedValue );
return self();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,37 @@ public void data_tables_are_generated_correctly_in_text_reports() throws Unsuppo
" | 3 | 1234567 | arg31 | Success |\n" );
}

@Test
@FeatureDataTables
@Issue( "#104" )
public void parameters_with_equal_values_but_different_formatting_result_in_different_placeholders()
throws UnsupportedEncodingException {
given()
.a_report_model_with_one_scenario()
.and().the_scenario_has_$_cases( 2 )
.and().parameters( "aParam", "anotherParam" )
.given().case_$_has_arguments( 1, "false", "false" )
.and().case_$_has_a_when_step_$_with_argument_$_and_argument_name_$( 1, "some arg step", "false", "anArg" )
.with().formatted_value( "off" )
.and().case_$_has_a_when_step_$_with_argument_$_and_argument_name_$( 1, "another arg step", "false", "anotherArg" )
.with().formatted_value( "is not" )
.given().case_$_has_arguments( 2, "true", "true" )
.and().case_$_has_a_when_step_$_with_argument_$_and_argument_name_$( 2, "some arg step", "true", "anArg" )
.with().formatted_value( "on" )
.and().case_$_has_a_when_step_$_with_argument_$_and_argument_name_$( 2, "another arg step", "true", "anotherArg" )
.with().formatted_value( "is" );

when().the_plain_text_report_is_generated();

then().the_report_contains_text( "<anArg>" )
.and().the_report_contains_text( "<anotherArg>" )
.and().the_report_contains_text( "\n" +
" | # | anArg | anotherArg | Status |\n" +
" +---+-------+------------+---------+\n" +
" | 1 | off | is not | Success |\n" +
" | 2 | on | is | Success |\n" );
}

@Test
@FeatureDataTables
public void data_tables_are_generated_for_empty_strings() throws UnsupportedEncodingException {
Expand Down

0 comments on commit 33eb875

Please sign in to comment.