Skip to content

Commit

Permalink
replace underlines with spaces in parameter names (fixes #147)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Schäfer committed Oct 31, 2015
1 parent 4ce063e commit 4bca08a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
## New Features

* Scenarios without steps can now be excluded from the report by using the new `--exclude-empty-scenarios` report generator option [#151](https://github.com/TNG/JGiven/issues/151)
* Underlines in parameter names are now replaced with spaces [#147](https://github.com/TNG/JGiven/issues/147)

# v0.9.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,15 @@ public void setArguments( List<String> arguments ) {
}

public void setParameterNames( List<String> parameterNames ) {
scenarioModel.setExplicitParameters( parameterNames );
scenarioModel.setExplicitParameters( removeUnderlines( parameterNames ) );
}

private static List<String> removeUnderlines( List<String> parameterNames ) {
List<String> result = Lists.newArrayListWithCapacity( parameterNames.size() );
for( String paramName : parameterNames ) {
result.add( WordUtil.fromSnakeCase( paramName ) );
}
return result;
}

private String getDescription( Method paramMethod ) {
Expand Down Expand Up @@ -273,7 +281,7 @@ private List<String> getStackTrace( Throwable exception, boolean filterStackTrac
}

private static String nameWithoutUnderlines( Method paramMethod ) {
return paramMethod.getName().replace( '_', ' ' );
return WordUtil.fromSnakeCase( paramMethod.getName() );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,7 @@ public static String splitCamelCaseToReadableText( String camelCase ) {
);
}

public static String fromSnakeCase( String name ) {
return name.replace( '_', ' ' );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.tngtech.jgiven.impl.util.AnnotationUtil;
import com.tngtech.jgiven.impl.util.ApiUtil;
import com.tngtech.jgiven.impl.util.ReflectionUtil;
import com.tngtech.jgiven.impl.util.WordUtil;

public class StepFormatter {
public static final String DEFAULT_NUMBERED_HEADER = "#";
Expand Down Expand Up @@ -416,7 +417,7 @@ private void addArgument( List<Word> formattedWords, int argCount, CharSequence
"Parameters annotated with @Table must be the last ones. They cannot be used for $ substitution" );
}
String formattedValue = formatUsingFormatterOrNull( formatter, value );
String argumentName = arguments.get( index ).name;
String argumentName = WordUtil.fromSnakeCase( arguments.get( index ).name );

formattedWords.add( Word.argWord( argumentName, defaultFormattedValue, formattedValue ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.jgiven.StepFunction;
import com.tngtech.jgiven.annotation.Description;
import com.tngtech.jgiven.examples.coffeemachine.steps.GivenCoffee;
import com.tngtech.jgiven.examples.coffeemachine.steps.ThenCoffee;
Expand Down Expand Up @@ -67,10 +68,10 @@ public void not_enough_money_message_is_shown_when_insufficient_money_was_given(
"0, 5, Error: No coffees left",
"1, 5, Enjoy your coffee!",
} )
public void correct_messages_are_shown( int coffeesLeft, int numberOfCoins, String message ) throws Exception {
public void correct_messages_are_shown( int coffees_left, int number_of_coins, String message ) throws Exception {
given().a_coffee_machine()
.and().there_are_$_coffees_left_in_the_machine( coffeesLeft );
when().I_insert_$_one_euro_coins( numberOfCoins )
.and().there_are_$_coffees_left_in_the_machine( coffees_left );
when().I_insert_$_one_euro_coins( number_of_coins )
.and().I_press_the_coffee_button();
then().the_message_$_is_shown( message );
}
Expand All @@ -79,13 +80,13 @@ public void correct_messages_are_shown( int coffeesLeft, int numberOfCoins, Stri
@FeatureDataTables
@Issue( "#15" )
@DataProvider( { "1", "3", "10" } )
public void serving_a_coffee_reduces_the_number_of_available_coffees_by_one( int initialCoffees ) {
public void serving_a_coffee_reduces_the_number_of_available_coffees_by_one( int initial_coffees ) {
given().a_coffee_machine()
.and().there_are_$_coffees_left_in_the_machine( initialCoffees );
.and().there_are_$_coffees_left_in_the_machine( initial_coffees );
when().I_insert_$_one_euro_coins( 2 )
.and().I_press_the_coffee_button();
then().a_coffee_should_be_served()
.and().there_are_$_coffees_left_in_the_machine( initialCoffees - 1 );
.and().there_are_$_coffees_left_in_the_machine( initial_coffees - 1 );
}

@Test
Expand Down Expand Up @@ -161,4 +162,16 @@ public void intro_words_are_not_required() {
then().an_error_should_be_shown()
.no_coffee_should_be_served();
}

@Test( timeout = 1000 )
public void shouldFailWithUnexpectedRuntimeException() throws Exception {
then().$( "should throw a runtime exception", //$NON-NLS-1$
new StepFunction<ThenCoffee>() {
@Override
public void apply( final ThenCoffee stage )
throws Exception {
Thread.sleep( 2000 );
}
} );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public ThenCoffee an_error_should_be_shown() {
return self();
}

public ThenCoffee there_are_$_coffees_left_in_the_machine( int coffeesLeft ) {
assertThat( coffeeMachine.coffees ).isEqualTo( coffeesLeft );
public ThenCoffee there_are_$_coffees_left_in_the_machine( int coffees_left ) {
assertThat( coffeeMachine.coffees ).isEqualTo( coffees_left );
return self();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public void DataProviderRunner_can_be_used( int intArg, boolean booleanArg, int
assertThat( arguments ).containsExactly( "" + intArg, "" + booleanArg, "" + caseNr );
}

@Test
@DataProvider( { "0", "1" } )
public void underlines_in_parameters_are_replaced_with_spaces( int int_arg ) {
given().some_integer_value( int_arg );

List<String> explicitParameters = getScenario().getScenarioModel().getExplicitParameters();
assertThat( explicitParameters ).containsExactly( "int arg" );
}

@DataProvider
public static Object[][] trickyData() {
return new Object[][] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void some_stage_with_method_called_during_construction() {}

public void the_scenario_is_executed() {}

public void no_exeception_is_thrown() {
public void no_exception_is_thrown() {
assertThat( true ).as( "no exception is thrown" ).isTrue();
}

Expand Down Expand Up @@ -85,6 +85,6 @@ public static Object[][] primitiveArrays() {
public void step_methods_can_have_primitive_arrays_as_parameters( String type, Object array ) {
given().a_step_method_with_a_primitive_$_array_$_as_parameter( type, array );
when().the_scenario_is_executed();
then().no_exeception_is_thrown();
then().no_exception_is_thrown();
}
}

0 comments on commit 4bca08a

Please sign in to comment.