Skip to content

Commit

Permalink
Issue #148 - Standard Java, Binary, Unicode and Hexadecimal represent…
Browse files Browse the repository at this point in the history
…ation of objects
  • Loading branch information
mariuszs authored and joel-costigliola committed Jan 26, 2014
1 parent 340f829 commit b5c5f9c
Show file tree
Hide file tree
Showing 204 changed files with 2,271 additions and 891 deletions.
53 changes: 53 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractAssert.java
Expand Up @@ -119,6 +119,59 @@ public S as(Description description) {
return describedAs(description);
}

/**
* Use hexadecimal object representation instead of standard representation in error messages.
* <p/>
* It can be useful when comparing UNICODE characters - many unicode chars have duplicate characters assigned,
* it is thus impossible to find differences from the standard error message:
* <p/>
* With standard message:
* <pre>
* assertThat("µµµ").contains("μμμ");
*
* java.lang.AssertionError:
* Expecting:
* <"µµµ">
* to contain:
* <"μμμ">
* </pre>
*
* With Hexadecimal message:
* <pre>
* assertThat("µµµ").asHexadecimal().contains("μμμ");
*
* java.lang.AssertionError:
* Expecting:
* <"['00B5', '00B5', '00B5']">
* to contain:
* <"['03BC', '03BC', '03BC']">
* </pre>
*
* @return {@code this} assertion object.
*/
protected S asHexadecimal() {
info.representationAsHexadecimal();
return myself;
}

/**
* Use binary object representation instead of standard representation in error messages.
* <p/>
* Example:
* <pre>
* assertThat(1).asBinary().isEqualTo(2);
*
* org.junit.ComparisonFailure:
* Expected :0b00000000_00000000_00000000_00000010
* Actual :0b00000000_00000000_00000000_00000001
*
* @return {@code this} assertion object.
*/
protected S asBinary() {
info.representationAsBinary();
return myself;
}

/** {@inheritDoc} */
@Override
public S describedAs(String description, Object... args) {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractCharSequenceAssert.java
Expand Up @@ -594,4 +594,45 @@ public S usingDefaultComparator() {
this.strings = Strings.instance();
return myself;
}

@Override
public S asHexadecimal() {
return super.asHexadecimal();
}

/**
* Use unicode character representation instead of standard representation in error messages.
* <p/>
* It can be useful when comparing UNICODE characters - many unicode chars have duplicate characters assigned,
* it is thus impossible to find differences from the standard error message:
* <p/>
* With standard message:
* <pre>
* assertThat("µµµ").contains("μμμ");
*
* java.lang.AssertionError:
* Expecting:
* <"µµµ">
* to contain:
* <"μμμ">
* </pre>
*
* With Hexadecimal message:
* <pre>
* assertThat("µµµ").asUnicode().contains("μμμ");
*
* java.lang.AssertionError:
* Expecting:
* <\u00b5\u00b5\u00b5>
* to contain:
* <\u03bc\u03bc\u03bc>
* </pre>
*
* @return {@code this} assertion object.
*/
public S asUnicode() {
info.representationAsUnicode();
return myself;
}

}
33 changes: 33 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractCharacterAssert.java
Expand Up @@ -150,6 +150,38 @@ public S isGreaterThan(char other) {
return myself;
}

/**
/**
* Use unicode character representation instead of standard representation in error messages.
* <p/>
* It can be useful when comparing UNICODE characters - many unicode chars have duplicate characters assigned,
* it is thus impossible to find differences from the standard error message:
* <p/>
* With standard error message:
* <pre>
* assertThat('µ').isEqualTo('μ');
*
* org.junit.ComparisonFailure:
* Expected :'μ'
* Actual :'µ'
* </pre>
*
* With unicode based error message:
* <pre>
* assertThat('µ').asUnicode().isEqualTo('μ');
*
* org.junit.ComparisonFailure:
* Expected :\u03bc
* Actual :\u00b5
* </pre>
*
* @return {@code this} assertion object.
*/
public S asUnicode() {
info.representationAsUnicode();
return myself;
}

/**
* Verifies that the actual value is greater than or equal to the given one.
* <p>
Expand Down Expand Up @@ -195,6 +227,7 @@ public S isGreaterThanOrEqualTo(char other) {
* @throws AssertionError if the actual value is {@code null}.
* @throws AssertionError if the actual value is not a lowercase character.
*/

public S isLowerCase() {
characters.assertLowerCase(info, actual);
return myself;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractComparableAssert.java
Expand Up @@ -80,4 +80,14 @@ public S usingDefaultComparator() {
this.comparables = Comparables.instance();
return myself;
}

@Override
public S asHexadecimal() {
return super.asHexadecimal();
}

@Override
public S asBinary() {
return super.asBinary();
}
}
7 changes: 3 additions & 4 deletions src/main/java/org/assertj/core/api/AbstractDateAssert.java
Expand Up @@ -3,7 +3,6 @@
import static org.assertj.core.util.Dates.newIsoDateFormat;
import static org.assertj.core.util.Dates.newIsoDateTimeFormat;
import static org.assertj.core.util.Dates.newIsoDateTimeWithMsFormat;
import static org.assertj.core.util.ToString.toStringOf;

