Skip to content

Commit

Permalink
Merge pull request #607 from pimterry/theory-enum-failure-messages
Browse files Browse the repository at this point in the history
Theories failure messages describe enum arguments unhelpfully
  • Loading branch information
David Saff committed Jan 18, 2013
2 parents 74f3286 + 7a12b70 commit 3aca014
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.junit.experimental.theories;

import static java.lang.String.format;

public abstract class PotentialAssignment {
public static class CouldNotGenerateValueException extends Exception {
private static final long serialVersionUID = 1L;
Expand All @@ -8,24 +10,36 @@ public static class CouldNotGenerateValueException extends Exception {
public static PotentialAssignment forValue(final String name, final Object value) {
return new PotentialAssignment() {
@Override
public Object getValue() throws CouldNotGenerateValueException {
public Object getValue() {
return value;
}

@Override
public String toString() {
return String.format("[%s]", value);
return format("[%s]", value);
}

@Override
public String getDescription()
throws CouldNotGenerateValueException {
return name;
public String getDescription() {
String valueString;

if (value == null) {
valueString = "null";
} else {
try {
valueString = format("\"%s\"", value);
} catch (Throwable e) {
valueString = format("[toString() threw %s: %s]",
e.getClass().getSimpleName(), e.getMessage());
}
}

return format("%s <from %s>", valueString, name);
}
};
}

public abstract Object getValue() throws CouldNotGenerateValueException;

public abstract String getDescription() throws CouldNotGenerateValueException;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.junit.tests.experimental.theories;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.experimental.theories.PotentialAssignment;
import org.junit.experimental.theories.PotentialAssignment.CouldNotGenerateValueException;

public class PotentialAssignmentTest {

@Test
public void shouldUseQuotedValueInDescription() throws CouldNotGenerateValueException {
String name = "stringDatapoint";
Object value = new Object() {
@Override
public String toString() {
return "string value";
}
};

PotentialAssignment assignment = PotentialAssignment.forValue(name, value);

assertEquals("\"string value\" <from stringDatapoint>", assignment.getDescription());
}

@Test
public void shouldNotUseQuotesForNullValueDescriptions() throws CouldNotGenerateValueException {
String name = "nullDatapoint";
Object value = null;

PotentialAssignment assignment = PotentialAssignment.forValue(name, value);

assertEquals("null <from nullDatapoint>", assignment.getDescription());
}

@Test
public void shouldIncludeFailureInDescriptionIfToStringFails() throws CouldNotGenerateValueException {
String name = "explodingValue";
Object value = new Object() {
@Override
public String toString() {
throw new RuntimeException("Oh no!");
}
};

PotentialAssignment assignment = PotentialAssignment.forValue(name, value);

assertEquals("[toString() threw RuntimeException: Oh no!] <from explodingValue>", assignment.getDescription());
}

@Test
public void shouldReturnGivenValue() throws CouldNotGenerateValueException {
Object value = new Object();
PotentialAssignment assignment = PotentialAssignment.forValue("name", value);
assertEquals(value, assignment.getValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void foo(@TestedOn(ints = {1}) int x) {
public void descriptionStatesParameterName() throws Exception {
TestedOnSupplier supplier = new TestedOnSupplier();
List<PotentialAssignment> assignments = supplier.getValueSources(signatureOfFoo());
assertThat(assignments.get(0).getDescription(), is("ints"));
assertThat(assignments.get(0).getDescription(), is("\"1\" <from ints>"));
}

private ParameterSignature signatureOfFoo() throws NoSuchMethodException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void everythingIsZero(int x, int y) {
@Test
public void reportBadParams() throws Exception {
assertThat(testResult(DoesntUseParams.class),
hasSingleFailureContaining("everythingIsZero(ONE, ONE)"));
hasSingleFailureContaining("everythingIsZero(\"1\" <from ONE>, \"1\" <from ONE>)"));
}

@RunWith(Theories.class)
Expand Down

0 comments on commit 3aca014

Please sign in to comment.