Skip to content

Commit

Permalink
Issue cucumber#196 use undefinedStepTracker to detect undefined steps
Browse files Browse the repository at this point in the history
  • Loading branch information
klausbayrhammer committed Mar 31, 2012
1 parent e72bb71 commit de3d9ef
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 23 deletions.
13 changes: 4 additions & 9 deletions core/src/main/java/cucumber/runtime/Runtime.java
Expand Up @@ -31,7 +31,7 @@ public class Runtime implements UnreportedStepExecutor {
private static final Object DUMMY_ARG = new Object();
private static final byte ERRORS = 0x1;

private final UndefinedStepsTracker undefinedStepsTracker = new UndefinedStepsTracker();
final UndefinedStepsTracker undefinedStepsTracker = new UndefinedStepsTracker();

private final Glue glue;
private final RuntimeOptions runtimeOptions;
Expand Down Expand Up @@ -121,20 +121,15 @@ public List<Throwable> getErrors() {

public byte exitStatus() {
byte result = 0x0;
if (hasErrors() || hasPendingStepsAndIsStrict()) {
if (hasErrors() || hasUndefinedStepsAndIsStrict()) {
result |= ERRORS;
}
return result;
}

private boolean hasPendingStepsAndIsStrict()
private boolean hasUndefinedStepsAndIsStrict()
{
return runtimeOptions.strict && hasPendingSteps();
}

private boolean hasPendingSteps()
{
return !getSnippets().isEmpty();
return runtimeOptions.strict && undefinedStepsTracker.hasUndefinedSteps();
}

private boolean hasErrors()
Expand Down
Expand Up @@ -72,4 +72,9 @@ private Step givenWhenThenStep(Step step, I18n i18n) {
return new Step(step.getComments(), lastGivenWhenThenStepKeyword, step.getName(), step.getLine(), step.getRows(), step.getDocString());
}
}

public boolean hasUndefinedSteps()
{
return !undefinedSteps.isEmpty();
}
}
55 changes: 41 additions & 14 deletions core/src/test/java/cucumber/runtime/RuntimeTest.java
Expand Up @@ -3,7 +3,9 @@
import cucumber.io.ClasspathResourceLoader;
import cucumber.io.ResourceLoader;
import cucumber.runtime.model.CucumberFeature;
import gherkin.I18n;
import gherkin.formatter.JSONPrettyFormatter;
import gherkin.formatter.model.Step;

import org.junit.Test;

Expand All @@ -19,6 +21,9 @@
import static org.mockito.Mockito.when;

public class RuntimeTest {

private static final I18n ENGLISH = new I18n("en");

@Test
public void runs_feature_with_json_formatter() throws Exception {
CucumberFeature feature = feature("test.feature", "" +
Expand Down Expand Up @@ -91,45 +96,57 @@ public void runs_feature_with_json_formatter() throws Exception {
@Test
public void strict_without_pending_steps_or_errors()
{
Runtime runtime = createRuntime("-g anything", "--strict");
Runtime runtime = createStrictRuntime();

assertEquals(0x0, runtime.exitStatus());
}

@Test
public void non_strict_without_pending_steps_or_errors()
{
Runtime runtime = createRuntime("-g anything");
Runtime runtime = createNonStrictRuntime();

assertEquals(0x0, runtime.exitStatus());
}

@Test
public void strict_with_pending_steps_and_no_errors()
public void non_strict_with_undefined_steps()
{
Runtime runtime = createRuntime("-g anything", "--strict");
Runtime spy = spy(runtime);
Runtime runtime = createNonStrictRuntime();
runtime.undefinedStepsTracker.addUndefinedStep(new Step(null, "Given ", "A", 1, null, null), ENGLISH);
assertEquals(0x0, runtime.exitStatus());
}

addPendingSteps(spy);
@Test
public void strict_with_undefined_steps()
{
Runtime runtime = createStrictRuntime();
runtime.undefinedStepsTracker.addUndefinedStep(new Step(null, "Given ", "A", 1, null, null), ENGLISH);
assertEquals(0x1, runtime.exitStatus());
}

@Test
public void strict_with_pending_steps_and_no_errors()
{
Runtime runtime = createStrictRuntime();
runtime.addError(new PendingException());

assertEquals(0x1, spy.exitStatus());
assertEquals(0x1, runtime.exitStatus());
}

@Test
public void non_strict_with_pending_steps()
{
Runtime runtime = createRuntime("-g anything");
Runtime spy = spy(runtime);
Runtime runtime = createNonStrictRuntime();
runtime.addError(new PendingException());

addPendingSteps(spy);

assertEquals(0x0, spy.exitStatus());
assertEquals(0x1, runtime.exitStatus());
}

@Test
public void non_strict_with_errors()
{
Runtime runtime = createRuntime("-g anything");
Runtime runtime = createNonStrictRuntime();
runtime.addError(new RuntimeException());

assertEquals(0x1, runtime.exitStatus());
Expand All @@ -138,12 +155,22 @@ public void non_strict_with_errors()
@Test
public void strict_with_errors()
{
Runtime runtime = createRuntime("-g anything", "--strict");
Runtime runtime = createStrictRuntime();
runtime.addError(new RuntimeException());

assertEquals(0x1, runtime.exitStatus());
}

private Runtime createStrictRuntime()
{
return createRuntime("-g anything", "--strict");
}

private Runtime createNonStrictRuntime()
{
return createRuntime("-g anything");
}

private Runtime createRuntime(String ... runtimeArgs)
{
ResourceLoader resourceLoader = mock(ResourceLoader.class);
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/java/cucumber/runtime/UndefinedStepsTrackerTest.java
Expand Up @@ -10,11 +10,28 @@

import static java.util.Arrays.asList;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class UndefinedStepsTrackerTest {

private static final I18n ENGLISH = new I18n("en");

@Test
public void has_undefined_steps()
{
UndefinedStepsTracker undefinedStepsTracker = new UndefinedStepsTracker();
undefinedStepsTracker.addUndefinedStep(new Step(null, "Given ", "A", 1, null, null), ENGLISH);
assertTrue(undefinedStepsTracker.hasUndefinedSteps());
}

@Test
public void has_no_undefined_steps()
{
UndefinedStepsTracker undefinedStepsTracker = new UndefinedStepsTracker();
assertFalse(undefinedStepsTracker.hasUndefinedSteps());
}

@Test
public void removes_duplicates() {
Backend backend = new TestBackend();
Expand Down

0 comments on commit de3d9ef

Please sign in to comment.