Skip to content

Commit

Permalink
Remove 2 special cases for printing arrays:
Browse files Browse the repository at this point in the history
1. Remove the type name from the output:

Not true that <(Object[]) [A, 5]> is empty
=>
Not true that <[A, 5]> is empty

Rationale: We don't include type names for Iterables (though of course that's in part because we would have to guess in some cases), and no one seems to mind. Similarly, when we introduced types like ImmutableIntArray, we didn't seem to think it was important to include there. Note that we still display the types in case of type mismatch (at least in most cases -- I'm not sure about nested array mismatches offhand, but there's not always a good way to represent such types, anyway).

2. Make named() supplement the existing actual value text, not replace it.

Not true that foo has length <1>
=>
Not true that foo (<[A, 5]>) has length <1>

Rationale: This is how named() behaves for normal types. The current behavior has been there since Truth was imported into [], so I don't know of any particular requests for it to be this way. My best guess is that it's intended to avoid "Not true that <... extremely long array ...> is...," most checks are probably going to be isEqualTo() checks, so we're going to end up with an extremely long array in the message, anyway, for the expected value. I suppose that abbreviation could help somewhat with length checks, but if someone really needs to avoid printing the value, I'd suggest `assertThat(array.length).isEqualTo(...)`.

RELNOTES:
  - Removed the type name from the output of arrays.
  - For array subjects, made `named()` supplement the existing actual value text, not replace it. This brings it in line with other subjects' behaviors.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=186110526
  • Loading branch information
cpovirk authored and ronshapiro committed Feb 20, 2018
1 parent 6cd8068 commit f4fabf2
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,9 @@ public void hasLength(int length) {
/** Returns a List representation suitable for displaying in a string. */
abstract List<?> listRepresentation();

// TODO(cgruber): Kill once displayedAs() exists, since this attempts to make .named() do that.
@Override
protected String actualCustomStringRepresentation() {
Object listRepresentation = actual() == null ? "null" : listRepresentation();
return (internalCustomName() == null)
? "(" + underlyingType() + brackets() + ") " + listRepresentation + ""
: "";
return actual() == null ? "null" : listRepresentation().toString();
}

void failWithBadType(Object expected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,23 @@ public void hasLengthFail() {
expectFailure.whenTesting().that(objectArray("A", 5L)).hasLength(1);
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("Not true that <(Object[]) [A, 5]> has length <1>");
.isEqualTo("Not true that <[A, 5]> has length <1>");
}

@Test
public void hasLengthFailNamed() {
expectFailure.whenTesting().that(objectArray("A", 5L)).named("foo").hasLength(1);
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("Not true that foo (<[A, 5]>) has length <1>");
}

@Test
public void hasLengthMultiFail() {
expectFailure.whenTesting().that(new Object[][] {{"A"}, {5L}}).hasLength(1);
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("Not true that <(Object[][]) [[A], [5]]> has length <1>");
.isEqualTo("Not true that <[[A], [5]]> has length <1>");
}

@Test
Expand All @@ -94,7 +102,7 @@ public void isEmptyFail() {
expectFailure.whenTesting().that(objectArray("A", 5L)).isEmpty();
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("Not true that <(Object[]) [A, 5]> is empty");
.isEqualTo("Not true that <[A, 5]> is empty");
}

@Test
Expand All @@ -108,16 +116,15 @@ public void isNotEmptyFail() {
expectFailure.whenTesting().that(EMPTY).isNotEmpty();
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("Not true that <(Object[]) []> is not empty");
.isEqualTo("Not true that <[]> is not empty");
}

@Test
public void isEqualTo_Fail_UnequalOrdering() {
expectFailure.whenTesting().that(objectArray("A", 5L)).isEqualTo(objectArray(5L, "A"));
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"Not true that <(Object[]) [A, 5]> is equal to <[5, A]>. It differs at index <[0]>");
.isEqualTo("Not true that <[A, 5]> is equal to <[5, A]>. It differs at index <[0]>");
}

@Test
Expand All @@ -129,7 +136,7 @@ public void isEqualTo_Fail_UnequalOrderingMultiDimensional_00() {
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"Not true that <(Object[][]) [[A], [5]]> is equal to <[[5], [A]]>."
"Not true that <[[A], [5]]> is equal to <[[5], [A]]>."
+ " It differs at index <[0][0]>");
}

Expand All @@ -142,7 +149,7 @@ public void isEqualTo_Fail_UnequalOrderingMultiDimensional_01() {
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"Not true that <(Object[][]) [[A, B], [5]]> is equal to <[[A], [5]]>."
"Not true that <[[A, B], [5]]> is equal to <[[A], [5]]>."
+ " It differs at index <[0][1]>");
}

