Include hex string in byte[] outputs when isEqualTo fails.
https://github.com/google/truth/issues/239
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=148148598
diff --git a/core/src/main/java/com/google/common/truth/PrimitiveByteArraySubject.java b/core/src/main/java/com/google/common/truth/PrimitiveByteArraySubject.java
index f9ff235..40e0069 100644
--- a/core/src/main/java/com/google/common/truth/PrimitiveByteArraySubject.java
+++ b/core/src/main/java/com/google/common/truth/PrimitiveByteArraySubject.java
@@ -55,13 +55,31 @@
try {
byte[] expectedArray = (byte[]) expected;
if (!Arrays.equals(actual, expectedArray)) {
- fail("is equal to", Arrays.toString(expectedArray));
+ failureStrategy.failComparing(
+ "Not true that "
+ + getDisplaySubject()
+ + " is equal to <"
+ + Arrays.toString(expectedArray)
+ + ">;",
+ base16(expectedArray),
+ base16(getSubject()));
}
} catch (ClassCastException e) {
failWithBadType(expected);
}
}
+ // We could add a dep on com.google.common.io, but that seems overkill for base16 encoding
+ private static String base16(byte[] bytes) {
+ StringBuilder sb = new StringBuilder(2 * bytes.length);
+ for (byte b : bytes) {
+ sb.append(hexDigits[(b >> 4) & 0xf]).append(hexDigits[b & 0xf]);
+ }
+ return sb.toString();
+ }
+
+ private static final char[] hexDigits = "0123456789ABCDEF".toCharArray();
+
/**
* A proposition that the actual array and {@code expected} are not arrays of the same length and
* type, containing elements such that each element in {@code expected} is equal to each element
diff --git a/core/src/test/java/com/google/common/truth/PrimitiveByteArraySubjectTest.java b/core/src/test/java/com/google/common/truth/PrimitiveByteArraySubjectTest.java
index 26025e3..91abb56 100644
--- a/core/src/test/java/com/google/common/truth/PrimitiveByteArraySubjectTest.java
+++ b/core/src/test/java/com/google/common/truth/PrimitiveByteArraySubjectTest.java
@@ -49,6 +49,34 @@
}
@Test
+ public void isEqualTo_Fail() {
+ byte[] actual =
+ new byte[] {
+ 124, 112, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 7, 101, 120, 97, 109, 112, 108, 101, 3, 99, 111,
+ 109, 0, 0, 1, 0, 0
+ };
+ byte[] expect =
+ new byte[] {
+ 124, 112, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 7, 101, 120, 97, 109, 112, 108, 101, 3, 99, 111,
+ 109, 0, 0, 1, 0, 1
+ };
+ try {
+ assertThat(actual).isEqualTo(expect);
+ throw new Error("Expected to throw.");
+ } catch (AssertionError e) {
+ assertThat(e)
+ .hasMessageThat()
+ .isEqualTo(
+ "Not true that <(byte[]) [124, 112, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 7, 101, 120, 97, "
+ + "109, 112, 108, 101, 3, 99, 111, 109, 0, 0, 1, 0, 0]> is equal to "
+ + "<[124, 112, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 7, 101, 120, 97, 109, 112, 108, "
+ + "101, 3, 99, 111, 109, 0, 0, 1, 0, 1]>; "
+ + "expected:<...C6503636F6D000001000[1]>"
+ + " but was:<...C6503636F6D000001000[0]>");
+ }
+ }
+
+ @Test
public void isEqualTo_Fail_UnequalOrdering() {
try {
assertThat(array(BYTE_0, BYTE_1)).isEqualTo(array(BYTE_1, BYTE_0));
@@ -56,7 +84,9 @@
} catch (AssertionError e) {
assertThat(e)
.hasMessageThat()
- .isEqualTo("Not true that <(byte[]) [0, 1]> is equal to <[1, 0]>");
+ .isEqualTo(
+ "Not true that <(byte[]) [0, 1]> is equal to <[1, 0]>; "
+ + "expected:<0[100]> but was:<0[001]>");
}
}