| 1 | |
package org.jbehave.core.steps; |
| 2 | |
|
| 3 | |
import org.apache.commons.lang.StringEscapeUtils; |
| 4 | |
import org.apache.commons.lang.StringUtils; |
| 5 | |
import org.apache.commons.lang.WordUtils; |
| 6 | |
import org.jbehave.core.annotations.Pending; |
| 7 | |
import org.jbehave.core.configuration.Keywords; |
| 8 | |
import org.jbehave.core.steps.StepCreator.PendingStep; |
| 9 | |
|
| 10 | |
import static java.text.MessageFormat.format; |
| 11 | |
|
| 12 | |
public class PendingStepMethodGenerator { |
| 13 | |
|
| 14 | |
private static final String METHOD_SOURCE = "@{0}(\"{1}\")\n@{2}\npublic void {3}() '{'\n // {4}\n'}'\n"; |
| 15 | |
|
| 16 | |
private final Keywords keywords; |
| 17 | |
|
| 18 | 3 | public PendingStepMethodGenerator(Keywords keywords) { |
| 19 | 3 | this.keywords = keywords; |
| 20 | 3 | } |
| 21 | |
|
| 22 | |
public String generateMethod(PendingStep step) { |
| 23 | 5 | String stepAsString = step.stepAsString(); |
| 24 | 5 | String previousNonAndStepAsString = step.previousNonAndStepAsString(); |
| 25 | 5 | StepType stepType = null; |
| 26 | 5 | if (keywords.isAndStep(stepAsString) && previousNonAndStepAsString != null) { |
| 27 | 1 | stepType = keywords.stepTypeFor(previousNonAndStepAsString); |
| 28 | |
} else { |
| 29 | 4 | stepType = keywords.stepTypeFor(stepAsString); |
| 30 | |
} |
| 31 | 5 | String stepPattern = keywords.stepWithoutStartingWord(stepAsString, stepType); |
| 32 | 5 | String stepAnnotation = StringUtils.capitalize(stepType.name().toLowerCase()); |
| 33 | 5 | String methodName = methodName(stepType, stepPattern); |
| 34 | 5 | String pendingAnnotation = Pending.class.getSimpleName(); |
| 35 | 5 | return format(METHOD_SOURCE, stepAnnotation, StringEscapeUtils.escapeJava(stepPattern), pendingAnnotation, |
| 36 | |
methodName, keywords.pending()); |
| 37 | |
} |
| 38 | |
|
| 39 | |
private String methodName(StepType stepType, String stepPattern) { |
| 40 | 5 | String name = stepType.name().toLowerCase() + WordUtils.capitalize(stepPattern); |
| 41 | 5 | char filteredName[]=new char[name.length()]; |
| 42 | 5 | int index=0; |
| 43 | 375 | for(int i=0;i<name.length();i++) { |
| 44 | 370 | char ch=name.charAt(i); |
| 45 | 370 | if(Character.isJavaIdentifierPart(ch) && ch!='$' && ch!=127) { |
| 46 | 254 | filteredName[index++]=ch; |
| 47 | |
} |
| 48 | |
} |
| 49 | 5 | return new String(filteredName,0,index); |
| 50 | |
} |
| 51 | |
|
| 52 | |
} |