Expand All @@ -155,7 +162,7 @@ public void isEqualTo_Fail_UnequalOrderingMultiDimensional_11() {
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"Not true that <(Object[][]) [[A], [5]]> is equal to <[[A], [5, 6]]>."
"Not true that <[[A], [5]]> is equal to <[[A], [5, 6]]>."
+ " It differs at index <[1][1]>");
}

Expand Down Expand Up @@ -191,7 +198,7 @@ public void isNotEqualTo_FailEquals() {
expectFailure.whenTesting().that(objectArray("A", 5L)).isNotEqualTo(objectArray("A", 5L));
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("<(Object[]) [A, 5]> unexpectedly equal to [A, 5].");
.isEqualTo("<[A, 5]> unexpectedly equal to [A, 5].");
}

@Test
Expand All @@ -202,7 +209,7 @@ public void isNotEqualTo_FailEqualsMultiDimensional() {
.isNotEqualTo(new Object[][] {{"A"}, {5L}});
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("<(Object[][]) [[A], [5]]> unexpectedly equal to [[A], [5]].");
.isEqualTo("<[[A], [5]]> unexpectedly equal to [[A], [5]].");
}

@SuppressWarnings("TruthSelfEquals")
Expand All @@ -212,7 +219,7 @@ public void isNotEqualTo_FailSame() {
expectFailure.whenTesting().that(same).isNotEqualTo(same);
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("<(Object[]) [A, 5]> unexpectedly equal to [A, 5].");
.isEqualTo("<[A, 5]> unexpectedly equal to [A, 5].");
}

@SuppressWarnings("TruthSelfEquals")
Expand All @@ -222,7 +229,7 @@ public void isNotEqualTo_FailSameMultiDimensional() {
expectFailure.whenTesting().that(same).isNotEqualTo(same);
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("<(Object[][]) [[A], [5]]> unexpectedly equal to [[A], [5]].");
.isEqualTo("<[[A], [5]]> unexpectedly equal to [[A], [5]].");
}

@Test
Expand All @@ -247,7 +254,7 @@ public void stringArrayIsEqualTo_Fail_UnequalLength() {
expectFailure.whenTesting().that(objectArray("A", "B")).isEqualTo(objectArray("B"));
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("<(String[]) [A, B]> has length 2. Expected length is 1");
.isEqualTo("<[A, B]> has length 2. Expected length is 1");
}

@Test
Expand All @@ -258,16 +265,15 @@ public void stringArrayIsEqualTo_Fail_UnequalLengthMultiDimensional() {
.isEqualTo(new String[][] {{"A"}});
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("<(String[][]) [[A], [B]]> has length 2. Expected length is 1");
.isEqualTo("<[[A], [B]]> has length 2. Expected length is 1");
}

@Test
public void stringArrayIsEqualTo_Fail_UnequalOrdering() {
expectFailure.whenTesting().that(objectArray("A", "B")).isEqualTo(objectArray("B", "A"));
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"Not true that <(String[]) [A, B]> is equal to <[B, A]>. It differs at index <[0]>");
.isEqualTo("Not true that <[A, B]> is equal to <[B, A]>. It differs at index <[0]>");
}

@Test
Expand All @@ -279,7 +285,7 @@ public void stringArrayIsEqualTo_Fail_UnequalOrderingMultiDimensional() {
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"Not true that <(String[][]) [[A], [B]]> is equal to <[[B], [A]]>."
"Not true that <[[A], [B]]> is equal to <[[B], [A]]>."
+ " It differs at index <[0][0]>");
}

Expand All @@ -292,8 +298,7 @@ public void SetArrayIsEqualTo_Fail_UnequalOrdering() {
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"Not true that <(Set[]) [[A], [B]]> is equal to <[[B], [A]]>. "
+ "It differs at index <[0]>");
"Not true that <[[A], [B]]> is equal to <[[B], [A]]>. It differs at index <[0]>");
// Maybe one day:
// .hasMessage("Not true that <(Set<String>[]) [[A], [B]]> is equal to <[[B], [A]]>");
}
Expand All @@ -313,7 +318,7 @@ public void primitiveMultiDimensionalArrayIsEqualTo_Fail_UnequalOrdering() {
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"Not true that <(int[][]) [[1, 2], [3], [4, 5, 6]]> "
"Not true that <[[1, 2], [3], [4, 5, 6]]> "
+ "is equal to <[[1, 2], [3], [4, 5, 6, 7]]>. It differs at index <[2][3]>");
}

