Skip to content

Commit

Permalink
Fix Describe block with empty message does not run #5
Browse files Browse the repository at this point in the history
[Issue #5](#5)
  • Loading branch information
Paul Warren committed Mar 15, 2017
1 parent da4270a commit 0f2ac2e
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 74 deletions.
37 changes: 37 additions & 0 deletions src/main/java/impl/com/github/paulcwarren/ginkgo4j/IdBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package impl.com.github.paulcwarren.ginkgo4j;

import java.util.Stack;

public class IdBuilder {

private static final String EMPTY_ID = "_EMPTY_";

private IdBuilder() {}


public static String id(String id) {
if (id == null || id.trim().length() == 0) {
id = EMPTY_ID;
}
return id;
}

public static String fqid(String id, Stack<?> context) {
if (id == null || id.trim().length() == 0) {
id = EMPTY_ID;
}
StringBuilder builder = new StringBuilder();
int i;
for (i=0; i < context.size(); i++) {
if (i > 0) {
builder.append(".");
}
builder.append(context.elementAt(i));
}
if (i > 0) {
builder.append(".");
}
builder.append(id);
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package impl.com.github.paulcwarren.ginkgo4j;

public class TitleBuilder {

private static final String EMPTY_TEXT = " ";

private TitleBuilder() {}

public static String title(String title) {
if (title == null || title.trim().length() == 0) {
return EMPTY_TEXT;
}
return title;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@

import impl.com.github.paulcwarren.ginkgo4j.Context;
import impl.com.github.paulcwarren.ginkgo4j.Describe;
import impl.com.github.paulcwarren.ginkgo4j.IdBuilder;
import impl.com.github.paulcwarren.ginkgo4j.builder.TestVisitor;

public class ExecutableChainBuilder implements TestVisitor {

private String filter;
private ExecutableChain chain;

public ExecutableChainBuilder(String filter) {
this.filter = filter;
this.chain = new ExecutableChain(filter);
public ExecutableChainBuilder(String specId) {
this.filter = specId;
this.chain = new ExecutableChain(specId);
}

public ExecutableChain getExecutableChain() {
Expand All @@ -24,6 +25,8 @@ public ExecutableChain getExecutableChain() {

@Override
public void describe(String text, ExecutableBlock block, boolean isFocused) {
text = IdBuilder.id(text);

if (filter.startsWith(text + ".")) {
filter = splitFilter(filter, text);
chain.setIsFocused(isFocused);
Expand All @@ -38,6 +41,8 @@ public void describe(String text, ExecutableBlock block, boolean isFocused) {

@Override
public void context(String text, ExecutableBlock block, boolean isFocused) {
text = IdBuilder.id(text);

if (filter.startsWith(text + ".")) {
filter = splitFilter(filter, text);
chain.setIsFocused(isFocused |= chain.isFocused());
Expand All @@ -52,18 +57,18 @@ public void context(String text, ExecutableBlock block, boolean isFocused) {

@Override
public void beforeEach(ExecutableBlock block) {
// chain.getBeforeEachs().add(block);
chain.getContext().get(chain.getContext().size() - 1).setBeforeEach(block);
}

@Override
public void justBeforeEach(ExecutableBlock block) {
// chain.getJustBeforeEachs().add(block);
chain.getContext().get(chain.getContext().size() - 1).setJustBeforeEach(block);
}

@Override
public void it(String text, ExecutableBlock block, boolean isFocused) {
text = IdBuilder.id(text);

if (filter.equals(text)) {
filter = splitFilter(filter, text);
try {
Expand All @@ -77,7 +82,6 @@ public void it(String text, ExecutableBlock block, boolean isFocused) {

@Override
public void afterEach(ExecutableBlock block) {
// chain.getAfterEachs().add(0, block);
chain.getContext().get(chain.getContext().size() - 1).setAfterEach(block);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.github.paulcwarren.ginkgo4j.ExecutableBlock;

import impl.com.github.paulcwarren.ginkgo4j.IdBuilder;
import impl.com.github.paulcwarren.ginkgo4j.Spec;
import impl.com.github.paulcwarren.ginkgo4j.builder.TestVisitor;

Expand All @@ -19,6 +20,8 @@ public List<Spec> getSpecs() {
}

public void describe(String text, ExecutableBlock block, boolean isFocused) {
text = IdBuilder.id(text);

context.push(text);
try {
block.invoke();
Expand All @@ -30,6 +33,8 @@ public void describe(String text, ExecutableBlock block, boolean isFocused) {
}

public void context(String text, ExecutableBlock block, boolean isFocused) {
text = IdBuilder.id(text);

context.push(text);
try {
block.invoke();
Expand All @@ -47,32 +52,16 @@ public void justBeforeEach(ExecutableBlock block) {
}

public void it(String text, ExecutableBlock block, boolean isFocused) {
Spec spec = new Spec(getId(text), block, isFocused);
String fqid = IdBuilder.fqid(text, context);

Spec spec = new Spec(fqid, block, isFocused);
specs.add(spec);
}

public void afterEach(ExecutableBlock block) {
}

String getId(String text) {
StringBuilder builder = new StringBuilder();
int i;
for (i=0; i < context.size(); i++) {
if (i > 0) {
builder.append(".");
}
builder.append(context.elementAt(i));
}
if (i > 0) {
builder.append(".");
}
builder.append(text);
return builder.toString();
}

@Override
public void test(Object test) {
// TODO Auto-generated method stub

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@

import com.github.paulcwarren.ginkgo4j.ExecutableBlock;

import impl.com.github.paulcwarren.ginkgo4j.IdBuilder;
import impl.com.github.paulcwarren.ginkgo4j.TitleBuilder;
import impl.com.github.paulcwarren.ginkgo4j.builder.TestVisitor;

public class JunitDescriptionsCollector implements TestVisitor {

private Map<String, Description> descriptions = new HashMap<>();
private Stack<Description> context = new Stack<>();
private Stack<Description> descContext = new Stack<>();
private Stack<String> idContext = new Stack<>();
private Description description;

public JunitDescriptionsCollector(Description description) {
Expand All @@ -27,38 +30,52 @@ public Map<String,Description> getDescriptions() {
}

public void describe(String text, ExecutableBlock block, boolean isFocused) {
String id = this.getId(text);
Description desc = Description.createSuiteDescription(text, id, (Annotation[])null);
descriptions.put(id, desc);
if (context.isEmpty()) {

text = TitleBuilder.title(text);

String id = IdBuilder.id(text);
String fqid = IdBuilder.fqid(text, idContext);

Description desc = Description.createSuiteDescription(text, fqid, (Annotation[])null);
descriptions.put(fqid, desc);
if (descContext.isEmpty()) {
description.addChild(desc);
} else {
safePeek(desc);
}
context.push(desc);
descContext.push(desc);
idContext.push(id);
try {
block.invoke();
} catch (Throwable e) {}
finally {
context.pop();
descContext.pop();
idContext.pop();
}
}

public void context(String text, ExecutableBlock block, boolean isFocused) {
String id = this.getId(text);
Description childDesc = Description.createSuiteDescription(text, id, (Annotation[])null);
descriptions.put(id, childDesc);
if (context.isEmpty()) {

text = TitleBuilder.title(text);

String id = IdBuilder.id(text);
String fqid = IdBuilder.fqid(text, idContext);

Description childDesc = Description.createSuiteDescription(text, fqid, (Annotation[])null);
descriptions.put(fqid, childDesc);
if (descContext.isEmpty()) {
description.addChild(childDesc);
} else {
safePeek(childDesc);
}
context.push(childDesc);
descContext.push(childDesc);
idContext.push(id);
try {
block.invoke();
} catch (Throwable e) {}
finally {
context.pop();
descContext.pop();
idContext.pop();
}
}

Expand All @@ -69,7 +86,11 @@ public void justBeforeEach(ExecutableBlock block) {
}

public void it(String text, ExecutableBlock block, boolean isFocused) {
String id = this.getId(text);

text = TitleBuilder.title(text);

String id = IdBuilder.fqid(text, idContext);

Description itDesc = Description.createTestDescription("It", text, id);
descriptions.put(id, itDesc);
try {
Expand All @@ -80,32 +101,14 @@ public void it(String text, ExecutableBlock block, boolean isFocused) {
public void afterEach(ExecutableBlock block) {
}

private String getId(String text) {
StringBuilder builder = new StringBuilder();
int i;
for (i=0; i < context.size(); i++) {
if (i > 0) {
builder.append(".");
}
builder.append(context.elementAt(i));
}
if (i > 0) {
builder.append(".");
}
builder.append(text);
return builder.toString();
}

private void safePeek(Description childDesc) {
try {
context.peek().addChild(childDesc);
descContext.peek().addChild(childDesc);
} catch (EmptyStackException ese) {}
}

@Override
public void test(Object test) {
// TODO Auto-generated method stub

}
}

This file was deleted.

13 changes: 12 additions & 1 deletion src/test/java/com/github/paulcwarren/ginkgo4j/ExampleTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Context;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Describe;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.It;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.runner.RunWith;

import junit.framework.Assert;

@Ignore
@RunWith(Ginkgo4jRunner.class)
//@Ginkgo4jConfiguration(threads=1)
Expand All @@ -18,6 +21,14 @@ public class ExampleTests {
private Example example;

{
Describe("", () -> {
Context("", () -> {
It("", () -> {
assertThat(true, is(true));
});
});
});

Describe("A describe", () -> {

BeforeEach(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ public class Ginkgo4jRunnerTests {
assertThat(skipperThreads(workers), is(5));
});
});
Context("when called with an empty describe label", () -> {
BeforeEach(() -> {
runner = new Ginkgo4jRunner(EmptyDescribeClass.class);
runner.getDescription();
});
It("should return a SpecRunnerThread for all tests in the fitted describe", () -> {
List<ExecutableChain> chains = Ginkgo4jRunner.calculateExecutionChains(EmptyDescribeClass.class);
List<Runner> workers = Ginkgo4jRunner.calculateWorkerThreads(chains);

assertThat(workers, is(not(nullValue())));
assertThat(workers.size(), is(1));
assertThat(runnerThreads(workers), is(1));
});
});
});
}

Expand Down Expand Up @@ -201,4 +215,10 @@ public static class FittedDescribeClass {{
It("test5", () -> {});
});
}}

public static class EmptyDescribeClass {{
Describe("", () -> {
It("stuff", () -> {});
});
}}
}
Loading

0 comments on commit 0f2ac2e

Please sign in to comment.