Title: [874] trunk/jbehave-core/src/java/org/jbehave/scenario: [Liz] Cleaned up the code that creates the candidate steps; there is now only one kind of step (the others weren't doing much anyway) and you can choose the words that signify the start of a step.

Diff

Modified: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioBehaviour.java (873 => 874)

--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioBehaviour.java	2008-07-20 14:17:53 UTC (rev 873)
+++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioBehaviour.java	2008-07-20 20:28:57 UTC (rev 874)
@@ -43,7 +43,7 @@
 		stub(stepParser.findSteps("my_scenario")).toReturn(Arrays.asList(new String[] {
 				"Given I have 2 cows",
 				"When I leave them over the winter",
-				"Then I have 2 cows"}));
+				"Then I should have 2 cows"}));
 
 		new MyScenario(fileLoader, stepParser, runner, steps).runUsingSteps();
 		
@@ -51,7 +51,7 @@
 		ensureThat(output.toString(), equalTo(
 				"Given I have 2 cows" + NL + 
 				"When I leave them over the winter (PENDING)" + NL +
-				"Then I have 2 cows (NOT PERFORMED)" + NL));
+				"Then I should have 2 cows (NOT PERFORMED)" + NL));
 	}
 	
 	@Test
@@ -67,7 +67,7 @@
         stub(stepParser.findSteps("my_scenario")).toReturn(Arrays.asList(new String[] {
                 "Given I have 2 cows",
                 "When I leave them over the winter",
-                "Then I have 2 cows"}));
+                "Then I should have 2 cows"}));
 
         new MyScenario(fileLoader, stepParser, runner, steps).runUsingSteps();
         
@@ -75,7 +75,7 @@
         ensureThat(buffer.toString(), equalTo(
                 "Given I have 2 cows" + NL + 
                 "When I leave them over the winter (PENDING)" + NL +
-                "Then I have 2 cows (NOT PERFORMED)" + NL));
+                "Then I should have 2 cows (NOT PERFORMED)" + NL));
     }
 	
     @Test
@@ -97,12 +97,12 @@
 				"Given I have 2 cows",
 				"When I put them in a field",
 				"Then my cows should not die",
-				"Then I have 2 cows"}));
+				"Then I should have 2 cows"}));
 		
 
 		try {
 			new MyScenario(fileLoader, stepParser, runner, steps).runUsingSteps();
-			fail("Excpected the error to be rethrown");
+			fail("Expected the error to be rethrown");
 		} catch (IllegalAccessError e) {
 			ensureThat(e, equalTo(steps.error));
 		}
@@ -111,7 +111,7 @@
 				"Given I have 2 cows" + NL + 
 				"When I put them in a field" + NL +
 				"Then my cows should not die (FAILED)" + NL +
-				"Then I have 2 cows (NOT PERFORMED)" + NL));
+				"Then I should have 2 cows (NOT PERFORMED)" + NL));
 	}
 
 	
@@ -134,7 +134,7 @@
 		@When("I put them in a field")
 		public void ignoreCows() {}
 		
-		@Then("I have $n cows")
+		@Then("I should have $n cows")
 		public void checkCows(int numberOfCows) {
 			ensureThat(this.numberOfCows, equalTo(numberOfCows));
 		}

Modified: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/CandidateStepBehaviour.java (873 => 874)

--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/CandidateStepBehaviour.java	2008-07-20 14:17:53 UTC (rev 873)
+++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/CandidateStepBehaviour.java	2008-07-20 20:28:57 UTC (rev 874)
@@ -17,18 +17,16 @@
 
     @Test
     public void shouldMatchASimpleString() throws Exception {
-        CandidateStep candidateStep = new Given("I laugh", SomeSteps.class.getMethod("aMethod"), null,
-                PATTERN_BUILDER, MONITOR);
+        CandidateStep candidateStep = new CandidateStep("I laugh", SomeSteps.class.getMethod("aMethod"), null,
+                PATTERN_BUILDER, MONITOR, "Given", "When", "Then");
 
         ensureThat(candidateStep.matches("Given I laugh"));
-        ensureThat(not(candidateStep.matches("When I laugh")));
-        ensureThat(not(candidateStep.matches("Then I laugh")));
     }
 
     @Test
     public void shouldMatchAStringWithArguments() throws Exception {
-        CandidateStep candidateStep = new When("windows on the $nth floor", SomeSteps.class.getMethod("aMethod"), null,
-                PATTERN_BUILDER, MONITOR);
+        CandidateStep candidateStep = new CandidateStep("windows on the $nth floor", SomeSteps.class.getMethod("aMethod"), null,
+                PATTERN_BUILDER, MONITOR, "Given", "When", "Then");
         ensureThat(candidateStep.matches("When windows on the 1st floor"));
         ensureThat(not(candidateStep.matches("When windows on the 1st floor are open")));
     }
