| 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 | |
import static java.util.Arrays.asList; |
| 12 | |
|
| 13 | |
public class PendingStepMethodGenerator { |
| 14 | |
|
| 15 | |
private static final String METHOD_SOURCE = "@{0}(\"{1}\")\n@{2}\npublic void {3}()'{'\n // {4}\n'}'\n"; |
| 16 | |
|
| 17 | |
private final Keywords keywords; |
| 18 | |
|
| 19 | 3 | public PendingStepMethodGenerator(Keywords keywords) { |
| 20 | 3 | this.keywords = keywords; |
| 21 | 3 | } |
| 22 | |
|
| 23 | |
public String generateMethod(PendingStep step) { |
| 24 | 3 | String stepAsString = step.stepAsString(); |
| 25 | 3 | String previousNonAndStepAsString = step.previousNonAndStepAsString(); |
| 26 | 3 | StepType stepType = null; |
| 27 | 3 | if ( keywords.isAndStep(stepAsString)) { |
| 28 | 1 | stepType = keywords.stepTypeFor(previousNonAndStepAsString); |
| 29 | |
} else { |
| 30 | 2 | stepType = keywords.stepTypeFor(stepAsString); |
| 31 | |
} |
| 32 | 3 | String stepPattern = keywords.stepWithoutStartingWord(stepAsString, stepType); |
| 33 | 3 | String stepAnnotation = StringUtils.capitalize(stepType.name().toLowerCase()); |
| 34 | 3 | String methodName = methodName(stepType, stepPattern); |
| 35 | 3 | String pendingAnnotation = Pending.class.getSimpleName(); |
| 36 | 3 | return format(METHOD_SOURCE, stepAnnotation, StringEscapeUtils.escapeJava(stepPattern), pendingAnnotation, methodName, keywords.pending()); |
| 37 | |
} |
| 38 | |
|
| 39 | |
private String methodName(StepType stepType, String stepPattern) { |
| 40 | 3 | String name = stepType.name().toLowerCase() + WordUtils.capitalize(stepPattern); |
| 41 | 3 | for (String remove : asList(" ", "\'", "\"", "\\.", "\\,", "\\;", "\\:", "\\!", "\\|", "<", ">", "\\*")) { |
| 42 | 36 | name = name.replaceAll(remove, ""); |
| 43 | |
} |
| 44 | 3 | return name; |
| 45 | |
} |
| 46 | |
|
| 47 | |
|
| 48 | |
} |