Expand All @@ -331,9 +336,7 @@ public void primitiveMultiDimensionalArrayIsNotEqualTo_Fail_Equal() {
.isNotEqualTo(new int[][] {{1, 2}, {3}, {4, 5, 6}});
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"<(int[][]) [[1, 2], [3], [4, 5, 6]]> unexpectedly "
+ "equal to [[1, 2], [3], [4, 5, 6]].");
.isEqualTo("<[[1, 2], [3], [4, 5, 6]]> unexpectedly equal to [[1, 2], [3], [4, 5, 6]].");
}

private static Object[] objectArray(Object... ts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public void isEqualTo_Fail_UnequalOrdering() {
expectFailure.whenTesting().that(array(true, false, true)).isEqualTo(array(false, true, true));
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"Not true that <(boolean[]) [true, false, true]> is equal to <[false, true, true]>");
.isEqualTo("Not true that <[true, false, true]> is equal to <[false, true, true]>");
}

@Test
Expand Down Expand Up @@ -83,7 +82,7 @@ public void isNotEqualTo_FailEquals() {
expectFailure.whenTesting().that(array(true, false)).isNotEqualTo(array(true, false));
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("<(boolean[]) [true, false]> unexpectedly equal to [true, false].");
.isEqualTo("<[true, false]> unexpectedly equal to [true, false].");
}

@SuppressWarnings("TruthSelfEquals")
Expand All @@ -93,7 +92,7 @@ public void isNotEqualTo_FailSame() {
expectFailure.whenTesting().that(same).isNotEqualTo(same);
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("<(boolean[]) [true, false]> unexpectedly equal to [true, false].");
.isEqualTo("<[true, false]> unexpectedly equal to [true, false].");
}

private static boolean[] array(boolean... ts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void isEqualTo_Fail_shortVersion() {
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"Not true that <(byte[]) [124, 112, 12, 11, 10]> is equal to <[24, 12, 2, 1, 0]>; "
"Not true that <[124, 112, 12, 11, 10]> is equal to <[24, 12, 2, 1, 0]>; "
+ "expected:<[180C020100]> but was:<[7C700C0B0A]>");
}

Expand All @@ -79,7 +79,7 @@ public void isEqualTo_Fail() {
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"Not true that <(byte[]) [124, 112, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 7, 101, 120, 97, "
"Not true that <[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]>; "
Expand All @@ -96,7 +96,7 @@ public void isEqualTo_Fail_UnequalOrdering() {
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"Not true that <(byte[]) [0, 123]> is equal to <[123, 0]>; "
"Not true that <[0, 123]> is equal to <[123, 0]>; "
+ "expected:<[7B00]> but was:<[007B]>");
}

Expand Down Expand Up @@ -128,7 +128,7 @@ public void isNotEqualTo_FailEquals() {
expectFailure.whenTesting().that(array(BYTE_0, BYTE_1)).isNotEqualTo(array(BYTE_0, BYTE_1));
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("<(byte[]) [0, 1]> unexpectedly equal to [0, 1].");
.isEqualTo("<[0, 1]> unexpectedly equal to [0, 1].");
}

@SuppressWarnings("TruthSelfEquals")
Expand All @@ -138,7 +138,7 @@ public void isNotEqualTo_FailSame() {
expectFailure.whenTesting().that(same).isNotEqualTo(same);
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("<(byte[]) [0, 1]> unexpectedly equal to [0, 1].");
.isEqualTo("<[0, 1]> unexpectedly equal to [0, 1].");
}

private static byte[] array(byte... ts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void isEqualTo_Fail_UnequalOrdering() {
expectFailure.whenTesting().that(array('a', 'q')).isEqualTo(array('q', 'a'));
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("Not true that <(char[]) [a, q]> is equal to <[q, a]>");
.isEqualTo("Not true that <[a, q]> is equal to <[q, a]>");
}

@Test
Expand Down Expand Up @@ -82,7 +82,7 @@ public void isNotEqualTo_FailEquals() {
expectFailure.whenTesting().that(array('a', 'q')).isNotEqualTo(array('a', 'q'));
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("<(char[]) [a, q]> unexpectedly equal to [a, q].");
.isEqualTo("<[a, q]> unexpectedly equal to [a, q].");
}

@SuppressWarnings("TruthSelfEquals")
Expand All @@ -92,7 +92,7 @@ public void isNotEqualTo_FailSame() {
expectFailure.whenTesting().that(same).isNotEqualTo(same);
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("<(char[]) [a, q]> unexpectedly equal to [a, q].");
.isEqualTo("<[a, q]> unexpectedly equal to [a, q].");
}

private static char[] array(char... ts) {
Expand Down
Loading

0 comments on commit f4fabf2

Please sign in to comment.