Skip to content

Commit

Permalink
Merge pull request #285 from qvdk/feature/support-cucumber-expression…
Browse files Browse the repository at this point in the history
…-fast

support built-in cucumber expressions
  • Loading branch information
girijant authored Nov 13, 2018
2 parents 9ecf49e + f1d84b8 commit f980f0c
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 9 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package cucumber.eclipse.editor.editors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.junit.Ignore;
import org.junit.Test;

import cucumber.eclipse.steps.integration.Step;
Expand All @@ -22,6 +26,15 @@ public void simpleStepMatches() {
assertEquals(s, stepMatcher.matchSteps("en", Collections.singleton(s), "When I run a test"));
}

@Test
public void noStepMatches() {

Step s1 = createStep("^I run a test$");
Step s2 = createStep("^This is an other step$");

assertNull(stepMatcher.matchSteps("en", new HashSet<Step>(Arrays.asList(s1, s2)), "When I write anything"));
}

@Test
public void otherLanguageStepMatches() {

Expand Down Expand Up @@ -118,6 +131,48 @@ public void scenarioAliasNonMatchingMultiple() {
"Given there are two cucumbers and fifteen gherkins"));
}

@Test
public void cucumberExpressionStepMatches() {

Step s = createStep("I run a {word}");
assertEquals(s, stepMatcher.matchSteps("en", Collections.singleton(s), "When I run a test"));

s = createStep("I expect {int} cucumber(s)");
assertEquals(s, stepMatcher.matchSteps("en", Collections.singleton(s), "Then I expect 5 cucumbers"));
assertEquals(s, stepMatcher.matchSteps("en", Collections.singleton(s), "Then I expect 1 cucumber"));
}

@Test
@Ignore("custom cucumber expressions are not detected but do not crash the editor anymore")
public void customCucumberExpressionStepMatches() {

Step s = createStep("I have a {color} ball");
assertEquals(s, stepMatcher.matchSteps("en", Collections.singleton(s), " Given I have a red ball"));
}

@Test
public void customCucumberExpressionStepMatchesButDoesNotThrowException() {

Step s = createStep("I have a {color} ball");
try {
stepMatcher.matchSteps("en", Collections.singleton(s), " Given I have a red ball");
}
catch (Exception e) {
fail("Ooops ! I do not except exception");
}
}

@Test
public void shouldNotThrowExceptionWhenPatternIsMalformed() {
try {
Step s = createStep("^(<p>|?>region|regions) of type (.*) (?>are|is) covering all references$");
stepMatcher.matchSteps("en", Collections.singleton(s), " Given I have a red ball");
}
catch (Exception e) {
fail("Ooops ! I do not except exception");
}
}

private Step createStep(String text) {

Step s = new Step();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class StepMatcher {
private Pattern groupPatternNonParameterMatch = Pattern.compile("(\\(\\?:.+?\\))");
private Pattern groupPattern = Pattern.compile("(\\(.+?\\))");


public Step matchSteps(String languageCode, Set<Step> steps, String currentLine) {

//System.out.println("StepMatcher matchSteps() steps = " + steps);
Expand Down
14 changes: 11 additions & 3 deletions cucumber.eclipse.steps.integration/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ClassPath: .,
META-INF/lib/cucumber-core-1.2.5.jar,
META-INF/lib/gherkin-2.12.2.jar
META-INF/lib/gherkin-2.12.2.jar,
META-INF/lib/cucumber-expressions-6.1.0.jar
Export-Package: cucumber.eclipse.steps.integration,
cucumber.runtime,
cucumber.runtime.autocomplete,
cucumber.runtime.formatter,
cucumber.runtime.io,
cucumber.runtime.model,
cucumber.runtime.snippets,
cucumber.runtime.table,
cucumber.runtime.xstream,
gherkin,
gherkin.formatter,
gherkin.formatter.ansi,
gherkin.formatter.model,
gherkin.parser,
gherkin.lexer,
gherkin.util
gherkin.parser,
gherkin.util,
io.cucumber.cucumberexpressions
Binary file not shown.
3 changes: 2 additions & 1 deletion cucumber.eclipse.steps.integration/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ bin.includes = META-INF/,\
.,\
plugin.xml,\
META-INF/lib/cucumber-core-1.2.5.jar,\
META-INF/lib/gherkin-2.12.2.jar
META-INF/lib/gherkin-2.12.2.jar,\
META-INF/lib/cucumber-expressions-6.1.0.jar
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package cucumber.eclipse.steps.integration;

import java.util.regex.Pattern;
import java.util.List;
import java.util.Locale;
import java.util.regex.PatternSyntaxException;

import org.eclipse.core.resources.IResource;

import io.cucumber.cucumberexpressions.Argument;
import io.cucumber.cucumberexpressions.Expression;
import io.cucumber.cucumberexpressions.ExpressionFactory;
import io.cucumber.cucumberexpressions.ParameterTypeRegistry;
import io.cucumber.cucumberexpressions.UndefinedParameterTypeException;

public class Step {

private String text;
private IResource source;
private int lineNumber;
private String lang;
private Pattern compiledText;
private Expression expression;

//Added By Girija
//For Reading Steps from External-ClassPath-JAR
Expand All @@ -24,7 +32,26 @@ public String getText() {
}
public void setText(String text) {
this.text = text;
this.compiledText = Pattern.compile(text);
Locale locale = this.lang == null ? Locale.getDefault() : new Locale(this.lang);
try {
this.expression = new ExpressionFactory(new ParameterTypeRegistry(locale)).createExpression(text);
}

catch (UndefinedParameterTypeException e) {
// the cucumber expression have a custom parameter type
// without definition.
// For example, "I have a {color} ball"
// But the "color" parameter type was not register
// thanks to a TypeRegistryConfigurer.
this.expression = null;
}
catch (PatternSyntaxException e) {
// This fix #286
// the regular expression is wrong
// we do not expect to match something with it
// but we do not want to crash the F3
this.expression = null;
}
}
public IResource getSource() {
return source;
Expand All @@ -41,7 +68,10 @@ public void setLineNumber(int lineNumber) {
}

public boolean matches(String s) {
return compiledText.matcher(s).matches();
if(this.expression == null)
return false;
List<Argument<?>> match = this.expression.match(s);
return match != null;
}

public String getLang() {
Expand Down
10 changes: 9 additions & 1 deletion cucumber.eclipse.steps.jdt/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ClassPath: .,
META-INF/lib/cucumber-java-1.2.2.jar
Export-Package: cucumber.eclipse.steps.jdt
Export-Package: cucumber.eclipse.steps.jdt;
uses:="org.osgi.framework,
org.eclipse.core.runtime,
org.eclipse.jdt.core,
org.eclipse.jdt.core.dom,
cucumber.eclipse.steps.integration,
org.eclipse.core.resources,
org.eclipse.core.commands,
org.eclipse.ui.plugin"

0 comments on commit f980f0c

Please sign in to comment.