Skip to content

Commit

Permalink
Added OptionalThrowable and its spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
bvenners committed Jun 22, 2013
1 parent de5f50f commit ad5db8f
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 2 deletions.
58 changes: 58 additions & 0 deletions src/main/java/sbt/testing/OptionalThrowable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package sbt.testing;

/**
* Indicates an event was about the entire suite whose class had the fully qualified name specified as
* the <code>fullyQualifiedName</code> attribute the event.
*/
public final class OptionalThrowable {

private Throwable exception;

// TODO: NPE
public OptionalThrowable(Throwable exception) {
if (exception == null) {
throw new NullPointerException("Cannot pass a null exception to OptionalThrowable's constructor.");
}
this.exception = exception;
}

public OptionalThrowable() {
}

public boolean isDefined() {
return exception != null;
}

public boolean isEmpty() {
return exception == null;
}

public Throwable get() {
if (exception == null) {
throw new IllegalStateException("This OptionalThrowable is not defined");
}
return exception;
}

@Override public boolean equals(Object o) {
boolean retVal = false;
if (o instanceof OptionalThrowable) {
OptionalThrowable ot = (OptionalThrowable) o;
retVal = ot.exception == exception;
}
return retVal;
}

@Override public int hashCode() {
return (exception == null) ? 0 : exception.hashCode();
}

@Override public String toString() {
String retVal = "OptionalThrowable()";
if (exception != null) {
retVal = "OptionalThrowable(" + exception + ")";
}
return retVal;
}
}

4 changes: 2 additions & 2 deletions src/main/java/sbt/testing/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public interface Runner {
* <p>
* The test framework may send a summary (i.e., a message giving total tests succeeded, failed, and
* so on) to the user via a log message. If so, it should return the summary from <code>done</code>.
* If not, it should return an empty string. The client may use the return value of <code>done</code>
* to decide whether to display its own summary message.
* If not, it should return an empty string. The client may use the return value of <code>done</code>
* to decide whether to display its own summary message.
* </p>
*
* <p>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/sbt/testing/SuiteSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public final class SuiteSelector extends Selector {
@Override public int hashCode() {
return 29;
}

@Override public String toString() {
return "SuiteSelector";
}
Expand Down
70 changes: 70 additions & 0 deletions src/test/scala/sbt/testing/OptionalThrowableSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package sbt.testing

import org.scalatest._

class OptionalThrowableSpec extends UnitSpec {

object `an OptionalThrowable` {
val ex1 = new Error("red alert")
val ex2 = new RuntimeException("feeling blue")
val def1 = new OptionalThrowable(ex1)
val def2 = new OptionalThrowable(ex2)
val undef = new OptionalThrowable()
object `when defined` {
def `should say it is defined` {
def1.isDefined shouldBe true
def2.isDefined shouldBe true
}
def `should say it is not empty` {
def1.isEmpty shouldBe false
def2.isEmpty shouldBe false
}
def `should return the Throwable from its get method` {
def1.get shouldBe ex1
def2.get shouldBe ex2
}
}
object `when empty` {
def `should say it is not defined` {
undef.isDefined shouldBe false
}
def `should say it is empty` {
undef.isEmpty shouldBe true
}
def `should return throw IllegalStateException from its get method` {
an [IllegalStateException] should be thrownBy {
undef.get
}
}
}
def `should throw NPE from constructor if null passed` {
a [NullPointerException] should be thrownBy {
new OptionalThrowable(null)
}
}
def `should have a properly behaving equals method` {
def1 shouldEqual def1
def1 shouldEqual new OptionalThrowable(ex1)
def2 shouldEqual def2
def2 shouldEqual new OptionalThrowable(ex2)
def1 should not equal null
def1 should not equal undef
def1 should not equal "howdy"
def1 should not equal new OptionalThrowable(ex2)
undef shouldEqual undef
undef shouldEqual new OptionalThrowable()
undef should not equal def1
}
def `should have a properly behaving hashCode method` {
def1.hashCode shouldEqual (new OptionalThrowable(ex1)).hashCode
def2.hashCode shouldEqual (new OptionalThrowable(ex2)).hashCode
undef.hashCode shouldEqual (new OptionalThrowable()).hashCode
}
def `should have a pretty toString` {
def1.toString should (startWith ("OptionalThrowable(") and endWith (")") and not be ("OptionalThrowable()"))
def2.toString should (startWith ("OptionalThrowable(") and endWith (")") and not be ("OptionalThrowable()"))
undef.toString shouldBe "OptionalThrowable()"
}
}
}

0 comments on commit ad5db8f

Please sign in to comment.