@@ -36,8 +34,8 @@
     @Test
     public void shouldProvideARealStepUsingTheMatchedString() throws Exception {
         SomeSteps someSteps = new SomeSteps();
-        CandidateStep candidateStep = new Then("I live on the $nth floor", SomeSteps.class.getMethod("aMethodWith",
-                String.class), someSteps, PATTERN_BUILDER, MONITOR);
+        CandidateStep candidateStep = new CandidateStep("I live on the $nth floor", SomeSteps.class.getMethod("aMethodWith",
+                String.class), someSteps, PATTERN_BUILDER, MONITOR, "Given", "When", "Then");
         Step step = candidateStep.createFrom("Then I live on the 1st floor");
         step.perform();
         ensureThat((String) someSteps.args, equalTo("1st"));
@@ -45,31 +43,31 @@
 
     @Test
     public void shouldMatchMultilineStrings() throws Exception {
-        CandidateStep candidateStep = new Then("the grid should look like $grid", SomeSteps.class.getMethod("aMethod"),
-                null, PATTERN_BUILDER, MONITOR);
+        CandidateStep candidateStep = new CandidateStep("the grid should look like $grid", SomeSteps.class.getMethod("aMethod"),
+                null, PATTERN_BUILDER, MONITOR, "Given", "When", "Then");
         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 Then("I should live in no. $no", SomeSteps.class.getMethod("aMethodWith",
-                int.class), someSteps, PATTERN_BUILDER, MONITOR);
+        CandidateStep candidateStep = new CandidateStep("I should live in no. $no", SomeSteps.class.getMethod("aMethodWith",
+                int.class), someSteps, PATTERN_BUILDER, MONITOR, "Given", "When", "Then");
         candidateStep.createFrom("Then I should live in no. 14").perform();
         ensureThat((Integer) someSteps.args, equalTo(14));
 
-        candidateStep = new Then("I should live in no. $no", SomeSteps.class.getMethod("aMethodWith", long.class),
-                someSteps, PATTERN_BUILDER, MONITOR);
+        candidateStep = new CandidateStep("I should live in no. $no", SomeSteps.class.getMethod("aMethodWith", long.class),
+                someSteps, PATTERN_BUILDER, MONITOR, "Given", "When", "Then");
         candidateStep.createFrom("Then I should live in no. 14").perform();
         ensureThat((Long) someSteps.args, equalTo(14L));
 
-        candidateStep = new Then("I should live in no. $no", SomeSteps.class.getMethod("aMethodWith", double.class),
-                someSteps, PATTERN_BUILDER, MONITOR);
+        candidateStep = new CandidateStep("I should live in no. $no", SomeSteps.class.getMethod("aMethodWith", double.class),
+                someSteps, PATTERN_BUILDER, MONITOR, "Given", "When", "Then");
         candidateStep.createFrom("Then I should live in no. 14").perform();
         ensureThat((Double) someSteps.args, equalTo(14.0));
 
-        candidateStep = new Then("I should live in no. $no", SomeSteps.class.getMethod("aMethodWith", float.class),
-                someSteps, PATTERN_BUILDER, MONITOR);
+        candidateStep = new CandidateStep("I should live in no. $no", SomeSteps.class.getMethod("aMethodWith", float.class),
+                someSteps, PATTERN_BUILDER, MONITOR, "Given", "When", "Then");
         candidateStep.createFrom("Then I should live in no. 14").perform();
         ensureThat((Float) someSteps.args, equalTo(14.0f));
     }
@@ -78,8 +76,8 @@
     public void shouldProvideAStepWithADescriptionThatMatchesTheCandidateStep() throws Exception {
         ScenarioReporter reporter = mock(ScenarioReporter.class);
         SomeSteps someSteps = new SomeSteps();
-        CandidateStep candidateStep = new Then("I live on the $nth floor", SomeSteps.class.getMethod("aMethodWith",
-                String.class), someSteps, PATTERN_BUILDER, MONITOR);
+        CandidateStep candidateStep = new CandidateStep("I live on the $nth floor", SomeSteps.class.getMethod("aMethodWith",
+                String.class), someSteps, PATTERN_BUILDER, MONITOR, "Given", "When", "Then");
         Step step = candidateStep.createFrom("Then I live on the 1st floor");
 
         StepResult result = step.perform();
@@ -95,10 +93,10 @@
     	String systemNewline = System.getProperty("line.separator");
     	
         SomeSteps someSteps = new SomeSteps();
-        CandidateStep candidateStep = new Then(
+        CandidateStep candidateStep = new CandidateStep(
         		"the grid should look like $grid", 
         		SomeSteps.class.getMethod("aMethodWith", String.class), 
-        		someSteps, PATTERN_BUILDER, MONITOR);
+        		someSteps, PATTERN_BUILDER, MONITOR, "Given", "When", "Then");
         Step step = candidateStep.createFrom(
         		"Then the grid should look like" + windowsNewline +
         		".." + unixNewline +

Modified: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/StepsBehaviour.java (873 => 874)

--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/StepsBehaviour.java	2008-07-20 14:17:53 UTC (rev 873)
+++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/StepsBehaviour.java	2008-07-20 20:28:57 UTC (rev 874)
@@ -13,11 +13,10 @@
 		CandidateStep[] candidateSteps = steps.getSteps();
 		ensureThat(candidateSteps.length, equalTo(3));
 		
-		for (CandidateStep candidateStep : candidateSteps) {
-			String stepType = candidateStep.getClass().getSimpleName();
-			candidateStep.createFrom(stepType + " a " + stepType).perform();
-		}
-		
+		candidateSteps[0].createFrom("Given a given").perform();
+		candidateSteps[1].createFrom("When a when").perform();
+		candidateSteps[2].createFrom("Then a then").perform();
+
 		ensureThat(steps.given);
 		ensureThat(steps.when);
 		ensureThat(steps.then);

Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/Scenario.java (873 => 874)

--- trunk/jbehave-core/src/java/org/jbehave/scenario/Scenario.java	2008-07-20 14:17:53 UTC (rev 873)
+++ trunk/jbehave-core/src/java/org/jbehave/scenario/Scenario.java	2008-07-20 20:28:57 UTC (rev 874)
@@ -16,7 +16,7 @@
  * <p>
  * A scenario is a collection of candidate steps that need to be run using a scenario runner.
  * </p>
- * <p>Users extend Scenario by providing condidate steps appropriate for the behaviour</p>
+ * <p>Users extend Scenario by providing candidate steps appropriate for the behaviour</p>
  * 
  * @author Elizabeth Keogh
  * @author Mauro Talevi

Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/steps/CandidateStep.java (873 => 874)

--- trunk/jbehave-core/src/java/org/jbehave/scenario/steps/CandidateStep.java	2008-07-20 14:17:53 UTC (rev 873)
+++ trunk/jbehave-core/src/java/org/jbehave/scenario/steps/CandidateStep.java	2008-07-20 20:28:57 UTC (rev 874)
@@ -6,37 +6,39 @@
 import java.util.regex.Pattern;
 
 
-public abstract class CandidateStep {
+public class CandidateStep {
 
     private final Method method;
     private final Steps steps;
     private final StepMonitor monitor;
     private Pattern pattern;
+	private String[] startingWords;
 
-    public CandidateStep(String matchThis, Method method, Steps steps, StepPatternBuilder patternConverter, StepMonitor monitor) {
+    public CandidateStep(String matchThis, Method method, Steps steps, StepPatternBuilder patternConverter, StepMonitor monitor, String... startingWords) {
         this.method = method;
         this.steps = steps;
         this.monitor = monitor;
+        this.startingWords = startingWords;
         pattern = patternConverter.buildPattern(matchThis);
     }
 
     public boolean matches(String step) {
-        if (step.startsWith(precursor())) {
-            String trimmed = trimPrecursor(step);
-            Matcher matcher = pattern.matcher(trimmed);
-            boolean matches = matcher.matches();
-            monitor.stepMatchesPattern(step, matches, pattern.pattern());
-            return matches;
-        }
-        return false;
+    	String word = findStartingWord(step);
+    	if (word == null) { return false; }
+        String trimmed = trimStartingWord(word, step);
+        Matcher matcher = pattern.matcher(trimmed);
+        boolean matches = matcher.matches();
+        monitor.stepMatchesPattern(step, matches, pattern.pattern());
+        return matches;
     }
 
-    private String trimPrecursor(String string) {
-        return string.substring(precursor().length());
+    private String trimStartingWord(String word, String step) {
+        return step.substring(word.length() + 1); // 1 for the space after
     }
 
     public Step createFrom(final String stepAsString) {
-        Matcher matcher = pattern.matcher(trimPrecursor(stepAsString));
+    	String startingWord = findStartingWord(stepAsString);
+        Matcher matcher = pattern.matcher(trimStartingWord(startingWord, stepAsString));
         matcher.find();
         final Object[] args = new Object[matcher.groupCount()];
         for (int group = 0; group < args.length; group++) {
@@ -47,6 +49,15 @@
         return createStep(stepAsString, args);
     }
 
+	private String findStartingWord(final String stepAsString) {
+    	for (String word : startingWords) {
+            if (stepAsString.startsWith(word)) {
+            	return word;
+            }
+    	}
+    	return null;
+	}
+
 	private Step createStep(final String stepAsString, final Object[] args) {
 		return new Step() {
             public StepResult perform() {
@@ -90,7 +101,4 @@
     private Object replaceNewlinesWithSystemNewlines(String value) {
 		return value.replaceAll("(\n)|(\r\n)", System.getProperty("line.separator"));
 	}
-
-	protected abstract String precursor();
-
 }

Deleted: trunk/jbehave-core/src/java/org/jbehave/scenario/steps/Given.java (873 => 874)

--- trunk/jbehave-core/src/java/org/jbehave/scenario/steps/Given.java	2008-07-20 14:17:53 UTC (rev 873)
+++ trunk/jbehave-core/src/java/org/jbehave/scenario/steps/Given.java	2008-07-20 20:28:57 UTC (rev 874)
@@ -1,17 +0,0 @@
-package org.jbehave.scenario.steps;
-
-import java.lang.reflect.Method;
-
-
-public class Given extends CandidateStep {
-
-    public Given(String matchThis, Method method, Steps steps, StepPatternBuilder patternBuilder, StepMonitor monitor) {
-        super(matchThis, method, steps, patternBuilder, monitor);
-    }
-
-    @Override
-    protected String precursor() {
-        return "Given ";
-    }
-
-}

Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java (873 => 874)

--- trunk/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java	2008-07-20 14:17:53 UTC (rev 873)
+++ trunk/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java	2008-07-20 20:28:57 UTC (rev 874)
@@ -3,18 +3,27 @@
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 
+import org.jbehave.scenario.annotations.Given;
+import org.jbehave.scenario.annotations.Then;
+import org.jbehave.scenario.annotations.When;
+
 public class Steps {
 	
 	private final StepPatternBuilder patternBuilder;
     private final StepMonitor monitor;
+    @SuppressWarnings("unchecked")
+    private final String[] startingWords;
 	
 	public Steps() {
-		this(new DollarStepPatternBuilder(), new SilentStepMonitor());
+		this(new DollarStepPatternBuilder(), new SilentStepMonitor(),
+		"Given", "When", "Then", "And");
 	}
 
-	public Steps(StepPatternBuilder patternConverter, StepMonitor monitor) {
+	public Steps(StepPatternBuilder patternConverter, StepMonitor monitor, 
+			String... startingWords) {
 		this.patternBuilder = patternConverter;
 		this.monitor = monitor;
+		this.startingWords = startingWords;
 	}
 
 	/**
@@ -23,26 +32,27 @@
 	public CandidateStep[] getSteps() {
 		ArrayList<CandidateStep> steps = new ArrayList<CandidateStep>();
 		for (Method method : this.getClass().getMethods()) {
-			if (method.isAnnotationPresent(org.jbehave.scenario.annotations.Given.class)) {
-				steps.add(given(method.getAnnotation(org.jbehave.scenario.annotations.Given.class).value(), method));
+			if (method.isAnnotationPresent(Given.class)) {
+				createCandidateStep(steps, method, method.getAnnotation(Given.class).value());
 			}
-			if (method.isAnnotationPresent(org.jbehave.scenario.annotations.When.class)) {
-				steps.add(when(method.getAnnotation(org.jbehave.scenario.annotations.When.class).value(), method));
+			if (method.isAnnotationPresent(When.class)) {
+				createCandidateStep(steps, method, method.getAnnotation(When.class).value());
 			}
-			if (method.isAnnotationPresent(org.jbehave.scenario.annotations.Then.class)) {
-				steps.add(then(method.getAnnotation(org.jbehave.scenario.annotations.Then.class).value(), method));
+			if (method.isAnnotationPresent(Then.class)) {
+				createCandidateStep(steps, method, method.getAnnotation(Then.class).value());
 			}
 		}
 		return steps.toArray(new CandidateStep[steps.size()]);
 	}
-	
-	private Given given(String value, Method method) {
-		return new Given(value, method, this, patternBuilder, monitor);
+
+	private void createCandidateStep(ArrayList<CandidateStep> steps,
+			Method method, String stepAsString) {
+		steps.add(new CandidateStep(
+				stepAsString, 
+				method, 
+				this, 
+				patternBuilder, 
+				monitor, 
+				startingWords));
 	}
-	private When when(String value, Method method) {
-		return new When(value, method, this, patternBuilder, monitor);
-	}
-	private Then then(String value, Method method) {
-		return new Then(value, method, this, patternBuilder, monitor);
-	}
 }

Deleted: trunk/jbehave-core/src/java/org/jbehave/scenario/steps/Then.java (873 => 874)

--- trunk/jbehave-core/src/java/org/jbehave/scenario/steps/Then.java	2008-07-20 14:17:53 UTC (rev 873)
+++ trunk/jbehave-core/src/java/org/jbehave/scenario/steps/Then.java	2008-07-20 20:28:57 UTC (rev 874)
@@ -1,17 +0,0 @@
-package org.jbehave.scenario.steps;
-
-import java.lang.reflect.Method;
-
-
-public class Then extends CandidateStep {
-
-	public Then(String matchThis, Method method, Steps steps, StepPatternBuilder patternBuilder, StepMonitor monitor) {
-		super(matchThis, method, steps, patternBuilder, monitor);
-	}
-
-	@Override
-	protected String precursor() {
-		return "Then ";
-	}
-
-}

Deleted: trunk/jbehave-core/src/java/org/jbehave/scenario/steps/When.java (873 => 874)

--- trunk/jbehave-core/src/java/org/jbehave/scenario/steps/When.java	2008-07-20 14:17:53 UTC (rev 873)
+++ trunk/jbehave-core/src/java/org/jbehave/scenario/steps/When.java	2008-07-20 20:28:57 UTC (rev 874)
@@ -1,17 +0,0 @@
-package org.jbehave.scenario.steps;
-
-import java.lang.reflect.Method;
-
-
-public class When extends CandidateStep {
-
-	public When(String value, Method method, Steps steps, StepPatternBuilder patternBuilder, StepMonitor monitor) {
-		super(value, method, steps, patternBuilder, monitor);
-	}
-
-	@Override
-	protected String precursor() {
-		return "When ";
-	}
-
-}


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to