Uploaded image for project: 'JBehave'
  1. JBEHAVE-261

New syntax for Given with many parameters

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Core
    • Labels:
      None

      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);
      }
      
      }
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              brunobieth Bruno Bieth
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated: