diff --git a/CHANGELOG.md b/CHANGELOG.md index 37bcca19da..da36331d9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/report/analysis/CaseArgumentAnalyser.java b/jgiven-core/src/main/java/com/tngtech/jgiven/report/analysis/CaseArgumentAnalyser.java index 077d06fef8..46be4a5dc9 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/report/analysis/CaseArgumentAnalyser.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/report/analysis/CaseArgumentAnalyser.java @@ -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 ); } @@ -89,11 +90,9 @@ private boolean wordsAreEqual( List firstWords, List words ) { } private static final class CaseArguments { - final ScenarioCaseModel caseModel; final List arguments; - private CaseArguments( ScenarioCaseModel model, List arguments ) { - this.caseModel = model; + private CaseArguments( List arguments ) { this.arguments = arguments; } @@ -149,13 +148,6 @@ private void reduceMatrix( ScenarioModel scenarioModel, List 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 ); @@ -192,8 +184,6 @@ private Collection getPossibleParameterNames( List arguments ) { static class ParameterMatch { String parameter; int index; - boolean formattedValueMatches; } static class ArgumentHolder { @@ -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(); @@ -285,8 +274,9 @@ private Set 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 ); + } } } } diff --git a/jgiven-examples/src/test/java/com/tngtech/jgiven/examples/parameters/ParameterFormattingTest.java b/jgiven-examples/src/test/java/com/tngtech/jgiven/examples/parameters/ParameterFormattingTest.java new file mode 100644 index 0000000000..bdbc82902c --- /dev/null +++ b/jgiven-examples/src/test/java/com/tngtech/jgiven/examples/parameters/ParameterFormattingTest.java @@ -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 { + + @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 ) {} + + } + +} diff --git a/jgiven-tests/src/test/java/com/tngtech/jgiven/report/model/GivenReportModel.java b/jgiven-tests/src/test/java/com/tngtech/jgiven/report/model/GivenReportModel.java index 256b2ed251..bfd4cd89da 100644 --- a/jgiven-tests/src/test/java/com/tngtech/jgiven/report/model/GivenReportModel.java +++ b/jgiven-tests/src/test/java/com/tngtech/jgiven/report/model/GivenReportModel.java @@ -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; @@ -20,6 +24,8 @@ public class GivenReportModel> extends Stage" ) + .and().the_report_contains_text( "" ) + .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 {