Skip to content

Commit

Permalink
implemented workaround for eclipse bug displaying newline characters
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejmikosik committed Mar 12, 2016
2 parents 61e414f + 28f99a2 commit 7858f8b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 27 deletions.
32 changes: 11 additions & 21 deletions main/java/org/quackery/junit/FixEmptySuiteBug.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
package org.quackery.junit;

import static org.quackery.Suite.suite;

import org.quackery.Case;
import org.quackery.Suite;
import org.quackery.Test;
import org.quackery.run.Visitor;

public class FixEmptySuiteBug {
public static Test fixEmptySuiteBug(Test test) {
return test instanceof Suite
? fix((Suite) test)
: test;
}

private static Test fix(Suite suite) {
return suite.tests.isEmpty()
? fixEmpty(suite)
: fixChildren(suite);
return new Visitor() {
protected Test visit(Suite visiting) {
Suite suite = (Suite) super.visit(visiting);
return suite.tests.isEmpty()
? successfulCase(suite.name)
: suite;
}
}.visit(test);
}

private static Case fixEmpty(Suite suite) {
return new Case(suite.name) {
private static Case successfulCase(String name) {
return new Case(name) {
public void run() {}
};
}

private static Test fixChildren(Suite suite) {
Suite fixedSuite = suite(suite.name);
for (Test test : suite.tests) {
fixedSuite = fixedSuite.add(fixEmptySuiteBug(test));
}
return fixedSuite;
}
}
33 changes: 33 additions & 0 deletions main/java/org/quackery/junit/FixNewlineBug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.quackery.junit;

import static org.quackery.Suite.suite;

import org.quackery.Case;
import org.quackery.Suite;
import org.quackery.Test;
import org.quackery.run.Visitor;

public class FixNewlineBug {
public static Test fixNewlineBug(Test test) {
return new Visitor() {
protected Test visit(Suite visiting) {
Suite suite = (Suite) super.visit(visiting);
return suite(fix(suite.name)).addAll(suite.tests);
}

protected Test visit(final Case visiting) {
return new Case(fix(visiting.name)) {
public void run() throws Throwable {
visiting.run();
}
};
}
}.visit(test);
}

private static String fix(String name) {
return name
.replace('\n', ' ')
.replace('\r', ' ');
}
}
9 changes: 7 additions & 2 deletions main/java/org/quackery/junit/QuackeryRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.runner.Description.createTestDescription;
import static org.quackery.Suite.suite;
import static org.quackery.junit.FixEmptySuiteBug.fixEmptySuiteBug;
import static org.quackery.junit.FixNewlineBug.fixNewlineBug;
import static org.quackery.junit.ScanJunitTests.scanJunitTests;
import static org.quackery.junit.ScanQuackeryTests.scanQuackeryTests;

Expand Down Expand Up @@ -42,7 +43,7 @@ public void run(RunNotifier notifier) {
}

private static Runner quackeryTestsRunner(Class<?> annotatedClass, List<Test> tests) {
final Test root = fixEmptySuiteBug(tests.size() == 1
final Test root = fixAllBugs(tests.size() == 1
? tests.get(0)
: suite(annotatedClass.getName()).addAll(tests));
final Description description = describe(root);
Expand All @@ -60,7 +61,7 @@ public void run(RunNotifier notifier) {
private static Runner quackeryAndJunitTestsRunner(final List<Test> tests, final Runner junitRunner) {
final Description description = junitRunner.getDescription();
for (Test test : tests) {
description.addChild(describe(fixEmptySuiteBug(test)));
description.addChild(describe(fixAllBugs(test)));
}
return new Runner() {
public Description getDescription() {
Expand Down Expand Up @@ -148,4 +149,8 @@ public String toString() {
}
};
}

private static Test fixAllBugs(Test test) {
return fixNewlineBug(fixEmptySuiteBug(test));
}
}
4 changes: 2 additions & 2 deletions main/java/org/quackery/run/Visitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public Test visit(Test visiting) {
: visit((Suite) visiting);
}

protected Case visit(Case visiting) {
protected Test visit(Case visiting) {
return visiting;
}

protected Suite visit(Suite visiting) {
protected Test visit(Suite visiting) {
List<Test> visitedChildren = new ArrayList<>();
for (Test child : visiting.tests) {
visitedChildren.add(child instanceof Case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import org.quackery.report.AssumeException;

public class test_QuackeryRunner_quackery_annotation {
private final int emptySuiteWorkaround = 1;
private final String name = "name " + hashCode();
private final String message = "message";
private final String firstLine = "first line";
private final String secondLine = "second line";
private Throwable throwable = new Throwable();
private Result result;
private boolean invoked, otherInvoked;
Expand Down Expand Up @@ -126,15 +129,32 @@ public void empty_suite_does_not_confuse_runner() {

result = run(test);

assertEquals(result.getRunCount(), 1); // 1 because of empty suite workaround
assertEquals(result.getRunCount(), 0 + emptySuiteWorkaround);
assertEquals(result.getFailureCount(), 0);
}

public void name_with_line_feed_is_escaped() {
test = mockCase(firstLine + "\n" + secondLine, new Throwable());

result = run(test);

assertEquals(result.getFailures().get(0).getDescription().getMethodName(),
firstLine + " " + secondLine);
}

public void name_with_carriage_return_is_escaped() {
test = mockCase(firstLine + "\r" + secondLine, new Throwable());

result = run(test);
assertEquals(result.getFailures().get(0).getDescription().getMethodName(),
firstLine + " " + secondLine);
}

public void class_can_have_no_annotated_methods() {
result = run(new JunitClassBuilder()
.load());

assertEquals(result.getRunCount(), 1); // 1 because of empty suite workaround
assertEquals(result.getRunCount(), 0 + emptySuiteWorkaround);
assertEquals(result.getFailureCount(), 0);
}

Expand Down

0 comments on commit 7858f8b

Please sign in to comment.