Skip to content

Commit

Permalink
Change behavior for DefaultAsProvider.
Browse files Browse the repository at this point in the history
This patch changes the behavior for the DefaultAsProvider by moving the
case of absent @as annotations into the provider. This allows us to just
fall back to the default provider and allows users to utilize the default
behavior by extending from DefaultAsProvider.

relates to TNG#189
  • Loading branch information
Airblader committed Jul 11, 2016
1 parent 8eff753 commit 41e46d0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@
@Documented
public @interface As {

/**
* Dummy value to indicate that no value was set
*/
public static final String NO_VALUE = " - no value - ";

/**
* The alternate representation of the step or scenario summary.
*/
String value();
String value() default NO_VALUE;

/**
* An optional provider to generate the representation of the stage method, scenario or scenario class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.CaseFormat;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
Expand All @@ -28,6 +27,7 @@
import com.tngtech.jgiven.format.ObjectFormatter;
import com.tngtech.jgiven.impl.format.ParameterFormattingUtil;
import com.tngtech.jgiven.impl.intercept.ScenarioListener;
import com.tngtech.jgiven.impl.params.DefaultAsProvider;
import com.tngtech.jgiven.impl.util.AnnotationUtil;
import com.tngtech.jgiven.impl.util.AssertionUtil;
import com.tngtech.jgiven.impl.util.ReflectionUtil;
Expand Down Expand Up @@ -72,7 +72,7 @@ public void scenarioStarted( String description ) {
if( description.contains( "_" ) ) {
readableDescription = description.replace( '_', ' ' );
} else if( !description.contains( " " ) ) {
readableDescription = camelCaseToCapitalizedReadableText( description );
readableDescription = WordUtil.camelCaseToCapitalizedReadableText( description );
}

scenarioCaseModel = new ScenarioCaseModel();
Expand All @@ -82,14 +82,6 @@ public void scenarioStarted( String description ) {
scenarioModel.setDescription( readableDescription );
}

private static String camelCaseToCapitalizedReadableText( String camelCase ) {
return WordUtil.capitalize( camelCaseToReadableText( camelCase ) );
}

private static String camelCaseToReadableText( String camelCase ) {
return CaseFormat.LOWER_CAMEL.to( CaseFormat.LOWER_UNDERSCORE, camelCase ).replace( '_', ' ' );
}

public void addStepMethod( Method paramMethod, List<NamedArgument> arguments, InvocationMode mode, boolean hasNestedSteps ) {
StepModel stepModel = createStepModel( paramMethod, arguments, mode );

Expand Down Expand Up @@ -216,13 +208,16 @@ private String getDescription( Method paramMethod ) {
if( description != null ) {
return description.value();
}

As as = paramMethod.getAnnotation( As.class );
if( as != null ) {
AsProvider provider = ReflectionUtil.newInstance( as.provider() );
return provider.as( as, paramMethod );
AsProvider provider;
if( as == null ) {
provider = new DefaultAsProvider();
} else {
provider = ReflectionUtil.newInstance( as.provider() );
}

return nameWithSpaces( paramMethod );
return provider.as( as, paramMethod );
}

public void setSuccess( boolean success ) {
Expand Down Expand Up @@ -252,14 +247,6 @@ private List<String> getStackTrace( Throwable exception, boolean filterStackTrac
return stackTrace;
}

private static String nameWithSpaces( Method paramMethod ) {
String paraMethodName = paramMethod.getName();
if( paramMethod.getName().contains( "_" ) ) {
return WordUtil.fromSnakeCase( paraMethodName );
}
return camelCaseToReadableText( paraMethodName );
}

@Override
public void stepMethodFailed( Throwable t ) {
if( currentStep != null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import com.tngtech.jgiven.annotation.As;
import com.tngtech.jgiven.annotation.AsProvider;
import com.tngtech.jgiven.config.AbstractJGivenConfiguration;
import com.tngtech.jgiven.config.ConfigurationUtil;
import com.tngtech.jgiven.impl.util.WordUtil;

/**
* The default provider for a stage method, scenario or scenario class.
Expand All @@ -12,19 +15,42 @@
public class DefaultAsProvider implements AsProvider {

/**
* Returns the value of the {@link As} annotation.
* Returns the value of the {@link As} annotation if a value was specified.
* Otherwise, this transforms the method name into a readable sentence and returns it.
*/
@Override
public String as( As annotation, Method method ) {
return annotation.value();
if( annotation != null && annotationHasExplicitValue( annotation ) ) {
return annotation.value();
}

String methodName = method.getName();
if( method.getName().contains( "_" ) ) {
return WordUtil.fromSnakeCase( methodName );
}

return WordUtil.camelCaseToReadableText( methodName );
}

/**
* Returns the value of the {@link As} annotation.
* Otherwise, this transforms the class name into a readable sentence and returns it.
*/
@Override
public String as( As annotation, Class<?> scenarioClass ) {
return annotation.value();
if( annotation != null && annotationHasExplicitValue( annotation ) ) {
return annotation.value();
}

AbstractJGivenConfiguration configuration = ConfigurationUtil.getConfiguration( scenarioClass );
String regEx = configuration.getTestClassSuffixRegEx();
String classNameWithoutSuffix = scenarioClass.getSimpleName().replaceAll( regEx + "$", "" );

return WordUtil.splitCamelCaseToReadableText( classNameWithoutSuffix );
}

private boolean annotationHasExplicitValue( As annotation ) {
return !As.NO_VALUE.equals( annotation.value() );
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tngtech.jgiven.impl.util;

import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
Expand Down Expand Up @@ -65,6 +66,14 @@ public static String splitCamelCaseToReadableText( String camelCase ) {
);
}

public static String camelCaseToCapitalizedReadableText( String camelCase ) {
return WordUtil.capitalize( camelCaseToReadableText( camelCase ) );
}

public static String camelCaseToReadableText( String camelCase ) {
return CaseFormat.LOWER_CAMEL.to( CaseFormat.LOWER_UNDERSCORE, camelCase ).replace( '_', ' ' );
}

public static String fromSnakeCase( String name ) {
return name.replace( '_', ' ' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
import com.tngtech.jgiven.annotation.As;
import com.tngtech.jgiven.annotation.AsProvider;
import com.tngtech.jgiven.annotation.Description;
import com.tngtech.jgiven.config.AbstractJGivenConfiguration;
import com.tngtech.jgiven.config.ConfigurationUtil;
import com.tngtech.jgiven.impl.params.DefaultAsProvider;
import com.tngtech.jgiven.impl.util.AssertionUtil;
import com.tngtech.jgiven.impl.util.ReflectionUtil;
import com.tngtech.jgiven.impl.util.WordUtil;

public class ReportModel {
/**
Expand Down Expand Up @@ -184,21 +182,14 @@ public synchronized void setTestClass( Class<?> testClass ) {
setDescription( testClass.getAnnotation( Description.class ).value() );
}

if( testClass.isAnnotationPresent( As.class ) ) {
As as = testClass.getAnnotation( As.class );

AsProvider provider = ReflectionUtil.newInstance( as.provider() );
name = provider.as( as, testClass );
As as = testClass.getAnnotation( As.class );
AsProvider provider;
if( as == null ) {
provider = new DefaultAsProvider();
} else {
name = getTestNameToReadableString( testClass );
provider = ReflectionUtil.newInstance( as.provider() );
}
}

private String getTestNameToReadableString( Class<?> testClass ) {
AbstractJGivenConfiguration configuration = ConfigurationUtil.getConfiguration( testClass );
String regEx = configuration.getTestClassSuffixRegEx();
String classNameWithoutSuffix = testClass.getSimpleName().replaceAll( regEx + "$", "" );
return WordUtil.splitCamelCaseToReadableText( classNameWithoutSuffix );
name = provider.as( as, testClass );
}

public String getName() {
Expand Down

0 comments on commit 41e46d0

Please sign in to comment.