- Revision
- 1363
- Author
- mauro
- Date
- 2009-10-26 15:38:12 -0500 (Mon, 26 Oct 2009)
Log Message
JBEHAVE-196: Introduced StepType enum and refactored CandidateStep to be aware of type. The starting word must now match the type (in the appropriate locale). Deprecated the Steps(String[] startingWords) constructors as these cannot resolve the word by type.
Modified Paths
- trunk/core/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/steps/LolCatzSteps.java
- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java
- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/trader_sells_all_stocks.scenario
- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/CandidateStepBehaviour.java
- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/ConfusingStepsBehavior.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/CandidateStep.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/StepsConfiguration.java
Added Paths
Diff
Modified: trunk/core/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/steps/LolCatzSteps.java (1362 => 1363)
--- trunk/core/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/steps/LolCatzSteps.java 2009-10-24 12:55:39 UTC (rev 1362) +++ trunk/core/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/steps/LolCatzSteps.java 2009-10-26 20:38:12 UTC (rev 1363) @@ -13,6 +13,7 @@ import org.jbehave.scenario.annotations.Given; import org.jbehave.scenario.annotations.Then; import org.jbehave.scenario.annotations.When; +import org.jbehave.scenario.definition.KeyWords; import org.jbehave.scenario.steps.Steps; import com.lunivore.noughtsandcrosses.NoughtsAndCrosses; @@ -30,9 +31,13 @@ } public LolCatzSteps(OAndXUniverse universe) { - super("Gief", "Wen", "Den", "And"); + super(lolCatsKeywords()); this.universe = universe; } + + private static KeyWords lolCatsKeywords(){ + return new KeyWords("Scenaio", "GiefScenaio", "TibleScenaio", "Gief", "Wen", "Den", "And"); + } @Given("game") public void givenTheGameIsRunning() {
Modified: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java (1362 => 1363)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java 2009-10-24 12:55:39 UTC (rev 1362) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java 2009-10-26 20:38:12 UTC (rev 1363) @@ -71,6 +71,11 @@ ensureThat(stock.getStatus().name(), equalTo(status)); } + @Given("the alert status is OFF") // shows that matching pattern need only be unique for step type + public void theAlertStatusIsReset() { + stock.resetAlert(); + } + @Then("the alert status is %status") @Alias("the alert status will be %status") // single alias public void theAlertStatusIsWithNamedParam(@Named("status") String status) {
Modified: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/trader_sells_all_stocks.scenario (1362 => 1363)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/trader_sells_all_stocks.scenario 2009-10-24 12:55:39 UTC (rev 1362) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/trader_sells_all_stocks.scenario 2009-10-26 20:38:12 UTC (rev 1363) @@ -2,6 +2,7 @@ GivenScenarios: org/jbehave/examples/trader/scenarios/trader_is_alerted_of_status.scenario +Given the alert status is OFF Given a trader of name Mauro Given a stock of symbol STK1 and a threshold of 1.5 When the stock is traded at price 2.0
Modified: trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/CandidateStepBehaviour.java (1362 => 1363)
--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/CandidateStepBehaviour.java 2009-10-24 12:55:39 UTC (rev 1362) +++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/CandidateStepBehaviour.java 2009-10-26 20:38:12 UTC (rev 1363) @@ -4,6 +4,9 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.jbehave.Ensure.ensureThat; import static org.jbehave.Ensure.not; +import static org.jbehave.scenario.steps.StepType.GIVEN; +import static org.jbehave.scenario.steps.StepType.THEN; +import static org.jbehave.scenario.steps.StepType.WHEN; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -32,18 +35,27 @@ private static final String NL = System.getProperty("line.separator"); private Map<String, String> tableRow = new HashMap<String, String>(); private Paranamer paranamer = new CachingParanamer(new BytecodeReadingParanamer()); + private Map<StepType, String> startingWords = startingWords(); + private Map<StepType, String> startingWords() { + Map<StepType, String> map = new HashMap<StepType, String>(); + map.put(GIVEN, "Given"); + map.put(WHEN, "When"); + map.put(THEN, "Then"); + return map; + } + @Test public void shouldMatchASimpleString() throws Exception { - CandidateStep candidateStep = new CandidateStep("I laugh", SomeSteps.class.getMethod("aMethod"), null, - PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + CandidateStep candidateStep = new CandidateStep("I laugh", GIVEN, SomeSteps.class.getMethod("aMethod"), + null, PATTERN_BUILDER, new ParameterConverters(), startingWords); ensureThat(candidateStep.matches("Given I laugh")); } @Test public void shouldMatchAStringWithArguments() throws Exception { - CandidateStep candidateStep = new CandidateStep("windows on the $nth floor", SomeSteps.class - .getMethod("aMethod"), null, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + CandidateStep candidateStep = new CandidateStep("windows on the $nth floor", WHEN, SomeSteps.class + .getMethod("aMethod"), null, PATTERN_BUILDER, new ParameterConverters(), startingWords); ensureThat(candidateStep.matches("When windows on the 1st floor")); ensureThat(not(candidateStep.matches("When windows on the 1st floor are open"))); } @@ -51,8 +63,8 @@ @Test public void shouldProvideARealStepUsingTheMatchedString() throws Exception { SomeSteps someSteps = new SomeSteps(); - CandidateStep candidateStep = new CandidateStep("I live on the $nth floor", SomeSteps.class.getMethod( - "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + CandidateStep candidateStep = new CandidateStep("I live on the $nth floor", THEN, SomeSteps.class.getMethod( + "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); Step step = candidateStep.createFrom(tableRow, "Then I live on the 1st floor"); step.perform(); ensureThat((String) someSteps.args, equalTo("1st")); @@ -60,31 +72,31 @@ @Test public void shouldMatchMultilineStrings() throws Exception { - CandidateStep candidateStep = new CandidateStep("the grid should look like $grid", SomeSteps.class - .getMethod("aMethod"), null, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + CandidateStep candidateStep = new CandidateStep("the grid should look like $grid", THEN, SomeSteps.class + .getMethod("aMethod"), null, PATTERN_BUILDER, new ParameterConverters(), startingWords); ensureThat(candidateStep.matches("Then the grid should look like " + NL + "...." + NL + "...." + NL)); } @Test public void shouldConvertArgsToAppropriateNumbers() throws Exception { SomeSteps someSteps = new SomeSteps(); - CandidateStep candidateStep = new CandidateStep("I should live in no. $no", SomeSteps.class.getMethod( - "aMethodWith", int.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + CandidateStep candidateStep = new CandidateStep("I should live in no. $no", THEN, SomeSteps.class.getMethod( + "aMethodWith", int.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "Then I should live in no. 14").perform(); ensureThat((Integer) someSteps.args, equalTo(14)); - candidateStep = new CandidateStep("I should live in no. $no", SomeSteps.class.getMethod("aMethodWith", - long.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + candidateStep = new CandidateStep("I should live in no. $no", THEN, SomeSteps.class.getMethod("aMethodWith", + long.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "Then I should live in no. 14").perform(); ensureThat((Long) someSteps.args, equalTo(14L)); - candidateStep = new CandidateStep("I should live in no. $no", SomeSteps.class.getMethod("aMethodWith", - double.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + candidateStep = new CandidateStep("I should live in no. $no", THEN, SomeSteps.class.getMethod("aMethodWith", + double.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "Then I should live in no. 14").perform(); ensureThat((Double) someSteps.args, equalTo(14.0)); - candidateStep = new CandidateStep("I should live in no. $no", SomeSteps.class.getMethod("aMethodWith", - float.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + candidateStep = new CandidateStep("I should live in no. $no", THEN, SomeSteps.class.getMethod("aMethodWith", + float.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "Then I should live in no. 14").perform(); ensureThat((Float) someSteps.args, equalTo(14.0f)); } @@ -93,8 +105,8 @@ public void shouldProvideAStepWithADescriptionThatMatchesTheCandidateStep() throws Exception { ScenarioReporter reporter = mock(ScenarioReporter.class); SomeSteps someSteps = new SomeSteps(); - CandidateStep candidateStep = new CandidateStep("I live on the $nth floor", SomeSteps.class.getMethod( - "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + CandidateStep candidateStep = new CandidateStep("I live on the $nth floor", THEN, SomeSteps.class.getMethod( + "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); Step step = candidateStep.createFrom(tableRow, "Then I live on the 1st floor"); StepResult result = step.perform(); @@ -108,8 +120,8 @@ String unixNewline = "\n"; String systemNewline = System.getProperty("line.separator"); SomeSteps someSteps = new SomeSteps(); - CandidateStep candidateStep = new CandidateStep("the grid should look like $grid", SomeSteps.class.getMethod( - "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + CandidateStep candidateStep = new CandidateStep("the grid should look like $grid", THEN, SomeSteps.class.getMethod( + "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); Step step = candidateStep.createFrom(tableRow, "Then the grid should look like" + windowsNewline + ".." + unixNewline + ".." + windowsNewline); step.perform(); @@ -120,22 +132,22 @@ public void shouldConvertArgsToListOfNumbers() throws Exception { SomeSteps someSteps = new SomeSteps(); CandidateStep candidateStep = new CandidateStep("windows on the $nth floors", - SomeSteps.methodFor("aMethodWithListOfLongs"), someSteps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + WHEN, SomeSteps.methodFor("aMethodWithListOfLongs"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When windows on the 1L,2L,3L floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1L, 2L, 3L).toString())); - candidateStep = new CandidateStep("windows on the $nth floors", SomeSteps.methodFor("aMethodWithListOfIntegers"), - someSteps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + candidateStep = new CandidateStep("windows on the $nth floors", WHEN, + SomeSteps.methodFor("aMethodWithListOfIntegers"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When windows on the 1,2,3 floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1, 2, 3).toString())); - candidateStep = new CandidateStep("windows on the $nth floors", SomeSteps.methodFor("aMethodWithListOfDoubles"), - someSteps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + candidateStep = new CandidateStep("windows on the $nth floors", WHEN, + SomeSteps.methodFor("aMethodWithListOfDoubles"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When windows on the 1.1,2.2,3.3 floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1.1, 2.2, 3.3).toString())); - candidateStep = new CandidateStep("windows on the $nth floors", SomeSteps.methodFor("aMethodWithListOfFloats"), - someSteps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + candidateStep = new CandidateStep("windows on the $nth floors", WHEN, + SomeSteps.methodFor("aMethodWithListOfFloats"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When windows on the 1.1f,2.2f,3.3f floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1.1f, 2.2f, 3.3f).toString())); @@ -145,7 +157,7 @@ public void shouldConvertArgsToListOfStrings() throws Exception { SomeSteps someSteps = new SomeSteps(); CandidateStep candidateStep = new CandidateStep("windows on the $nth floors", - SomeSteps.methodFor("aMethodWithListOfStrings"), someSteps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + WHEN, SomeSteps.methodFor("aMethodWithListOfStrings"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When windows on the 1,2,3 floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList("1", "2", "3").toString())); } @@ -154,7 +166,7 @@ public void shouldMatchMethodParametersByAnnotatedNamesInNaturalOrder() throws Exception { AnnotationNamedParameterSteps steps = new AnnotationNamedParameterSteps(); CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth", - stepMethodFor("methodWithNamedParametersInNaturalOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When I live on the first floor but some call it the ground").perform(); ensureThat(steps.ith, equalTo("first")); ensureThat(steps.nth, equalTo("ground")); @@ -164,7 +176,7 @@ public void shouldMatchMethodParametersByAnnotatedNamesInverseOrder() throws Exception { AnnotationNamedParameterSteps steps = new AnnotationNamedParameterSteps(); CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth", - stepMethodFor("methodWithNamedParametersInInverseOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + WHEN, stepMethodFor("methodWithNamedParametersInInverseOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When I live on the first floor but some call it the ground").perform(); ensureThat(steps.ith, equalTo("first")); ensureThat(steps.nth, equalTo("ground")); @@ -176,7 +188,7 @@ tableRow.put("ith", "first"); tableRow.put("nth", "ground"); CandidateStep candidateStep = new CandidateStep("I live on the ith floor but some call it the nth", - stepMethodFor("methodWithNamedParametersInNaturalOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When I live on the <ith> floor but some call it the <nth>").perform(); ensureThat(steps.ith, equalTo("first")); ensureThat(steps.nth, equalTo("ground")); @@ -186,7 +198,7 @@ public void shouldMatchMethodParametersByParanamerNamesInNaturalOrder() throws Exception { ParanamerNamedParameterSteps steps = new ParanamerNamedParameterSteps(); CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth", - stepMethodFor("methodWithNamedParametersInNaturalOrder", ParanamerNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", ParanamerNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.useParanamer(paranamer); candidateStep.createFrom(tableRow, "When I live on the first floor but some call it the ground").perform(); ensureThat(steps.ith, equalTo("first")); @@ -197,7 +209,7 @@ public void shouldMatchMethodParametersByParanamerInverseOrder() throws Exception { ParanamerNamedParameterSteps steps = new ParanamerNamedParameterSteps(); CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth", - stepMethodFor("methodWithNamedParametersInInverseOrder", ParanamerNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + WHEN, stepMethodFor("methodWithNamedParametersInInverseOrder", ParanamerNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.useParanamer(paranamer); candidateStep.createFrom(tableRow, "When I live on the first floor but some call it the ground").perform(); ensureThat(steps.ith, equalTo("first")); @@ -210,7 +222,7 @@ tableRow.put("ith", "first"); tableRow.put("nth", "ground"); CandidateStep candidateStep = new CandidateStep("I live on the ith floor but some call it the nth", - stepMethodFor("methodWithNamedParametersInNaturalOrder", ParanamerNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), "Given", "When", "Then"); + WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", ParanamerNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.useParanamer(paranamer); candidateStep.createFrom(tableRow, "When I live on the <ith> floor but some call it the <nth>").perform(); ensureThat(steps.ith, equalTo("first"));
Modified: trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/ConfusingStepsBehavior.java (1362 => 1363)
--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/ConfusingStepsBehavior.java 2009-10-24 12:55:39 UTC (rev 1362) +++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/ConfusingStepsBehavior.java 2009-10-26 20:38:12 UTC (rev 1363) @@ -2,41 +2,52 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.jbehave.Ensure.ensureThat; -import org.jbehave.scenario.annotations.AfterScenario; + +import java.util.HashMap; +import java.util.Map; + import org.jbehave.scenario.annotations.Given; import org.jbehave.scenario.annotations.When; +import org.jbehave.scenario.steps.CandidateStep.StartingWordNotFound; import org.junit.Test; -import java.util.HashMap; -import java.util.Map; - public class ConfusingStepsBehavior { private Map<String, String> tableRow = new HashMap<String, String>(); @Test - public void shouldBeAbleToDisambiguateSimilarSteps() { + public void shouldAllowStepOfDifferentTypesWithPatternThatMatchesSameStep() { MySteps steps = new MySteps(); CandidateStep[] candidateSteps = steps.getSteps(); ensureThat(candidateSteps.length, equalTo(2)); candidateSteps[0].createFrom(tableRow, "Given foo named xyz").perform(); candidateSteps[1].createFrom(tableRow, "When foo named Bar is created").perform(); - ensureThat(steps.given, equalTo(true)); - ensureThat(steps.when, equalTo(true)); + ensureThat(steps.givenName, equalTo("xyz")); + ensureThat(steps.whenName, equalTo("Bar")); } + @Test(expected=StartingWordNotFound.class) + public void shouldFailWhenTryingToMatchStepOfWrongType() { + MySteps steps = new MySteps(); + CandidateStep[] candidateSteps = steps.getSteps(); + ensureThat(candidateSteps.length, equalTo(2)); + candidateSteps[0].createFrom(tableRow, "Given foo named xyz").perform(); + ensureThat(steps.givenName, equalTo("xyz")); + candidateSteps[0].createFrom(tableRow, "Then foo named xyz").perform(); + } + static class MySteps extends Steps { - private boolean when; - private boolean given; + private String givenName; + private String whenName; @Given("foo named $name") public void givenFoo(String name) { - given = true; + givenName = name; } @When("foo named $name is created") public void createFoo(String name) { - when = true; + whenName = name; } }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/CandidateStep.java (1362 => 1363)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/CandidateStep.java 2009-10-24 12:55:39 UTC (rev 1362) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/CandidateStep.java 2009-10-26 20:38:12 UTC (rev 1363) @@ -27,24 +27,27 @@ public class CandidateStep { private final String stepAsString; + private final StepType stepType; private final Method method; private final CandidateSteps steps; private final ParameterConverters parameterConverters; - private final String[] startingWords; + private final Map<StepType, String> startingWordsByType; private final Pattern pattern; private final String[] groupNames; private StepMonitor stepMonitor = new SilentStepMonitor(); private Paranamer paranamer = new NullParanamer(); - public CandidateStep(String stepAsString, Method method, + public CandidateStep(String stepAsString, StepType stepType, Method method, CandidateSteps steps, StepPatternBuilder patternBuilder, - ParameterConverters parameterConverters, String... startingWords) { + ParameterConverters parameterConverters, + Map<StepType, String> startingWords) { this.stepAsString = stepAsString; + this.stepType = stepType; this.method = method; this.steps = steps; this.parameterConverters = parameterConverters; - this.startingWords = startingWords; + this.startingWordsByType = startingWords; this.pattern = patternBuilder.buildPattern(stepAsString); this.groupNames = patternBuilder.extractGroupNames(stepAsString); } @@ -200,12 +203,12 @@ private String findStartingWord(final String stepAsString) throws StartingWordNotFound { - for (String word : startingWords) { - if (stepAsString.startsWith(word)) { - return word; - } + String startingWord = startingWordsByType.get(stepType); + if (startingWord != null && stepAsString.startsWith(startingWord)) { + return startingWord; } - throw new StartingWordNotFound(stepAsString, startingWords); + throw new StartingWordNotFound(stepAsString, stepType, + startingWordsByType); } private Step createStep(final String stepAsString, final Object[] args) { @@ -267,9 +270,10 @@ @SuppressWarnings("serial") public static class StartingWordNotFound extends RuntimeException { - public StartingWordNotFound(String step, String[] startingWords) { - super("No starting word found for step " + step + " amongst " - + asList(startingWords)); + public StartingWordNotFound(String step, StepType stepType, + Map<StepType, String> startingWordsByType) { + super("No starting word found for step " + step + " of type " + + stepType + " amongst " + startingWordsByType); } }
Added: trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/StepType.java (0 => 1363)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/StepType.java (rev 0) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/StepType.java 2009-10-26 20:38:12 UTC (rev 1363) @@ -0,0 +1,23 @@ +package org.jbehave.scenario.steps; + +/** + * Enum representing the step types + */ +public enum StepType { + + /** + * Represents a precondition + */ + GIVEN, + + /** + * Represents an event + */ + WHEN, + + /** + * Represents an outcome + */ + THEN + +}
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java (1362 => 1363)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java 2009-10-24 12:55:39 UTC (rev 1362) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java 2009-10-26 20:38:12 UTC (rev 1363) @@ -3,6 +3,9 @@ import static org.jbehave.scenario.annotations.AfterScenario.Outcome.ANY; import static org.jbehave.scenario.annotations.AfterScenario.Outcome.FAILURE; import static org.jbehave.scenario.annotations.AfterScenario.Outcome.SUCCESS; +import static org.jbehave.scenario.steps.StepType.GIVEN; +import static org.jbehave.scenario.steps.StepType.THEN; +import static org.jbehave.scenario.steps.StepType.WHEN; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; @@ -67,7 +70,7 @@ public class Steps implements CandidateSteps { private final StepsConfiguration configuration; - + /** * Creates Steps with default configuration */ @@ -80,8 +83,8 @@ * keywords * * @param keywords - * the KeyWords which hold the words with which we expect steps in - * the scenarios to start + * the KeyWords which hold the words with which we expect steps + * in the scenarios to start */ public Steps(KeyWords keywords) { this(new StepsConfiguration(keywords)); @@ -93,6 +96,7 @@ * * @param startingWords * the words with which we expect steps in the scenarios to start + * @deprecated Use Steps(KeyWords) */ public Steps(String... startingWords) { this(new StepsConfiguration(startingWords)); @@ -129,18 +133,18 @@ for (Method method : stepsClass.getMethods()) { if (method.isAnnotationPresent(Given.class)) { String value = encode(method.getAnnotation(Given.class).value()); - createCandidateStep(steps, method, value); - createCandidateStepsFromAliases(steps, method); + createCandidateStep(steps, method, GIVEN, value); + createCandidateStepsFromAliases(steps, method, GIVEN); } if (method.isAnnotationPresent(When.class)) { String value = encode(method.getAnnotation(When.class).value()); - createCandidateStep(steps, method, value); - createCandidateStepsFromAliases(steps, method); + createCandidateStep(steps, method, WHEN, value); + createCandidateStepsFromAliases(steps, method, WHEN); } if (method.isAnnotationPresent(Then.class)) { String value = encode(method.getAnnotation(Then.class).value()); - createCandidateStep(steps, method, value); - createCandidateStepsFromAliases(steps, method); + createCandidateStep(steps, method, THEN, value); + createCandidateStepsFromAliases(steps, method, THEN); } } return steps.toArray(new CandidateStep[steps.size()]); @@ -151,12 +155,12 @@ } private void createCandidateStep(List<CandidateStep> steps, Method method, - String stepAsString) { + StepType stepType, String stepAsString) { checkForDuplicateCandidateSteps(steps, stepAsString); - CandidateStep step = new CandidateStep(stepAsString, method, this, - configuration.getPatternBuilder(), configuration + CandidateStep step = new CandidateStep(stepAsString, stepType, method, + this, configuration.getPatternBuilder(), configuration .getParameterConverters(), configuration - .getStartingWords()); + .getStartingWordsByType()); step.useStepMonitor(configuration.getMonitor()); step.useParanamer(configuration.getParanamer()); steps.add(step); @@ -172,16 +176,16 @@ } private void createCandidateStepsFromAliases(List<CandidateStep> steps, - Method method) { + Method method, StepType stepType) { if (method.isAnnotationPresent(Aliases.class)) { String[] aliases = method.getAnnotation(Aliases.class).values(); for (String alias : aliases) { - createCandidateStep(steps, method, alias); + createCandidateStep(steps, method, stepType, alias); } } if (method.isAnnotationPresent(Alias.class)) { - createCandidateStep(steps, method, method - .getAnnotation(Alias.class).value()); + createCandidateStep(steps, method, stepType, method.getAnnotation( + Alias.class).value()); } }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/StepsConfiguration.java (1362 => 1363)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/StepsConfiguration.java 2009-10-24 12:55:39 UTC (rev 1362) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/StepsConfiguration.java 2009-10-26 20:38:12 UTC (rev 1363) @@ -1,5 +1,12 @@ package org.jbehave.scenario.steps; +import static org.jbehave.scenario.steps.StepType.GIVEN; +import static org.jbehave.scenario.steps.StepType.THEN; +import static org.jbehave.scenario.steps.StepType.WHEN; + +import java.util.HashMap; +import java.util.Map; + import org.jbehave.scenario.definition.KeyWords; import org.jbehave.scenario.i18n.I18nKeyWords; import org.jbehave.scenario.parser.PrefixCapturingPatternBuilder; @@ -29,6 +36,7 @@ private ParameterConverters parameterConverters; private KeyWords keywords; private String[] startingWords; + private Map<StepType,String> startingWordsByType; public StepsConfiguration() { this(new I18nKeyWords()); @@ -53,8 +61,12 @@ this.parameterConverters = parameterConverters; this.keywords = keywords; this.startingWords = startingWordsFrom(this.keywords); + this.startingWordsByType = startingWordsByType(this.keywords); } + /** + * @deprecated Use StepsConfiguration(KeyWords) + */ public StepsConfiguration(String... startingWords) { this(new PrefixCapturingPatternBuilder(), new SilentStepMonitor(), new NullParanamer(), new ParameterConverters(), startingWords); @@ -74,6 +86,15 @@ protected String[] startingWordsFrom(KeyWords keywords) { return new String[]{keywords.given(), keywords.when(), keywords.then(), keywords.and()}; } + + + protected Map<StepType, String> startingWordsByType(KeyWords keywords) { + Map<StepType, String> words = new HashMap<StepType, String>(); + words.put(GIVEN, keywords.given()); + words.put(WHEN, keywords.when()); + words.put(THEN, keywords.then()); + return words; + } public StepPatternBuilder getPatternBuilder() { return patternBuilder; @@ -111,6 +132,10 @@ return startingWords; } + public Map<StepType, String> getStartingWordsByType() { + return startingWordsByType; + } + public void useStartingWords(String... startingWords) { this.startingWords = startingWords; }
To unsubscribe from this list please visit: