New syntax for Given with many parameters
-----------------------------------------

                 Key: JBEHAVE-261
                 URL: http://jira.codehaus.org/browse/JBEHAVE-261
             Project: JBehave
          Issue Type: Improvement
          Components: Core
            Reporter: Bruno Bieth
             Fix For: 2.5.1


A given step might look like : 
{code}Given a person (first name) Bruno (last name) Bieth born 2010-01-01 in 
Tombouctou living in Canada driving 10,000 miles per year{code}

It can be hard to read. Passed a certain amount of parameters I suggest the 
following syntax :

{code}
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
{code}

The java code would look like :
{code}
@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);
}
{code}

The factory could use another CandidateSteps instance, say :
{code}
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);
        }

}
{code}

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to