Skip to content

Commit

Permalink
Implement skip/pending tests. Fixes #8, fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
hgcummings committed Jul 18, 2014
1 parent dda791c commit 5f6ac38
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
*.iml
target
target
bak
6 changes: 4 additions & 2 deletions src/main/java/io/hgc/jarspec/ExceptionBehaviour.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.hgc.jarspec;

import java.util.Optional;

/**
* Mixin providing convenience method for Specifications making statements about exception behaviour
*/
Expand All @@ -16,7 +18,7 @@ public interface ExceptionBehaviour {
default public <T extends Throwable> SpecificationNode itThrows(Class<T> throwable, String forCase, Test testCase) {
return new SpecificationNode.Statement(
String.format("throws %s %s", throwable.getSimpleName(), forCase),
() -> {
Optional.of(() -> {
Throwable exception = null;
try {
testCase.run();
Expand All @@ -30,7 +32,7 @@ default public <T extends Throwable> SpecificationNode itThrows(Class<T> throwab
throwable.getSimpleName(), exception)
);
}
}
})
);
}
}
4 changes: 3 additions & 1 deletion src/main/java/io/hgc/jarspec/JarSpecJUnitRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ private List<Description> visitTree(SpecificationNode specificationNode, Optiona
String text = prefix + specificationNode.description();

List<Description> descriptions = new ArrayList<>();
Description description = Description.createTestDescription(specClass, text);

if (specificationNode.test().isPresent()) {
Description description = Description.createTestDescription(specClass, text);
descriptions.add(description);

if (notifier.isPresent()) {
runTest(specificationNode.test().get(), notifier.get(), description);
}
} else if (specificationNode.children().size() == 0 && notifier.isPresent()) {
notifier.get().fireTestIgnored(description);
}

if (specificationNode.children().size() > 0) {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/io/hgc/jarspec/Specification.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* Represents a Specification consisting of statements about behaviour and automated tests
Expand Down Expand Up @@ -43,7 +44,15 @@ default public SpecificationNode describe(String unit, ByMultiple specifications
* @return a specification consisting of single automatically verifiable statement
*/
default public SpecificationNode it(String statement, Test test) {
return new SpecificationNode.Statement(statement, test);
return new SpecificationNode.Statement(statement, Optional.of(test));
}

/**
* @param statement a statement about the behaviour of a unit
* @return a specification consisting of single statement with no automated test
*/
default public SpecificationNode it(String statement) {
return new SpecificationNode.Statement(statement, Optional.<Test>empty());
}

/**
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/io/hgc/jarspec/SpecificationNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
public abstract class SpecificationNode {
private SpecificationNode() {}

public SpecificationNode skip() {
return new Statement(this.description(), Optional.<Test>empty());
}

abstract Optional<Test> test();

abstract String description();

abstract List<SpecificationNode> children();

static class Aggregate extends SpecificationNode {
private String unit;
private List<SpecificationNode> children;
private final String unit;
private final List<SpecificationNode> children;

public Aggregate(String unit, List<SpecificationNode> children) {
this.unit = unit;
Expand All @@ -40,17 +44,17 @@ List<SpecificationNode> children() {
}

static class Statement extends SpecificationNode {
private String behaviour;
private Test test;
private final String behaviour;
private final Optional<Test> test;

public Statement(String behaviour, Test test) {
public Statement(String behaviour, Optional<Test> test) {
this.behaviour = behaviour;
this.test = test;
}

@Override
Optional<Test> test() {
return Optional.of(test);
return test;
}

@Override
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/io/hgc/jarspec/JarSpecJUnitRunnerSpec.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.hgc.jarspec;

import io.hgc.jarspec.fixtures.AdditionSpec;
import io.hgc.jarspec.fixtures.AssumptionFailureSpec;
import io.hgc.jarspec.fixtures.ErrorInDescribeSpec;
import io.hgc.jarspec.fixtures.ErrorInRootSpec;
import io.hgc.jarspec.fixtures.*;
import org.junit.runner.*;
import org.junit.runner.notification.Failure;

Expand Down Expand Up @@ -54,6 +51,11 @@ public SpecificationNode root() {
Result result = new JUnitCore().run(AssumptionFailureSpec.class);
assertEquals(0, result.getFailureCount());
}),
it("reports skipped tests as ignored", () -> {
Result result = new JUnitCore().run(SkippedTestSpec.class);
assertEquals(0, result.getFailureCount());
assertEquals(3, result.getIgnoreCount());
}),
describe("error handling", () -> {
Result result = new JUnitCore().run(ErrorInDescribeSpec.class);

Expand Down
22 changes: 22 additions & 0 deletions src/test/java/io/hgc/jarspec/fixtures/SkippedTestSpec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.hgc.jarspec.fixtures;

import io.hgc.jarspec.JarSpecJUnitRunner;
import io.hgc.jarspec.Specification;
import io.hgc.jarspec.SpecificationNode;
import org.junit.runner.RunWith;

import static org.junit.Assert.fail;

@RunWith(JarSpecJUnitRunner.class)
public class SkippedTestSpec implements Specification {
@Override
public SpecificationNode root() {
return describe("skipped", () -> by(
describe("a describe marked as skipped", () ->
it("has a failing test", () -> fail("skipped test should not run")
)).skip(),
it("is a statement with no test"),
it("is a statement marked as skipped", () -> fail("skipped test should not run")).skip()
));
}
}

0 comments on commit 5f6ac38

Please sign in to comment.