Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cucumber strict #284

Merged
merged 5 commits into from
Apr 19, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions core/src/main/java/cucumber/runtime/Runtime.java
Original file line number Diff line number Diff line change
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,12 +121,44 @@ public List<Throwable> getErrors() {

public byte exitStatus() {
byte result = 0x0;
if (!errors.isEmpty()) {
if (hasErrors() || hasUndefinedOrPendingStepsAndIsStrict()) {
result |= ERRORS;
}
return result;
}

private boolean hasUndefinedOrPendingStepsAndIsStrict()
{
return runtimeOptions.strict && hasUndefinedOrPendingSteps();
}

private boolean hasUndefinedOrPendingSteps()
{
return hasUndefinedSteps() || hasPendingSteps();
}

private boolean hasUndefinedSteps()
{
return undefinedStepsTracker.hasUndefinedSteps();
}

private boolean hasPendingSteps()
{
return !errors.isEmpty() && !hasErrors();
}

private boolean hasErrors()
{
for (Throwable error : errors)
{
if (!(error instanceof PendingException))
{
return true;
}
}
return false;
}

public List<String> getSnippets() {
return undefinedStepsTracker.getSnippets(backends);
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/cucumber/runtime/RuntimeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class RuntimeOptions {
public List<String> glue = new ArrayList<String>();
public File dotCucumber;
public boolean dryRun;
public boolean strict = false;
public List<String> tags = new ArrayList<String>();
public List<Formatter> formatters = new ArrayList<Formatter>();
public List<String> featurePaths = new ArrayList<String>();
Expand Down Expand Up @@ -69,6 +70,8 @@ private void parse(ArrayList<String> args) {
dotCucumber = new File(args.remove(0));
} else if (arg.equals("--dry-run") || arg.equals("-d")) {
dryRun = true;
} else if (arg.equals("--strict") || arg.equals("-s")) {
strict = true;
} else if (arg.equals("--monochrome") || arg.equals("-m")) {
monochrome = true;
} else {
Expand Down
Original file line number Diff line number Diff line change
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();
}
}
19 changes: 19 additions & 0 deletions core/src/test/java/cucumber/runtime/RuntimeOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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 RuntimeOptionsTest {
Expand Down Expand Up @@ -43,4 +44,22 @@ public void creates_formatter() {
RuntimeOptions options = new RuntimeOptions("--format", "html:some/dir", "--glue", "somewhere");
assertEquals(HTMLFormatter.class, options.formatters.get(0).getClass());
}

@Test
public void assigns_strict() {
RuntimeOptions options = new RuntimeOptions("--strict", "--glue", "somewhere");
assertTrue(options.strict);
}

@Test
public void assigns_strict_short() {
RuntimeOptions options = new RuntimeOptions("-s", "--glue", "somewhere");
assertTrue(options.strict);
}

@Test
public void default_strict() {
RuntimeOptions options = new RuntimeOptions("--glue", "somewhere");
assertFalse(options.strict);
}
}
100 changes: 100 additions & 0 deletions core/src/test/java/cucumber/runtime/RuntimeTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package cucumber.runtime;

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;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static cucumber.runtime.TestHelper.feature;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
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 @@ -81,4 +92,93 @@ public void runs_feature_with_json_formatter() throws Exception {
"]";
assertEquals(expected, out.toString());
}

@Test
public void strict_without_pending_steps_or_errors()
{
Runtime runtime = createStrictRuntime();

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

@Test
public void non_strict_without_pending_steps_or_errors()
{
Runtime runtime = createNonStrictRuntime();

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

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

@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, runtime.exitStatus());
}

@Test
public void non_strict_with_pending_steps()
{
Runtime runtime = createNonStrictRuntime();
runtime.addError(new PendingException());

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

@Test
public void non_strict_with_errors()
{
Runtime runtime = createNonStrictRuntime();
runtime.addError(new RuntimeException());

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

@Test
public void strict_with_errors()
{
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);
ClassLoader classLoader = mock(ClassLoader.class);
RuntimeOptions runtimeOptions = new RuntimeOptions(runtimeArgs);
Backend backend = mock(Backend.class);
Collection<Backend> backends = Arrays.asList(backend);

return new Runtime(resourceLoader, classLoader, backends, runtimeOptions);
}
}
17 changes: 17 additions & 0 deletions core/src/test/java/cucumber/runtime/UndefinedStepsTrackerTest.java
Original file line number Diff line number Diff line change
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
10 changes: 9 additions & 1 deletion junit/src/main/java/cucumber/junit/Cucumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public Cucumber(Class clazz) throws InitializationError, IOException {
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
runtime = new Runtime(resourceLoader, classLoader, runtimeOptions);

jUnitReporter = new JUnitReporter(runtimeOptions.reporter(classLoader), runtimeOptions.formatter(classLoader));
jUnitReporter = new JUnitReporter(runtimeOptions.reporter(classLoader), runtimeOptions.formatter(classLoader)
, runtimeOptions.strict);
addChildren(runtimeOptions.cucumberFeatures(resourceLoader));
}

Expand Down Expand Up @@ -118,6 +119,12 @@ private void addChildren(List<CucumberFeature> cucumberFeatures) throws Initiali
*/
boolean dryRun() default false;

/**
* Scenarios fail if
* @return
*/
boolean strict() default false;

/**
* @return the paths to the feature(s)
*/
Expand All @@ -142,5 +149,6 @@ private void addChildren(List<CucumberFeature> cucumberFeatures) throws Initiali
* @return whether or not to use monochrome output
*/
boolean monochrome() default false;

}
}
Loading