Details
-
Type: Improvement
-
Status: Open
-
Priority: Major
-
Resolution: Unresolved
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: Core
-
Labels:None
-
Number of attachments :
Description
A given step might look like :
Given a person (first name) Bruno (last name) Bieth born 2010-01-01 in Tombouctou living in Canada driving 10,000 miles per year
It can be hard to read. Passed a certain amount of parameters I suggest the following syntax :
Given a person - First Name : Bruno - Last Name : Bieth - Birth date : 2010-01-01 - Birth country : Tombouctou - Country of residency : Canada - Miles driven per year : 10,000
The java code would look like :
@GivenDetail( "a person" ) public void aPerson( @Named("First Name") String firstName, @Named("Last Name") String lastName, @Named("Birth date") DateTime birthDate, @Named("Birth country") String birthCountry, @Named("Country of residency") String countryOfResidency, @Named("Miles driven per year") Integer milesPerYear ) { person = new Person(firstName,lastName,birthDate,birthCountry,countryOfResidency,milesPerYear); }
The factory could use another CandidateSteps instance, say :
public class DetailedSteps implements CandidateSteps { private CandidateSteps delegate; private StepsConfiguration configuration; private Object instance; public DetailedSteps(StepsConfiguration configuration, CandidateSteps delegate, Object instance) { this.configuration = configuration; this.delegate = delegate; this.instance = instance; } @Override public CandidateStep[] getSteps() { return getSteps( instance.getClass() ); } @Override public CandidateStep[] getSteps(Class<?> stepsClass) { List<CandidateStep> steps = new ArrayList<CandidateStep>( Arrays.asList( delegate.getSteps(stepsClass))); for( Method method : stepsClass.getMethods() ) { GivenDetail annotation = method.getAnnotation(GivenDetail.class); if( annotation != null ) { steps.add( createGivenDetail( method, annotation ) ); } } return steps.toArray(new CandidateStep[steps.size()]); } private CandidateStep createGivenDetail(final Method method, GivenDetail annotation) { return new CandidateStep(annotation.value(), annotation.priority(), StepType.GIVEN, method, instance, new StepPatternBuilder() { @Override public String[] extractGroupNames(String string) { List<String> names = new ArrayList<String>(); for( Annotation[] annotations : method.getParameterAnnotations() ) { for( Annotation annotation : annotations ) { if( annotation instanceof Named ) { names.add( ((Named)annotation).value() ); } } } return names.toArray(new String[names.size()]); } @Override public Pattern buildPattern(String matchThis) { String[] groups = extractGroupNames(""); String pattern = matchThis.replaceAll(" ", "\\\\s+" ); for( String group : groups ) { pattern += "\\s+-\\s+" + group.replaceAll( " ", "\\\\s+" ) + "\\s*:\\s+(.*)"; } return Pattern.compile( pattern ); } },configuration.getParameterConverters(),configuration.getStartingWordsByType()); } @Override public List<Step> runAfterScenario() { return delegate.runAfterScenario(); } @Override public List<Step> runAfterStory(boolean embeddedStory) { return delegate.runAfterStory(embeddedStory); } @Override public List<Step> runBeforeScenario() { return delegate.runBeforeScenario(); } @Override public List<Step> runBeforeStory(boolean embeddedStory) { return delegate.runBeforeStory(embeddedStory); } }
Activity
Mauro Talevi
made changes -
Field | Original Value | New Value |
---|---|---|
Fix Version/s | 2.5.1 [ 16283 ] |
Have you considered using ExamplesTable? E.g.
Given a person with details
The only thing it does no have at present is the auto-conversion of the parameter values to Java type.
I would not think that should be too hard though.