import java.text.DateFormat;
import java.text.ParseException;
Expand Down Expand Up @@ -2157,7 +2156,7 @@ public static void useDefaultDateFormats() {
* @throws AssertionError if the string can't be parsed as a Date
*/
@VisibleForTesting
static Date parse(String dateAsString) {
Date parse(String dateAsString) {
if (dateAsString == null) return null;
// use synchronized block because SimpleDateFormat which is not thread safe (sigh).
// parse with date format specified by user
Expand All @@ -2167,7 +2166,7 @@ static Date parse(String dateAsString) {
return customDateFormat.parse(dateAsString);
} catch (ParseException e) {
throw new AssertionError("Failed to parse " + dateAsString + " with date format: "
+ toStringOf(customDateFormat));
+ info.representation().toStringOf(customDateFormat));
}
}
}
Expand All @@ -2182,7 +2181,7 @@ static Date parse(String dateAsString) {
}
// no suitable date format
throw new AssertionError("Failed to parse " + dateAsString + " with any of these date formats: "
+ toStringOf(defaultDateFormats));
+ info.representation().toStringOf(defaultDateFormats));
}
}

Expand Down
43 changes: 43 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractEnumerableAssert.java
Expand Up @@ -40,4 +40,47 @@ public S hasSameSizeAs(Object other) {
protected AbstractEnumerableAssert(final A actual, final Class<?> selfType) {
super(actual, selfType);
}

/**
* Enable hexadecimal object representation of Itearble elements instead of standard java representation in error messages.
* <p/>
* It can be useful to better understand what the error was with a more meaningful error message.
* <p/>
* Example
* <pre>
* assertThat(new byte[]{0x10,0x20}).asHex().contains(new byte[]{0x30});
* </pre>
*
* With standard error message:
* <pre>
* Expecting:
* <[16, 32]>
* to contain:
* <[48]>
* but could not find:
* <[48]>
* </pre>
*
* With Hexadecimal error message:
* <pre>
* Expecting:
* <[0x10, 0x20]>
* to contain:
* <[0x30]>
* but could not find:
* <[0x30]>
* </pre>
*
* @return {@code this} assertion object.
*/
@Override
public S asHexadecimal() {
return super.asHexadecimal();
}

@Override
public S asBinary() {
return super.asBinary();
}

}
80 changes: 80 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractIterableAssert.java
Expand Up @@ -763,4 +763,84 @@ protected S usingComparisonStrategy(ComparisonStrategy comparisonStrategy) {
public S usingElementComparatorIgnoringFields(String... fields) {
return usingComparisonStrategy(new IgnoringFieldsComparisonStrategy(fields));
}

/**
* Enable hexadecimal representation of Iterable elements instead of standard representation in error messages.
* <p/>
* It can be useful to better understand what the error was with a more meaningful error message.
* <p/>
* Example
* <pre>
* final List<Byte> bytes = newArrayList((byte)0x10, (byte) 0x20);
* </pre>
*
* With standard error message:
* <pre>
* assertThat(bytes).contains((byte)0x30);
*
* Expecting:
* <[16, 32]>
* to contain:
* <[48]>
* but could not find:
* <[48]>
* </pre>
*
* With Hexadecimal error message:
* <pre>
* assertThat(bytes).asHexadecimal().contains((byte)0x30);
*
* Expecting:
* <[0x10, 0x20]>
* to contain:
* <[0x30]>
* but could not find:
* <[0x30]>
* </pre>
*
* @return {@code this} assertion object.
*/
@Override
public S asHexadecimal() { // TODO rename to asHexadecimalElements() ?
return super.asHexadecimal();
}

/**
* Enable binary representation of Iterable elements instead of standard representation in error messages.
* <p/>
* Example:
* <pre>
* final List<Byte> bytes = newArrayList((byte)0x10, (byte) 0x20);
* </pre>
*
* With standard error message:
* <pre>
* assertThat(bytes).contains((byte)0x30);
*
* Expecting:
* <[16, 32]>
* to contain:
* <[48]>
* but could not find:
* <[48]>
* </pre>
*
* With binary error message:
* <pre>
* assertThat(bytes).asBinary().contains((byte)0x30);
*
* Expecting:
* <[0b00010000, 0b00100000]>
* to contain:
* <[0b00110000]>
* but could not find:
* <[0b00110000]>
* </pre>
*
* @return {@code this} assertion object.
*/
@Override
public S asBinary() {
return super.asBinary();
}
}
Expand Up @@ -528,5 +528,46 @@ public <P> ObjectArrayAssert<P> extractingResultOf(String method, Class<P> extra
return new ObjectArrayAssert<P>(values);
}



/**
* Enable hexadecimal object representation of Itearble elements instead of standard java representation in error messages.
* <p/>
* It can be useful to better understand what the error was with a more meaningful error message.
* <p/>
* Example
* <pre>
* assertThat(new Byte[]{0x10,0x20}).asHexadecimal().contains(new Byte[]{0x30});
* </pre>
*
* With standard error message:
* <pre>
* Expecting:
* <[16, 32]>
* to contain:
* <[48]>
* but could not find:
* <[48]>
* </pre>
*
* With Hexadecimal error message:
* <pre>
* Expecting:
* <[0x10, 0x20]>
* to contain:
* <[0x30]>
* but could not find:
* <[0x30]>
* </pre>
*
* @return {@code this} assertion object.
*/
@Override
public S asHexadecimal() {
return super.asHexadecimal();
}

@Override
public S asBinary() {
return super.asBinary();
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/assertj/core/api/AssertionInfo.java
Expand Up @@ -15,6 +15,7 @@
package org.assertj.core.api;

import org.assertj.core.description.Description;
import org.assertj.core.presentation.Representation;

/**
* Information about an assertion.
Expand All @@ -36,4 +37,5 @@ public interface AssertionInfo {
*/
Description description();

Representation representation();
}

0 comments on commit b5c5f9c

Please sign in to comment.